移动窗口逐步回归预测

时间:2016-01-04 17:16:50

标签: r forecasting

我正在尝试执行以下操作:

  1. 使用移动的24个月窗口拟合逐步回归模型
  2. 在每个步骤中,预测基于模型的退货1个月
  3. 计算每个预测的标准误差
  4. 绘制实际回报,预测回报,标准错误
  5. 我的数据如下(第一个是依赖,第二个和第三个是自变量) - 我无法让格式看起来更好,并且目前无法使用dropbox来附加文件:

    FundA.Return    Explanatory.Variable 1  Explanatory.Variable 2
    2.17%       1.96%           -2.34%
    0.35%       -2.11%          -2.00%
    1.18%       0.29%           -0.09%
    2.23%       0.47%           1.35%
    2.38%       2.04%           3.73%
    1.39%       -0.39%          -1.54%
    -0.63%      -0.16%          -4.20%
    -2.04%      -0.18%          0.53%
    1.80%       0.23%           -1.85%
    2.19%       0.66%           1.10%
    -0.22%      -1.59%          3.68%
    0.68%       -0.12%          -1.96%
    0.75%       -0.27%          -1.76%
    3.41%       0.52%           -0.42%
    -1.18%      -0.75%          3.72%
    -0.17%      0.36%           3.49%
    1.98%       0.85%           1.77%
    1.21%       -0.33%          4.04%
    -1.38%      0.17%           -3.73%
    -0.87%      -0.37%          0.27%
    -4.40%      2.20%           -0.75%
    -1.18%      6.31%           -0.56%
    0.43%       -1.28%          -2.12%
    0.24%       3.11%           -2.58%
    2.27%       0.15%           6.85%
    0.36%       0.49%           1.94%
    0.91%       -0.38%          -1.18%
    1.69%       1.18%           3.21%
    3.17%       3.39%           3.06%
    1.46%       3.93%           -1.74%
    

    我的代码如下(稍微编辑但我仍然收到错误):

    library(ISLR)
    Returns <- read.csv("Returns New Factors2.csv",header=TRUE)
    library(leaps)
    P <- nrow(Returns)-24
    results <- rep(0,P)
    for(i in 1:P){
      stepfit <- regsubsets(FundA.Return~.,data=Returns[i:24+i,],method="forward")
      coefi <- coef(stepfit,id=1)
      pred = model.matrix[P-24-i+1,]%*%coefi
      summary(coefi)
      SE[i+1] = ((Returns$FundA.Return[i+1]-pred)^2)
      plot(FundA.Return,pred,SE)
    }
    

    我收到以下错误。我做错了什么?

    model.matrix中的错误[P - 24 - i + 1,]:   'closure'类型的对象不是可子集化的

0 个答案:

没有答案