Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

Making linear models in a for loop using R programming

I have a dataset that I'll call dataset1 with a predictor variable (e.g. Price). I'm hoping to get a nice single predictor of price based on the n other predictors that exist in the dataset. But if n is large, I can't manually make and examine all these models, so I was hoping to use something like this:

for (i in names(dataset1)) {
    model = lm(Price~i, dataset1)
    # Do stuff here with model, such as analyze R^2 values.
}

(I thought this would work since replacing the inside of the for loop with print(i) results in the correct names.) The error is as follows:

Error in model.frame.default(formula = Price ~ i, data = dataset1, drop.unused.levels =    TRUE) : 
variable lengths differ (found for 'i')

Does anyone have advice for dealing with the problem regarding how R reads in the i variable? I know how to approach this problem using other software, but I would like to get a sense of how R works.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would go for some sort of *apply here personally:

dat <- data.frame(price=1:10,y=10:1,z=1:10)
sapply(dat[2:3], function(q) coef(summary(lm(dat$price ~ q)))[2])

 y  z 
-1  1 

or to get a list with full model results:

lapply(dat[2:3], function(q) coef(summary(lm(dat$price ~ q))))

$y
            Estimate   Std. Error       t value      Pr(>|t|)
(Intercept)       11 1.137008e-15  9.674515e+15 1.459433e-125
q                 -1 1.832454e-16 -5.457163e+15 1.423911e-123

$z
                Estimate   Std. Error      t value      Pr(>|t|)
(Intercept) 1.123467e-15 2.457583e-16 4.571429e+00  1.822371e-03
q           1.000000e+00 3.960754e-17 2.524772e+16 6.783304e-129

to get the r-squared value as you mentioned:

sapply(dat[2:3], function(q) summary(lm(dat$price ~ q))$r.squared) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...