0

I'm still learning R and I need some advice regarding very simple matter.

for (i in 1:6) {
model.i = lm(data1[,i+1] ~ data1[,"mkt"]+data1[,"riskfree"])
print(summary(model(i)))
print(anova(model(i)))
}

I want to make six different simple linear regression, and assign different linear regressions to different names; like model.1, model.2, model.3......

But what I wrote there doesn't work as I expected. I would appreciate your help.

Thanks

Jay YK Kim
  • 11
  • 1
  • 3
    This kind of question has been asked many times before, here is [one good example](http://stackoverflow.com/q/2679193/271616). In general, if you think you need to dynamically assign to different variable names, you're probably doing it wrong. – nograpes Sep 24 '13 at 20:00

1 Answers1

2

Put them in a list:

lapply((1:6), function(i) lm(data1[,i+1] ~ data1[,"mkt"]+data1[,"riskfree"]))

It's much easier.

Thomas
  • 43,637
  • 12
  • 109
  • 140