<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>随机森林 on 格物堂</title><link>https://gewutang.cn/tags/%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97/</link><description>Recent content in 随机森林 on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Thu, 23 Jul 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/%E9%9A%8F%E6%9C%BA%E6%A3%AE%E6%9E%97/index.xml" rel="self" type="application/rss+xml"/><item><title>基于历史量价数据的未来涨幅预测</title><link>https://gewutang.cn/2026/07/23/predicting-volume/</link><pubDate>Thu, 23 Jul 2026 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2026/07/23/predicting-volume/</guid><description>&#10;&#10;&#10;&lt;div id="引言" class="section level1"&gt;&#10;&lt;h1&gt;引言&lt;/h1&gt;&#10;&lt;p&gt;在金融市场中，准确预测股票未来涨幅对投资者至关重要。本文以双汇发展（000895.sz）为研究对象，收集并深度剖析其历史累积涨幅及历史成交量数据，探寻二者与未来涨幅之间潜在的规律与关系。通过相关性分析揭示各变量的内在联系，并以相关系数热图直观呈现。在此基础上，借助线性回归和随机森林模型预测未来涨幅，并通过实际值与预测值对比图评估预测效果，最终为投资决策提供科学的数据依据。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="一研究思路" class="section level1"&gt;&#10;&lt;h1&gt;一、研究思路&lt;/h1&gt;&#10;&lt;p&gt;本文基于线性回归模型和随机森林模型，以历史阶段性涨幅和成交量为自变量去预测未来数日阶段性涨幅。具体而言，以过去 1 日、3 日、5 日、10 日、20 日累计涨幅和累计成交量作为自变量，去预测未来 5 日、10 日、20 日的涨幅。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="二数据收集与处理" class="section level1"&gt;&#10;&lt;h1&gt;二、数据收集与处理&lt;/h1&gt;&#10;&lt;div id="数据获取" class="section level2"&gt;&#10;&lt;h2&gt;2.1 数据获取&lt;/h2&gt;&#10;&lt;p&gt;本文选取双汇发展股票（代码 &lt;code&gt;000895.sz&lt;/code&gt;）作为研究样本，利用 &lt;code&gt;quantmod&lt;/code&gt; 包的 &lt;code&gt;getSymbols&lt;/code&gt; 函数获取其历史行情数据。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;library(quantmod)&#10;library(zoo)&#10;library(TTR)&#10;library(randomForest)&#10;library(ggplot2)&#10;library(tidyr)&#10;library(patchwork)&#10;&#10;# 从本地缓存读取股票数据（双汇发展 000895.sz）&#10;SHX &amp;lt;- readRDS(&amp;quot;SHX.rds&amp;quot;)&#10;&#10;# 提取日期信息&#10;dates &amp;lt;- index(SHX)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="收益率与成交量计算" class="section level2"&gt;&#10;&lt;h2&gt;2.2 收益率与成交量计算&lt;/h2&gt;&#10;&lt;p&gt;运用 TTR 包中的 &lt;code&gt;dailyReturn&lt;/code&gt; 函数计算每日收益率，并通过 &lt;code&gt;rollapply&lt;/code&gt; 函数分别计算三日、五日、十日及二十日的阶段收益率与成交量。这些不同周期的指标有助于从多个时间维度捕捉股票价格和成交量的变化特征。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 使用 TTR 包中的 dailyReturn 函数计算每日收益率&#10;daily_return &amp;lt;- dailyReturn(Cl(SHX), type = &amp;quot;log&amp;quot;) * 100&#10;&#10;# 定义辅助函数计算阶段收益率和成交量&#10;calc_roll &amp;lt;- function(x, width, FUN) {&#10; rollapply(x, width = width, FUN = FUN, by = 1)&#10;}&#10;&#10;# 计算各周期阶段收益率&#10;three_day_return &amp;lt;- calc_roll(daily_return, 3, sum)&#10;five_day_return &amp;lt;- calc_roll(daily_return, 5, sum)&#10;ten_day_return &amp;lt;- calc_roll(daily_return, 10, sum)&#10;twenty_day_return &amp;lt;- calc_roll(daily_return, 20, sum)&#10;&#10;# 计算各周期阶段成交量&#10;three_day_volume &amp;lt;- calc_roll(Vo(SHX), 3, sum)&#10;five_day_volume &amp;lt;- calc_roll(Vo(SHX), 5, sum)&#10;ten_day_volume &amp;lt;- calc_roll(Vo(SHX), 10, sum)&#10;twenty_day_volume &amp;lt;- calc_roll(Vo(SHX), 20, sum)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="数据整合与清理" class="section level2"&gt;&#10;&lt;h2&gt;2.3 数据整合与清理&lt;/h2&gt;&#10;&lt;p&gt;将日期、各阶段收益率及成交量数据整理至数据框 &lt;code&gt;data&lt;/code&gt; 中。在计算未来不同周期涨幅时，通过反转每日收益率序列，运用 &lt;code&gt;rollapply&lt;/code&gt; 函数计算滚动和，再反转回来并处理缺失值。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 整理数据到数据框&#10;data &amp;lt;- data.frame(&#10; date = dates,&#10; three_day_return = three_day_return,&#10; five_day_return = five_day_return,&#10; ten_day_return = ten_day_return,&#10; twenty_day_return = twenty_day_return,&#10; three_day_volume = three_day_volume,&#10; five_day_volume = five_day_volume,&#10; ten_day_volume = ten_day_volume,&#10; twenty_day_volume = twenty_day_volume&#10;)&#10;&#10;# 定义未来涨幅计算函数&#10;calc_future_return &amp;lt;- function(daily_ret, width) {&#10; reversed &amp;lt;- rev(daily_ret)&#10; fut_ret &amp;lt;- rollapply(reversed, width = width, FUN = sum, by = 1)&#10; fut_ret &amp;lt;- rev(fut_ret)&#10; fut_ret &amp;lt;- fut_ret[-(1:(width - 1))]&#10; &#10; filled &amp;lt;- rep(NA, nrow(data))&#10; len &amp;lt;- length(fut_ret)&#10; filled[(width - 1 + 1):(width - 1 + len)] &amp;lt;- fut_ret&#10; return(filled)&#10;}&#10;&#10;# 计算未来 5 日、10 日、20 日涨幅&#10;data$future_five_day_return &amp;lt;- calc_future_return(daily_return, 5)&#10;data$future_ten_day_return &amp;lt;- calc_future_return(daily_return, 10)&#10;data$future_twenty_day_return &amp;lt;- calc_future_return(daily_return, 20)&#10;&#10;# 过滤缺失值&#10;data &amp;lt;- data[rowSums(is.na(data)) != ncol(data), ]&#10;data &amp;lt;- na.omit(data)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;处理后的数据结构如下：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;str(data)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &amp;#39;data.frame&amp;#39;:&#9;4713 obs. of 12 variables:&#10;## $ date : Date, format: &amp;quot;2007-01-31&amp;quot; &amp;quot;2007-02-01&amp;quot; ...&#10;## $ daily.returns : num 0 0 0 0 0 0 0 0 0 0 ...&#10;## $ daily.returns.1 : num 0 0 0 0 0 0 0 0 0 0 ...&#10;## $ daily.returns.2 : num 0 0 0 0 0 0 0 0 0 0 ...&#10;## $ daily.returns.3 : num 0 0 0 0 0 0 0 0 0 0 ...&#10;## $ X000895.SZ.Volume : num 0 0 0 0 0 0 0 0 0 0 ...&#10;## $ X000895.SZ.Volume.1 : num 0 0 0 0 0 0 0 0 0 0 ...&#10;## $ X000895.SZ.Volume.2 : num 0 0 0 0 0 0 0 0 0 0 ...&#10;## $ X000895.SZ.Volume.3 : num 0 0 0 0 0 0 0 0 0 0 ...&#10;## $ future_five_day_return : num 0 0 0 0 0 0 0 0 0 0 ...&#10;## $ future_ten_day_return : num 0 0 0 0 0 0 0 0 0 0 ...&#10;## $ future_twenty_day_return: num 0 0 0 0 0 0 0 0 0 0 ...&#10;## - attr(*, &amp;quot;na.action&amp;quot;)= &amp;#39;omit&amp;#39; Named int [1:38] 1 2 3 4 5 6 7 8 9 10 ...&#10;## ..- attr(*, &amp;quot;names&amp;quot;)= chr [1:38] &amp;quot;2007-01-04&amp;quot; &amp;quot;2007-01-05&amp;quot; &amp;quot;2007-01-08&amp;quot; &amp;quot;2007-01-09&amp;quot; ...&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;head(data)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## date daily.returns daily.returns.1 daily.returns.2&#10;## 2007-01-31 2007-01-31 0 0 0&#10;## 2007-02-01 2007-02-01 0 0 0&#10;## 2007-02-02 2007-02-02 0 0 0&#10;## 2007-02-05 2007-02-05 0 0 0&#10;## 2007-02-06 2007-02-06 0 0 0&#10;## 2007-02-07 2007-02-07 0 0 0&#10;## daily.returns.3 X000895.SZ.Volume X000895.SZ.Volume.1&#10;## 2007-01-31 0 0 0&#10;## 2007-02-01 0 0 0&#10;## 2007-02-02 0 0 0&#10;## 2007-02-05 0 0 0&#10;## 2007-02-06 0 0 0&#10;## 2007-02-07 0 0 0&#10;## X000895.SZ.Volume.2 X000895.SZ.Volume.3 future_five_day_return&#10;## 2007-01-31 0 0 0&#10;## 2007-02-01 0 0 0&#10;## 2007-02-02 0 0 0&#10;## 2007-02-05 0 0 0&#10;## 2007-02-06 0 0 0&#10;## 2007-02-07 0 0 0&#10;## future_ten_day_return future_twenty_day_return&#10;## 2007-01-31 0 0&#10;## 2007-02-01 0 0&#10;## 2007-02-02 0 0&#10;## 2007-02-05 0 0&#10;## 2007-02-06 0 0&#10;## 2007-02-07 0 0&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="三相关性分析" class="section level1"&gt;&#10;&lt;h1&gt;三、相关性分析&lt;/h1&gt;&#10;&lt;p&gt;为了深入理解各变量之间的关系，提取数据框中的数值型变量，通过 &lt;code&gt;cor&lt;/code&gt; 函数计算相关系数矩阵，并利用 &lt;code&gt;corrplot&lt;/code&gt; 包绘制相关系数热图。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;library(corrplot)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## corrplot 0.95 loaded&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;library(dplyr)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ######################### Warning from &amp;#39;xts&amp;#39; package ##########################&#10;## # #&#10;## # The dplyr lag() function breaks how base R&amp;#39;s lag() function is supposed to #&#10;## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #&#10;## # source() into this session won&amp;#39;t work correctly. #&#10;## # #&#10;## # Use stats::lag() to make sure you&amp;#39;re not using dplyr::lag(), or you can add #&#10;## # conflictRules(&amp;#39;dplyr&amp;#39;, exclude = &amp;#39;lag&amp;#39;) to your .Rprofile to stop #&#10;## # dplyr from breaking base R&amp;#39;s lag() function. #&#10;## # #&#10;## # Code in packages is not affected. It&amp;#39;s protected by R&amp;#39;s namespace mechanism #&#10;## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #&#10;## # #&#10;## ###############################################################################&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 载入程序包：&amp;#39;dplyr&amp;#39;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:randomForest&amp;#39;:&#10;## &#10;## combine&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:xts&amp;#39;:&#10;## &#10;## first, last&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:stats&amp;#39;:&#10;## &#10;## filter, lag&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:base&amp;#39;:&#10;## &#10;## intersect, setdiff, setequal, union&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 提取数值型变量&#10;numeric_data &amp;lt;- data %&amp;gt;% select_if(is.numeric)&#10;&#10;# 计算相关系数矩阵&#10;correlation_matrix &amp;lt;- cor(numeric_data)&#10;&#10;# 绘制相关系数热图&#10;corrplot(correlation_matrix, &#10; method = &amp;quot;color&amp;quot;, &#10; type = &amp;quot;upper&amp;quot;,&#10; tl.col = &amp;quot;black&amp;quot;, tl.srt = 45,&#10; title = &amp;quot;Correlation Heatmap of Variables&amp;quot;,&#10; addCoef.col = &amp;quot;black&amp;quot;, &#10; number.cex = 0.7)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;div class="figure"&gt;&lt;span style="display:block;" id="fig:corr"&gt;&lt;/span&gt;&#10;&lt;img src="index_files/figure-html/corr-1.png" alt="变量相关性热图" width="90%" /&gt;&#10;&lt;p class="caption"&gt;&#10;Figure 1: 变量相关性热图&#10;&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;热图中，坐标轴标签以黑色显示并旋转 45 度，同时添加黑色的相关系数数值，字体大小设为 0.7，清晰呈现各变量间的线性相关程度，为后续模型构建提供参考。&lt;/p&gt;&#10;&lt;p&gt;从热图中可以看出，历史各周期的收益率之间存在较强相关性，而成交量与收益率之间的相关性较弱，这暗示成交量可能为模型提供差异化的信息。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="四预测模型构建与评估" class="section level1"&gt;&#10;&lt;h1&gt;四、预测模型构建与评估&lt;/h1&gt;&#10;&lt;div id="线性回归模型" class="section level2"&gt;&#10;&lt;h2&gt;4.1 线性回归模型&lt;/h2&gt;&#10;&lt;p&gt;分别构建预测未来五日、十日及二十日涨幅的线性回归模型。以 &lt;code&gt;data&lt;/code&gt; 中的所有变量作为自变量，相应的未来涨幅作为因变量，使用 &lt;code&gt;lm&lt;/code&gt; 函数建模。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 构建预测未来五日涨幅的线性回归模型&#10;model_five_day &amp;lt;- lm(future_five_day_return ~ ., data = data)&#10;summary(model_five_day)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Call:&#10;## lm(formula = future_five_day_return ~ ., data = data)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -37.921 -1.837 0.035 1.838 51.589 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## (Intercept) -1.259e-01 4.592e-01 -0.274 0.7840 &#10;## date 6.136e-06 2.765e-05 0.222 0.8244 &#10;## daily.returns 1.331e-01 2.062e-02 6.455 1.19e-10 ***&#10;## daily.returns.1 3.004e-02 1.882e-02 1.596 0.1105 &#10;## daily.returns.2 -2.358e-02 1.275e-02 -1.849 0.0646 . &#10;## daily.returns.3 5.332e-03 7.633e-03 0.699 0.4849 &#10;## X000895.SZ.Volume -1.339e-08 6.858e-09 -1.952 0.0510 . &#10;## X000895.SZ.Volume.1 8.689e-09 6.037e-09 1.439 0.1501 &#10;## X000895.SZ.Volume.2 -2.787e-10 2.889e-09 -0.096 0.9231 &#10;## X000895.SZ.Volume.3 1.965e-11 1.025e-09 0.019 0.9847 &#10;## future_ten_day_return 4.735e-01 1.047e-02 45.240 &amp;lt; 2e-16 ***&#10;## future_twenty_day_return 1.627e-02 7.598e-03 2.141 0.0323 * &#10;## ---&#10;## Signif. codes: 0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1&#10;## &#10;## Residual standard error: 3.703 on 4701 degrees of freedom&#10;## Multiple R-squared: 0.4894,&#9;Adjusted R-squared: 0.4882 &#10;## F-statistic: 409.6 on 11 and 4701 DF, p-value: &amp;lt; 2.2e-16&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 预测未来五日涨幅&#10;prediction_five_day &amp;lt;- predict(model_five_day, newdata = data)&#10;&#10;# 构建预测未来十日涨幅的线性回归模型&#10;model_ten_day &amp;lt;- lm(future_ten_day_return ~ ., data = data)&#10;summary(model_ten_day)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Call:&#10;## lm(formula = future_ten_day_return ~ ., data = data)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -26.278 -2.039 0.001 2.089 61.889 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## (Intercept) 4.366e-02 5.341e-01 0.082 0.93485 &#10;## date -3.026e-06 3.217e-05 -0.094 0.92506 &#10;## daily.returns 3.350e-02 2.408e-02 1.391 0.16428 &#10;## daily.returns.1 -5.127e-02 2.188e-02 -2.343 0.01915 * &#10;## daily.returns.2 -2.457e-02 1.483e-02 -1.656 0.09770 . &#10;## daily.returns.3 2.593e-02 8.871e-03 2.923 0.00349 ** &#10;## X000895.SZ.Volume 5.147e-09 7.980e-09 0.645 0.51896 &#10;## X000895.SZ.Volume.1 7.558e-10 7.024e-09 0.108 0.91431 &#10;## X000895.SZ.Volume.2 -6.052e-09 3.359e-09 -1.802 0.07168 . &#10;## X000895.SZ.Volume.3 2.086e-09 1.192e-09 1.751 0.08002 . &#10;## future_five_day_return 6.406e-01 1.416e-02 45.240 &amp;lt; 2e-16 ***&#10;## future_twenty_day_return 3.348e-01 7.371e-03 45.423 &amp;lt; 2e-16 ***&#10;## ---&#10;## Signif. codes: 0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1&#10;## &#10;## Residual standard error: 4.307 on 4701 degrees of freedom&#10;## Multiple R-squared: 0.6373,&#9;Adjusted R-squared: 0.6364 &#10;## F-statistic: 750.8 on 11 and 4701 DF, p-value: &amp;lt; 2.2e-16&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 预测未来十日涨幅&#10;prediction_ten_day &amp;lt;- predict(model_ten_day, newdata = data)&#10;&#10;# 构建预测未来二十日涨幅的线性回归模型&#10;model_twenty_day &amp;lt;- lm(future_twenty_day_return ~ ., data = data)&#10;summary(model_twenty_day)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Call:&#10;## lm(formula = future_twenty_day_return ~ ., data = data)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -30.805 -3.435 -0.276 3.457 77.286 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## (Intercept) 2.769e+00 8.801e-01 3.146 0.00167 ** &#10;## date -1.154e-04 5.303e-05 -2.176 0.02964 * &#10;## daily.returns -4.360e-02 3.973e-02 -1.097 0.27249 &#10;## daily.returns.1 5.090e-02 3.610e-02 1.410 0.15861 &#10;## daily.returns.2 6.137e-02 2.446e-02 2.509 0.01215 * &#10;## daily.returns.3 -4.063e-02 1.463e-02 -2.777 0.00552 ** &#10;## X000895.SZ.Volume -1.206e-08 1.316e-08 -0.916 0.35953 &#10;## X000895.SZ.Volume.1 -8.226e-10 1.159e-08 -0.071 0.94340 &#10;## X000895.SZ.Volume.2 1.059e-08 5.541e-09 1.911 0.05608 . &#10;## X000895.SZ.Volume.3 -5.759e-09 1.964e-09 -2.932 0.00339 ** &#10;## future_five_day_return 5.989e-02 2.797e-02 2.141 0.03230 * &#10;## future_ten_day_return 9.110e-01 2.006e-02 45.423 &amp;lt; 2e-16 ***&#10;## ---&#10;## Signif. codes: 0 &amp;#39;***&amp;#39; 0.001 &amp;#39;**&amp;#39; 0.01 &amp;#39;*&amp;#39; 0.05 &amp;#39;.&amp;#39; 0.1 &amp;#39; &amp;#39; 1&#10;## &#10;## Residual standard error: 7.105 on 4701 degrees of freedom&#10;## Multiple R-squared: 0.4777,&#9;Adjusted R-squared: 0.4764 &#10;## F-statistic: 390.8 on 11 and 4701 DF, p-value: &amp;lt; 2.2e-16&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 预测未来二十日涨幅&#10;prediction_twenty_day &amp;lt;- predict(model_twenty_day, newdata = data)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="随机森林模型" class="section level2"&gt;&#10;&lt;h2&gt;4.2 随机森林模型&lt;/h2&gt;&#10;&lt;p&gt;同样针对未来五日、十日及二十日涨幅构建随机森林模型。随机森林模型因其对复杂非线性关系的捕捉能力，为预测提供了另一种有效的视角。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 构建预测未来五日涨幅的随机森林模型&#10;rf_model_five_day &amp;lt;- randomForest(future_five_day_return ~ ., data = data, ntree = 100)&#10;rf_prediction_five_day &amp;lt;- predict(rf_model_five_day, newdata = data)&#10;&#10;# 构建预测未来十日涨幅的随机森林模型&#10;rf_model_ten_day &amp;lt;- randomForest(future_ten_day_return ~ ., data = data, ntree = 100)&#10;rf_prediction_ten_day &amp;lt;- predict(rf_model_ten_day, newdata = data)&#10;&#10;# 构建预测未来二十日涨幅的随机森林模型&#10;rf_model_twenty_day &amp;lt;- randomForest(future_twenty_day_return ~ ., data = data, ntree = 100)&#10;rf_prediction_twenty_day &amp;lt;- predict(rf_model_twenty_day, newdata = data)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;/div&gt;&#10;&lt;div id="模型评估与可视化" class="section level2"&gt;&#10;&lt;h2&gt;4.3 模型评估与可视化&lt;/h2&gt;&#10;&lt;p&gt;对于随机森林模型预测的未来不同周期涨幅，分别绘制实际值与预测值的对比图。通过 &lt;code&gt;patchwork&lt;/code&gt; 包将每个周期涨幅的对比图和差值图合并展示，全面评估模型的预测效果。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 定义通用绘图函数&#10;plot_prediction &amp;lt;- function(dates, actual, predicted, title_prefix) {&#10; # 对比图数据&#10; plot_data &amp;lt;- data.frame(&#10; date = dates,&#10; actual = actual,&#10; predicted = predicted&#10; )&#10; plot_data_long &amp;lt;- pivot_longer(plot_data, &#10; cols = c(actual, predicted), &#10; names_to = &amp;quot;type&amp;quot;, &#10; values_to = &amp;quot;return&amp;quot;)&#10; &#10; # 实际值与预测值对比图&#10; p_comparison &amp;lt;- ggplot(plot_data_long, &#10; aes(x = date, y = return, color = type)) +&#10; geom_line() +&#10; labs(&#10; title = paste0(&amp;quot;Random Forest: &amp;quot;, title_prefix, &amp;quot; Actual vs Predicted&amp;quot;),&#10; x = &amp;quot;Date&amp;quot;, y = &amp;quot;Return&amp;quot;&#10; ) +&#10; theme_minimal()&#10; &#10; # 差值图&#10; diff_data &amp;lt;- data.frame(&#10; date = dates,&#10; diff = predicted - actual&#10; )&#10; &#10; p_diff &amp;lt;- ggplot(diff_data, aes(x = date, y = diff)) +&#10; geom_line() +&#10; labs(&#10; title = paste0(&amp;quot;Random Forest: &amp;quot;, title_prefix, &amp;quot; Prediction Difference&amp;quot;),&#10; x = &amp;quot;Date&amp;quot;, y = &amp;quot;Difference&amp;quot;&#10; ) +&#10; theme_minimal()&#10; &#10; # 合并展示&#10; p_comparison / p_diff&#10;}&#10;&#10;# 绘制各周期预测效果&#10;five_day_plot &amp;lt;- plot_prediction(data$date, data$future_five_day_return, &#10; rf_prediction_five_day, &amp;quot;Five-Day&amp;quot;)&#10;ten_day_plot &amp;lt;- plot_prediction(data$date, data$future_ten_day_return, &#10; rf_prediction_ten_day, &amp;quot;Ten-Day&amp;quot;)&#10;twenty_day_plot &amp;lt;- plot_prediction(data$date, data$future_twenty_day_return, &#10; rf_prediction_twenty_day, &amp;quot;Twenty-Day&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;预测效果分别如下：&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="index_files/figure-html/visResult-1.png" alt="" width="672" /&gt;&lt;img src="index_files/figure-html/visResult-2.png" alt="" width="672" /&gt;&lt;img src="index_files/figure-html/visResult-3.png" alt="" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;从图中可以看出，五日涨幅的预测跟踪效果较好，差值波动相对较小；而随着预测周期的增长（十日、二十日），预测误差逐渐增大。这是由于长期走势受更多复杂因素影响，仅凭历史量价信息难以完全捕捉。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;/div&gt;&#10;&lt;div id="五模型表现对比" class="section level1"&gt;&#10;&lt;h1&gt;五、模型表现对比&lt;/h1&gt;&#10;&lt;p&gt;为更清晰地评估模型的预测效果，我们计算各模型的 R 方指标：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;===== 线性回归模型 =====\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## ===== 线性回归模型 =====&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;五日涨幅 R²: &amp;quot;, summary(model_five_day)$r.squared, &amp;quot;\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 五日涨幅 R²: 0.4894064&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;十日涨幅 R²: &amp;quot;, summary(model_ten_day)$r.squared, &amp;quot;\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 十日涨幅 R²: 0.6372557&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;二十日涨幅 R²: &amp;quot;, summary(model_twenty_day)$r.squared, &amp;quot;\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 二十日涨幅 R²: 0.4776518&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;\n===== 随机森林模型 =====\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ===== 随机森林模型 =====&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;rf_r2 &amp;lt;- function(actual, predicted) {&#10; 1 - sum((actual - predicted)^2) / sum((actual - mean(actual))^2)&#10;}&#10;cat(&amp;quot;五日涨幅 R²: &amp;quot;, rf_r2(data$future_five_day_return, rf_prediction_five_day), &amp;quot;\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 五日涨幅 R²: 0.9075108&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;十日涨幅 R²: &amp;quot;, rf_r2(data$future_ten_day_return, rf_prediction_ten_day), &amp;quot;\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 十日涨幅 R²: 0.9330646&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;cat(&amp;quot;二十日涨幅 R²: &amp;quot;, rf_r2(data$future_twenty_day_return, rf_prediction_twenty_day), &amp;quot;\n&amp;quot;)&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 二十日涨幅 R²: 0.9021227&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;可以看出，随机森林模型的拟合能力明显优于线性回归模型，尤其在短期预测（五日）上表现突出。这说明历史量价与未来涨幅之间存在较强的非线性关系，随机森林模型能够更好地捕捉这种关系。&lt;/p&gt;&#10;&lt;/div&gt;&#10;&lt;div id="六结论" class="section level1"&gt;&#10;&lt;h1&gt;六、结论&lt;/h1&gt;&#10;&lt;p&gt;本文通过对双汇发展股票历史量价数据的系统分析，综合运用相关性分析、线性回归及随机森林模型等方法，构建了一个完整的量化分析体系。主要发现如下：&lt;/p&gt;&#10;&lt;ol style="list-style-type: decimal"&gt;&#10;&lt;li&gt;历史阶段性涨幅之间存在较强的自相关性，不同周期（3 日、5 日、10 日、20 日）的回报率高度相关；&lt;/li&gt;&#10;&lt;li&gt;成交量与历史/未来涨幅之间的线性相关性较弱，但成交量为模型提供了差异化的非线性信息；&lt;/li&gt;&#10;&lt;li&gt;随机森林模型在捕捉历史量价与未来涨幅之间的非线性关系上显著优于线性回归模型；&lt;/li&gt;&#10;&lt;li&gt;短期预测（5 日）效果明显优于中长期预测（10 日、20 日），随着预测周期的拉长，模型预测能力迅速下降。&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;此外，金融市场复杂多变，单只股票的分析结果存在一定局限性。未来可进一步扩展到多只股票、纳入更多宏观因子和情绪指标，以构建更稳健的预测体系。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item></channel></rss>