1

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)
bmciv
  • 144
  • 6
  • I'm not sure I understand. When you do `dd<-data.frame(A=sample(letters[1:10], 100, replace=T), B = runif(100), z=rexp(100)); lm(z~A*B, dd)` you'll see that there is no estimate for `Aa` or `Aa:B` because those are not distinguisable when you have an intercept term. So are you fitting a model where this is not automatically being dropped? A [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) would certainly help here. – MrFlick Sep 08 '14 at 22:44
  • 2
    So what do you want to happen to those observations? Are they to become a reference category and absorbed into the intercept? – bstockton Sep 08 '14 at 22:51
  • Instead of using lm, I am trying to estimate a multinomial logit model using the mlogit package. Basically, you have to make some identification assumptions for mlogit (1 additional coefficient needs to be set equal to 0). Unlike lm, where if you have perfect collinearity it will drop one of the variables for you / report NA coefficient, mlogit will not converge. Hope this helps, but at any rate, I just need to drop one of the interaction terms. – bmciv Sep 08 '14 at 23:05
  • @MrFlick, thank you for your comment. In your example, I would like to also remove B from the formula. I have extended my question to your example. I think it is clearer now, but am happy to clarify again if needed. – bmciv Sep 08 '14 at 23:17

1 Answers1

1

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.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Had to use this to add variables to df because I couldn't use contrasts directly in mlogit, but this idea worked for me. Thank you. – bmciv Sep 10 '14 at 04:19
  • I'd be surprised if mlogit didn't call model.frame before regression. You can also assign contrasts to factors directly. Since you posted an lm() example, that's what I tested on. Next time it would probably be better to create a reproducible example closer to your actual situation. – MrFlick Sep 10 '14 at 04:25