I need to fit a number of models with different number of predictors in each model. Say for example, I have three predictors: x1, x2, x3 and I want to fit three models: lm(y~x1+x2) lm(y~x2+x3) lm(y~x1+x2+x3) Instead of typing all models, what I want is to create a variable which can take the right hand side of the models. I tried this with paste function. xxx <- paste("x1","x2",sep=+) for the first xxx <- paste("x2","x3", sep = +) for the second xxx <- paste("x1","x2","x2", sep = +) for the third and then fit a single model lm(y~xxx) It did not work. Please suggest how to do it. Thanks. Jun
Gabor Grothendieck
2005-Nov-18 05:59 UTC
[R] Fitting model with varying number of predictors
Try this: x <- data.frame(x1 = x1, x2 = x2, x3 = x3) lm(y ~., x[,1:2]) lm(y ~., x[,2:3]) lm(y ~., x[,1:3]) On 11/18/05, Juni Joshi <joshi_juni at hotmail.com> wrote:> > I need to fit a number of models with different number of predictors > in each model. Say for example, I have three predictors: x1, x2, x3 > and I want to fit three models: > > lm(y~x1+x2) > lm(y~x2+x3) > lm(y~x1+x2+x3) > > Instead of typing all models, what I want is to create a variable > which can take the right hand side of the models. I tried this with > paste function. > > xxx <- paste("x1","x2",sep=+) for the first > xxx <- paste("x2","x3", sep = +) for the second > xxx <- paste("x1","x2","x2", sep = +) for the third and then fit a > single model > > lm(y~xxx) > > It did not work. Please suggest how to do it. > > Thanks. > > Jun > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Prof Brian Ripley
2005-Nov-18 08:32 UTC
[R] Fitting model with varying number of predictors
On Fri, 18 Nov 2005, Juni Joshi wrote:> > I need to fit a number of models with different number of predictors > in each model. Say for example, I have three predictors: x1, x2, x3 > and I want to fit three models: > > lm(y~x1+x2) > lm(y~x2+x3) > lm(y~x1+x2+x3) > > Instead of typing all models, what I want is to create a variable > which can take the right hand side of the models. I tried this with > paste function. > > xxx <- paste("x1","x2",sep=+) for the firstThis gives a syntax error!> xxx <- paste("x2","x3", sep = +) for the second > xxx <- paste("x1","x2","x2", sep = +) for the third and then fit a > single model > > lm(y~xxx) > > It did not work. Please suggest how to do it.You want a formula here, and you also need to use code without syntax errors. For example> xxx <- paste("x1", "x2", sep="+") > lm(as.formula(paste("y ~", xxx)))But a better idea would be to put all your variables into a data frame DF and do use <- c("x1", "x2") # set as appropriate lm(y ~ ., data=DF[c("y", use)]) -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Frank E Harrell Jr
2005-Nov-18 13:04 UTC
[R] Fitting model with varying number of predictors
Juni Joshi wrote:> I need to fit a number of models with different number of predictors > in each model. Say for example, I have three predictors: x1, x2, x3 > and I want to fit three models: > > lm(y~x1+x2) > lm(y~x2+x3) > lm(y~x1+x2+x3)Stepwise variable selection has so many problems that it generally ruins what you are trying to accomplish. Trying many models leads to multiple coparison problems and bias in coefficient estimates, standard errors, R-squared. With only three models being compared it's not a huge issue but in general beware. Frank Harrell> > Instead of typing all models, what I want is to create a variable > which can take the right hand side of the models. I tried this with > paste function. > > xxx <- paste("x1","x2",sep=+) for the first > xxx <- paste("x2","x3", sep = +) for the second > xxx <- paste("x1","x2","x2", sep = +) for the third and then fit a > single model > > lm(y~xxx) > > It did not work. Please suggest how to do it. > > Thanks. > > Jun-- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University
Reasonably Related Threads
- help for performing regressions based on combination of predictors
- rms plot.Predict question: swapping x- and y- axis for categorical predictors
- Categorical Predictors for SVM (e1071)
- looping through predictors
- selecting significant predictors from ANOVA result