Skip to content
Prev 19405 / 20628 Next

contrast sum with interaction

Hi Mark,

"The situation is that I want a categorical variable that is coded with a
sum constraint but only included in some rows "

Almost sounds like you could also simply delete all cont=0 rows... But I
assume you want something like this?

dat<-data.frame(y=1,cont.var=rep(0:1,each=2),
                disc.var=rep(c("a","b"),2));
dat$disc.var<-factor(dat$disc.var,levels=c("a","b"))
dat$cont.var<-as.factor(dat$cont.var)

model.matrix(y~ cont.var+cont.var:disc.var  ,data=dat,contrasts.arg =
               list(disc.var="contr.sum",cont.var="contr.treatment"))

If you dont want to have the "cont.var0:disc.var1" column in this matrix,
then all you need is a numeric (nonfactor) "dummy variable" in the data
frame:

dat$dummy<-0
dat$dummy[dat$cont.var==1]<-1

which you then can multiply with the interaction in the actual model
formula: e.g.
glmr(y~ cont.var+cont.var:disc.var*dummy ...)
As dummy is continuous it will always set the interaction term to 0 if
cont=0; this means if cont=0 there will only be an intercept estimate which
then is the effective mean of both a & b together (if cont=0). Not sure you
want this...

Hope this helps.

Best, Ren?

Am Do., 15. Juli 2021 um 20:44 Uhr schrieb Mark Sorel <marks6 at uw.edu>: