Amirgiano
Amirgiano

Reputation: 77

For Loop for Regression in R?

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.

Upvotes: 0

Views: 91

Answers (2)

Amirgiano
Amirgiano

Reputation: 77

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(..).

Upvotes: 0

Roland
Roland

Reputation: 132969

You can fit a model with multiple dependent variables simultaneously.

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

Upvotes: 1

Related Questions