I have an excel files with 12 columns. I need to regress six of these on one column (i.e. six univariate linear regressions.) I would like to write a loop which does the regressions, and then store all the summary statistics (intercept
, beta
, R^2
, standard deviations
, statistical significance
) in a matrix and print all of them at once.
I can run the regressions separately if need be but I am trying to do it more efficiently. I looked at this How to Loop/Repeat a Linear Regression in R but I couldn't figure it out.
I tried writing the following loop:
for(i in [,7]:[,12]) {
lmoutput<-lm(i~rm)
print(lmoutput)
}
but it doesn't work. I get the following error:
#for(i in [,7]:[,12]) {
#Error: unexpected '[' in "for(i in ["
# lmoutput<-lm(i~rm)
#Error in model.frame.default(formula = i ~ rm, drop.unused.levels = TRUE) :
# variable lengths differ (found for 'rm')
# print(lmoutput)
#Error in print(lmoutput) : object 'lmoutput' not found
#}
#Error: unexpected '}' in "}"
Columns 7-12 are the ones I have to regress on column 2, which I named rm
.
I would appreciate any help.