简体   繁体   中英

How to add one variable each time into the regression model?

I have a question about how to add one variable each time into the regression model to evaluate the adjusted R squared.

For example,

lm(y~x1)  

next time, I want to do

lm(y~x1+x2) 

and then,

lm(y~x1+x2+x3)

I tried paste, it does not work. for example, lm(y~paste("x1","x2",sep="+")) .

Any idea?

Assuming you fit 3 variables to your linear regression model: x1, x2 and x3

lm.fit1 = lm(y ~ x1 + x2 + x3)

Introducing an additional variable (x4) can be achieved by using the update function:

lm.fit2 = update(lm.fit1, .~. + x4)

You could even introduce an interaction term if required:

lm.fit3 = update(lm.fit2, .~. + x2:x3)

Further details on adding variables to regression models can be obtained here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM