[R] Fitting model with varying number of predictors

Frank E Harrell Jr f.harrell at vanderbilt.edu
Fri Nov 18 14:04:51 CET 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)

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




More information about the R-help mailing list