[R] formula, how to express for transforming the whole model.matrix, data=Orthodont

kjetil brinchmann halvorsen kjetil at entelnet.bo
Sun Mar 16 13:39:35 CET 2003


On 15 Mar 2003 at 6:22, Peng wrote:

I don't know of a way to do this with formulas. 
But what you want to do is simply multiply each row
of a numeric matrix (doesn't matter that the matrix was formed by 
a call to model.matrix) with a number, which for row i
can be taken as element i of a vector. 

This is the same as premultiply the matrix with a diagonal matrix 
with the constants on the diagonal, but that is not a good way to 
implement it!

I cannot see anything simpler/faster than a simple for loop:

> age <- round(runif(20,3,15))
> age
 [1]  8 14  8  4  5 10  4 13 13 11 13 15  6 15 10  4 14  5  9 13
> sex <- factor(rep(c("F","M"),10))
> sex
 [1] F M F M F M F M F M F M F M F M F M F M
Levels: F M
> mm <- model.matrix(~age*sex)
> mm
   (Intercept) age sexM age:sexM
1            1   8    0        0
2            1  14    1       14
3            1   8    0        0
4            1   4    1        4
5            1   5    0        0
6            1  10    1       10
7            1   4    0        0
8            1  13    1       13
9            1  13    0        0
10           1  11    1       11
11           1  13    0        0
12           1  15    1       15
13           1   6    0        0
14           1  15    1       15
15           1  10    0        0
16           1   4    1        4
17           1  14    0        0
18           1   5    1        5
19           1   9    0        0
20           1  13    1       13
attr(,"assign")
[1] 0 1 2 3
attr(,"contrasts")
attr(,"contrasts")$sex
[1] "contr.treatment"

> c <- 1:20
> for (i in 1:20) {
+      mm[i,] <- mm[i,]*c[i]}
> mm
   (Intercept) age sexM age:sexM
1            1   8    0        0
2            2  28    2       28
3            3  24    0        0
4            4  16    4       16
5            5  25    0        0
6            6  60    6       60
7            7  28    0        0
8            8 104    8      104
9            9 117    0        0
10          10 110   10      110
11          11 143    0        0
12          12 180   12      180
13          13  78    0        0
14          14 210   14      210
15          15 150    0        0
16          16  64   16       64
17          17 238    0        0
18          18  90   18       90
19          19 171    0        0
20          20 260   20      260
attr(,"assign")
[1] 0 1 2 3
attr(,"contrasts")
attr(,"contrasts")$sex
[1] "contr.treatment"


Kjetil Halvorsen

> Hi, R or S+ users,
> 
> I want to make a simple transformation for the model,
> but for the whole design matrix.
> The model is distance ~ age * Sex, where Sex is a
> factor. So the design matrix may look like the
> following:
>     (Intercept) age SexFemale age:SexFemale
> 1             1   8         0             0
> 2             1  10         0             0
> 3             1  12         0             0
> 4             1  14         0             0
> 5             1   8         0             0
> 6             1  10         0             0
> 7             1  12         0             0
> 8             1  14         0             0
> ... 
> 101           1   8         1             8
> 102           1  10         1            10
> 103           1  12         1            12
> 104           1  14         1            14
> 105           1   8         1             8
> 106           1  10         1            10
> 107           1  12         1            12
> 108           1  14         1            14
> 
> I want to have the whole design matrix transformed by
> a vector of multiplicator, say c(m1, m2, m3, ... ,
> m26, m27 ), for each Subject. Then the design matrix
> will look like this.
>     (Intercept)    age SexFemale age:SexFemale
> 1            m1    8m1         0             0
> 2            m1   10m1         0             0
> 3            m1   12m1         0             0
> 4            m1   14m1         0             0
> 5            m2    8m2         0             0
> 6            m2   10m2         0             0
> 7            m2   12m2         0             0
> 8            m2   14m2         0             0
> ... 
> 101         m26   8m26       m26          8m26
> 102         m26  10m26       m26         10m26
> 103         m26  12m26       m26         12m26
> 104         m26  14m26       m26         14m26
> 105         m27   8m27       m27          8m27
> 106         m27  10m27       m27         10m27
> 107         m27  12m27       m27         12m27
> 108         m27  14m27       m27         14m27
> 
> I tried to add a new column in the data, say "m". But
> the following formula expression does not work. Since
> Sex is a factor.
> y ~ m + I(m*age) + I(m*Sex) + I(m*age*Sex)
> Moreover, in order to implement the EM from "Robust
> Estimation in Linear Mixed-Effects Models
> Using the Multivariate t-Distribution" by Dr.
> Pinheiro, I also need to transform the response, y, in
> the same way. 
> I did it by writing SAS liked codes (making a new
> table, coding factors, finishing transformation,
> making groupedData, call lme), but that is not
> readable, and it will be complicated to realized that
> for any other datasets.
> 
> I am wondering whether the formula has some sorts of
> expression to realize transformation for the whole
> design matrix, especially when having factors in the
> formula.
> 
> Thanks a lot!
> 
> Peng
> 
> Peng Liu
> ------------------------------
> Peng Liu                      |
> Division of Statistics        |
> Northern Illinois University  |
> De Kalb, IL 60115, USA        |
> E-mail: pliu at math.niu.edu     |
> ------------------------------
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help



More information about the R-help mailing list