Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we combine predictors from two different linear models into one?

Tags:

r

statistics

I was trying to automate a final step model building. I would like to combine predictors from two separate models into one final model. I played around with update.formula() but realized I can update an old lmfit$call to a new one, e.g update.formula(lmfit$call,lmfitnew$call). here i need to cherry pick variables from both models and run the final one

lmfit1 <- lm(y~ x1+x2+x3, data = modelready)
best.ngc_fit <- stepAIC(lmfit1, direction="backward")
best.ngc_fit$call

lm(formula = y~ x2+x3, data = modelready)

lmfit2 <- lm(y ~ a+b+c+d+f, data=fcstmodel)
best.fcst_fit <- stepAIC(lmfit2, direction ="backward")
best.fcst_fit$call

lm(formula = y~ a+c+d+f, data = fcstmodel)

This is what I would like to have in my final model

best.full_fit <- lm(y~x2+x3+a+c+d+f, data = fullmodel) 

I can do it manually without a problem, but I would like to automate it in order to make the whole process less tedious.

Any help will be much appreciated

like image 446
Anand Avatar asked Feb 14 '12 19:02

Anand


2 Answers

If this is just a matter of extracting the components of each model and combine them into a new design matrix, then the following should work, irrespective of the fact you used stepAIC:

dfrm <- data.frame(y=rnorm(100), replicate(7, rnorm(100)))
lm1 <- lm(y ~ X1+X2+X3, dfrm)
lm2 <- lm(y ~ X5+X7, dfrm)
lm1.fm <- attr(terms(lm1), "term.labels")
lm2.fm <- attr(terms(lm2), "term.labels")
lm3.fm <- as.formula(paste("y ~ ", paste(c(lm1.fm, lm2.fm), collapse= "+")))
lm3 <- lm(lm3.fm, dfrm)

To fix the ideas, here we have

> names(dfrm)
[1] "y"  "X1" "X2" "X3" "X4" "X5" "X6" "X7"
> lm3.fm
y ~ X1 + X2 + X3 + X5 + X7

See help(terms.object) to get more information on what it returns. With your example, you'll need to replace lm1 with best.ngc_fit and lm2 with best.fcst_fit.

like image 143
chl Avatar answered Oct 18 '22 00:10

chl


For more advanced manipulation of fomulas you can use Formula package.

formula(as.Formula(terms(lm1),formula(Formula(terms(lm2)), lhs=0)), collapse=TRUE)

y ~ X1 + X2 + X3 + (X5 + X7)
like image 44
Wojciech Sobala Avatar answered Oct 18 '22 01:10

Wojciech Sobala