How do you exclude interaction term in r lm?

3.3k Views Asked by At

I'm working with a model from the Prestige dataset in the car package in R.

library(car)
library(carData)

data = na.omit(Prestige)
prestige = data$prestige
income = data$income
education = data$education
type = data$type

I'm trying to fit the model lm(prestige ~ income + education + type + income:type + education:type). For class I'm starting with the full model and working down to a smaller model, just backward selection. One of the least useful covariates according to p-value is the education:typeprof. How do I just delete that covariate from the model without taking out all the education:type interactions? In general how do you exclude interactions with factors? I saw an answer with the update function specifying which interaction to exclude but it didn't work in my case. Maybe I implemented it incorrectly.

fit4 = lm(prestige ~ income + education + type + income:type + education:type)
newfit = update(fit4, . ~ . - education:typeprof)

Unfortunately this didn't work for me.

1

There are 1 best solutions below

0
On

So there is a way to drop a single interaction term. Suppose you have the linear model

fullmodel = lm(y_sim ~ income + education + type + income:type + education:type - 1)

You can call model.matrix on fullmodel which will give you the X matrix for your linear model. From there you can specify which column you'd like to drop and refit your model.

X = model.matrix(fullmodel)
drop = which(colnames(X) == 'education:typeprof')
X1 = X[,-1]
newfit = lm(presitge ~ X1 - 1)