Question

How would one produce a matrix that looks like this using model.matrix?

      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    1    1    0    0    0    0
 [2,]    1    2    0    0    0    0
 [3,]    1    3    0    0    0    0
 [4,]    0    0    1    1    0    0
 [5,]    0    0    1    2    0    0
 [6,]    0    0    1    3    0    0
 [7,]    0    0    0    0    1    1
 [8,]    0    0    0    0    1    2
 [9,]    0    0    0    0    1    3

I produced the first matrix by

fit = lmer(Temp ~ 1 + (1 + Time|Id), data = Data) 
getME(fit, name = c("Z"))

Where

Time = rep(1:3, 3)

And Id

Id = c(0L, cumsum(diff(Time) < 0))

This is as close as I could get.

id = rep(c("a","b","c"),each = 3)
Z = model.matrix(~0+id)

  [,1] [,2] [,3]
1    1    0    0
2    1    0    0
3    1    0    0
4    0    1    0
5    0    1    0
6    0    1    0
7    0    0    1
8    0    0    1
9    0    0    1
Was it helpful?

Solution

i'm not sure I get what you try to do, but does this work for you?

val = rep(1:3,3)
z = model.matrix(~0+id+id:val)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top