Dear R-ers, I have a data frame data with predictors x1 through x5 and the response variable y. I am running a simple regression: reg<-lm(y~x1, data=data) I would like to loop through all predictors. Something like: predictors<-c("x1","x2",... "x10) for(i in predictors){ reg<-lm(y~i) etc. } But it's not working. I am getting an error: Error in model.frame.default(formula = Y ~ x1 + x2 + x3 + i, data = sample, : variable lengths differ (found for 'i') How can I make it take predictor names in the lm formula? Thank you! -- Dimitri Liakhovitski Ninah.com Dimitri.Liakhovitski at ninah.com
Dimitri - Without commenting on the wiseness of such an approach, here's one way to do what you want: regs = lapply(predictors,function(var)lm(data$y~data[,var])) names(regs) = predictors Now regs[['x1']] holds the lm output from the regression of y on x1, regs[['x2']] holds the lm output from the regression of y on x2, etc. Suppose you wanted to know what the slopes for each regressor were. First, find what you want for one: coef(regs[['x1']])[2] Next, write a function to extract this information: getslope = function(reg)coef(reg)[2] Now use sapply to get all the slopes of the individual regressions: sapply(regs,getslope) Hope this helps. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Tue, 9 Mar 2010, Dimitri Liakhovitski wrote:> Dear R-ers, > I have a data frame data with predictors x1 through x5 and the > response variable y. > I am running a simple regression: > > reg<-lm(y~x1, data=data) > > I would like to loop through all predictors. Something like: > predictors<-c("x1","x2",... "x10) > for(i in predictors){ > reg<-lm(y~i) > etc. > } > > But it's not working. I am getting an error: > Error in model.frame.default(formula = Y ~ x1 + x2 + x3 + i, data = sample, : > variable lengths differ (found for 'i') > > How can I make it take predictor names in the lm formula? > Thank you! > > -- > Dimitri Liakhovitski > Ninah.com > Dimitri.Liakhovitski at ninah.com > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
I already answered a very similar question earlier today. It was in the context of a question about a different regression function, but the formula methods are generic. Look for "Question on passing in parameter to Cox hazard". (That does not mean I approve of such fishing expeditions.) -- David. On Mar 9, 2010, at 6:26 PM, Dimitri Liakhovitski wrote:> Dear R-ers, > I have a data frame data with predictors x1 through x5 and the > response variable y. > I am running a simple regression: > > reg<-lm(y~x1, data=data) > > I would like to loop through all predictors. Something like: > predictors<-c("x1","x2",... "x10) > for(i in predictors){ > reg<-lm(y~i) > etc. > } > > But it's not working. I am getting an error: > Error in model.frame.default(formula = Y ~ x1 + x2 + x3 + i, data = > sample, : > variable lengths differ (found for 'i') > > How can I make it take predictor names in the lm formula? > Thank you! > > -- > Dimitri Liakhovitski > Ninah.com > Dimitri.Liakhovitski at ninah.com >David Winsemius, MD West Hartford, CT
Maybe Matching Threads
- randomForest: predictor importance (for regressions)
- replacing period with a space
- referring to a row number and to a row condition, and to columns simultaneously
- Code is too slow: mean-centering variables in a data frame by subgroup
- More elegant way of excluding rows with equal values in any 2 columns?