-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrePCA_demo.jmd
129 lines (97 loc) · 3.13 KB
/
rePCA_demo.jmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
---
title: "rePCA() Demo"
author: "Reinhold Kliegl"
date: 2020-02-26
---
## Background
In the R-based `lme4` package, `rePCA()` results are not invariant with linear transformation of predictor
if based on (relative) covariance matrix. In the Julia-based `MixedModels` package, by default they are based
on the correlation, not the covariance matrix. The covariance-based version is available with
`MixedModels.PCA(model, corr=false)`.
## Packages
```julia
using DataFrames, DataFramesMeta, MixedModels, RCall
using StatsBase, StatsModels, BenchmarkTools
```
## Sleepstudy
```julia
MixedModels.datasets()
sleepstudy = MixedModels.dataset(:sleepstudy)
colnames = ["Subj", "days", "reaction"]
rename!(sleepstudy, Symbol.(colnames))
sleepstudy = @linq sleepstudy |>
transform(days2 = :days .*2,
days3 = :days ./2);
````
## LMMs
LMM `fm2` and `fm3` use a linear transformation of `days`
```julia
fm1 = fit(LinearMixedModel, @formula(reaction ~ 1 + days + (1 + days | Subj)), sleepstudy)
fm2 = fit(LinearMixedModel, @formula(reaction ~ 1 + days2 + (1 + days2 | Subj)), sleepstudy)
fm3 = fit(LinearMixedModel, @formula(reaction ~ 1 + days3 + (1 + days3 | Subj)), sleepstudy)
```
## Inspect rePCA property
When we use `corr=true` -- normalized cumulative proportion of variance is the same
```julia
fm1.rePCA
```
```julia
fm2.rePCA
```
```julia
fm3.rePCA
```
Equivalent info for `MixedModels.PCA(fm, corr=true)`
```julia
MixedModels.PCA(fm1, corr=true)
```
```julia
MixedModels.PCA(fm2, corr=true)
```
```julia
MixedModels.PCA(fm3, corr=true)
```
... but not for `MixedModels.PCA(fm, corr=false)`
```julia
MixedModels.PCA(fm1, corr=false)
```
```julia
MixedModels.PCA(fm2, corr=false)
```
```julia
MixedModels.PCA(fm3, corr=false)
```
The normalized cumulative proportions of variance reported for `corr=false`
agree with results from `lme4::rePCA()` as shown below.
## MixedModel.PCA() default options
The `show()` provides additional information. Here are the options.
```
Base.show(pca::PCA;
ndigitsmat=2, ndigitsvec=2, ndigitscum=4,
covcor=true, loadings=true, variances=false, stddevs=false
```
For example, to obtain only inequivalent cumulative proportions of variance.
```julia
fm1_pca_f = MixedModels.PCA(fm1, corr=false);
show(fm1_pca_f.Subj, covcor=false, loadings=false, variances=false, stddevs=false)
# Note: Must add group identifier '.Subj' to model object name!
fm2_pca_f = MixedModels.PCA(fm2, corr=false);
show(fm2_pca_f.Subj, covcor=false, loadings=false, variances=false, stddevs=false)
fm3_pca_f = MixedModels.PCA(fm3, corr=false);
show(fm3_pca_f.Subj, covcor=false, loadings=false, variances=false, stddevs=false)
```
## R-based lme4::rePCA() results
```julia
R"""
library(Matrix)
library(lme4)
Days2 <- sleepstudy$Days*2
Days3 <- sleepstudy$Days/2
fm1 <- lmer(Reaction ~ 1 + Days + (1 + Days | Subject), sleepstudy, REML=FALSE)
fm2 <- lmer(Reaction ~ 1 + Days2 + (1 + Days2 | Subject), sleepstudy, REML=FALSE)
fm3 <- lmer(Reaction ~ 1 + Days3 + (1 + Days3 | Subject), sleepstudy, REML=FALSE)
print(summary(rePCA(fm1)))
print(summary(rePCA(fm2)))
print(summary(rePCA(fm3)))
""";
```