[R-sig-ME] contrast sum with interaction

René b|mono@om @end|ng |rom gm@||@com
Sun Jul 18 17:09:37 CEST 2021

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?


model.matrix(y~ cont.var+cont.var:disc.var  ,data=dat,contrasts.arg =

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


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 using uw.edu>:

> Not specifically a glmmTMB question, but maybe someone here can help.
> I'd like to use a contrast sum constraint with an interaction between a
> continuous and discrete variable, but I can't seem to get model.matrix to
> do what I want.
> The situation is that I want a categorical variable that is coded with a
> sum constraint but only included in some rows of the model matrix (which is
> why I am using the interaction with the continuous 0/1 valued variable).
> Maybe there is a better way to code this.
> Here is an example of what I mean:
> dat<-data.frame(y=1,cont.var=rep(0:1,each=2),disc.var=rep(c("a","b"),2));
> model.matrix(y~ cont.var  : disc.var  ,data=dat,contrasts.arg =
> list(disc.var="contr.sum"))
> What I'd like in this example is a model matrix with two columns, where the
> second column has 0 when cont.var =0, 1 when cont.var=1 & disc.var = a, and
> -1 when cont.var=1 and disc.var=b.
> Thanks so much for your help! -Mark
> --
> Mark Sorel
> Doctoral Student
> Washington Cooperative Fish and Wildlife Research Unit
> School of Aquatic and Fishery Sciences
> University of Washington
> Cell: 607-351-7352
> He/ him/ his
> *The University of Washington acknowledges the Coast Salish peoples of this
> land, the land which touches the shared waters of all tribes and bands
> within the Suquamish, Tulalip and Muckleshoot nations*.
>         [[alternative HTML version deleted]]
> _______________________________________________
> R-sig-mixed-models using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models

	[[alternative HTML version deleted]]

More information about the R-sig-mixed-models mailing list