*Building models

In R building and changing a model is an interactive and iterative process.

Assume that we start with this model

model1 <- lm(infmor ~ urb + gnpcap + doct)store it as a object we will modify later using appropriate functions
summary(model1)Display the regression table

infmor ~ urb + gnpcap + doct) is a formula (click on the link if you do not know what this means, as this is essential for what follows.

Adding and removing terms from a regression

Nested/hierarchical regression is not implemented as a specific method, you simply add or remove variables you consider as "blocks" to be dealt together simultaneously.

model2 = update(model1, .~.,-doct)Take the initial model and remove doct. .~. this syntax means the same dependent and independents (dot means the same
model3=update(model1,log(.)~.)Same model, but log of dependent
model4=update(model1,~.~,+lit - gnpcap) Add lit and remove gnpcap
Stepwise regression

Stepwise regression as well works from a model you have already defined as above.

There are two basic functions add1 and drop1 to add and remove variables from a model of type lm or glm. step calls these functions repeatedly

step(model1, direction="backward")
step(model1, direction="forward")

Other variable selection functions are available

Package {MASS}

model <- lm(infmor~urb+gnpcap+lit,data=world)Model as usual
model1 <- stepAIC(model, direction="both")Use stepAIC()
summary(model1) Display regression table

All Subsets regression: Package {leaps}

summary(model)Display results
plot(model,scale="r2")table of models, ordered by r2
subsets(leaps, statistic="rsq") {car} alternative model plots from {car}
See also