The accuracy of retouching a regression model by regressing a rolling window with a quantum

I tried to repeat the predictability of regression (trying to get predictions one step ahead) by introducing rolling regression and calculating and fixing the difference between the estimate and the last available day, for each day in the past, in the column.

I tried applying Christoph_J's answer in rolling regression returns multiple objects

There is no syntax error in the code. However, I am not sure if there is a semantic error. Is the value in row i of the predicted column a preliminary prediction of the value of row i of the OpCl column?

 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) 
+4
source share

All Articles