Solved – Compare linear models with and without an interaction effect

I want to compare the following two linear models:

model 1: y = mean + A + B   model 2: y = mean + A + A*B 

Is model 2 equivalent to y = mean + A + B + A*B? Can I use anova(model1, model2) in R to compare the two nested models?

If not, how can I compare them in R?

In R, if you want to test the interaction then you want

m1 <- lm(y ~ A + B, data = foo) m2 <- lm(y ~ A * B, data = foo) 

then

anova(m1, m2) 

will give an F test for the interaction.

Note that m2 could be written longhand as

m2 <- lm(y ~ A + B + A:B, data = foo) 

wherein the nesting become clear as A:B is the interaction term and its coefficient would be = 0 in m1. Note the intercept/constant term is implied in these model formulae.

Note that in

m2 <- lm(y ~ A + A*B) 

the first A is redundant as A*B expands to A + B + A:B. R is clever enough to work out you don't want the following model A + A + B + A:B and only includes A once.

In R, A and B would be factors containing a recording of the label or type of each observation. If by your notation you have 3 levels/types in A and 4 in B, then A and B would be factor vectors with length(levels(A)) and length(levels(B)) equal to 3 and 4 respectively. R will create the design matrix for the model from the symbolic representation you provided via the formula. To see what R is doing, you can do head(model.matrix(~ A + A*B, data = foo)) to see the first 6 rows of the design matrix. This is coded using treatment contrasts but others are available. See ?contr.sum for details on the various contrast types built into R.

Similar Posts:

Rate this post

Leave a Comment