Home > Net >  For Loop for Regression in R?
For Loop for Regression in R?

Time:11-16

I'm trying to run regression for the CAPM model. This is the formula Ri= a b(rmrf) e my Ri is XS1 I have XS1-XS10 and want to run the regression and store them all.

CodePudding user response:

You can fit a model with multiple dependent variables simultaneously.

reg <- lm(paste0("cbind(", paste0("XS", 1:10, collapse = ","), ") ~ rmrf"), 
          data = df)
summary(reg) 

CodePudding user response:

for (i in (1:10)){
  assign(paste0("reg_", i), lm(paste0("XS", i, "~rmrf", sep= ""),data=df))
 }

This is how you do it. paste0 helps you to add the i into your formula. And you will have reg_1 through reg_10 . assign helps you to assign to the first part the input of the formula from the second part. To reg_i the formula of lm(..).

  • Related