bmciv
bmciv

Reputation: 144

How to drop some interaction terms from R formula

I would like to drop some interaction terms from an R formula. My situation is that I have one factor variable with lots of levels (call this A, and it takes values from 1-50), and another continuous variable that I would like to interact it with (call this B).

A*B

creates terms A1:B, A2:B, A3:B,... I want a simple way to get rid of the first A1:B term.

Note: I saw some previous answers for the lm case that called update and then dropped some terms. This will not work for me as I am trying to estimate a multinomial logit model with the mlogit package, and I cannot make the first estimation without dropping some interactions.

Edit: Although I am not attempting to use lm, if I could the following to occur, then I think it would solve my problem.

dd<-data.frame(A=sample(letters[1:10], 100, replace=T),B = runif(100),z=rexp(100))

#need to drop B term below
reg1 <- lm(z~A*B, dd)

#or need to drop Aa:B term here
reg2 <- lm(z~A*B - B, dd)

#but this doesn't work (I realize why, but this is an
#example of what I would like to have happen)
reg3 <- lm(z~A*B - B - Aa:B, dd)

Upvotes: 1

Views: 2451

Answers (1)

MrFlick
MrFlick

Reputation: 206616

I think you should be able to work with contrasts her to make this happen. Here we create our own contrast that adjusts the default contrast.treament behavior to skip the first two variables.

contr.skip2 <- function (n, contrasts = TRUE, sparse = FALSE) 
{
    contr <- contr.treatment(n, 1, contrasts, sparse)
    contr[2, ] <- 0
    contr[, -1]
}

and then we can fit the model and pass along our special contrast

lm(z~A*B, dd, contrasts=list(A="contr.skip2"))

# Call:
# lm(formula = z ~ A * B, data = dd, contrasts = list(A = "contr.skip2"))
# 
# Coefficients:
# (Intercept)           Ac           Ad           Ae           Af           Ag           Ah  
#     1.09981     -0.14541     -0.86334     -0.18478     -0.77302      0.19681      0.23845  
#          Ai           Aj            B         Ac:B         Ad:B         Ae:B         Af:B  
#    -0.74962     -0.49014      0.09729      0.14705      1.09606      0.14706      0.88919  
#        Ag:B         Ah:B         Ai:B         Aj:B  
#    -0.62796     -0.70155      1.60253     -0.20564 

and as you can see we no longer have Ab terms in the model.

Upvotes: 1

Related Questions