【发布时间】:2016-09-21 15:07:49
【问题描述】:
我需要关于如何获得结果的建议 我的回归分析到一个对象。
我不想逐行执行回归分析 有 20 天的窗口期。 对象 Slope 应将每天回归分析的结果(斜率)保存在窗口中。
#Loading Library
require(quantmod)
#Initiation of Example
mc_result <- matrix(sample(c(1:200)), ncol = 200, nrow =1)
mc_result1 <- matrix(sample(c(1:200)), ncol =200, nrow =1)
mc_result <- rbind(mc_result, mc_result1)
a <- c(1:200)
Slope <- matrix(ncol=2, nrow=181)
注意这个循环不起作用。 循环应该逐行应用 Rollapply 并将每天的结果保存在对象 Slope 中。
但是,结果应该是这样的,但斜率值会发生变化。目前斜率值是稳定的,我不知道为什么。
for (i in 1:2) {
Slope[,i] <- rollapply(data =mc_result[i,], width=20,
FUN = function(z)
summary(lm(mc_result[i,] ~ a, data = as.data.frame(z)))$coefficients[2], by.column = FALSE)
}
【问题讨论】: