I am running 503 separate regressions, each with a separate dependent variables, with 3 independent variables and 1 AR(1) term.
Data:
# fake data
set.seed(333)
df <- data.frame(seq(as.Date("2017/1/1"), as.Date("2017/2/19"), "days"),
matrix(runif(50*506), nrow = 50, ncol = 506))
names(df) <- c("Date", paste0("var", 1:503), c("mktrf", "smb", "hml"))
I create the AR(1) process as follows, using a function called lagpad:
lagpad <- function(x, k=1) {
i<-is.vector(x)
if(is.vector(x)) x<-matrix(x) else x<-matrix(x,nrow(x))
if(k>0) {
x <- rbind(matrix(rep(NA, k*ncol(x)),ncol=ncol(x)), matrix(x[1:(nrow(x)-k),], ncol=ncol(x)))
}
else {
x <- rbind(matrix(x[(-k+1):(nrow(x)),], ncol=ncol(x)),matrix(rep(NA, -k*ncol(x)),ncol=ncol(x)))
}
if(i) x[1:length(x)] else x
}
Then I store my necessary variables for regression:
All the dep var
x = df[,505:507]
All the indep var
y <- df[,2:504]
AR(1) process
y_lag <- lagpad(y, -1)
Fit all the models
list_models_AR= lapply(y, function(y) with(x, lm(y ~ mktrf + smb + hml + y_lag, na.action = na.exclude)))
I'm having trouble figuring out how to use lapply
in this case, since there are multiple components of y_lag that need to be called, one for each regression.
I am not sure of your lag term, it seems to be actually the next y-column. If so you can create 502 models as follows:
list_models_AR= lapply(1:(ncol(y)-1), function(i) lm(y[,i]~x[,1] + x[,2] + x[,3] +y[,i+1], na.action=na.exclude))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.