Using a for loop to perform multiple regressions
I am currently doing style analysis using the following method: http://www.r-bloggers.com/style-analysis/ . This is a limited regression of a single asset on multiple criteria, over a rolling 36-month window.
My problem is that I need to perform this regression for quite a large number of assets, and it will take a huge amount of time to do it one by one. To be more precise: is there a way to say R for one-shot regression of columns 1-100 on columns 101-116. Of course, this also means printing 100 different charts, one for each asset. I'm new to R and have been stuck for days now.
Hopefully it doesn't matter that the following snippet is not reproduced as the code works as originally intended.
# Style Regression over Window, constrained
#--------------------------------------------------------------------------
# setup
load.packages('quadprog')
style.weights[] = NA
style.r.squared[] = NA
# Setup constraints
# 0 <= x.i <= 1
constraints = new.constraints(n, lb = 0, ub = 1)
# SUM x.i = 1
constraints = add.constraints(rep(1, n), 1, type = '=', constraints)
# 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], constraints )
style.weights[i,] = fit$coefficients
style.r.squared[i,] = fit$r.squared
}
# plot
aa.style.summary.plot('Style Constrained', style.weights, style.r.squared, window.len)
Thanks for the advice!
source to share
"Is there a way to tell R to rewrite columns 1-100 once in columns 101-116."
Yes! You can use a for loop, but you also have a whole bunch of "apply" functions that are appropriate. Here's a generalized solution with a random / toy dataset and using lm()
, but you can use any regression function.
# data frame of 116 cols of 20 rows
set.seed(123)
dat <- as.data.frame(matrix(rnorm(116*20), ncol=116))
# with a for loop
models <- list() # empty list to store models
for (i in 1:100) {
models[[i]] <-
lm(formula=x~., data=data.frame(x=dat[, i], dat[, 101:116]))
}
# with lapply
models2 <-
lapply(1:100,
function(i) lm(formula=x~.,
data=data.frame(x=dat[, i], dat[, 101:116])))
# compare. they give the same results!
all.equal(models, models2)
# to access a single model, use [[#]]
models2[[1]]
source to share