coriolis
coriolis

Reputation: 33

Using a for loop for performing several regressions

I am currently performing a style analysis using the following method: http://www.r-bloggers.com/style-analysis/ . It is a constrained regression of one asset on a number of benchmarks, over a rolling 36 month window.

My problem is that I need to perform this regression for a fairly large number of assets and doing it one by one would take a huge amount of time. To be more precise: Is there a way to tell R to regress columns 1-100 one by one on colums 101-116. Of course this also means printing 100 different plots, one for each asset. I am new to R and have been stuck for several days now.

I hope it doesn't matter that the following excerpt isn't reproducible, since 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)

Thank you very much for any tips!

Upvotes: 3

Views: 540

Answers (1)

arvi1000
arvi1000

Reputation: 9582

"Is there a way to tell R to regress columns 1-100 one by one on colums 101-116."

Yes! You can use a for loop, but you there's also a whole family of 'apply' functions which are appropriate. Here's a generalized solution with a random / toy dataset and using lm(), but you can sub in whatever regression function you want

# 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]]

Upvotes: 2

Related Questions