R 风格因子回归中的误差

R 风格因子回归中的误差,r,R,我试图调整R-bloggers提供的风格因素分析: 我刚开始学习R是为了解决大学里的一个项目,我在编码方面没有很好的经验,sry就是这样 当我运行代码R时,会显示以下错误消息: Error in lm.fit(x, y) : INTEGER() can only be applied to a 'integer', not a 'NULL' 你能给我一些提示如何解决这个问题吗??非常感谢。 更新:数据帧不同的原因: > nrow(hist.returns[window.index, 1

我试图调整R-bloggers提供的风格因素分析:

我刚开始学习R是为了解决大学里的一个项目,我在编码方面没有很好的经验,sry就是这样

当我运行代码R时,会显示以下错误消息:

Error in lm.fit(x, y) : INTEGER() can only be applied to a 'integer', not a 'NULL'
你能给我一些提示如何解决这个问题吗??非常感谢。

更新:数据帧不同的原因:

> nrow(hist.returns[window.index, 1])
NULL
也许你可以给我一些提示,告诉我如何修复这个数据帧

我的数据如下所示: 108个观察值内1个自变量和20个因变量

    Date   Fund     X1   X2     X3     X4    X5    X6     X7    X8    X9   X10    X11    X12    X13   X14   X15   X16   X17   X18   X19   X20
1   31/01/08  -8.77  -7.95 0.54   3.88   1.81  1.45  2.30  -4.65  2.60 -3.20  1.96  -6.67  -2.74  -7.95 -0.57  1.94  2.52 -2.36 -1.13 -0.12 -1.27
108 30/12/16 0.55 2.36 0.07 1.74 -0.78 0.59 0.74 1.85 2.88 -2.56 2.66 -0.74 -1.09 -2.94 -0.4 -0.24 0.58 1.82 0.33 0.85 0.88
这是我的实际代码:

# data upload
hist.returns <- read.csv('Upload1.csv', header=TRUE, sep=";")

# setup
ndates = nrow(hist.returns)
n = ncol(hist.returns)-1
window.len = 36

style.weights = hist.returns[, -1]
style.weights[] = NA
style.r.squared = hist.returns[, 1]
style.r.squared[] = NA

# main loop
for( i in window.len:ndates ) {
window.index = (i - window.len + 1) : i

fit = lm.constraint( hist.returns[window.index, -1], hist.returns[window.index, 1] )    
    style.weights[i,] = fit$coefficients
    style.r.squared[i,] = fit$r.squared
}

# plot 
aa.style.summary.plot('Style UnConstrained', style.weights, style.r.squared, window.len)
#数据上传
历史返回