The following example may help explain the difference between interactions of numeric and factor variables versus interaction of two factor variables. To make it less confusing I'm not using an individual covariate like in your example.
Create a dataframe with 4 records with each combination of 0:1 and A,B. First w=0,2 is a numeric and then a factor variable. I used 0,2 to make use of numeric clear, otherwise the DM looks the same as a factor variable.
- Code: Select all
> df=data.frame(w=rep(c(0,2),2),x=factor(c("A","B","B","A")))
> df
w x
1 0 A
2 2 B
3 0 B
4 2 A
Full interaction of w and x. First column is intercept (value when w=0), second column is slope for w when x=A, third column is effect of x=B relative to A, and fourth column is difference in slope for w when x=B relative to slope for w when x=A. This is a model in which both slope and intercept can differ for A and B.
- Code: Select all
> model.matrix(~w*x,df)
(Intercept) w xB w:xB
1 1 0 0 0
2 1 2 1 2
3 1 0 1 0
4 1 2 0 0
attr(,"assign")
[1] 0 1 2 3
attr(,"contrasts")
attr(,"contrasts")$x
[1] "contr.treatment"
Interaction only of x and w. Firs column is intercept (value when w=0 for both A and B), the second is the slope for A and the third is the slope for B. These slopes are no longer relative to each other. This would be a linear model with constant intercept but different slopes.
- Code: Select all
> model.matrix(~w:x,df)
(Intercept) w:xA w:xB
1 1 0 0
2 1 0 2
3 1 0 0
4 1 2 0
attr(,"assign")
[1] 0 1 1
attr(,"contrasts")
attr(,"contrasts")$x
[1] "contr.treatment"
Now treat w as a factor variable with 2 levels of 0 and 2. When you interact 2 factor variables with 2 levels each you should get 2*2=4 parameters - one for each of the combinations. We no longer have slopes we simply get means - for each combination.
Below is the full interaction model. First column is the overall intercept, second column is the effect of w=2 value (no longer twice the effect), third column is the effect of B, and fourth column is the effect of B for w=2.
- Code: Select all
> model.matrix(~as.factor(w)*x,df)
(Intercept) as.factor(w)2 xB as.factor(w)2:xB
1 1 0 0 0
2 1 1 1 1
3 1 0 1 0
4 1 1 0 0
attr(,"assign")
[1] 0 1 2 3
attr(,"contrasts")
attr(,"contrasts")$`as.factor(w)`
[1] "contr.treatment"
attr(,"contrasts")$x
[1] "contr.treatment"
Now if you did the interaction only it gives you 5 columns which is too many parameters. The intercept is not needed because all you want is the 4 unique values which gives an identity design matrix if you rearrange columns 2-5:
- Code: Select all
> model.matrix(~as.factor(w):x,df)
(Intercept) as.factor(w)0:xA as.factor(w)2:xA as.factor(w)0:xB as.factor(w)2:xB
1 1 1 0 0 0
2 1 0 0 0 1
3 1 0 0 1 0
4 1 0 1 0 0
attr(,"assign")
[1] 0 1 1 1 1
attr(,"contrasts")
attr(,"contrasts")$`as.factor(w)`
[1] "contr.treatment"
attr(,"contrasts")$x
[1] "contr.treatment"
To remove the intercept for this type of DM, just include -1 which removes the intercept.
- Code: Select all
> model.matrix(~-1+as.factor(w):x,df)
as.factor(w)0:xA as.factor(w)2:xA as.factor(w)0:xB as.factor(w)2:xB
1 1 0 0 0
2 0 0 0 1
3 0 0 1 0
4 0 1 0 0
attr(,"assign")
[1] 1 1 1 1
attr(,"contrasts")
attr(,"contrasts")$`as.factor(w)`
[1] "contr.treatment"
attr(,"contrasts")$x
[1] "contr.treatment"
>
$x
[1] "contr.treatment"
>
The confusing aspect here is that if your numeric variable is 0/1 like winter, the numeric variable is being used simply to restrict the effect of an individual covariate to particular times. You only use the -1 when you are interacting two factor variables. And as it explains in the documentation that approach will not work if you have something like ~-1+y+x:w where x,y and w are factor variables. In that case you need to use the remove.intercept argument. To convince yourself of that create a dataframe with 3 factor variables and try model.matrix with the formula.