Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 通过quantmod滚动窗口回归对回归模型精度的回溯检验_R_Regression_Xts_Zoo_Quantmod - Fatal编程技术网

R 通过quantmod滚动窗口回归对回归模型精度的回溯检验

R 通过quantmod滚动窗口回归对回归模型精度的回溯检验,r,regression,xts,zoo,quantmod,R,Regression,Xts,Zoo,Quantmod,我一直在尝试通过实施滚动窗口回归,并在一列中计算和记录过去每一天的估计值与最后可用日期之间的差异,来对回归的可预测性进行回溯测试(尝试获得提前一步的预测) 我试着用Christoph_J的答案 代码中没有语法错误。然而,我不确定是否有语义错误。“预测”列的行i中的值是否为OpCl列的行i值的事前预测 library(zoo) library(dynlm) library(quantmod) sp <- getSymbols("^GSPC", auto.assign=FALSE) sp$G

我一直在尝试通过实施滚动窗口回归,并在一列中计算和记录过去每一天的估计值与最后可用日期之间的差异,来对回归的可预测性进行回溯测试(尝试获得提前一步的预测)

我试着用Christoph_J的答案

代码中没有语法错误。然而,我不确定是否有语义错误。“预测”列的行
i
中的值是否为
OpCl
列的行
i
值的事前预测

library(zoo) 
library(dynlm)
library(quantmod)
sp <- getSymbols("^GSPC", auto.assign=FALSE)
sp$GSPC.Adjusted <- NULL
colnames(sp) <- gsub("^GSPC\\.","",colnames(sp))

sp$Number<-NA
sp$Number<-1:nrow(sp)

sp$OpCl <- OpCl(sp) 
sp$ClHi <- HiCl(sp) 
sp$LoCl <- LoCl(sp) 
sp$LoHi <- LoHi(sp) 

#### LAG

spLag <- lag(sp)
colnames(spLag) <- paste(colnames(sp),"lag",sep="")
sp <- na.omit(merge(sp, spLag))

### REGRESSION

f <- OpCl ~ Openlag + Highlag + OpCllag + ClHilag 

OpClLM <- lm(f, data=sp)

#sp$OpClForecast <- NA
#sp$OpClForecast <- tail(fitted(OpClLM),1)

#####################################################

rolling.regression <- function(series) {
mod <- dynlm(formula = OpCl ~ L(Open) + L(High) + L(OpCl) + L(ClHi), 
data = as.zoo(series))    

nextOb <- min(series[,6])+1 # To get the first row that follows the window
if (nextOb<=nrow(sp)) {   # You won't predict the last one

# 1) Make Predictions
predicted=predict(mod,newdata=data.frame(OpCl=sp[nextOb,'OpCl'],
Open=sp[nextOb,'Open'],High=sp[nextOb,'High'],


OpCl=sp[nextOb,'OpCl'], ClHi=sp[nextOb,'ClHi']))
attributes(predicted)<-NULL

#Solution ; Get column names right
c(predicted=predicted, 
AdjR = summary(mod)$adj.r.squared)
}
}

rolling.window <- 300
results.sp <-  rollapply(sp, width=rolling.window, 
FUN=rolling.regression,  by.column=F, align='right')
sp<-cbind(sp,results.sp)

View(sp)
图书馆(动物园)
图书馆(dynlm)
图书馆(quantmod)
服务提供商