"
28 | ],
29 | "text/plain": [
30 | "275996×31 DataFrames.DataFrame. Omitted printing of 25 columns\n",
31 | "│ Row │ ReadingTime │ Subject │ Sex │ Age │ NPoems │ MultipleChoiceRT │\n",
32 | "├────────┼─────────────┼─────────┼─────┼──────┼────────┼──────────────────┤\n",
33 | "│ 1 │ 340 │ Subj281 │ f │ 22.0 │ 58.0 │ 9704 │\n",
34 | "│ 2 │ 344 │ Subj290 │ f │ 30.0 │ 17.0 │ 25469 │\n",
35 | "│ 3 │ 453 │ Subj119 │ m │ 21.0 │ 17.0 │ 23015 │\n",
36 | "│ 4 │ 290 │ Subj112 │ m │ 23.0 │ 5.0 │ 7110 │\n",
37 | "│ 5 │ 343 │ Subj294 │ m │ 21.0 │ 5.0 │ 12547 │\n",
38 | "│ 6 │ 296 │ Subj52 │ f │ 22.0 │ 17.0 │ 15515 │\n",
39 | "│ 7 │ 321 │ Subj118 │ m │ 48.0 │ 17.0 │ 9313 │\n",
40 | "│ 8 │ 290 │ Subj6 │ f │ 19.0 │ 5.0 │ 15442 │\n",
41 | "│ 9 │ 1187 │ Subj48 │ f │ 20.0 │ 5.0 │ 18047 │\n",
42 | "│ 10 │ 422 │ Subj324 │ f │ 47.0 │ 5.0 │ 18922 │\n",
43 | "│ 11 │ 460 │ Subj180 │ m │ 23.0 │ 5.0 │ 16764 │\n",
44 | "⋮\n",
45 | "│ 275985 │ 187 │ Subj212 │ f │ 21.0 │ 5.0 │ 8641 │\n",
46 | "│ 275986 │ 890 │ Subj9 │ f │ 22.0 │ 5.0 │ 22875 │\n",
47 | "│ 275987 │ 1203 │ Subj167 │ f │ 51.0 │ 58.0 │ 26297 │\n",
48 | "│ 275988 │ 1412 │ Subj316 │ f │ 23.0 │ 5.0 │ 25977 │\n",
49 | "│ 275989 │ 453 │ Subj238 │ m │ 25.0 │ 5.0 │ 16813 │\n",
50 | "│ 275990 │ 301 │ Subj118 │ m │ 48.0 │ 17.0 │ 9313 │\n",
51 | "│ 275991 │ 532 │ Subj57 │ m │ 22.0 │ 5.0 │ 5203 │\n",
52 | "│ 275992 │ 440 │ Subj137 │ m │ 25.0 │ 17.0 │ 14631 │\n",
53 | "│ 275993 │ 515 │ Subj282 │ m │ 18.0 │ 5.0 │ 2547 │\n",
54 | "│ 275994 │ 266 │ Subj99 │ m │ 32.0 │ 5.0 │ 8078 │\n",
55 | "│ 275995 │ 422 │ Subj86 │ f │ 23.0 │ 17.0 │ 15844 │\n",
56 | "│ 275996 │ 282 │ Subj132 │ f │ 24.0 │ 5.0 │ 4953 │"
57 | ]
58 | },
59 | "execution_count": 2,
60 | "metadata": {},
61 | "output_type": "execute_result"
62 | }
63 | ],
64 | "source": [
65 | "poems = rcopy(R\"RePsychLing::poems\")"
66 | ]
67 | },
68 | {
69 | "cell_type": "code",
70 | "execution_count": 3,
71 | "metadata": {},
72 | "outputs": [
73 | {
74 | "data": {
75 | "text/plain": [
76 | "(275996, 31)"
77 | ]
78 | },
79 | "execution_count": 3,
80 | "metadata": {},
81 | "output_type": "execute_result"
82 | }
83 | ],
84 | "source": [
85 | "size(poems)"
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "execution_count": 4,
91 | "metadata": {},
92 | "outputs": [
93 | {
94 | "data": {
95 | "text/plain": [
96 | "31-element Array{Symbol,1}:\n",
97 | " :ReadingTime \n",
98 | " :Subject \n",
99 | " :Sex \n",
100 | " :Age \n",
101 | " :NPoems \n",
102 | " :MultipleChoiceRT \n",
103 | " :Trial \n",
104 | " :NumberOfWordsIntoLine\n",
105 | " :PositionBegMidEnd \n",
106 | " :SentenceLength \n",
107 | " :Poem \n",
108 | " :Word \n",
109 | " :WordFrequencyInPoem \n",
110 | " ⋮ \n",
111 | " :WordFormFrequency \n",
112 | " :NumberOfMeanings \n",
113 | " :IsFunctionWord \n",
114 | " :HasPunctuationMark \n",
115 | " :NumberOfMorphemes \n",
116 | " :LogReadingTime \n",
117 | " :LogWordFormFrequency \n",
118 | " :LogLemmaFrequency \n",
119 | " :LogFamilySize \n",
120 | " :LogMultipleChoiceRT \n",
121 | " :LogNPoems \n",
122 | " :LogNumberOfMeanings "
123 | ]
124 | },
125 | "execution_count": 4,
126 | "metadata": {},
127 | "output_type": "execute_result"
128 | }
129 | ],
130 | "source": [
131 | "names(poems)"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": 5,
137 | "metadata": {},
138 | "outputs": [
139 | {
140 | "ename": "KeyError",
141 | "evalue": "KeyError: key :Lrt not found",
142 | "output_type": "error",
143 | "traceback": [
144 | "KeyError: key :Lrt not found",
145 | "",
146 | "Stacktrace:",
147 | " [1] getindex at ./dict.jl:478 [inlined]",
148 | " [2] getindex at /home/bates/.julia/packages/DataFrames/utxEh/src/other/index.jl:123 [inlined]",
149 | " [3] getindex(::DataFrames.DataFrame, ::Symbol) at /home/bates/.julia/packages/DataFrames/utxEh/src/dataframe/dataframe.jl:258",
150 | " [4] (::getfield(StatsModels, Symbol(\"##18#19\")){DataFrames.DataFrame})(::Symbol) at /home/bates/.julia/packages/StatsModels/AYB2E/src/modelframe.jl:145",
151 | " [5] iterate at ./generator.jl:47 [inlined]",
152 | " [6] _collect(::Array{Any,1}, ::Base.Generator{Array{Any,1},getfield(StatsModels, Symbol(\"##18#19\")){DataFrames.DataFrame}}, ::Base.EltypeUnknown, ::Base.HasShape{1}) at ./array.jl:632",
153 | " [7] collect_similar(::Array{Any,1}, ::Base.Generator{Array{Any,1},getfield(StatsModels, Symbol(\"##18#19\")){DataFrames.DataFrame}}) at ./array.jl:561",
154 | " [8] map(::Function, ::Array{Any,1}) at ./abstractarray.jl:1987",
155 | " [9] #ModelFrame#17(::Dict{Any,Any}, ::Type, ::StatsModels.Terms, ::DataFrames.DataFrame) at /home/bates/.julia/packages/StatsModels/AYB2E/src/modelframe.jl:145",
156 | " [10] Type at ./none:0 [inlined]",
157 | " [11] #ModelFrame#20 at /home/bates/.julia/packages/StatsModels/AYB2E/src/modelframe.jl:157 [inlined]",
158 | " [12] Type at ./none:0 [inlined]",
159 | " [13] #LinearMixedModel#12(::Array{Any,1}, ::Dict{Any,Any}, ::Distributions.Normal{Float64}, ::Type, ::StatsModels.Formula, ::DataFrames.DataFrame) at /home/bates/.julia/packages/MixedModels/3CQyw/src/pls.jl:63",
160 | " [14] LinearMixedModel(::StatsModels.Formula, ::DataFrames.DataFrame) at /home/bates/.julia/packages/MixedModels/3CQyw/src/pls.jl:63",
161 | " [15] top-level scope at In[5]:1"
162 | ]
163 | }
164 | ],
165 | "source": [
166 | "m0 = fit!(LinearMixedModel(@formula(Lrt ~ Fre + Mul + Age + (1|Poem)+ (1|Subject)+ (1|Word)), poems))"
167 | ]
168 | }
169 | ],
170 | "metadata": {
171 | "kernelspec": {
172 | "display_name": "Julia 1.0.0",
173 | "language": "julia",
174 | "name": "julia-1.0"
175 | },
176 | "language_info": {
177 | "file_extension": ".jl",
178 | "mimetype": "application/julia",
179 | "name": "julia",
180 | "version": "1.0.0"
181 | }
182 | },
183 | "nbformat": 4,
184 | "nbformat_minor": 1
185 | }
186 |
--------------------------------------------------------------------------------
/inst/Stan/KBStan.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "A Bayesian Linear Mixed Model Analysis of the Kronmüller and Barr (2007) data using Stan"
3 | author: "Shravan Vasishth and Douglas Bates"
4 | date: "`r Sys.Date()`"
5 | bibliography: RePsychLing.bib
6 | output: rmarkdown::html_vignette
7 | vignette: >
8 | %\VignetteIndexEntry{KB07 Bayesian data analysis}
9 | %\VignetteEngine{knitr::rmarkdown}
10 | \usepackage[utf8]{inputenc}
11 | ---
12 |
13 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
14 | library(RePsychLing)
15 | library(knitr)
16 | library(rstan)
17 | library(parallel)
18 | library(xtable)
19 | opts_chunk$set(comment=NA)
20 | options(width=92,
21 | show.signif.stars = FALSE)
22 | ```
23 |
24 | ## Structure of the data
25 |
26 | The data from @Kronmuller:Barr:2007 is available as the `kb07` data frame in the
27 | [RePsychLing package](https://github.com/dmbates/RePsychLing) for [R](http://www.r-project.org).
28 |
29 | ```{r kb07str}
30 | str(kb07)
31 | ```
32 |
33 | As is common with factorial designs the experimental factors and their interactions are given short names: a single character (`S`,`P`,`C`) for the main effects, two-character names for the two-factor interactions and a three-character name for the three-factor interaction.
34 |
35 | The data are from 56 subjects who responded to 32 iterms. Specifically, subjects had to select one of several objects presented on a monitor with a cursor. The manipulations involved
36 |
37 | 1. auditory instructions that maintained or broke a precedent of reference for the objects established over prior trials, (`P`)
38 | 2. with the instruction being presented by the speaker who established the precedent (i.e., an old speaker) or a new speaker (`S`), and
39 | 3. whether the task had to be performed without or with a cognitive load consisting of six random digits. (`C`)
40 |
41 | All factors were expressed using a `±1` encoding which ensures that the scale of the interactions is the same as the
42 | scale of the main effects and of the intercept term. The columns of the full-factorial model matrix, `X`
43 | ```{r X}
44 | X <- unname(model.matrix(~ 1+S+P+C+SP+SC+PC+SPC, kb07))
45 | attr(X,"assign") <- NULL
46 | str(X)
47 | ```
48 | have the same length and are nearly orthogonal.
49 | ```{r XtX}
50 | crossprod(X) # X'X
51 | ```
52 |
53 | The response, `RTtrunc`, is the reaction time after truncating some unusually large values. The minimum value is 336 and the maximum 5144 ms.
54 |
55 | ```{r rtsummary}
56 | summary(kb07$RTtrunc)
57 | hist(kb07$RTtrunc,freq=FALSE,xlab="msec",
58 | main="Distribution of RTtrunc")
59 | ```
60 |
61 | ## Details of the linear mixed models
62 |
63 | We fit two models, the maximal model, and the final model that was settled on in the accompanying paper [@BatesEtAlParsimonious]. We attempt to provide more detail than usual in order to help researchers unfamiliar with the matrix notation of linear mixed models; for a more simplified introduction to fitting LMMs in Stan, please see the tutorial article @SorensenVasishth.
64 |
65 | ### Maximal model
66 |
67 | The model in matrix form is:
68 |
69 | $$\mathrm{y} =
70 | X\beta + Z_s b_s + Z_i b_i + \epsilon$$
71 |
72 | Here, $X$ is the $N\times p$ model matrix (p=8 since we have eight predictors, including the intercept), $\beta$ is a $p\times 1$ vector of fixed effects parameters, $Z_s$ and $Z_i$ are the subject and item model matrices ($N\times p$), and $b_s$ and $b_i$ are the by-subject and by-item adjustments to the fixed effects estimates; these are often referred to as the Best Linear Unbiased Predictors (BLUPs) in the linear mixed modeling literature. $\epsilon$ refers to the residual error ($N\times 1$).
73 |
74 | We assume that $\epsilon \sim N(0,\sigma^2)$, and that $b_s \sim N(0,\Sigma_s)$, and
75 | $b_i \sim N(0,\Sigma_i)$. These are assumed to be mutually independent.
76 | $\Sigma_s$ and $\Sigma_i$ are $8\times 8$ variance-covariance matrices for subject and item random effects respectively; the diagonals have the variance estimates for each predictor, and the off-diagonals have the respective pairwise covariances. For example, a $2\times 2$ variance covariance matrix for two random variables $X_1$ and $X_2$ would have the form
77 |
78 | $$
79 | \Sigma_u =
80 | \left[ \begin{array}{cc}
81 | \sigma_1^2 & \rho_{12} \, \sigma_{1} \sigma_{2} \\
82 | \rho_{12} \, \sigma_{1} \sigma_{2} & \sigma_{2}^2
83 | \end{array} \right]
84 | $$
85 |
86 | Here, $\rho_{12} \, \sigma_{1} \sigma_{2}$ is the covariance $Cov(X_1,X_2)$, and $\rho_{12}$ is the correlation between the two random variables. Notice that we can construct a $2\times 2$ __correlation matrix__ for the above covariance matrix as shown below; the diagonals will always have 1's in them as a random variable is perfectly correlated with itself. This becomes relevant in the next subsection on prior specificiation.
87 |
88 | $$
89 | \left[ \begin{array}{cc}
90 | 1 & \rho_{12} \\
91 | \rho_{12} & 1
92 | \end{array} \right]
93 | $$
94 |
95 | In a Bayesian analysis, we have to specify a prior distribution for each parameter. We discuss this point next.
96 |
97 | #### Prior specification
98 |
99 | The parameters in the maximal model are: eight $\beta$ coefficients, the variance of the residual error $\sigma^2$, eight variance components for subject, another eight for item, and 28 subject and 28 item correlations.
100 |
101 | We define the following priors:
102 |
103 | 1. $\beta$ have a flat uniform improper prior.
104 | 2. $\tau_s \sim Cauchy(0,2.5)$
105 | 3. $\tau_i \sim Cauchy(0,2.5)$
106 | 4. $\sigma$ has a uniform prior distribution with a lower bound of 0.
107 | 4. The prior for the correlations needs some explanation.
108 | First, note that a correlation matrix can be decomposed into something analogous to a square root of a matrix, using the Cholesky decomposition. For example, given a matrix like
109 |
110 | ```{r choldecompexample}
111 | C<-matrix(c(1,.1,.5,.1,1,-.3,.5,-.3,1),
112 | byrow=TRUE,ncol=3)
113 | C
114 | ```
115 |
116 | We can decompose this matrix as follows:
117 |
118 | ```{r choldecompexample2}
119 | L<-chol(C)
120 | L
121 | ```
122 |
123 | This decomposition gives a kind of square root of the matrix: we can recover the correlation matrix by squaring $L$:
124 |
125 | ```{r choldecompexample3}
126 | ## Gives back original matrix C:
127 | t(L)%*%L
128 | ```
129 |
130 | In Stan, we define priors on $L$, the Cholesky decomposition of the correlation matrix, using the so-called LKJ priors, with parameter $\eta=2$ (see the Stan manual for details). Once we have a prior on $L$, we can (a) compute the posterior estimates of the correlation matrix, and (b) generate the adjustments (BLUPs) $b_s$ and $b_i$ shown in the equation above.
131 |
132 | The procedure for generating$b_s$ is:
133 |
134 | 1. Premultiply the diagonalized by-subject variance vector $\tau_s$ with the Cholesky decomposition $L$ to get a matrix $\Lambda_s$.
135 | 2. Generate values from a random variable $u_s$ that has a N(0,1) distribution.
136 |
137 | 3. Multiply $\Lambda_s$ with $u_s$ to obtain the correlated random variables $b_s$. The procedure for generating $b_i$ is analogous.
138 |
139 | As an illustration of how this procedure works, consider the simple case where we have two random variables, which implies two variance components. Let the correlation between the random variables be $-0.6$. We can generate five pairs of correlated $b_s$ values as follows:
140 |
141 | ```{r examplegenerationofcorrelatedRVs}
142 | ## given two variance components:
143 | sigmas<-c(1,10)
144 | ## and a correlation of -0.6:
145 | corrs<-c(1,-.6)
146 | C<-matrix(c(corrs,corrs[c(2,1)]),
147 | ncol=2,byrow=T)
148 | C
149 | ## Cholesky decomposition:
150 | L<-chol(C)
151 | L
152 | ## generate 5 pairs of N(0,1) random variables:
153 | z<-rnorm(2*5)
154 | z<-matrix(z,ncol=2)
155 | t(z)
156 | ## generate correlated random variables:
157 | sigmas<-sigmas*diag(2)
158 | Lambda<-sigmas%*%L
159 | b_s<-Lambda%*%t(z)
160 | b_s
161 | ## check that the random variables have
162 | ## the expected correlation:
163 | cor(t(b_s))
164 | ```
165 |
166 | This completes the explanation for how priors are defined.
167 | We now turn to the implementation details of the Stan model.
168 |
169 | ### Formulating the maximal Stan model
170 |
171 | [Stan](http://mc-stan.org) is a system for creating Markov Chain Monte Carlo (MCMC) samplers for statistical models [@stan-manual:2014].
172 |
173 | In the model specification the data, including dimensions of arrays, are described first.
174 | Consistent with lme4 terminology, we will use the terms fixed effect and random effect, although note that in the Bayesian setting this distinction disappears (this is discussed below).
175 |
176 | ```{r standat}
177 | standat <- '
178 | data {
179 | int N; // num observations
180 | int K; // length of fixed-effects vector
181 | int M; // num subjects
182 | int J; // length of subj vector-valued random effects
183 | int L; // num items
184 | int I; // length of item vector-values random effects
185 | int subj[N]; // subject indicator
186 | int item[N]; // item indicator
187 | row_vector[K] X[N]; // model matrix for fixed-effects parameters
188 | row_vector[J] Zs[N]; // generator model matrix for subj random effects
189 | row_vector[I] Zi[N]; // generator model matrix for item random effects
190 | vector[N] y; // response vector (reaction time)
191 | }
192 | '
193 | ```
194 | The response vector, `y`, is expressed as a vector of `N` elements. The integer vectors `subj` and `item` are the indicators of subject and of item for each response.
195 |
196 | The fixed-effects model matrix, `X`, the subject-specific random effects model matrix, `Zs`,
197 | and the item-specific random effects model matrix, `Zi`, are stored as vectors of `N` `row_vector`'s.
198 | The reason for storing the model matrices like this is to provide fast access to individual rows when formulating the model.
199 |
200 | For generality the number of columns in these matrices is specified separately as `K` (number of fixed effects), `J` (number of subject random effects) and `I` (number of item random effects). For the _maximal model_ these will all be eight. The numbers of subjects and items are `M` and `L`, respectively.
201 |
202 | ## Model parameters
203 |
204 | In a Bayesian formulation all the random variables are considered _model parameters_; there is no distinction
205 | between the random effects and the fixed-effects coefficients. As discussed above, the covariance matrices for the within-subject
206 | and within-item random effects are specified according to the Cholesky factor of the correlation matrix,
207 | a `cholesky_factor_corr` type and the standard deviations, which are called `taus` and `taui`, respectively.
208 |
209 |
210 | The _spherical random effects_, often written as $u$, are called `us` and `ui` respectively.
211 | These are matrices that are stored as vectors of vectors.
212 |
213 | (A note regarding the phrase "spherical random effect":
214 | The unconditional distribution of the random variable U is multivariate normal with mean 0 and covariance matrix $\sigma^2I$. Because the contours of constant probability density of such a distribution are spheres centered at the origin, it is called a "spherical normal" distribution and so we call U the "spherical random effects".)
215 |
216 | ```{r stanpars}
217 | stanpars <- '
218 | parameters {
219 | cholesky_factor_corr[J] Ls; // Cholesky factor of subj r.e. correlations
220 | cholesky_factor_corr[I] Li; // Cholesky factor of item r.e. correlations
221 | vector[J] taus; // standard deviations of unconditional subj r.e. dist
222 | vector[I] taui; // standard deviations of unconditional item r.e. dist
223 | vector[J] us[M]; // spherical subj random effects
224 | vector[I] ui[L]; // spherical item random effects
225 | vector[K] beta; // fixed-effects
226 | real sigma; // standard deviation of response given random effects
227 | }
228 |
229 | '
230 | ```
231 |
232 | The correlation matrices formed from the Cholesky factors are considered _transformed parameters_
233 | ```{r stantrans}
234 | stantrans <- '
235 | transformed parameters {
236 | matrix[J,J] corrs;
237 | matrix[I,I] corri;
238 | corrs <- tcrossprod(Ls); // for monitoring subj correlations
239 | corri <- tcrossprod(Li); // for monitoring item correlations
240 | }
241 |
242 | '
243 | ```
244 |
245 | Finally, the model is expressed in terms of square matrices `Lambdas` and `Lambdai` that are
246 | the Cholesky factors of the covariance matrices for the subject-specific and item-specific
247 | random effects, respectively.
248 | The _prior distributions_ for the parameters (`taus`, `taui`, `Ls`, `Li`, `us`, and `ui`) are
249 | also specified at this point.
250 | No prior distribution is provided for `beta`, implying that this vector has a "flat" or "locally uniform" improper prior distribution.
251 | It could be given, say, a diffuse multivariate Gaussian distribution, but doing so has no effect on inferences.
252 |
253 | ```{r model}
254 | stanmod <- '
255 | model {
256 | matrix[J,J] Lambdas;
257 | vector[J] bs[M];
258 | matrix[I,I] Lambdai;
259 | vector[I] bi[L];
260 | taus ~ cauchy(0,2.5);
261 | taui ~ cauchy(0,2.5);
262 | Ls ~ lkj_corr_cholesky(2);
263 | Li ~ lkj_corr_cholesky(2);
264 | Lambdas <- diag_pre_multiply(taus,Ls);
265 | Lambdai <- diag_pre_multiply(taui,Li);
266 | for (m in 1:M) {
267 | us[m] ~ normal(0,1);
268 | bs[m] <- Lambdas * us[m];
269 | }
270 | for (l in 1:L) {
271 | ui[l] ~ normal(0,1);
272 | bi[l] <- Lambdai * ui[l];
273 | }
274 | for (n in 1:N)
275 | y[n] ~ normal(X[n] * beta + Zs[n] * bs[subj[n]] + Zi[n] * bi[item[n]], sigma);
276 | }
277 |
278 | '
279 | ```
280 | In the last loop the linear predictor for `y[n]` is evaluated as the sum of three products of
281 | `row_vector`s and `vector`s. A `row_vector` multiplied by a `vector` is a scalar, which is the reason for storing `X`, `Zs` and `Zi` as vectors of `row_vector`s.
282 |
283 | These pieces are concatenated to form the model
284 | ```{r}
285 | model <- paste(standat, stanpars, stantrans, stanmod)
286 | ```
287 |
288 | ## Compiling the model
289 |
290 | The model is compiled via a call to `stan` that includes the model and the data.
291 | The data must be specified as a list or as an environment.
292 |
293 | For the _maximal model_, `X`, `Zs` and `Zi` are the model matrix for the full factorial design.
294 |
295 | ```{r maxdat}
296 | maxdat <-
297 | within(list(), {
298 | N <- nrow(X)
299 | K <- J <- I <- ncol(X)
300 | M <- length(levels(kb07$subj))
301 | L <- length(levels(kb07$item))
302 | X <- Zs <- Zi <- unname(X)
303 | y <- kb07$RTtrunc
304 | subj <- as.integer(kb07$subj)
305 | item <- as.integer(kb07$item)
306 | }
307 | )
308 | str(maxdat)
309 | ```
310 |
311 | Initially, we set the number of chains to zero to check that the model compiles properly.
312 |
313 | ```{r maxmodel}
314 | maxmodel <- stan(model_name="maxmodel", model_code=model, data=maxdat, chains=0)
315 | ```
316 |
317 | ## Creating the chains.
318 |
319 | We use `mclapply` from the `parallel` package to generate the chains in parallel.
320 |
321 | ```{r KB07_stan,cache=TRUE}
322 | system.time(KB07_stan <-
323 | sflist2stanfit(
324 | mclapply(1:4, mc.cores = 4, # adjust number of cores to suit
325 | function(i) stan(fit = maxmodel,
326 | data = maxdat,
327 | iter=2000,
328 | chains = 1,
329 | chain_id = i,
330 | refresh = -1))
331 | )
332 | )
333 | ```
334 |
335 | We see that the elapsed time is considerably less than the user time. This is because four processes are running in parallel.
336 |
337 | A close examination of the timing shows that much more time is spent in the "warmup" phase than in actually generating samples. Stan defaults to using a form of Hamiltonian Monte Carlo (HMC) sampling called a "No U-Turn Sampler" (NUTS) and it is tuning these samplers that is taking most of the time.
338 |
339 | ```{r KB07_results,cache=FALSE}
340 | KB07_results<- summary(KB07_stan,
341 | pars=c("beta", "sigma",
342 | "taus","taui",
343 | "corrs","corri"),
344 | probs = c(0.025, 0.975), digits_summary = 3)
345 | rownames(KB07_results$summary)
346 | ```
347 |
348 | Note that all of the correlation matrix elements are monitored even though the diagonal
349 | elements are, by definition, unity and the matrix must be symmetric. An arduous extraction provides the table
350 |
351 | ```{r printmaxmodel,echo=FALSE,eval=TRUE,cache=FALSE,results="asis"}
352 | print(xtable(KB07_results$summary[c(1:25,27:33,36:41,45:49,55:57,62:64,81,91:97,100:105),c(1,4,5)]), type="html")
353 | ```
354 |
355 | Note that most of the correlations, especially those for the item-specific random effects, have a mean close to zero and upper and lower limits that are more-or-less symmetric. This is strong evidence that these could be zero.
356 |
357 | Because of the way the priors are defined the `taus` and `taui` values cannot become zero. However, many of these values are very close to zero. The only standard deviations that are substantion are the by-subject intercept and the by-item intercept and coefficient for `P`.
358 |
359 |
360 | ```{r maximalfigure, echo=FALSE, eval=FALSE}
361 | maxres<-KB07_results$summary[c(1:25,27:33,36:41,45:49,55:57,62:64,81,91:97,100:105),c(1,4,5)]
362 |
363 | stanfixefmax<-maxres[1:8,]
364 | stanvarcompmax<-maxres[c(9,10:25),]
365 | stanvarcorrsmax<-maxres[c(26:50),]
366 | stanvarcorrimax<-maxres[c(51:63),]
367 | ```
368 |
369 |
370 | ## Final model
371 |
372 | The final, reduced model has a single column (the intercept) in `Zs` and two columns (intercept and main-effect for `P`) in `Zi`.
373 |
374 | Notice that the model specification is the same as the one used above for the maximal model. The only thing that has changed is the specification in the data of the different variables. In particular, notice that J and L have changed to reflect the number of subject and item random effects, respectively, and the Zs and Zi matrices are reduced versions of the original matrices used in the maximal model above.
375 |
376 | ```{r datreduced}
377 | finaldat <-
378 | within(list(), {
379 | N <- nrow(X)
380 | K <- ncol(X)
381 | J <- 1L
382 | I <- 2L
383 | M <- length(levels(kb07$subj))
384 | L <- length(levels(kb07$item))
385 | X <- X
386 | Zs <- X[, 1, drop=FALSE]
387 | Zi <- X[, 1:2]
388 | y <- kb07$RTtrunc
389 | subj <- as.integer(kb07$subj)
390 | item <- as.integer(kb07$item)
391 | }
392 | )
393 | str(finaldat)
394 | ```
395 | ```{r}
396 | system.time(KB07_finalstan <-
397 | sflist2stanfit(
398 | mclapply(1:4, mc.cores = 4, # adjust number of cores to suit
399 | function(i) stan(fit = maxmodel,
400 | data = finaldat,
401 | iter=2000,
402 | chains = 1,
403 | chain_id = i,
404 | refresh = -1))
405 | )
406 | )
407 | ```
408 |
409 | # References
410 |
--------------------------------------------------------------------------------
/inst/Stan/KKLStan.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "A Bayesian Linear Mixed Model Analysis of Kliegl et al (2015) data using Stan"
3 | author: "Shravan Vasishth"
4 | date: "`r Sys.Date()`"
5 | output: rmarkdown::html_vignette
6 | vignette: >
7 | %\VignetteIndexEntry{KKL Bayesian data analysis}
8 | %\VignetteEngine{knitr::rmarkdown}
9 | \usepackage[utf8]{inputenc}
10 | ---
11 |
12 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
13 | library(lme4)
14 | library(RePsychLing)
15 | library(knitr)
16 | library(parallel)
17 | library(rstan)
18 | opts_chunk$set(comment=NA)
19 | options(width=92,show.signif.stars = FALSE)
20 | ```
21 |
22 | ## Load data
23 |
24 | ```{r preparedata,echo=TRUE,eval=FALSE,cache=FALSE}
25 | mm <- model.matrix(~ sze*(spt+obj+grv)*orn, data=KKL)
26 | spt_orn <- mm[ ,11]
27 | obj_orn <- mm[, 12]
28 | grv_orn <- mm[, 13]
29 |
30 | KKL$spt_orn<-spt_orn
31 | KKL$obj_orn<-obj_orn
32 | KKL$grv_orn<-grv_orn
33 |
34 | dat <-
35 | c(unclass(subset(KKL, select = c(lrt,
36 | sze, spt, obj, grv, orn,
37 | spt_orn, obj_orn, grv_orn,
38 | sze_spt, sze_obj, sze_grv, sze_orn,
39 | sze_spt_orn, sze_obj_orn, sze_grv_orn))),
40 | subj = list(as.integer(KKL$subj)),
41 | N = nrow(KKL),
42 | I = nlevels(KKL$subj))
43 | ```
44 |
45 | ## Maximal model
46 |
47 | ```{r runmodel,echo=TRUE,eval=FALSE,cache=FALSE}
48 | if (file.exists("../data/KKL0_maxstanresults.rda")) {
49 | load("../data/KKL0_maxstanresults.rda")
50 | } else {
51 | KKLmaxmodel <- stan("KKLmaxmodel.stan",
52 | data = dat,
53 | chains = 0)
54 |
55 | ## Adjust cores to match your computer:
56 | sflist <-
57 | mclapply(1:4, mc.cores = 4,
58 | function(i) stan(fit = KKLmaxmodel,
59 | data = dat,
60 | iter=2000,
61 | chains = 1,
62 | chain_id = i,
63 | refresh = -1))
64 |
65 | KKL0_maxstan <- sflist2stanfit(sflist)
66 |
67 | ## save as matrix
68 | KKL0maxmat<-as.matrix(KKL0_maxstan)
69 | KKL0maxreduced<-KKL0maxmat[,1:89]
70 | param_names<-colnames(KKL0maxreduced)
71 | resultsKKL0max<-matrix(rep(NA,89*3),ncol=3)
72 | for(i in 1:89){
73 | resultsKKL0max[i,]<-c(mean(resultsKKL0max[,i]),
74 | quantile(resultsKKL0max[,i],
75 | probs=c(0.025,0.975)))
76 | }
77 |
78 | resultsKKL0max<-data.frame(resultsKKL0max)
79 | colnames(resultsKKL0max)<-c("mean","lower","upper")
80 | ```
81 |
82 | Save results:
83 |
84 | ```{r maxmodelsave,echo=TRUE,eval=FALSE,cache=FALSE}
85 | ## saved in RePsychLing data dir:
86 | save(resultsKKL0max,
87 | file="../data/KKL0_maxstanresults.rda",
88 | compress="xz")
89 | }
90 | ```
91 |
92 | ## Final model
93 |
94 | ```{r finalmodelanalysis,echo=TRUE,eval=FALSE,cache=FALSE}
95 | if (file.exists("../data/KKL0_finstanresults.rda")) {
96 | load("../data/KKL0_finstanresults.rda")
97 | } else {
98 | KKLfinmodel <- stan("KKLfinmodel.stan",
99 | data = dat,
100 | chains = 0)
101 |
102 | ## Adjust cores to match your computer:
103 | sflist <-
104 | mclapply(1:4, mc.cores = 4,
105 | function(i) stan(fit = KKLfinmodel,
106 | data = dat,
107 | iter=2000,
108 | chains = 1,
109 | chain_id = i,
110 | refresh = -1))
111 |
112 | KKL0fin_stan <- sflist2stanfit(sflist)
113 |
114 | ## get only relevant part:
115 | ## save as matrix
116 | KKL0finmat<-as.matrix(KKL0fin_stan)
117 | params<- c(1:20,546:548)
118 | KKL0finreduced<-KKL0finmat[,params]
119 |
120 | param_names<-c("Intercept","sze",
121 | "spt","obj",
122 | "grv","orn",
123 | "sze_spt","sze_obj",
124 | "sze_grv","sze_orn",
125 | "spt_orn","obj_orn",
126 | "grv_orn","sze_spt_orn",
127 | "sze_obj_orn",
128 | "sze_grv_orn","sigma_e","sigma_u1",
129 | "sigma_u2","sigma_u3","sigma_u_obj",
130 | "sigma_u_spt_orn",
131 | "sigma_u_orn")
132 |
133 |
134 | resultsKKL0fin<-matrix(rep(NA,
135 | length(params)*3),
136 | ncol=3)
137 |
138 | for(i in 1:length(params)){
139 | resultsKKL0fin[i,]<-c(mean(KKL0finreduced[,i]),
140 | quantile(KKL0finreduced[,i],
141 | probs=c(0.025,0.975)))
142 | }
143 |
144 | rownames(resultsKKL0fin)<-param_names
145 |
146 | resultsKKL0fin<-data.frame(resultsKKL0fin)
147 | colnames(resultsKKL0fin)<-c("mean","lower",
148 | "upper")
149 | ```
150 |
151 | ```{r finalmodelsave,echo=TRUE,eval=FALSE,cache=FALSE}
152 |
153 | ## saved in RePsychLing data dir:xxx
154 | save(resultsKKL0fin,
155 | file="../data/KKL_finstanresults.rda",
156 | compress="xz")
157 | ```
158 |
159 | ```{r finalmodelcorr,echo=TRUE,eval=FALSE,cache=FALSE}
160 |
161 | ## extract correlation parameters:
162 | e<-extract(KKL0fin_stan)
163 | n_samp <- dim(e[[1]])[1]
164 | ## correlations subject:
165 | cor_listu <- lapply(seq(n_samp),
166 | function(i) {
167 | L <- e$L_u[i, , ]
168 | t(L) %*% L
169 | })
170 |
171 | # number of random effects
172 | n_ran <- nrow(cor_listu[[1]])
173 |
174 | ## final model:
175 | kkl4<-lmer(lrt ~ sze * (spt + obj + grv) * orn + (spt + grv | subj) + (0 +
176 | obj | subj) + (0 + orn | subj) + (0 + spt_orn | subj),KKL)
177 |
178 | KKL4_cor_s <- attr(VarCorr(kkl4)$subj,"correlation")
179 |
180 | # create indices of correlation coefficients (upper triangular)
181 | cor_idx_s <- which(upper.tri(KKL4_cor_s), arr.ind = TRUE)
182 |
183 | cor_summary<-matrix(rep(NA,3*4),ncol=4)
184 | rnames<-rep(NA,3)
185 | for (i in 1:3) {
186 | idx <- cor_idx_s[i, , drop = FALSE]
187 | KKL4_cor <- KKL4_cor_s[idx]
188 | KKL4s_cor <- sapply(cor_listu, "[", idx)
189 | ## save means and quantiles, Stan:
190 | cor_summary[i,2:4]<-c(mean(KKL4s_cor),
191 | quantile(KKL4s_cor,
192 | probs=c(0.025,
193 | 0.975)))
194 | ## save lmer corr. estimate:
195 | cor_summary[i,1]<-KKL4_cor
196 | ## save rownames:
197 | rnames[i]<-paste(rownames(KKL4_cor_s)[idx], collapse = " : ")
198 | }
199 | rownames(cor_summary)<-rnames
200 | colnames(cor_summary)<-c("lmer est.",
201 | "Stan est.",
202 | "2.5th%ile",
203 | "97.5th%ile")
204 | ```
205 |
206 | ```{r finalmodelcorrsave,echo=TRUE,eval=FALSE,cache=FALSE}
207 | save(cor_summary,file="../data/KKLfincorsubj.rda",compress="xz")
208 | ```
209 |
210 |
211 |
--------------------------------------------------------------------------------
/inst/doc/BS.R:
--------------------------------------------------------------------------------
1 | ## ----preliminaries,echo=FALSE,include=FALSE,cache=FALSE------------------
2 | library(lme4)
3 | library(RePsychLing)
4 | library(knitr)
5 | opts_chunk$set(comment=NA)
6 | options(width=92,show.signif.stars = FALSE)
7 |
8 | ## ----bs10str-------------------------------------------------------------
9 | str(bs10)
10 |
11 | ## ----m0,warning=FALSE----------------------------------------------------
12 | m0 <- lmer(dif ~ 1+S+F+SF + (1+S+F+SF|subj) + (1+S+F+SF|item), bs10, REML=FALSE, start=thcvg$bs10$m0,
13 | control=lmerControl(optimizer="Nelder_Mead",optCtrl=list(maxfun=1L),
14 | check.conv.grad="ignore",check.conv.hess="ignore"))
15 | summary(m0)
16 |
17 | ## ----sv_m0_s-------------------------------------------------------------
18 | summary(rePCA(m0))
19 |
20 | ## ----m1------------------------------------------------------------------
21 | print(summary(m1 <- lmer(dif ~ 1+S+F+SF + (1+S+F+SF||subj) + (1+S+F+SF||item), bs10, REML=FALSE)), corr=FALSE)
22 | anova(m1, m0)
23 |
24 | ## ----pca1----------------------------------------------------------------
25 | summary(rePCA(m1))
26 |
27 | ## ----m2------------------------------------------------------------------
28 | print(summary(m2 <- lmer(dif ~ 1+S+F+SF + (1+S+SF||subj), bs10, REML=FALSE)), corr=FALSE)
29 |
30 | ## ----m21anova------------------------------------------------------------
31 | anova(m2,m1)
32 |
33 | ## ----m3------------------------------------------------------------------
34 | print(summary(m3 <- lmer(dif ~ 1+S+F+SF + (1+S||subj), bs10, REML=FALSE)), corr=FALSE)
35 | anova(m3, m2)
36 |
37 | ## ----m4------------------------------------------------------------------
38 | print(summary(m4 <- lmer(dif ~ 1+S+F+SF + (1+S|subj), bs10, REML=FALSE)), corr=FALSE)
39 | anova(m3, m4, m0)
40 |
41 | ## ----versions------------------------------------------------------------
42 | sessionInfo()
43 |
44 |
--------------------------------------------------------------------------------
/inst/doc/BS.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "RePsychLing Barr and Seyfeddinipur (2010)"
3 | author: "Reinhold Kliegl and Douglas Bates"
4 | date: "11 March, 2015"
5 | output:
6 | html_document: default
7 | pdf_document:
8 | highlight: tango
9 | keep_tex: yes
10 | word_document: default
11 | geometry: margin=1in
12 | fontsize: 12pt
13 | bibliography: RePsychLing.bib
14 | ---
15 |
19 |
20 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
21 | library(lme4)
22 | library(RePsychLing)
23 | library(knitr)
24 | opts_chunk$set(comment=NA)
25 | options(width=92,show.signif.stars = FALSE)
26 | ```
27 |
28 | ## Data from @barrseyfedd2010
29 |
30 | Some of the data from @barrseyfedd2010 are available as the data frame `bs10` in the `RePsychLing` package.
31 |
32 | ```{r bs10str}
33 | str(bs10)
34 | ```
35 |
36 | As with other data frames in this package, the subject and item factors are called `subj` and `item`. The response being modelled, `dif`, is the difference in two response times.
37 |
38 | The two experimental factors `S` and `F`, both at two levels, are represented in the -1/+1 encoding, as is their interaction, `SF`. The `S` factor is the speaker condition with levels -1 for the same speaker in both trials and +1 for different speakers. The `F` factor is the filler condition with levels -1 for `NS` and +1 for `FP`.
39 |
40 | ### Maximal linear mixed model (_maxLMM_)
41 |
42 | The maximal model has a full factorial design `1+S+F+SF` for the fixed-effects and for potentially correlated vector-valued random effects for `subj` and for `item`. We use the parameter estimates from an `lmm` fit using the [MixedModels package](https://github.com/dmbates/MixedModels.jl) package for [Julia](http://julialang.org), which can be much faster than fitting this model with `lmer`. (In addition to being faster, the fit from `lmm` produced a significantly lower deviance.)
43 |
44 | ```{r m0,warning=FALSE}
45 | m0 <- lmer(dif ~ 1+S+F+SF + (1+S+F+SF|subj) + (1+S+F+SF|item), bs10, REML=FALSE, start=thcvg$bs10$m0,
46 | control=lmerControl(optimizer="Nelder_Mead",optCtrl=list(maxfun=1L),
47 | check.conv.grad="ignore",check.conv.hess="ignore"))
48 | summary(m0)
49 | ```
50 |
51 | The model converges with warnings. Six of 12 correlation parameters are estimated at the +/-1 boundary.
52 |
53 | Notice that there are only 12 items. Expecting to estimate 4 variances and 6 covariances from 12 items is optimistic.
54 |
55 | ### PCA analysis of the _maxLMM_
56 |
57 | A summary of a principal components analysis (PCA) of the random-effects variance-covariance
58 | matrices shows
59 | ```{r sv_m0_s}
60 | summary(rePCA(m0))
61 | ```
62 |
63 | For both the by-subject and the by-item random effects the estimated variance-covariance matrices are singular. There are at least 2 dimensions with no variation in the subject-related random-effects and 3 directions with no variation for the item-related random-effects.
64 |
65 | Clearly the model is over-specified.
66 |
67 | ### Zero-correlation-parameter linear mixed model (_zcpLMM_)
68 |
69 | A zero-correlation-parameter model fits independent random effects for the intercept, the experimental factors and their interaction for each of the `subj` and `item` grouping factors.
70 | It can be conveniently specified using the `||` operator in the random-effects terms.
71 | ```{r m1}
72 | print(summary(m1 <- lmer(dif ~ 1+S+F+SF + (1+S+F+SF||subj) + (1+S+F+SF||item), bs10, REML=FALSE)), corr=FALSE)
73 | anova(m1, m0)
74 | ```
75 |
76 | The _zcpLMM_ fits significantly worse than the _maxLMM_. However, the results strongly suggest that quite a few of the variance components are not supported by the data.
77 |
78 | ### PCA for the _zcpLMM_
79 |
80 |
81 | ```{r pca1}
82 | summary(rePCA(m1))
83 | ```
84 |
85 | The random effect for filler, `F`, by subject has essentially zero variance and the random effect for the interaction, `SF`, accounts for less than 15% of the total variation in the random effects. There is no evidence for item-related random effects in `m1`.
86 |
87 | ### Iterative reduction of model complexity
88 |
89 | Remove all variance components estimated with a value of zero.
90 |
91 | ```{r m2}
92 | print(summary(m2 <- lmer(dif ~ 1+S+F+SF + (1+S+SF||subj), bs10, REML=FALSE)), corr=FALSE)
93 | ```
94 |
95 | Naturally, the fit for this model is equivalent to that for `m1` because it is only the variance components with zero estimates that are eliminated.
96 | ```{r m21anova}
97 | anova(m2,m1)
98 | ```
99 |
100 | Next we check whether the variance component for the interaction, `SF`, could reasonably be zero.
101 |
102 | ```{r m3}
103 | print(summary(m3 <- lmer(dif ~ 1+S+F+SF + (1+S||subj), bs10, REML=FALSE)), corr=FALSE)
104 | anova(m3, m2)
105 | ```
106 |
107 | Not quite significant, but could be considered. The fit is still worse than for the _maxLMM_ `m0`. We now reintroduce a correlation parameters in the vector-valued random effects for `subj`.
108 |
109 | ### Extending the reduced LMM with correlation parameters
110 |
111 | ```{r m4}
112 | print(summary(m4 <- lmer(dif ~ 1+S+F+SF + (1+S|subj), bs10, REML=FALSE)), corr=FALSE)
113 | anova(m3, m4, m0)
114 | ```
115 |
116 | Looks like we have evidence for a significant correlation parameter. Moreover, LMM `m4` fits as well as _maxLMM_.
117 |
118 | ### Summary
119 |
120 | LMM `m4` is the optimal model. It might be worth while to check the theoretical contribution of the correlation parameter.
121 |
122 | ## Versions of packages used
123 | ```{r versions}
124 | sessionInfo()
125 | ```
126 |
127 | # References
128 |
129 |
--------------------------------------------------------------------------------
/inst/doc/GB.R:
--------------------------------------------------------------------------------
1 | ## ----preliminaries,echo=FALSE,include=FALSE,cache=FALSE-----------------------------------
2 | library(RePsychLing)
3 | library(lme4)
4 | library(knitr)
5 | options(width=92,show.signif.stars = FALSE)
6 |
7 | ## ----gbstr--------------------------------------------------------------------------------
8 | str(gb12)
9 | summary(gb12)
10 |
11 | ## ----m0-----------------------------------------------------------------------------------
12 | print(summary(m0 <- lmer(
13 | sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF+(1+T+F+TF|session)+(1+T+P+TP|item), gb12,REML=FALSE)), corr=FALSE)
14 |
15 | ## ----m0PCA--------------------------------------------------------------------------------
16 | summary(rePCA(m0))
17 |
18 | ## ----gbm02--------------------------------------------------------------------------------
19 | m1 <-
20 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F+TF||session) + (1+T+P+TP||item),
21 | gb12, REML=FALSE)
22 | VarCorr(m1)
23 | anova(m1, m0)
24 |
25 | ## ----m2-----------------------------------------------------------------------------------
26 | m2 <-
27 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F||session) + (1+T||item),
28 | gb12, REML=FALSE)
29 | VarCorr(m2)
30 | anova(m2, m1, m0)
31 |
32 | ## ----m3-----------------------------------------------------------------------------------
33 | m3 <-
34 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F||session) + (1|item),
35 | gb12, REML=FALSE)
36 | VarCorr(m3)
37 | anova(m3, m2)
38 |
39 | ## ----m4-----------------------------------------------------------------------------------
40 | m4 <-
41 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F|session) + (1|item),
42 | gb12, REML=FALSE)
43 | VarCorr(m4)
44 | anova(m3, m4)
45 |
46 | ## ----m5-----------------------------------------------------------------------------------
47 | m5 <-
48 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+F|session) + (0+T|session) + (1|item),
49 | gb12, REML=FALSE)
50 | VarCorr(m5)
51 | anova(m5, m4)
52 |
53 | ## ----versions-----------------------------------------------------------------------------
54 | sessionInfo()
55 |
56 |
--------------------------------------------------------------------------------
/inst/doc/GB.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "RePsychLing Gann and Barr (2014)"
3 | author: "Reinhold Kliegl and Douglas Bates"
4 | date: '2015-03-11'
5 | output:
6 | html_document: default
7 | pdf_document:
8 | highlight: tango
9 | keep_tex: yes
10 | word_document: default
11 | geometry: margin=1in
12 | fontsize: 12pt
13 | bibliography: RePsychLing.bib
14 | ---
15 |
19 |
20 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
21 | library(RePsychLing)
22 | library(lme4)
23 | library(knitr)
24 | opts_chunk$set(comment=NA)
25 | options(width=92,show.signif.stars = FALSE)
26 | ```
27 |
28 | ## Data from @Gann:Barr:2014
29 |
30 | These data, also used in the online supplement to @Barr:Levy:Scheepers:Tily:13, are available as `gb12` in the `RePsychLing` package.
31 |
32 | ```{r gbstr}
33 | str(gb12)
34 | summary(gb12)
35 | ```
36 |
37 | ### Maximal linear mixed model (_maxLMM_)
38 |
39 | We assume `P`, the partner, is a between-session factor and `F`, feedback, is a between-item factor (i.e., they are not included in RE terms). The model fit in the paper is:
40 |
41 | ```{r m0,warning=FALSE}
42 | m0 <- lmer(
43 | sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF+(1+T+F+TF|session)+(1+T+P+TP|item),
44 | gb12, REML=FALSE, start=thcvg$gb12$m0,
45 | control=lmerControl(optimizer="Nelder_Mead",optCtrl=list(maxfun=1L),
46 | check.conv.grad="ignore",check.conv.hess="ignore"))
47 | print(summary(m0),corr=FALSE)
48 | ```
49 |
50 | The model converges without problems, but two correlation parameters are estimated as 1.
51 |
52 | ### Principal components analysis for _maxLMM_
53 |
54 | ```{r m0PCA}
55 | summary(rePCA(m0))
56 | ```
57 |
58 | The PCA results indicate two dimensions with no variability in the random
59 | effects for session and another two dimensions in the random effects for item.
60 |
61 | ### Zero-correlation-parameter linear mixed model (_zcpLMM_)
62 |
63 | ```{r gbm02}
64 | m1 <-
65 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F+TF||session) + (1+T+P+TP||item),
66 | gb12, REML=FALSE)
67 | VarCorr(m1)
68 | anova(m1, m0)
69 | ```
70 |
71 | The _zcpLMM_ fits significantly worse than the _maxLMM_, but it reveals several variance components with values close to or of zero.
72 |
73 | ### Iterative reduction of model complexity
74 |
75 | Let's refit the model without small variance components.
76 |
77 | ```{r m2}
78 | m2 <-
79 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F||session) + (1+T||item),
80 | gb12, REML=FALSE)
81 | VarCorr(m2)
82 | anova(m2, m1, m0)
83 | ```
84 |
85 | Let's check the support of item-related variance components
86 | ```{r m3}
87 | m3 <-
88 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F||session) + (1|item),
89 | gb12, REML=FALSE)
90 | VarCorr(m3)
91 | anova(m3, m2)
92 | ```
93 |
94 | Marginally significant drop. (Deleting the intercept too leads to a significant drop in goodness of fit.)
95 |
96 | ### Extending the reduced LMM with correlation parameters
97 |
98 | Let's check correlation parameters for `item`
99 |
100 | ```{r m4,warning=FALSE}
101 | m4 <-
102 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F|session) + (1|item),
103 | gb12, REML=FALSE)
104 | VarCorr(m4)
105 | anova(m3, m4)
106 | ```
107 |
108 | The correlation parameter is significant, but one correlation is 1.000, indicating a singular model. Let's remove the small correlation parameters.
109 |
110 | ### Pruning small correlation parameters
111 |
112 | ```{r m5}
113 | m5 <-
114 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+F|session) + (0+T|session) + (1|item),
115 | gb12, REML=FALSE)
116 | VarCorr(m5)
117 | anova(m5, m4)
118 | ```
119 |
120 | Now the model is clearly degenerate: The correlation is at the boundary (-1); `theta` returns a zero value for one of the variance components.
121 |
122 | ## Summary
123 |
124 |
125 | ## Versions of packages used
126 | ```{r versions}
127 | sessionInfo()
128 | ```
129 |
130 | ## References
131 |
--------------------------------------------------------------------------------
/inst/doc/KB.R:
--------------------------------------------------------------------------------
1 | ## ----preliminaries,echo=FALSE,include=FALSE,cache=FALSE------------------
2 | library(lme4)
3 | library(RePsychLing)
4 | library(knitr)
5 | opts_chunk$set(comment=NA)
6 | options(width=92,show.signif.stars = FALSE)
7 |
8 | ## ----kb07str-------------------------------------------------------------
9 | str(kb07)
10 |
11 | ## ----m0------------------------------------------------------------------
12 | m0 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1+S+P+C+SP+SC+PC+SPC|subj) + (1+S+P+C+SP+SC+PC+SPC|item),
13 | kb07, REML=FALSE, start=thcvg$kb07$m0,
14 | control=lmerControl(optimizer="Nelder_Mead",optCtrl=list(maxfun=1L),
15 | check.conv.grad="ignore",check.conv.hess="ignore"))
16 | print(summary(m0),corr=FALSE)
17 |
18 | ## ----chf0----------------------------------------------------------------
19 | summary(rePCA(m0))
20 |
21 | ## ----m1------------------------------------------------------------------
22 | m1 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1+S+P+C+SP+SC+PC+SPC||subj) +
23 | (1+S+P+C+SP+SC+PC+SPC||item), kb07, REML=FALSE)
24 | print(summary(m1),corr=FALSE)
25 |
26 | anova(m1, m0)
27 |
28 | ## ----rePCAm1-------------------------------------------------------------
29 | summary(rePCA(m1))
30 |
31 | ## ----m2------------------------------------------------------------------
32 | m2 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1+S+P+C+SP+PC||subj) +
33 | (1+P+C+SC+PC+SPC||item), kb07, REML=FALSE)
34 | anova(m2, m1) # not significant: prefer m2 over m1
35 |
36 | ## ----m3------------------------------------------------------------------
37 | m3 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1+C+SP+PC||subj) + (1+P+C+PC||item),kb07,REML=FALSE)
38 | anova(m3, m2) # not significant: prefer m3 over m2
39 |
40 | ## ----m4------------------------------------------------------------------
41 | m4 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1|item) + (0+P|item),kb07,REML=FALSE)
42 | anova(m4, m3) # not significant: prefer m4 over m3
43 | anova(m4, m1) # not significant: prefer m4 over m1 (no accumulation)
44 |
45 | ## ----m5------------------------------------------------------------------
46 | m5 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1|item), data=kb07, REML=FALSE)
47 | anova(m5, m4) # significant: prefer m4 over m5
48 |
49 | ## ----m6------------------------------------------------------------------
50 | m6 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1+P|item), kb07, REML=FALSE)
51 | print(summary(m6), corr=FALSE)
52 |
53 | anova(m4, m6) # significant: prefer m6 over m4
54 | anova(m6, m0) # not significant: prefer m6 over m0 (no accumulation)
55 |
56 | ## ----versions------------------------------------------------------------
57 | sessionInfo()
58 |
59 |
--------------------------------------------------------------------------------
/inst/doc/KB.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "RePsychLing Kronmüller & Barr (2007)"
3 | author: "Reinhold Kliegl"
4 | date: "March 5, 2015"
5 | output:
6 | html_document: default
7 | pdf_document:
8 | highlight: tango
9 | keep_tex: yes
10 | word_document: default
11 | geometry: margin=1in
12 | fontsize: 12pt
13 | ---
14 |
18 |
19 |
20 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
21 | library(lme4)
22 | library(RePsychLing)
23 | library(knitr)
24 | #opts_chunk$set(cache=TRUE)
25 | options(width=92,show.signif.stars = FALSE)
26 | ```
27 | # Revisiting Kronmüller & Barr (2007)
28 |
29 | We apply the iterative reduction of LMM complexity to truncated response times of a 2x2x2 factorial psycholinguistic experiment (Kronmüller \& Barr, 2007, Exp. 2; reanalyzed with an LMM in Barr et al., 2012). The data are from 56 subjects who responded to 32 items. Specifically, subjects had to select one of several objects presented on a monitor with a cursor. The manipulations involved (1) auditory instructions that maintained or broke a precedent of reference for the objects established over prior trials, (2) with the instruction being presented by the speaker who established the precedent (i.e., an old speaker) or a new speaker, and (3) whether the task had to be performed without or with a cognitive load consisting of six random digits. All factors were varied within subjects and within items. There were main effects of Load, Speaker, and Precedent; none of the interactions were significant. Although standard errors of fixed-effect coefficents varied slightly across models, our reanalyses afforded the same statistical inference about the experimental manipulations as the original article, irrespective of LMM specification. The purpose of the analysis is to illustrate an assessment of model complexity as far as variance components and correlation parameters are concerned, neither of which were in the focus of the original publication.
30 |
31 | ## Data from Kronmüller & Barr (2007)
32 |
33 | The data are available as `kb07` in the `RePsychLing` package.
34 |
35 | ```{r kb07str}
36 | str(kb07)
37 | ```
38 |
39 | ### Maximal linear mixed model (_maxLMM_)
40 |
41 | Barr et al. (2012, supplement) analyzed Kronmüller and Barr (2007, Exp. 2) with the _maxLMM_ comprising 16 variance components (eight each for the random factors `subj` and `item`, respectively) (Footnote below output). This model takes a long time to fit using `lmer` because there are so many parameters and the likelihood surface is very flat. The `lmm` function from the [MixedModels](https://github.com/dmbates/MixedModels.jl) package for [Julia](http://julialang.org) is much faster fitting this particular model, providing the results
42 | ```
43 | Linear mixed model fit by maximum likelihood
44 | Formula: RTtrunc ~ 1 + S + P + C + SP + SC + PC + SPC + ((1 + S + P + C + SP + SC + PC + SPC) | subj) + ((1 + S + P + C + SP + SC + PC + SPC) | item)
45 |
46 | logLik: -14293.158810, deviance: 28586.317621
47 |
48 | Variance components:
49 | Variance Std.Dev. Corr.
50 | subj 90767.624499 301.276658
51 | 5182.066776 71.986574 -0.43
52 | 5543.682920 74.455913 -0.47 -0.47
53 | 7586.760521 87.102012 0.21 0.21 0.21
54 | 8829.390369 93.964836 0.20 0.20 0.20 0.20
55 | 1821.475112 42.678743 0.47 0.47 0.47 0.47 0.47
56 | 7422.143831 86.151865 -0.10 -0.10 -0.10 -0.10 -0.10 -0.10
57 | 3802.221282 61.662154 -0.48 -0.48 -0.48 -0.48 -0.48 -0.48 -0.48
58 | item 129826.159849 360.313974
59 | 1855.215345 43.072211 -0.34
60 | 62394.709532 249.789330 -0.68 -0.68
61 | 2947.499298 54.290877 0.20 0.20 0.20
62 | 1042.837137 32.292989 0.57 0.57 0.57 0.57
63 | 1620.926380 40.260730 0.28 0.28 0.28 0.28 0.28
64 | 4700.349645 68.559096 0.08 0.08 0.08 0.08 0.08 0.08
65 | 4820.059886 69.426651 0.04 0.04 0.04 0.04 0.04 0.04 0.04
66 | Residual 399613.415344 632.149836
67 | Number of obs: 1790; levels of grouping factors: 56, 32
68 |
69 | Fixed-effects parameters:
70 | Estimate Std.Error z value
71 | (Intercept) 2180.63 76.8193 28.3865
72 | S -66.9899 19.3337 -3.46493
73 | P -333.881 47.6666 -7.0045
74 | C 78.987 21.2336 3.7199
75 | SP 22.1518 20.3356 1.08931
76 | SC -18.9244 17.506 -1.08102
77 | PC 5.26193 22.4211 0.234687
78 | SPC -23.951 21.0191 -1.13949
79 | ```
80 | This fit converges and produces what look like reasonable parameter estimates (i.e., no variance components with estimates close to zero; no correlation parameters with values close to +/-1).
81 |
82 | ```{r m0, eval=FALSE}
83 | m0 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC+(1+S+P+C+SP+SC+PC+SPC|subj)+(1+S+P+C+SP+SC+PC+SPC|item),kb07,start=kb07maxLMMtheta,control=lmerControl(optimizer="none"))
84 | ```
85 |
86 | ```{r load_m0,include=FALSE}
87 | load("kb07_m0.rda") # must be in working directory (or provide path)
88 | m0@theta <- Re(kb07maxLMMtheta)
89 | ```
90 | ```{r showm0}
91 | summary(m0)
92 | ```
93 |
94 | The model took 39,004 iterations for the nonlinear optimizer to converge, but produces what look like reasonable parameter estimates. The slow convergence is due to a total of 2 x 36 = 72 parameters in the optimization. These parameters are all in the relative covariance factors. The more easily estimated nine fixed-effects parameters have been "profiled out" of the optimization.
95 |
96 | Footnote: The model formula reported in the supplement of Barr et al. (2012) specified only five variance components for the random factor item. However, `lmer()` automatically includes all lower-order terms of interactions specified for random-factor terms, resulting in the _maxLMM_ for this experimental design.
97 |
98 | ### Evaluation of singular value decomposition (svd) for _maxLMM_
99 |
100 | Considering that there are only 56 subjects and 32 items it is quite optimistic to expect to estimate 36 highly nonlinear covariance parameters for `subj` and another 36 for `item`.
101 | ```{r chf0}
102 | summary(rePCA(m0))
103 | ```
104 | The directions are the principal components for this covariance matrix. We see that there are seven singular values of zero, that is there is zero variability in seven directions. Overall, the svd analysis of this model returns only eight principal components with variances larger than one percent. Thus, the _maxLMM_ is clearly too complex.
105 |
106 | ### Zero-correlation-parameter linear mixed model (_zcpLMM_)
107 |
108 | As a first step of model reduction, we propose to start with a model including all 16 variance components, but no correlation parameters. Note that here we go through the motion to be consistent with the recommended strategy. The large number of components with zero or close to zero variance in _maxLMM_ already strongly suggests the need for a reduction of the number of variance components--as done in the next step. For this _zcpLMM_, we extract the vector-valued variables from the model matrix without the intercept column which is provided by the R formula. Then, we use the new double-bar syntax for `lmer()` to force correlation parameters to zero.
109 |
110 | ```{r m1}
111 | m1 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1+S+P+C+SP+SC+PC+SPC||subj) +
112 | (1+S+P+C+SP+SC+PC+SPC||item), kb07, REML=FALSE)
113 | print(summary(m1),corr=FALSE)
114 |
115 | anova(m1, m0)
116 | ```
117 |
118 | Nominally, the _zcpLMM_ fits significantly worse than the _maxLMM_, but note that the \chi^2 for the LRT (85) is smaller than twice the degrees of freedom for the LRT (56). Also the degrees of freedom are somewhat of an underestimate. According to our judgement, _zcpLMM_ could be preferred to _maxLMM_.
119 |
120 | ### Principal components analysis for _zcpLMM_
121 |
122 | ```{r rePCAm1}
123 | summary(rePCA(m1))
124 | ```
125 |
126 | The PCM analysis of _zcpLMM_ returns 12 of 16 components with variances different from zero. Thus, using this result as guide, the _zcpLMM_ is still too complex. Inspection of _zcpLMM_ variance components (see _zcpLMM_ `m1`) suggests a further reduction of model complexity with drop1-LRT tests, starting with the smallest variance components.
127 |
128 | ### Dropping non-significant variance components
129 |
130 | A second step of model reduction is to remove variance components that are not significant according to a likelihood ratio test (LRT). Starting with the smallest variance component (or a set of them) this step can be repeated until significant change in goodness of fit is indicated. For the present case, variance components for `SC` and `SPC` for `subj` and `S` and `SP` for `item` are estimated with zero values. We refit the LMM without these variance components.
131 |
132 | ```{r m2}
133 | m2 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1+S+P+C+SP+PC||subj) +
134 | (1+P+C+SC+PC+SPC||item), kb07, REML=FALSE)
135 | anova(m2, m1) # not significant: prefer m2 over m1
136 | ```
137 |
138 | Obviously, these four variance components are not supported by information in the data. So we drop the next four smallest variance components, vc1 and vc2 for `subj` and vc5 and vc7 for `item`.
139 |
140 | ```{r m3}
141 | m3 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1+C+SP+PC||subj) + (1+P+C+PC||item),kb07,REML=FALSE)
142 | anova(m3, m2) # not significant: prefer m3 over m2
143 | ```
144 |
145 | There is no significant drop in goodness of fit. Therefore, we continue with dropping vc3, vc4, and vc6 for `SubjectID` and vc3 and vc6 for `item`.
146 |
147 | ```{r m4}
148 | m4 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1|item) + (0+P|item),kb07,REML=FALSE)
149 | anova(m4, m3) # not significant: prefer m4 over m3
150 | anova(m4, m1) # not significant: prefer m4 over m1 (no accumulation)
151 | ```
152 |
153 | As a final test, we refit the LMM without vc2 for `item`.
154 |
155 | ```{r m5}
156 | m5 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1|item), data=kb07, REML=FALSE)
157 | anova(m5, m4) # significant: prefer m4 over m5
158 | ```
159 |
160 | This time the LRT is significant. Therefore, we stay with LMM `m4` and test correlation parameters for this model.
161 |
162 | ### Extending the reduced LMM with a correlation parameter
163 |
164 | ```{r m6}
165 | m6 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1+P|item), kb07, REML=FALSE)
166 | print(summary(m6), corr=FALSE)
167 |
168 | anova(m4, m6) # significant: prefer m6 over m4
169 | anova(m6, m0) # not significant: prefer m6 over m0 (no accumulation)
170 | ```
171 |
172 | There is evidence for a reliable item-related negative correlation parameter between mean and precedence effect, that is there are reliable differences between items in the precedence effect. Finally, there is no significant difference between LMM `m6` and the _maxLMM_ `m0`. The final number of reliable dimensions is actually smaller than suggested by the PCA analysis of the _maxLMM_ `m0`.
173 |
174 | ### Profiling the parameters
175 |
176 | Confidence intervals for all parameters can be obtained
177 |
178 |
179 | ### Summary
180 | In our opinion, `m6` is the _optimal_ LMM for the data of this experiment. The general strategy of (1) starting with _maxLMM_, (2) followed by _zcpLMM_, (3) followed by iteratively dropping variance components until there is a significant decrease in goodness of model fit, (4) followed by inclusion of correlation parameters for the remaining variance components, and (5) using svd all along the way to check the principal dimensionality of the data for the respective intermediate models worked quite well again. Indeed, we also reanalyzed two additional experiments reported in the supplement of Barr et al. (2012). As documented in the `RePsychLing` package accompanying the present article, in each case, the _maxLMM_ was too complex for the information provided by the experimental data. In each case, the data supported only a very sparse random-effects structure beyond varying intercepts for subjects and items. Fortunately and interestingly, none of the analyses changed the statistical inference about fixed effects in these experiments. Obviously, this cannot be ruled out in general. If authors adhere to a strict criterion for significance, such as p < .05 suitably adjusted for multiple comparisons, there is always a chance that a t-value will fall above or below the criterion across different versions of an LMM.
181 |
182 | Given the degree of deconstruction (i.e., model simplification) reported for these models, one may wonder whether it might be more efficient to iteratively _increase_ rather the _decrease_ LMM complexity, that is to start with a minimal linear mixed model (_minLMM_), varying only intercepts of subject and item factors and adding variance components and correlation parameters to such a model. We will turn to this strategy in the next section.
183 |
184 | ## Versions of packages used
185 | ```{r versions}
186 | sessionInfo()
187 | ```
188 |
189 |
--------------------------------------------------------------------------------
/inst/doc/KBStan.R:
--------------------------------------------------------------------------------
1 | ## ----preliminaries,echo=FALSE,include=FALSE,cache=FALSE-----------------------------------
2 | library(RePsychLing)
3 | library(knitr)
4 | library(rstan)
5 | library(parallel)
6 | library(xtable)
7 | opts_chunk$set(comment=NA)
8 | options(width=92,
9 | show.signif.stars = FALSE)
10 |
11 | ## ----kb07str------------------------------------------------------------------------------
12 | str(kb07)
13 |
14 | ## ----X------------------------------------------------------------------------------------
15 | X <- unname(model.matrix(~ 1+S+P+C+SP+SC+PC+SPC, kb07))
16 | attr(X,"assign") <- NULL
17 | str(X)
18 |
19 | ## ----XtX----------------------------------------------------------------------------------
20 | crossprod(X) # X'X
21 |
22 | ## ----standat------------------------------------------------------------------------------
23 | standat <- '
24 | data {
25 | int N; // num observations
26 | int K; // length of fixed-effects vector
27 | int M; // num subjects
28 | int J; // length of subj vector-valued random effects
29 | int L; // num items
30 | int I; // length of item vector-values random effects
31 | int subj[N]; // subject indicator
32 | int item[N]; // item indicator
33 | row_vector[K] X[N]; // model matrix for fixed-effects parameters
34 | row_vector[J] Zs[N]; // generator model matrix for subj random effects
35 | row_vector[I] Zi[N]; // generator model matrix for item random effects
36 | vector[N] y; // response vector (reaction time)
37 | }
38 |
39 | '
40 |
41 | ## ----stanpars-----------------------------------------------------------------------------
42 | stanpars <- '
43 | parameters {
44 | cholesky_factor_corr[J] Ls; // Cholesky factor of subj r.e. correlations
45 | cholesky_factor_corr[I] Li; // Cholesky factor of item r.e. correlations
46 | vector[J] taus; // standard deviations of unconditional subj r.e. dist
47 | vector[I] taui; // standard deviations of unconditional item r.e. dist
48 | vector[J] us[M]; // spherical subj random effects
49 | vector[I] ui[L]; // spherical item random effects
50 | vector[K] beta; // fixed-effects
51 | real sigma; // standard deviation of response given random effects
52 | }
53 |
54 | '
55 |
56 | ## ----stantrans----------------------------------------------------------------------------
57 | stantrans <- '
58 | transformed parameters {
59 | matrix[J,J] corrs;
60 | matrix[I,I] corri;
61 | corrs <- tcrossprod(Ls); // for monitoring subj correlations
62 | corri <- tcrossprod(Li); // for monitoring item correlations
63 | }
64 |
65 | '
66 |
67 | ## ----model--------------------------------------------------------------------------------
68 | stanmod <- '
69 | model {
70 | matrix[J,J] Lambdas;
71 | vector[J] bs[M];
72 | matrix[I,I] Lambdai;
73 | vector[I] bi[L];
74 | taus ~ cauchy(0,2.5);
75 | taui ~ cauchy(0,2.5);
76 | Ls ~ lkj_corr_cholesky(2);
77 | Li ~ lkj_corr_cholesky(2);
78 | Lambdas <- diag_pre_multiply(taus,Ls);
79 | Lambdai <- diag_pre_multiply(taui,Li);
80 | for (m in 1:M) {
81 | us[m] ~ normal(0,1);
82 | bs[m] <- Lambdas * us[m];
83 | }
84 | for (l in 1:L) {
85 | ui[l] ~ normal(0,1);
86 | bi[l] <- Lambdai * ui[l];
87 | }
88 | for (n in 1:N)
89 | y[n] ~ normal(X[n] * beta + Zs[n] * bs[subj[n]] + Zi[n] * bi[item[n]], sigma);
90 | }
91 |
92 | '
93 |
94 | ## -----------------------------------------------------------------------------------------
95 | model <- paste(standat, stanpars, stantrans, stanmod)
96 |
97 | ## ----maxdat-------------------------------------------------------------------------------
98 | maxdat <-
99 | within(list(), {
100 | N <- nrow(X)
101 | K <- J <- I <- ncol(X)
102 | M <- length(levels(kb07$subj))
103 | L <- length(levels(kb07$item))
104 | X <- Zs <- Zi <- unname(X)
105 | y <- kb07$RTtrunc
106 | subj <- as.integer(kb07$subj)
107 | item <- as.integer(kb07$item)
108 | }
109 | )
110 | str(maxdat)
111 |
112 | ## ----maxmodel-----------------------------------------------------------------------------
113 | maxmodel <- stan(model_name="maxmodel", model_code=model, data=maxdat, chains=0)
114 |
115 | ## ----KB07_stan,cache=TRUE-----------------------------------------------------------------
116 | system.time(KB07_stan <-
117 | sflist2stanfit(
118 | mclapply(1:4, mc.cores = 4, # adjust number of cores to suit
119 | function(i) stan(fit = maxmodel,
120 | data = maxdat,
121 | iter=2000,
122 | chains = 1,
123 | chain_id = i,
124 | refresh = -1))
125 | )
126 | )
127 |
128 | ## ----KB07_results,cache=FALSE-------------------------------------------------------------
129 | KB07_results<- summary(KB07_stan,
130 | pars=c("beta", "sigma",
131 | "taus","taui",
132 | "corrs","corri"),
133 | probs = c(0.025, 0.975), digits_summary = 3)
134 | rownames(KB07_results$summary)
135 |
136 | ## ----printmaxmodel,echo=FALSE,eval=TRUE,cache=FALSE,results="asis"------------------------
137 | print(xtable(KB07_results$summary[c(1:25,27:33,36:41,45:49,55:57,62:64,81,91:97,100:105),c(1,4,5)]), type="html")
138 |
139 | ## ----datreduced---------------------------------------------------------------------------
140 | finaldat <-
141 | within(list(), {
142 | N <- nrow(X)
143 | K <- ncol(X)
144 | J <- 1L
145 | I <- 2L
146 | M <- length(levels(kb07$subj))
147 | L <- length(levels(kb07$item))
148 | X <- X
149 | Zs <- X[, 1, drop=FALSE]
150 | Zi <- X[, 1:2]
151 | y <- kb07$RTtrunc
152 | subj <- as.integer(kb07$subj)
153 | item <- as.integer(kb07$item)
154 | }
155 | )
156 | str(finaldat)
157 |
158 | ## -----------------------------------------------------------------------------------------
159 | system.time(KB07_finalstan <-
160 | sflist2stanfit(
161 | mclapply(1:4, mc.cores = 4, # adjust number of cores to suit
162 | function(i) stan(fit = maxmodel,
163 | data = finaldat,
164 | iter=2000,
165 | chains = 1,
166 | chain_id = i,
167 | refresh = -1))
168 | )
169 | )
170 |
171 |
--------------------------------------------------------------------------------
/inst/doc/KBStan.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "A Bayesian Linear Mixed Model Analysis of the Kronmüller and Barr (2007) data using Stan"
3 | author: "Shravan Vasishth and Douglas Bates"
4 | date: "`r Sys.Date()`"
5 | output: rmarkdown::html_vignette
6 | vignette: >
7 | %\VignetteIndexEntry{KB07 Bayesian data analysis}
8 | %\VignetteEngine{knitr::rmarkdown}
9 | \usepackage[utf8]{inputenc}
10 | ---
11 |
12 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
13 | library(RePsychLing)
14 | library(knitr)
15 | library(rstan)
16 | library(parallel)
17 | library(xtable)
18 | opts_chunk$set(comment=NA)
19 | options(width=92,
20 | show.signif.stars = FALSE)
21 | ```
22 |
23 | ## Structure of the data
24 |
25 | The data from Kronmüller and Barr (2007) is available as the `kb07` data frame in the
26 | [RePscyhLing package](https://github.com/dmbates/RePsychLing) for [R](http://www.r-project.org)
27 | ```{r kb07str}
28 | str(kb07)
29 | ```
30 |
31 | As is common with factorial designs the experimental factors and their interactions are given
32 | short names: a single character (`S`,`P`,`C`) for the main effects, two-character names for the two-factor
33 | interactions and a three-character name for the three-factor interaction.
34 |
35 | The data are from 56 subjects who responded to 32 iterms. Specifically, subjects had to select one of several objects presented on a monitor with a cursor. The manipulations involved
36 |
37 | 1. auditory instructions that maintained or broke a precedent of reference for the objects established over prior trials, (`P`)
38 | 2. with the instruction being presented by the speaker who established the precedent (i.e., an old speaker) or a new speaker (`S`) a
39 | 3. whether the task had to be performed without or with a cognitive load consisting of six random digits. (`C`)
40 |
41 | All factors were expressed using a `±1` encoding which ensures that the scale of the interactions is the same as the
42 | scale of the main effects and of the intercept term. The columns of the full-factorial model matrix, `X`
43 | ```{r X}
44 | X <- unname(model.matrix(~ 1+S+P+C+SP+SC+PC+SPC, kb07))
45 | attr(X,"assign") <- NULL
46 | str(X)
47 | ```
48 | have the same length and are nearly orthogonal.
49 | ```{r XtX}
50 | crossprod(X) # X'X
51 | ```
52 |
53 | The response, `RTtrunc`, is the reaction time after truncating some unusually large values.
54 |
55 | ## Formulating the Stan model
56 |
57 | [Stan](http://stan-mc.org) is a system for creating Markov chain Monte Carlo (MCMC) samplers for statistical models.
58 |
59 | In the model specification the data, including dimensions of arrays, are described first.
60 | ```{r standat}
61 | standat <- '
62 | data {
63 | int N; // num observations
64 | int K; // length of fixed-effects vector
65 | int M; // num subjects
66 | int J; // length of subj vector-valued random effects
67 | int L; // num items
68 | int I; // length of item vector-values random effects
69 | int subj[N]; // subject indicator
70 | int item[N]; // item indicator
71 | row_vector[K] X[N]; // model matrix for fixed-effects parameters
72 | row_vector[J] Zs[N]; // generator model matrix for subj random effects
73 | row_vector[I] Zi[N]; // generator model matrix for item random effects
74 | vector[N] y; // response vector (reaction time)
75 | }
76 |
77 | '
78 | ```
79 | The response vector, `y`, is expressed as a vector of `N` elements.
80 | The integer vectors `subj` and `item` are the indicators of subject and of item for each response.
81 |
82 | The fixed-effects model matrix, `X`, the subject-specific random effects model matrix, `Zs`,
83 | and the item-specific random effects model matrix, `Zi`, are stored as vectors of `N` `row_vector`'s.
84 | The reason for storing the model matrices like this is to provide fast access to individual rows when
85 | formulating the model.
86 |
87 | For generality the number of columns in these matrices is specified separately as `K`, `J` and `I`.
88 | For the _maximal model_ these will all be eight. The numbers of subjects and items are `M` and `L`, respectively.
89 |
90 | ## Model parameters
91 |
92 | In a Bayesian formulation all the random variables are considered _model parameters_; there is no distinction
93 | between the random effects and the fixed-effects coefficients. The covariance matrices for the within-subject
94 | and within-item random effects are specified according to the Cholesky factor of the correlation matrix,
95 | a `cholesky_factor_corr` type and the standard deviations, which are called `taus` and `taui`, respectively.
96 |
97 | The _spherical random effects_, often written as $u$, are called `us` and `ui` respectively.
98 | These are matrices that are stored as vectors of vectors.
99 |
100 | ```{r stanpars}
101 | stanpars <- '
102 | parameters {
103 | cholesky_factor_corr[J] Ls; // Cholesky factor of subj r.e. correlations
104 | cholesky_factor_corr[I] Li; // Cholesky factor of item r.e. correlations
105 | vector[J] taus; // standard deviations of unconditional subj r.e. dist
106 | vector[I] taui; // standard deviations of unconditional item r.e. dist
107 | vector[J] us[M]; // spherical subj random effects
108 | vector[I] ui[L]; // spherical item random effects
109 | vector[K] beta; // fixed-effects
110 | real sigma; // standard deviation of response given random effects
111 | }
112 |
113 | '
114 | ```
115 |
116 | The correlation matrices formed from the Cholesky factors are considered _transformed parameters_
117 | ```{r stantrans}
118 | stantrans <- '
119 | transformed parameters {
120 | matrix[J,J] corrs;
121 | matrix[I,I] corri;
122 | corrs <- tcrossprod(Ls); // for monitoring subj correlations
123 | corri <- tcrossprod(Li); // for monitoring item correlations
124 | }
125 |
126 | '
127 | ```
128 |
129 | Finally, the model is expressed in terms of square matrices `Lambdas` and `Lambdai` that are
130 | the Cholesky factors of the covariance matrices for the subject-specific and item-specific
131 | random effects, respectively.
132 | The _prior distributions_ for the parameters (`taus`, `taui`, `Ls`, `Li`, `us`, and `ui`) are
133 | also specified at this point.
134 | No prior distribution is provided for `beta`, implying that this vector has a "flat" or "locally uniform"
135 | improper prior distribution.
136 | It could be given, say, a diffuse multivariate Gaussian distribution, but doing so has no effect on inferences.
137 |
138 | ```{r model}
139 | stanmod <- '
140 | model {
141 | matrix[J,J] Lambdas;
142 | vector[J] bs[M];
143 | matrix[I,I] Lambdai;
144 | vector[I] bi[L];
145 | taus ~ cauchy(0,2.5);
146 | taui ~ cauchy(0,2.5);
147 | Ls ~ lkj_corr_cholesky(2);
148 | Li ~ lkj_corr_cholesky(2);
149 | Lambdas <- diag_pre_multiply(taus,Ls);
150 | Lambdai <- diag_pre_multiply(taui,Li);
151 | for (m in 1:M) {
152 | us[m] ~ normal(0,1);
153 | bs[m] <- Lambdas * us[m];
154 | }
155 | for (l in 1:L) {
156 | ui[l] ~ normal(0,1);
157 | bi[l] <- Lambdai * ui[l];
158 | }
159 | for (n in 1:N)
160 | y[n] ~ normal(X[n] * beta + Zs[n] * bs[subj[n]] + Zi[n] * bi[item[n]], sigma);
161 | }
162 |
163 | '
164 | ```
165 | In the last loop the linear predictor for `y[n]` is evaluated as the sum of three products of
166 | `row_vector`s and `vector`s. A `row_vector` multiplied by a `vector` is a scalar, which is the reason
167 | for storing `X`, `Zs` and `Zi` as vectors of `row_vector`s.
168 |
169 | These pieces are concatenated to form the model
170 | ```{r}
171 | model <- paste(standat, stanpars, stantrans, stanmod)
172 | ```
173 |
174 | ## Compiling the model
175 |
176 | The model is compiled via a call to `stan` that includes the model and the data.
177 | The data must be specified as a list or as an environment.
178 |
179 | For the _maximal model_, `X`, `Zs` and `Zi` are the model matrix for the full factorial
180 | ```{r maxdat}
181 | maxdat <-
182 | within(list(), {
183 | N <- nrow(X)
184 | K <- J <- I <- ncol(X)
185 | M <- length(levels(kb07$subj))
186 | L <- length(levels(kb07$item))
187 | X <- Zs <- Zi <- unname(X)
188 | y <- kb07$RTtrunc
189 | subj <- as.integer(kb07$subj)
190 | item <- as.integer(kb07$item)
191 | }
192 | )
193 | str(maxdat)
194 | ```
195 |
196 | Initially we set the number of chains to zero to check that the model compiles properly
197 | ```{r maxmodel}
198 | maxmodel <- stan(model_name="maxmodel", model_code=model, data=maxdat, chains=0)
199 | ```
200 |
201 | ## Creating the chains.
202 |
203 | We use `mclapply` from the `parallel` package to generate the chains in parallel.
204 | ```{r KB07_stan,cache=TRUE}
205 | system.time(KB07_stan <-
206 | sflist2stanfit(
207 | mclapply(1:4, mc.cores = 4, # adjust number of cores to suit
208 | function(i) stan(fit = maxmodel,
209 | data = maxdat,
210 | iter=2000,
211 | chains = 1,
212 | chain_id = i,
213 | refresh = -1))
214 | )
215 | )
216 | ```
217 | We see that the elapsed time is considerably less than the user time. This is because four processes are running in parallel.
218 |
219 |
220 | A close examination of the timing shows that much more time is spent in the "warmup" phase than in actually generating samples. Stan defaults to using a form of Hamiltonian Monte Carlo (HMC) sampling called a "No U-Turn Sampler" (NUTS) and it is tuning these samplers that is taking most of the time.
221 |
222 |
223 | ```{r KB07_results,cache=FALSE}
224 | KB07_results<- summary(KB07_stan,
225 | pars=c("beta", "sigma",
226 | "taus","taui",
227 | "corrs","corri"),
228 | probs = c(0.025, 0.975), digits_summary = 3)
229 | rownames(KB07_results$summary)
230 | ```
231 | Note that all of the correlation matrix elements are monitored even though
232 | > ?stan the diagonal
233 | elements are, by definition, unity and the matrix must be symmetric. An arduous extraction provides the table
234 |
235 | ```{r printmaxmodel,echo=FALSE,eval=TRUE,cache=FALSE,results="asis"}
236 | print(xtable(KB07_results$summary[c(1:25,27:33,36:41,45:49,55:57,62:64,81,91:97,100:105),c(1,4,5)]), type="html")
237 | ```
238 |
239 | Note that most of the correlations, especially those for the item-specific random effects, have a mean close to zero and upper and lower limits that are more-or-less symmetric. This is strong evidence that these could be zero.
240 |
241 | Because of the way the priors are defined the `taus` and `taui` values cannot become zero. However, many of these values are very close to zero. The only standard deviations that are substantion are the by-subject intercept and the by-item intercept and coefficient for `P`.
242 |
243 | ## Reduced model
244 |
245 | The final, reduced model has a single column (the intercept) in `Zs` and two columns (intercept and main-effect for `P`) in `Zi`.
246 | ```{r datreduced}
247 | finaldat <-
248 | within(list(), {
249 | N <- nrow(X)
250 | K <- ncol(X)
251 | J <- 1L
252 | I <- 2L
253 | M <- length(levels(kb07$subj))
254 | L <- length(levels(kb07$item))
255 | X <- X
256 | Zs <- X[, 1, drop=FALSE]
257 | Zi <- X[, 1:2]
258 | y <- kb07$RTtrunc
259 | subj <- as.integer(kb07$subj)
260 | item <- as.integer(kb07$item)
261 | }
262 | )
263 | str(finaldat)
264 | ```
265 | ```{r}
266 | system.time(KB07_finalstan <-
267 | sflist2stanfit(
268 | mclapply(1:4, mc.cores = 4, # adjust number of cores to suit
269 | function(i) stan(fit = maxmodel,
270 | data = finaldat,
271 | iter=2000,
272 | chains = 1,
273 | chain_id = i,
274 | refresh = -1))
275 | )
276 | )
277 | ```
278 |
279 |
280 |
--------------------------------------------------------------------------------
/inst/doc/KKL.R:
--------------------------------------------------------------------------------
1 | ## ----preliminaries,echo=FALSE,include=FALSE,cache=FALSE-----------------------------------
2 | library(lme4)
3 | library(knitr)
4 | library(RePsychLing)
5 |
6 | opts_chunk$set(cache=FALSE)
7 | options(show.signif.stars=FALSE,width=92)
8 |
9 | ## ----m0-----------------------------------------------------------------------------------
10 | mm <- model.matrix(~ sze*(spt+obj+grv)*orn, data=KKL)
11 | KKL$sze_spt <- mm[, 7]
12 | KKL$sze_obj <- mm[, 8]
13 | KKL$sze_grv <- mm[, 9]
14 | KKL$sze_orn <- mm[, 10]
15 | KKL$spt_orn <- mm[, 11]
16 | KKL$obj_orn <- mm[, 12]
17 | KKL$grv_orn <- mm[, 13]
18 | KKL$sze_spt_orn <- mm[, 14]
19 | KKL$sze_obj_orn <- mm[, 15]
20 | KKL$sze_grv_orn <- mm[, 16]
21 |
22 | m0 <- lmer(lrt ~ sze*(spt+obj+grv)*orn +
23 | (spt + obj + grv + orn + spt_orn + obj_orn + grv_orn | subj),
24 | data=KKL, REML=FALSE, control=lmerControl(optCtrl=list(maxfun=10000L)))
25 | #print(summary(m0), corr=FALSE)
26 |
27 | ## ----sv0----------------------------------------------------------------------------------
28 | chf0 <- getME(m0, "Tlist")[[1]]
29 | zapsmall(chf0, digits=4)
30 |
31 | sv0 <- svd(chf0)
32 |
33 | round(sv0$d^2/sum(sv0$d^2)*100, 1)
34 |
35 | ## ----m1-----------------------------------------------------------------------------------
36 | print(summary(m1 <- lmer(lrt ~ sze*(spt+obj+grv)*orn +
37 | (spt + obj + grv + orn + spt_orn + obj_orn + grv_orn || subj),
38 | data=KKL, REML=FALSE)), corr=FALSE)
39 |
40 | sv1 <- svd(diag(getME(m1, "theta")) )
41 | sv1$d
42 |
43 | round(sv1$d^2/sum(sv1$d^2)*100, 1)
44 |
45 | anova(m1, m0)
46 |
47 | ## ----m2-----------------------------------------------------------------------------------
48 | print(summary(m2 <- lmer(lrt ~ sze*(spt+obj+grv)*orn +
49 | (spt + obj + grv + orn + spt_orn || subj),
50 | data=KKL, REML=FALSE)), corr=FALSE)
51 |
52 | sv2 <- svd(diag(getME(m2, "theta")) )
53 | sv2$d
54 |
55 | round(sv2$d^2/sum(sv2$d^2)*100, 3)
56 |
57 | anova(m2, m1) # not significant: prefer m2 to m1
58 | anova(m2, m0) # significant: m2 is "reduced" too much
59 |
60 | ## ----m3-----------------------------------------------------------------------------------
61 | print(summary(m3 <- lmer(lrt ~ sze*(spt+obj+grv)*orn +
62 | (spt + obj + grv + orn + spt_orn | subj),
63 | data=KKL, REML=FALSE)), corr=FALSE)
64 |
65 | chf3 <- getME(m3, "Tlist")[[1]]
66 | zapsmall(chf3, digits=4)
67 |
68 | sv3 <- svd(chf3)
69 |
70 | round(sv3$d^2/sum(sv3$d^2)*100, 1)
71 |
72 | anova(m2, m3) # significant: prefer m3 to m2
73 | anova(m3, m0) # not significant: prefer m3 to m0
74 |
75 | ## ----m4-----------------------------------------------------------------------------------
76 | print(summary(m4 <- lmer(lrt ~ sze*(spt+obj+grv)*orn +
77 | (spt + grv | subj) + (0 + obj | subj) + (0 + orn | subj) + (0 + spt_orn | subj),
78 | data=KKL, REML=FALSE)), corr=FALSE)
79 |
80 | getME(m4, "Tlist") # Variance components look ok
81 | chf4 <- diag(c(diag(getME(m4, "Tlist")[[1]]), getME(m4, "Tlist")[[2]], getME(m4, "Tlist")[[3]], getME(m4, "Tlist")[[4]]))
82 | chf4[1:3, 1:3] <- getME(m4, "Tlist")[[1]]
83 |
84 | sv4 <- svd(chf4)
85 | sv4$d # singular value decomposition: ok
86 |
87 | round(sv4$d^2/sum(sv4$d^2)*100, 1) # percentages of variance accounted: ok
88 |
89 | anova(m4, m3) # not significant: prefer m4 to m3
90 | anova(m4, m0) # not significant: prefer m4 to m0
91 |
92 | ## ----prof4, eval=FALSE--------------------------------------------------------------------
93 | # # Profiled 9 parameters individually; saved all results in as list in "p_m4.rda"
94 | # p_m4_1 = profile(m4_1, which=1) # increment 1 to 9
95 | # confint(p_m4_1)
96 |
97 | ## ----versions-----------------------------------------------------------------------------
98 | sessionInfo()
99 |
100 |
--------------------------------------------------------------------------------
/inst/doc/KKL.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "RePsychLing Kliegl et al. (2014)"
3 | author: "Reinhold Kliegl"
4 | date: "July 24, 2014"
5 | output:
6 | html_document: default
7 | pdf_document:
8 | keep_tex: yes
9 | word_document: default
10 | geometry: margin=1in
11 | fontsize: 12pt
12 | ---
13 |
17 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
18 | library(lme4)
19 | library(knitr)
20 | library(RePsychLing)
21 |
22 | opts_chunk$set(cache=FALSE)
23 | options(show.signif.stars=FALSE,width=92)
24 | ```
25 |
26 | As a first demonstration that linear mixed models with a maximum random-effect structure may be asking too much, we re-analyze data from a visual-attention experiment [@Kliegl:Kuschela:Laubrock:2014]. The experiment shows that validly cued targets presented are detected faster than invalidly cued ones (i.e., spatial cueing effect; Posner, 1980) and that target presented at the opposite end of a rectangle at which the cue had occurred are detected faster than targets presented at a different rectangle but with the same physical distance (object-based effect; Egly, Driver, & Rafal, 1994). Different from earlier research, the two rectangles were not only presented in cardinal orientation (i.e., in horizontal or vertical orientation), but also diagonally (45° left or 45° right). This manipulation afforded a follow up of a hypothesis that attention can be shifted faster diagonally across the screen than vertically or horizontally across the screen [@Kliegl:Wei:Dambacher:Yan:Zhou:2011; Zhou et al., 2006]. Finally, data are from two groups of subjects, one group had to detect small targets and the other large targets. The experiment is a follow-up to @Kliegl:Kuschela:Laubrock:2014 who used only small targets and only cardinal orientations for rectangles. For an interpretation of fixed effects we refer to @Kliegl:Wei:Dambacher:Yan:Zhou:2011. The different model specifications reported in this section were of no consequence for the significance or interpretation of fixed effects. Here the focus is exploring the random-effect structure for these data.
27 |
28 | ### Data
29 | Eighty-six subjects participated in this experiment. There were 800 trials requiring detection of a small or large rectangle and 40 catch trials. The experiment is based on a size (2) x cue-target relation (4) x orientation (2) design. Targets were small or large; rectangles were displayed either in cardinal or diagonal orientation, and cue-target relation was valid (70% of all trials) or invalid in three different ways (10% of trials in each the invalid conditions), corresponding to targets presented (a) on the same rectangle as the cue, but at the other end, (b) at the same physical distance as in (a), but on the other rectangle, or (c) at the other end of the other rectangle. The three contrasts for cue-target relation test differences in means between neighboring levels: spatial effect, object effect, and gravitation effect (Kliegl et al., 2011). Orientation and size factors are also included as numeric contrasts in such a way that the fixed effects estimate the difference between factor levels. With this specification the LMM intercept estimates the grand mean of the 16 (= 2 x 4 x 2 ) experimental conditions. The data are available as `KKL` in the `RePsychLing` package. The dataframe already contains contrasts as numeric covariates. Dependent variables is the log of reaction time for corret trials completed within a 750-ms deadline. The total number of responses was 53765.
30 |
31 | ### Maximal linear mixed model (_maxLMM_)
32 | We start with the maximal linear mixed model (_maxLMM_) including all possible variance components and correlation parameters associated with the four within-subject contrasts in the random-effects structure of the LMM. Note that there are no interactions between the three contrasts associated with the four levels of the cue-target relation factor. Also, as factor size was manipulated between subjects, this contrast does not appear in the random-effect structure. Thus, the random-effect structure comprises eight variance components (i.e., the intercept estimating the grand mean of log reaction time, the three contrasts for the four types of cue-target relation, the contrast for the orientation factor, and three interactions) and 28 correlation parameters (8*7/2)--truly a very complex model.
33 |
34 | ```{r m0}
35 | mm <- model.matrix(~ sze*(spt+obj+grv)*orn, data=KKL)
36 | KKL$sze_spt <- mm[, 7]
37 | KKL$sze_obj <- mm[, 8]
38 | KKL$sze_grv <- mm[, 9]
39 | KKL$sze_orn <- mm[, 10]
40 | KKL$spt_orn <- mm[, 11]
41 | KKL$obj_orn <- mm[, 12]
42 | KKL$grv_orn <- mm[, 13]
43 | KKL$sze_spt_orn <- mm[, 14]
44 | KKL$sze_obj_orn <- mm[, 15]
45 | KKL$sze_grv_orn <- mm[, 16]
46 |
47 | m0 <- lmer(lrt ~ sze*(spt+obj+grv)*orn +
48 | (spt + obj + grv + orn + spt_orn + obj_orn + grv_orn | subj),
49 | data=KKL, REML=FALSE, control=lmerControl(optCtrl=list(maxfun=10000L)))
50 | #print(summary(m0), corr=FALSE)
51 | ```
52 |
53 | The _maxLMM_ converges with a warning (i.e., "maxfun < 10 * length(par)^2 is not recommended") that we may be asking too much of these data. Nevertheless, at a first glance, model parameters look agreeable. None of the variance components are very close to zero and none of the correlation parameters are at the boundary (i.e., assume values of +1 or -1).
54 |
55 | ### Evaluation of singular value decomposition (svd) for _maxLMM_
56 | We subject the model to a svd analysis and list the percentages of variance associated with the eight components.
57 |
58 | ```{r sv0}
59 | chf0 <- getME(m0, "Tlist")[[1]]
60 | zapsmall(chf0, digits=4)
61 |
62 | sv0 <- svd(chf0)
63 |
64 | round(sv0$d^2/sum(sv0$d^2)*100, 1)
65 | ```
66 |
67 | The svd analysis indicates that the _maxLMM_ is overparameterized. The Cholesky factor decomposition yields two columns with zero values. Thus, there is clear evidence for singularity. This can also be seen in the percentages accounted for by the eight principal components: Only six of them are larger than zero.
68 |
69 | ### Zero-correlation parameter linear mixed model (_zcpLMM_)
70 |
71 | Inspection of model results suggest that most correlation parameters are quite close to zero. Does the goodness of fit decrease significantly if we assume that the ensemble of correlation parameters is zero?
72 |
73 | ```{r m1}
74 | print(summary(m1 <- lmer(lrt ~ sze*(spt+obj+grv)*orn +
75 | (spt + obj + grv + orn + spt_orn + obj_orn + grv_orn || subj),
76 | data=KKL, REML=FALSE)), corr=FALSE)
77 |
78 | sv1 <- svd(diag(getME(m1, "theta")) )
79 | sv1$d
80 |
81 | round(sv1$d^2/sum(sv1$d^2)*100, 1)
82 |
83 | anova(m1, m0)
84 | ```
85 |
86 | The _zcpLMM_ fits significantly worse than the full model, judged by a likelihood-ratio test (LRT) for the two models, but the chi^2 is barely more than twice the number of degrees of freedom for this test. Nevertheless, at least some of the correlation parameters are likely to be significant. The Cholesky factor decomposition shows one of the eight components very close to zero. Thus, the _zcpLMM_ still is too complex for the information contained in the data of this experiment.
87 |
88 | ### Drop LRTs of variance components
89 | The estimates of variance components suggest that there is no reliable variance associated with the interaction between object and orientation contrasts (i.e., 6.437e-18). The next smallest variance component is the contrast for the interaction between gravitation and orientation (i.e., 4.823e-05). We drop these two variance components from the model and refit.
90 |
91 | ```{r m2}
92 | print(summary(m2 <- lmer(lrt ~ sze*(spt+obj+grv)*orn +
93 | (spt + obj + grv + orn + spt_orn || subj),
94 | data=KKL, REML=FALSE)), corr=FALSE)
95 |
96 | sv2 <- svd(diag(getME(m2, "theta")) )
97 | sv2$d
98 |
99 | round(sv2$d^2/sum(sv2$d^2)*100, 3)
100 |
101 | anova(m2, m1) # not significant: prefer m2 to m1
102 | anova(m2, m0) # significant: m2 is "reduced" too much
103 | ```
104 |
105 | The LRT shows that there is no loss in goodness of fit associated with this reduced model. (Footnote: Taking out one variance component at a time leads to the same result.) Removal of the third smallest variance component (i.e., the object contrast) would lead to a significant loss in goodness of fit. Importantly, the svd analysis indicates no singularity anymore. Thus, removal of the two smallest variance components leads to an identifiable LMM. The data of this experiment support six variance components, in agreement with the initial svd analysis of the _maxLMM_.
106 |
107 | ### Extending the reduced LMM with correlation parameters
108 | In the next step, we include the correlation parameters for the remaining six variance components.
109 |
110 | ```{r m3}
111 | print(summary(m3 <- lmer(lrt ~ sze*(spt+obj+grv)*orn +
112 | (spt + obj + grv + orn + spt_orn | subj),
113 | data=KKL, REML=FALSE)), corr=FALSE)
114 |
115 | chf3 <- getME(m3, "Tlist")[[1]]
116 | zapsmall(chf3, digits=4)
117 |
118 | sv3 <- svd(chf3)
119 |
120 | round(sv3$d^2/sum(sv3$d^2)*100, 1)
121 |
122 | anova(m2, m3) # significant: prefer m3 to m2
123 | anova(m3, m0) # not significant: prefer m3 to m0
124 | ```
125 |
126 | This model is also supported by the data: There is no evidence of singularity. Moreover, the model fits significantly better than the _zcpLMM_ and does not fit significantly worse than the overparameterized initial _maxLMM_. Thus, this is a model we would consider as acceptable.
127 |
128 |
129 | ### Pruning low correlation parameters
130 |
131 | The significant increase in goodness of fit when going from LMM `m2` to LMM `m3`, suggests that there is significant information associated with the ensemble of correlation parameters. Nevertheless, the object and orientation effects and the interaction between spatial and orientation effects are only weakly correlated with the mean as well as with spatial and gravitation effects (see LMM `m3`). So we remove these correlation parameters from the model.
132 |
133 | ```{r m4}
134 | print(summary(m4 <- lmer(lrt ~ sze*(spt+obj+grv)*orn +
135 | (spt + grv | subj) + (0 + obj | subj) + (0 + orn | subj) + (0 + spt_orn | subj),
136 | data=KKL, REML=FALSE)), corr=FALSE)
137 |
138 | getME(m4, "Tlist") # Variance components look ok
139 | chf4 <- diag(c(diag(getME(m4, "Tlist")[[1]]), getME(m4, "Tlist")[[2]], getME(m4, "Tlist")[[3]], getME(m4, "Tlist")[[4]]))
140 | chf4[1:3, 1:3] <- getME(m4, "Tlist")[[1]]
141 |
142 | sv4 <- svd(chf4)
143 | sv4$d # singular value decomposition: ok
144 |
145 | round(sv4$d^2/sum(sv4$d^2)*100, 1) # percentages of variance accounted: ok
146 |
147 | anova(m4, m3) # not significant: prefer m4 to m3
148 | anova(m4, m0) # not significant: prefer m4 to m0
149 | ```
150 |
151 | Clearly, there is no loss of goodness of fit associated with dropping most of the correlation parameters. The svd analysis reveals no problems. Interestingly, the goodness of fit for LMM `m4` is not significantly different from _maxLMM_ `m0`, despite 27 fewer model parameters. The model looks very acceptable for these data.
152 |
153 | ### Profiling the model parameters
154 |
155 | The new version of `lme4` yields confidence intervals for all models parameters with the `profile` method. This method is unlikely to work for degenerate models. However, even if the model is correctly parameterized, profiling can take a long time, especially for complex models. Therefore, to ascertain the significance of variance components and correlation parameters, we profile only parameters of the final LMM.
156 |
157 | ```{r prof4, eval=FALSE}
158 | # Profiled 9 parameters individually; saved all results in as list in "p_m4.rda"
159 | p_m4_1 = profile(m4_1, which=1) # increment 1 to 9
160 | confint(p_m4_1)
161 | ```
162 |
163 | Profiling of LMM parameters takes a long time. Fortunately, they can be profiled independently and in parallel. Table x lists the model parameters along with the profiling-based 95% confidence interval. All model parameters are significant, but the confidence intervals for the correlation parameters are quite wide despite the large number of 86 subjects, large at least when compared to usual pyschological experiments.
164 |
165 | *Table x. Estimates and confidence intervals for variance components and correlation parameters*
166 | ```
167 | Name | Estimate | 2.5% | 975%
168 | ---------------- |-------------- | -------- | ---------
169 | mean-SD | .16 | .14 | .19
170 | spatial-SD | .07 | .06 | .08
171 | gravitation-SD | .04 | .03 | .05
172 | object-SD | .02 | .01 | .03
173 | orientation-SD | .09 | .08 | .11
174 | spat:orient-SD | .03 | .02 | .05
175 | mean-spatial r | .49 | .30 | .64
176 | mean-gravit r | -.51 | -.73 | -.25
177 | spat-gravit r | -.58 | -.88 | -.30
178 | ```
179 |
180 | ### Estimation of model parameters with Stan
181 | Shravan's playground.
182 |
183 | ### Summary
184 |
185 | The data from this experiment are a follow-up to an experiment reported by Kliegl et al. (2011). The statistical inferences in that article, especially also with respect to correlation parameters, were based on the _maxLMM_. As far as the random-effect structure is concerned the important results were the following.
186 |
187 | First, there were reliable individual differences in the gravitation effect, although the fixed effect for this contrast was only 2 ms and therefore far from significant. This result serves as an important demonstration that a null result for a fixed effect does not rule reliable individual differences in this effect (i.e., a subject x contrast interaction). This pattern of results, unexpected in the initial study, was replicated with the present experiment, but this time the gravitation effect interacted significantly with target size and orientation, two experimental manipulations added to the design. For a substantive interpretation we refer to Kliegl et al. (2014).
188 |
189 | Second, there were reliable correlations between mean response time, spatial effect, and gravitation effect, both when computed on the basis of within-subject effects and when estimated as correlation parameters in the LMM. With one exception, the replication experiment exhibits the same profile of correlation and correlation parameters as the first one: The correlation parameter for spatial and gravitation effects was much stronger in the first than in the second experiment (i.e., -.93 vs. -.58). This value was also substantially larger than the -.50 correlation computed from within-subject effects.
190 |
191 | Kliegl et al. (2011) interpreted larger magnitudes of correlation parameters than corresponding within-subject effect correlations as a consequence of correction of unreliability of difference scores with LMM-based shrinkage (see also Kliegl, Masson, & Richter, 2010). This is correct in principle, but the primary reason for the large difference between the within-subject based effect correlation and the LMM correlation parameter was that the _maxLMM_ reported in Kliegl et al. (2011) was overparameterized, although correlation parameters were not estimated at the boundary. A reanalysis with an evaluation of svd as described above revealed a singularity in the random-effect structure not immediately apparent in the model results and not linked to only one of the variance components. The reanalysis of the Kliegl et al. (2011) data is part of the `RePsychLing` package accompanying the present article.
192 |
193 | In general, whenever the estimate of a correlation parameter approaches boundary values of +/-1, one should check whether the complexity of the model is supported by the data. Unfortunately, when a matrix of correlation parameters is larger than 2 x 2, computational constraints among the correlation parameters may lead to estimates away from the boundary, even if the model is degenerate. Figure X attempts to convey a geometric intuition about these constraints. **Doug's Figure?**
194 |
195 | One of the most promising advantages of LMMs is there potential to assess experimental effects and individual differences in experimental effects within a coherent analyses system (Kliegl et al., 2011). Significant correlation parameters are signature results in this context. If model complexity is not adequately assessed, spurious results masking as substantive ones may go unnoticed. For an exploration of the boundary conditions under which correlation parameters lead to degenerate models, readers may profit from the shrinkage app at:
196 | `https://alexanderdietz.shinyapps.io/shiny_shrinkage/Shiny_Shrinkage.Rmd`
197 | (Makowski, Dietz, & Kliegl, 2014). Using this app it can be demonstrated easily that a critical parameter for model degeneration is the within-subject, within-condition standard deviation. The larger this standard deviation, the higher is the risk of model degeneration. Also increasing the number of observations per subject per condition is more likely to protect against overparameterization than increasing the number of subjects.
198 |
199 | ## Versions of packages used
200 | ```{r versions}
201 | sessionInfo()
202 | ```
203 |
204 |
--------------------------------------------------------------------------------
/inst/doc/KWDYZ.R:
--------------------------------------------------------------------------------
1 | ## ----preliminaries,echo=FALSE,include=FALSE,cache=FALSE------------------
2 | library(lme4)
3 | library(knitr)
4 | library(RePsychLing)
5 | opts_chunk$set(comment=NA)
6 | options(width=92,show.signif.stars=FALSE)
7 |
8 | ## ----strKWDYZ------------------------------------------------------------
9 | str(KWDYZ)
10 |
11 | ## ----m0------------------------------------------------------------------
12 | summary(m0 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3|subj), KWDYZ, REML=FALSE))
13 | summary(rePCA(m0))
14 |
15 | ## ----chf0----------------------------------------------------------------
16 | chf0 <- getME(m0,"Tlist")[[1]]
17 | zapsmall(chf0)
18 |
19 | ## ----svd0----------------------------------------------------------------
20 | sv0 <- svd(chf0)
21 | sv0$d
22 |
23 | ## ----linalg--------------------------------------------------------------
24 | (xx<-tcrossprod(chf0))
25 | sum(diag(xx))
26 | diag(xx)
27 | str(sv0)
28 | sv0$v
29 | zapsmall(sv0$v)
30 | sv0$u # last column is the singular combination of random effects
31 |
32 | ## ----m1------------------------------------------------------------------
33 | m1 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3||subj), KWDYZ, REML=FALSE)
34 | VarCorr(m1)
35 | summary(rePCA(m1))
36 | anova(m1, m0) # significant: too much of a reduction
37 |
38 | ## ----ci_p1, eval=FALSE---------------------------------------------------
39 | ## (m1.ci_profile <- confint(m1, method="profile"))
40 |
41 | ## ----ci_b1, eval=FALSE---------------------------------------------------
42 | ## (m1.ci_boot <- confint(m1, method="boot"))
43 |
44 | ## ----m2.2----------------------------------------------------------------
45 | m2d <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2|subj), KWDYZ, REML=FALSE)
46 | VarCorr(m2d)
47 | summary(rePCA(m2d))
48 |
49 | ## ----m2.4----------------------------------------------------------------
50 | print(summary(m2i <- lmer(lrt ~ 1 + c1 + c2 + c3 + (1 + c1 + c2 + c3 | subj),
51 | REML=FALSE, data=KWDYZ)), corr=FALSE)
52 | summary(rePCA(m2i))
53 | print(summary(m2j <- lmer(prt ~ 1 + c1 + c2 + c3 + (1 + c1 + c2 + c3 | subj),
54 | REML=FALSE, data=KWDYZ)), corr=FALSE)
55 | summary(rePCA(m2j))
56 |
57 | ## ----versions------------------------------------------------------------
58 | sessionInfo()
59 |
60 |
--------------------------------------------------------------------------------
/inst/doc/KWDYZ.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "RePsychLing Kliegl et al. (2011)"
3 | author: "Reinhold Kliegl"
4 | date: "2015-03-11"
5 | output: html_document
6 | bibliography: RePsychLing.bib
7 | ---
8 |
9 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
10 | library(lme4)
11 | library(knitr)
12 | library(RePsychLing)
13 | opts_chunk$set(comment=NA)
14 | options(width=92,show.signif.stars=FALSE)
15 | ```
16 |
20 |
21 | This is a set of follow-up analyses of the data described in @Kliegl:Wei:Dambacher:Yan:Zhou:2011
22 |
23 | We are using the final set of data used in that paper, that is after filtering a few outlier responses, defining `sdif` contrasts for factor `tar` and corresponding vector-valued contrasts `spt`, `c2`, `c3` from the model matrix. The dataframe also includes transformations of the response time, `rt` (`lrt=log(rt)`, `srt=sqrt(rt)`, `rrt=1000/rt` (note change in effect direction), `prt=rt^0.4242424` (acc to boxcox); subj = factor(id)).
24 |
25 | ```{r strKWDYZ}
26 | str(KWDYZ)
27 | ```
28 |
29 | ## Models
30 |
31 | ### Maximal linear mixed model (_maxLMM_)
32 |
33 | The maximal model (_maxLMM_) reported in this paper is actually an overparameterized/degenerate model. Here we show how to identify the overparameterization and how we tried to deal with it.
34 |
35 |
36 | ```{r m0}
37 | summary(m0 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3|subj), KWDYZ, REML=FALSE))
38 | summary(rePCA(m0))
39 | ```
40 |
41 | The principal components analysis (PCA) of the estimated unconditional variance-covariance matrix indicates one dimension in the space of vector-valued random effects has no variability.
42 |
43 | That is, the model is degenerate.
44 |
45 | ### Evaluation of singular value decomposition (svd) for _maxLMM_
46 |
47 | The parameters are in the Cholesky factors of two relative covariance matrices, each of size 4 by 4. There are 10 parameters in each of these matices. To examine the structure of the relative covariance matrices for the random effects we generate a 4 by 4 lower triangular matrix from the first 10 elements. This matrix is the (lower) Cholesky factor of the relative covariance matrix for the random effects by `subj`.
48 |
49 | The singular values of the relative covariance factor are
50 |
51 | ```{r chf0}
52 | chf0 <- getME(m0,"Tlist")[[1]]
53 | zapsmall(chf0)
54 | ```
55 |
56 | To examine the rank of the relative covariance matrix we evaluate the singular value decomposition of `chf0`
57 |
58 | ```{r svd0}
59 | sv0 <- svd(chf0)
60 | sv0$d
61 | ```
62 |
63 | We see that that the last value is (close to) zero. These are the relative standard deviations in 4 orthogonal directions in the space of the random effects for `subj`. The directions are the principal components for this covariance matrix. In one direction there is zero variability. Finally, we get the percentage of variance associated with each component:
64 |
65 | Here is a bit more linear algebra on how these values are computed:
66 |
67 | ```{r linalg}
68 | (xx<-tcrossprod(chf0))
69 | sum(diag(xx))
70 | diag(xx)
71 | str(sv0)
72 | sv0$v
73 | zapsmall(sv0$v)
74 | sv0$u # last column is the singular combination of random effects
75 | ```
76 |
77 | In principle, the significance of model parameters can be determined with profiling or bootstrapping the model paramters to obtain confidence intervals [e.g., `confint(m0, method="profile")`] does not work for _maxLMM_. Bootstrapping the parameters [e.g., `confint(m0, method="boot")`] takes very long and yields strange values. Most likely, these are also consequences of the singularity of _maxLMM_.
78 |
79 | ### Zero-correlation parameter linear mixed model (zcppLMM)
80 |
81 | One option to reduce the complexity of the _maxLMM_ is to force correlation parameters to zero. This can be accomplished with the new double-bar syntax.
82 |
83 |
84 | ```{r m1}
85 | m1 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3||subj), KWDYZ, REML=FALSE)
86 | VarCorr(m1)
87 | summary(rePCA(m1))
88 | anova(m1, m0) # significant: too much of a reduction
89 | ```
90 |
91 | The PCA analysis reveals no exact singularity for the _zcpLMM_. This model, however, fits significantly worse than _maxLMM_. Thus, removing all correlation parameters was too much of a reduction in model complexity. Before checking invidual correlation parameters for inclusion, we check whether any of the variance components are not supported b the data.
92 |
93 | The following command takes time, but the results look fine:
94 |
95 | ```{r ci_p1, eval=FALSE}
96 | (m1.ci_profile <- confint(m1, method="profile"))
97 | ```
98 |
99 | Result:
100 | ```
101 | Computing profile confidence intervals ...
102 | 2.5 % 97.5 %
103 | .sig01 46.208468 66.139137
104 | .sig02 19.646992 29.614386
105 | .sig03 5.597126 15.166344
106 | .sig04 3.982159 13.692235
107 | .sigma 69.269014 70.415812
108 | (Intercept) 375.731344 403.724376
109 | c1 27.070830 40.478887
110 | c2 9.484679 18.518766
111 | c3 -1.485184 7.059293
112 | ```
113 |
114 |
115 | The following command takes time, but the results look fine:
116 |
117 | ```{r ci_b1, eval=FALSE}
118 | (m1.ci_boot <- confint(m1, method="boot"))
119 | ```
120 |
121 | Result:
122 | ```
123 | Computing bootstrap confidence intervals ...
124 | 2.5 % 97.5 %
125 | sd_(Intercept)|subj 46.1696994 65.177956
126 | sd_c1|subj 18.9972281 29.324149
127 | sd_c2|subj 4.4081808 14.810636
128 | sd_c3|subj 0.8622058 12.899966
129 | sigma 69.2213099 70.471296
130 | (Intercept) 375.0806542 404.386494
131 | c1 27.1196532 40.298967
132 | c2 9.1330003 18.326448
133 | c3 -1.8171621 7.315928
134 | ```
135 |
136 |
137 | ### Drop LRTs for vc's of maximal model
138 |
139 | ```{r m2.2}
140 | m2d <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2|subj), KWDYZ, REML=FALSE)
141 | VarCorr(m2d)
142 | summary(rePCA(m2d))
143 | ```
144 |
145 | Conclusion: Having both `subj.c1` and `subj.c3` as well as correlation parameters in the model generates singular covariance matrix.
146 |
147 | ### Using lrt=log(rt) or prt= rt^power (acc Box-Cox)
148 |
149 | ```{r m2.4}
150 | print(summary(m2i <- lmer(lrt ~ 1 + c1 + c2 + c3 + (1 + c1 + c2 + c3 | subj),
151 | REML=FALSE, data=KWDYZ)), corr=FALSE)
152 | summary(rePCA(m2i))
153 | print(summary(m2j <- lmer(prt ~ 1 + c1 + c2 + c3 + (1 + c1 + c2 + c3 | subj),
154 | REML=FALSE, data=KWDYZ)), corr=FALSE)
155 | summary(rePCA(m2j))
156 | ```
157 |
158 | Transformed dependent variables also yield degenerate models, indicated by the
159 | cumulative proportion of variance reaching 1.0 at the 3rd principal component.
160 |
161 | ## Package Versions
162 | ```{r versions}
163 | sessionInfo()
164 | ```
165 |
166 | ## References
--------------------------------------------------------------------------------
/inst/doc/PCA.R:
--------------------------------------------------------------------------------
1 | ## ----preliminaries,include=FALSE,cache=FALSE-----------------------------
2 | library(lme4)
3 | library(RePsychLing)
4 | library(knitr)
5 | opts_chunk$set(cache=FALSE,comment=NA)
6 | options(show.signif.stars=FALSE,width=92,digits = 5)
7 |
8 | ## ----m0------------------------------------------------------------------
9 | summary(m0 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3|subj), KWDYZ, REML=FALSE))
10 |
11 | ## ----theta---------------------------------------------------------------
12 | zapsmall(getME(m0,"theta"))
13 |
14 | ## ----chf-----------------------------------------------------------------
15 | zapsmall(chf <- getME(m0,"Tlist")[[1]])
16 |
17 | ## ----relvcov-------------------------------------------------------------
18 | tcrossprod(chf)
19 |
20 | ## ----vcov----------------------------------------------------------------
21 | tcrossprod(getME(m0,"sigma")*chf)
22 |
23 | ## ----rowlengths----------------------------------------------------------
24 | (rowlengths <- sqrt(rowSums(chf*chf)))
25 |
26 | ## ----corr----------------------------------------------------------------
27 | tcrossprod(chf/rowlengths)
28 |
29 | ## ----chfsvd--------------------------------------------------------------
30 | (svd0 <- svd(chf,nv=0))
31 |
32 | ## ----orthogonal----------------------------------------------------------
33 | zapsmall(crossprod(svd0$u))
34 |
35 | ## ----stddevs-------------------------------------------------------------
36 | zapsmall(getME(m0,"sigma")*svd0$d)
37 |
38 | ## ----propvar-------------------------------------------------------------
39 | vc <- svd0$d^2 # variances of principal components
40 | zapsmall(vc/sum(vc))
41 |
42 | ## ----cumsum--------------------------------------------------------------
43 | cumsum(vc/sum(vc))
44 |
45 | ## ----rePCAm0-------------------------------------------------------------
46 | prc <- rePCA(m0)
47 | class(prc)
48 | length(prc)
49 | names(prc)
50 | class(prc$subj)
51 | prc$subj
52 | summary(prc$subj)
53 |
54 | ## ----m1------------------------------------------------------------------
55 | VarCorr(m1 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3||subj), KWDYZ, REML=FALSE))
56 |
57 | ## ----m1Tlist-------------------------------------------------------------
58 | getME(m1,"Tlist")
59 |
60 | ## ----bdiag---------------------------------------------------------------
61 | bdiag(getME(m1,"Tlist"))
62 |
63 | ## ----svdbdiag------------------------------------------------------------
64 | svd(bdiag(getME(m1,"Tlist")),nv=0)
65 |
66 | ## ----m1rePCA-------------------------------------------------------------
67 | summary(rePCA(m1))
68 |
69 | ## ----m2------------------------------------------------------------------
70 | VarCorr(m2 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2|subj) + (0+c3|subj), KWDYZ, REML=FALSE))
71 |
72 | ## ----m2chf---------------------------------------------------------------
73 | (chf <- bdiag(getME(m2,"Tlist")))
74 | svd(chf,nu=0,nv=0)
75 | summary(rePCA(m2))
76 |
77 | ## ----m3------------------------------------------------------------------
78 | m3 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1+P|item), kb07, REML=FALSE)
79 | print(summary(m3),corr=FALSE)
80 |
81 | ## ----m3rePCA-------------------------------------------------------------
82 | summary(rePCA(m3))
83 |
84 | ## ----versions------------------------------------------------------------
85 | sessionInfo()
86 |
87 |
88 |
--------------------------------------------------------------------------------
/inst/doc/PCA.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Principal Components Analysis of LMM models"
3 | author: "Douglas Bates"
4 | date: "2015-03-06"
5 | output:
6 | html_document: default
7 | pdf_document:
8 | highlight: tango
9 | keep_tex: yes
10 | word_document: default
11 | geometry: margin=1in
12 | fontsize: 12pt
13 | bibliography: RePsychLing.bib
14 | ---
15 |
19 |
20 | ```{r preliminaries,include=FALSE,cache=FALSE}
21 | library(lme4)
22 | library(RePsychLing)
23 | library(knitr)
24 | opts_chunk$set(cache=FALSE,comment=NA)
25 | options(show.signif.stars=FALSE,width=92,digits = 5)
26 | ```
27 |
28 | In a linear mixed model (LMM) incorporating vector-valued random
29 | effects, say by-subject random effects for intercept and for slope,
30 | the _variance component_ parameters determine a variance-covariance
31 | matrix for these random effects.
32 |
33 | As described in @Bates:Maechler:Bolker:Walker:2015
34 | the parameters used in fitting the model are the entries in the
35 | Cholesky factor, $\Lambda$, of the relative variance-covariance
36 | matrix of the unconditional distribution of the random effects.
37 |
38 | Consider a _maximal model_ fit to the `KWDYZ` data from @Kliegl:Wei:Dambacher:Yan:Zhou:2011, available in this package,
39 | ```{r m0}
40 | summary(m0 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3|subj), KWDYZ, REML=FALSE))
41 | ```
42 | The parameter vector, $\theta$, for this model
43 | ```{r theta}
44 | zapsmall(getME(m0,"theta"))
45 | ```
46 | has a value that is effectively zero in the last position.
47 |
48 | (The `zapsmall` function, used here and in what follows, sets the format for printing a numeric vector or matrix so that very small elements do not cause a shift to scientific notation. In scientific notation it is more difficult to see at a glance which numbers are large and which are small.)
49 |
50 | These 10 parameter values are the values on and below the diagonal of a lower triangular Cholesky factor
51 | ```{r chf}
52 | zapsmall(chf <- getME(m0,"Tlist")[[1]])
53 | ```
54 | The $\theta$ vector elements fill the lower triangular matrix in _column major order_. That is, the first four elements of the vector are the first column, the next three are the elements on and below the diagonal in the second column, and so on.
55 |
56 | The _relative covariance_ matrix for the random effects is $\Lambda\Lambda'$, which can be evaluated as
57 | ```{r relvcov}
58 | tcrossprod(chf)
59 | ```
60 | (The expression `crossprod(X)` forms $X'X$ and `tcrossprod(X)` forms $XX'$.)
61 |
62 | To reproduce the covariance matrix `tcrossprod(chf)` must be scaled by $s^2$
63 | ```{r vcov}
64 | tcrossprod(getME(m0,"sigma")*chf)
65 | ```
66 | (Compare the diagonal entries of this result with the _variance components_ of the random effects listed in the model summary.)
67 |
68 | The (unconditional) correlation matrix of the random effects is obtained by scaling each row of $\Lambda$ to have unit length, then applying `tcrossprod`.
69 | ```{r rowlengths}
70 | (rowlengths <- sqrt(rowSums(chf*chf)))
71 | ```
72 | ```{r corr}
73 | tcrossprod(chf/rowlengths)
74 | ```
75 | (Compare the off-diagonal elements of this matrix with the correlations in the model summary.)
76 |
77 | ## Singularity in the Cholesky factor
78 |
79 | Some of the material in this section gets a bit technical. Don't be too concerned if parts seem unintelligible.
80 |
81 | Because the last column of `chf` is the zero vector, `chf` is rank-deficient. That is, although `chf` is a 4x4 matrix, the linear subspace formed by all possible linear combinations of the columns is 3-dimensional. The random-effects vectors that can be generated from this fitted model must lie in this 3-dimensional subspace. Thus there will be no variability in one direction of the space of random effects.
82 |
83 | The _singular value decomposition_ of `chf`
84 | ```{r chfsvd}
85 | (svd0 <- svd(chf,nv=0))
86 | ```
87 | returns the singular values, `d`, and the matrix of _left singular vectors_, `u`. In the language of principal components analysis (PCA), the columns of `u` are the component loadings. These columns have unit length and are mutually orthogonal.
88 | ```{r orthogonal}
89 | zapsmall(crossprod(svd0$u))
90 | ```
91 |
92 | The singular values, `svd0$d`, are on the standard deviation scale. To convert them to standard deviations on the scale of the response, multiply by the estimate of $\sigma$.
93 | ```{r stddevs}
94 | zapsmall(getME(m0,"sigma")*svd0$d)
95 | ```
96 |
97 | A more meaningful scaling, as used in PCA, is to consider the proportion of the overall variance accounted for by each component.
98 | ```{r propvar}
99 | vc <- svd0$d^2 # variances of principal components
100 | zapsmall(vc/sum(vc))
101 | ```
102 |
103 | The principal components are ordered so that the first component accounts for the largest proportion of the variance, the second component accounts for the second largest proportion, and so on. This ordering is enforced by the singular values which are defined to be non-negative values in decreasing order.
104 |
105 | The cumulative proportion of the variance shows how what proportion is in the subspace spanned by the first principal component, the first two components, the first three components and so on, indicating how many components we should retain.
106 | ```{r cumsum}
107 | cumsum(vc/sum(vc))
108 | ```
109 |
110 | We see that 100% of the variance is accounted for by the first three principal components, which is another way of saying that the covariance matrix is of rank 3. Furthermore, 96.7% of the variance in the random effects is in the first two prinipal components, indicating that it should be possible to reduce the model from three to two dimensions without too much of a drop in the quality of the fit.
111 |
112 | ## Using the `rePCA` function
113 |
114 | The `rePCA` (**r**andom-**e**ffects **P**rincipal **C**omponents **A**nalysis) function takes a object of class `lmerMod` (i.e. a model fit by `lmer`) and performs the steps decribed above to produce a list of `prcomp` objects.
115 | ```{r rePCAm0}
116 | prc <- rePCA(m0)
117 | class(prc)
118 | length(prc)
119 | names(prc)
120 | class(prc$subj)
121 | prc$subj
122 | summary(prc$subj)
123 | ```
124 |
125 | The `prcomplist` class has its own `summary` method, which simply applies `summary` to each element of the list.
126 |
127 | ## Multiple random-effects terms for the same grouping factor
128 |
129 | When several random-effects terms have the same grouping, the Cholesky factors from the terms are accumulated in a _block-diagonal_ matrix. For example, in the _zero correlation parameter_ model
130 | ```{r m1}
131 | VarCorr(m1 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3||subj), KWDYZ, REML=FALSE))
132 | ```
133 | the `"Tlist"` consists of four 1x1 matrices
134 | ```{r m1Tlist}
135 | getME(m1,"Tlist")
136 | ```
137 | all named "subj". The _block diagonal_ matrix, which in this case is simply a diagonal matrix, is created by
138 | ```{r bdiag}
139 | bdiag(getME(m1,"Tlist"))
140 | ```
141 |
142 | The singular value decomposition of a diagonal matrix is trivial; `d` is the diagonal of the matrix and `u` is the identity.
143 | ```{r svdbdiag}
144 | svd(bdiag(getME(m1,"Tlist")),nv=0)
145 | ```
146 | ```{r m1rePCA}
147 | summary(rePCA(m1))
148 | ```
149 |
150 | The _block diagonal_ nature of the Cholesky factor is better illustrated with the results of the model
151 | ```{r m2}
152 | VarCorr(m2 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2|subj) + (0+c3|subj), KWDYZ, REML=FALSE))
153 | ```
154 | ```{r m2chf}
155 | (chf <- bdiag(getME(m2,"Tlist")))
156 | svd(chf,nu=0,nv=0)
157 | summary(rePCA(m2))
158 | ```
159 |
160 | ## Two or more grouping factors
161 |
162 | When a model incorporates random effects with repect to two or more grouping factors, such as this model fit to the `kb07` data from @Kronmuller:Barr:2007 and also discussed in @Barr:Levy:Scheepers:Tily:13
163 | ```{r m3}
164 | m3 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1+P|item), kb07, REML=FALSE)
165 | print(summary(m3),corr=FALSE)
166 | ```
167 | the `rePCA` function produces a list of `prcomp` objects, one for each grouping factor.
168 | ```{r m3rePCA}
169 | summary(rePCA(m3))
170 | ```
171 |
172 | ## Summary
173 |
174 | Principal components analysis (PCA) of the estimated covariance matrices for the random effects in a linear mixed model allows for simple assessment of the dimensionality of the random effects distribution. As shown in other vignettes in this `RePsychLing` package, the _maximal_ model in many analyses of data from Psychology and Linguistics experiments, is almost always shown by this analysis to be degenerate.
175 |
176 |
177 | ## Package versions
178 | ```{r versions}
179 | sessionInfo()
180 |
181 | ```
182 |
183 | ## References
--------------------------------------------------------------------------------
/inst/doc/gamm.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmbates/RePsychLing/392adf11283a7608b1fda28876682bec106133ad/inst/doc/gamm.pdf
--------------------------------------------------------------------------------
/inst/doc/index.html:
--------------------------------------------------------------------------------
1 |
2 | R: Vignettes and other documentation
3 |
4 |
5 |
6 |
57 |
58 |
--------------------------------------------------------------------------------
/inst/supplementCave.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dmbates/RePsychLing/392adf11283a7608b1fda28876682bec106133ad/inst/supplementCave.pdf
--------------------------------------------------------------------------------
/man/KKL.Rd:
--------------------------------------------------------------------------------
1 | \name{KKL}
2 | \alias{KKL}
3 | \docType{data}
4 | \title{Kliegl, K and L}
5 | %\description{}
6 | \format{
7 | The format is:
8 | chr "KKL"
9 | }
10 | %\details{}
11 | %\source{}
12 | %\references{}
13 | \examples{
14 | str(KKL)
15 | }
16 | \keyword{datasets}
17 |
--------------------------------------------------------------------------------
/man/KWDYZ.Rd:
--------------------------------------------------------------------------------
1 | \name{KWDYZ}
2 | \alias{KWDYZ}
3 | \docType{data}
4 | \title{KWDYZ data}
5 | %\description{}
6 | \format{
7 | The format is:
8 | chr "KWDYZ"
9 | }
10 | %\details{}
11 | %\source{}
12 | %\references{}
13 | \examples{
14 | str(KWDYZ)
15 | }
16 | \keyword{datasets}
17 |
--------------------------------------------------------------------------------
/man/bs10.Rd:
--------------------------------------------------------------------------------
1 | \name{bs10}
2 | \alias{bs10}
3 | \docType{data}
4 | \title{Barr and ... 2010}
5 | \description{Data from a paper by Barr and ... 2010}
6 | \usage{data("bs10")}
7 | \format{
8 | The format is:
9 | chr "bs10"
10 | }
11 | %\details{}
12 | %\source{}
13 | %\references{}
14 | \examples{
15 | str(bs10)
16 | }
17 | \keyword{datasets}
18 |
--------------------------------------------------------------------------------
/man/createParamMx.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2 (4.1.0): do not edit by hand
2 | % Please edit documentation in R/createParamMx.R
3 | \name{createParamMx}
4 | \alias{createParamMx}
5 | \title{Create Population Parameter Matrix}
6 | \usage{
7 | createParamMx(nexp = 100000L, simparam.env = getParamRanges(),
8 | firstseed = NULL, h0 = TRUE, outfile = NULL)
9 | }
10 | \arguments{
11 | \item{nexp}{number of parameter-value sets to generate (default 100000)}
12 |
13 | \item{simparam.env}{an environment containing ranges of population parameters}
14 |
15 | \item{firstseed}{initial seed}
16 |
17 | \item{h0}{logical value indicating if h0 is true or false}
18 |
19 | \item{outfile}{name of save file for parameter-value matrix}
20 | }
21 | \value{
22 | a matrix of generated population parameter values
23 | }
24 | \description{
25 | Create a matrix of parameter values for simulations
26 | }
27 | \details{
28 | Generates a matrix of parameters. Each row are the population parameters
29 | used to generate data from a single hypothetical "experiment."
30 | }
31 |
32 |
--------------------------------------------------------------------------------
/man/gb12.Rd:
--------------------------------------------------------------------------------
1 | \name{gb}
2 | \alias{gb}
3 | \docType{data}
4 | \title{gb data}
5 | \description{Data from a paper whose author's initials are G and B}
6 | \format{
7 | The format is:
8 | chr "gb"
9 | }
10 | %\details{}
11 | %\source{}
12 | %\references{}
13 | \examples{
14 | str(gb12)
15 | }
16 | \keyword{datasets}
17 |
--------------------------------------------------------------------------------
/man/getParamRanges.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2 (4.1.0): do not edit by hand
2 | % Please edit documentation in R/getParamRanges.R
3 | \name{getParamRanges}
4 | \alias{getParamRanges}
5 | \title{Get the parameter ranges used in the Monte Carlo simulations.}
6 | \usage{
7 | getParamRanges()
8 | }
9 | \value{
10 | An environment containing variables and default ranges shown in the function sources.
11 | }
12 | \description{
13 | Generate default parameter ranges for the simulations.
14 | }
15 | \details{
16 | This generates default parameter ranges. The environment returned
17 | can subsequently be modified by the user.
18 | }
19 |
20 |
--------------------------------------------------------------------------------
/man/kb07.Rd:
--------------------------------------------------------------------------------
1 | \name{kb07}
2 | \alias{kb07}
3 | \docType{data}
4 | \title{Kronmueller and Barr, 2007 data}
5 | \description{
6 | Data described in Kronmueller and Barr, 2007 and referenced
7 | in Barr et al., 2013
8 | }
9 | \format{
10 | The format is:
11 | chr "kb07"
12 | }
13 | \source{
14 | Barr, Dale J., Levy, Roger, Scheepers, Christoph and Tily, Harry J
15 | (2013) Random effects structure for confirmatory hypothesis testing:
16 | Keep it maximal, \emph{Journal of Memory and Language}, \bold{68},
17 | pp. 255-278
18 | }
19 | \references{
20 | Kronmueller, Edmundo and Barr, Dale J (2007)
21 | Perspective-free pragmatics: Broken precedents and the
22 | recovery-from-preemption hypothesis, \emph{Journal of Memory and
23 | Language}, \bold{56}, pp. 436-455
24 | }
25 | \examples{
26 | str(kb07)
27 | }
28 | \keyword{datasets}
29 |
--------------------------------------------------------------------------------
/man/kbbb.Rd:
--------------------------------------------------------------------------------
1 | \name{kbbb}
2 | \alias{kbbb}
3 | \docType{data}
4 | \title{kbbb data}
5 | %\description{}
6 | \format{
7 | The format is:
8 | chr "kbbb"
9 | }
10 | %\details{}
11 | %\source{}
12 | %\references{}
13 | \examples{
14 | str(kbbb)
15 | }
16 | \keyword{datasets}
17 |
--------------------------------------------------------------------------------
/man/lfbg.Rd:
--------------------------------------------------------------------------------
1 | \name{lfbg}
2 | \alias{lfbg}
3 | \docType{data}
4 | \title{lfbg data}
5 | %\description{}
6 | \format{
7 | The format is:
8 | chr "lfbg"
9 | }
10 | %\details{}
11 | %\source{}
12 | %\references{}
13 | \examples{
14 | str(lfbg)
15 | }
16 | \keyword{datasets}
17 |
--------------------------------------------------------------------------------
/man/mkCovR.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2 (4.1.0): do not edit by hand
2 | % Please edit documentation in R/mkDf.R
3 | \name{mkCovR}
4 | \alias{mkCovR}
5 | \title{Upper Cholesky factor of covariance}
6 | \usage{
7 | mkCovR(v1, v2, r)
8 | }
9 | \arguments{
10 | \item{v1}{positive numeric, variance of first coordinate}
11 |
12 | \item{v2}{positive numeric, variance of second coordinate}
13 |
14 | \item{r}{numeric in the range [-1,1], correlation}
15 | }
16 | \value{
17 | The upper triangular Cholesky factor of the covariance matrix
18 | }
19 | \description{
20 | Upper Cholesky factor of a 2 by 2 covariance matrix
21 | }
22 |
23 |
--------------------------------------------------------------------------------
/man/mkDf.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2 (4.1.0): do not edit by hand
2 | % Please edit documentation in R/mkDf.R
3 | \name{mkDf}
4 | \alias{mkDf}
5 | \title{Return a dataframe with simulated data given a set of population parameters}
6 | \usage{
7 | mkDf(nsubj = 24L, nitem = 24L, wsbi = FALSE,
8 | mcr.params = createParamMx(1L, firstseed = sample.int(1000000L, 1L))[1L, ],
9 | missMeth = c("random", "none", "randomBig", "bycond", "bysubj",
10 | "bysubjcond"), rigen = FALSE)
11 | }
12 | \arguments{
13 | \item{nsubj}{number of subjects (default is 24)}
14 |
15 | \item{nitem}{number of items (default is 24)}
16 |
17 | \item{wsbi}{logical, is design between items (TRUE) or within items (default FALSE).}
18 |
19 | \item{mcr.params}{vector of parameters for generation of data (see createParamMx)}
20 |
21 | \item{missMeth}{method of generating missing data (default "random")}
22 |
23 | \item{rigen}{logical, use a random-intercepts-only generative model (default FALSE)}
24 | }
25 | \value{
26 | a data frame of simulated data
27 | }
28 | \description{
29 | Create simulated data given a row of parameters and the no. of subjects and items
30 | }
31 |
32 |
--------------------------------------------------------------------------------
/man/poems.Rd:
--------------------------------------------------------------------------------
1 | \name{poems}
2 | \alias{poems}
3 | \docType{data}
4 | \title{Baayen and Milin }
5 | \description{
6 | Data described in Baayen and Milin (2010).
7 | }
8 | \format{
9 | A data frame with 275996 observations on the following 24 variables.
10 | \describe{
11 | \item{\code{ReadingTime}}{a numeric vector of self-paced reading times}
12 | \item{\code{Subject}}{a factor with participant identifiers}
13 | \item{\code{Sex}}{a factor with levels \code{m} (male) and \code{f} (female)}
14 | \item{\code{Age}}{a numeric vector specifying the participant's age}
15 | \item{\code{NPoems}}{a numeric vector of the self-reported maximum number of poems read annually, according to a four-choice question}
16 | \item{\code{MultipleChoiceRT}}{a numeric vector with the response latency to the four-choice question}
17 | \item{\code{Trial}}{a numeric vector specifying the rank of the item in the subject's experimental list}
18 | \item{\code{NumberOfWordsIntoLine}}{a numeric vector specifying the position of the
19 | item in the line of poetry being read}
20 | \item{\code{PositionBegMidEnd}}{a factor specifying whether the word was initial
21 | \code{beg}, medial \code{mid} or final \code{end} in the sentence}
22 | \item{\code{SentenceLength}}{a numeric vector specifying sentence length}
23 | \item{\code{Poem}}{a factor with as levels identifiers for the poems}
24 | \item{\code{Word}}{a factor with as levels identifiers for the words}
25 | \item{\code{WordFrequencyInPoem}}{a numeric vector specifying the frequency of the word in the poem}
26 | \item{\code{RhymeFreqInPoem}}{a numeric vector specifying the frequency of the word's rhyme in the poem}
27 | \item{\code{OnsetFreqInPoem}}{a numeric vector specifying the frequency of the word's onset in the poem}
28 | \item{\code{WordLength}}{a numeric vector specifying the length of the word in letters}
29 | \item{\code{FamilySize}}{a numeric vector specifying the count of morphological family members}
30 | \item{\code{InflectionalEntropy}}{a numeric vector specifying Shannon's entropy calculated over the probability distribution of a word's inflected variants}
31 | \item{\code{LemmaFrequency}}{a numeric vector specifying the frequency of occurrence of the word in the lemma subsection of the CELEX lexical database}
32 | \item{\code{WordFormFrequency}}{a numeric vector specifying the frequency of occurrence of the word's inflected form in the word form subsection of the CELEX lexical database}
33 | \item{\code{NumberOfMeanings}}{a numeric vector specifying the number of synsets in WordNet in which the word is listed}
34 | \item{\code{IsFunctionWord}}{a factor specifying whether the word is a function word \code{TRUE} or not \code{FALSE}}
35 | \item{\code{HasPunctuationMark}}{a factor specifying whether the word is followed by a punctuation mark, levels \cite{FALSE} (absent) and \cite{TRUE} (present)}
36 | \item{\code{NumberOfMorphemes}}{a numeric vector specifying the scaled number of morphemes in a word}
37 | }
38 | }
39 | \source{
40 | Baayen, R. H. and Milin, P (2010) Analyzing reaction times.
41 | \emph{International Journal of Psychological Research},
42 | \bold{3.2}, pp. 12-28.
43 | }
44 | \references{
45 | Baayen, R. H. and Milin, P (2010) Analyzing reaction times.
46 | \emph{International Journal of Psychological Research},
47 | \bold{3.2}, pp. 12-28.
48 | }
49 | \examples{
50 | data(poems)
51 | par(mfrow=c(2,4))
52 | qqnorm(poems$ReadingTime)
53 | qqnorm(poems$WordFormFrequency)
54 | qqnorm(poems$LemmaFrequency)
55 | qqnorm(poems$FamilySize)
56 | qqnorm(poems$MultipleChoiceRT)
57 | qqnorm(poems$NPoems)
58 | qqnorm(poems$NumberOfMeanings)
59 | poems$LogReadingTime = log(poems$ReadingTime)
60 | poems$LogWordFormFrequency = log(poems$WordFormFrequency+1)
61 | poems$LogLemmaFrequency = log(poems$LemmaFrequency+1)
62 | poems$RecFamilySize = -100/(poems$FamilySize+1)
63 | poems$LogMultipleChoiceRT = log(poems$MultipleChoiceRT)
64 | poems$LogNPoems = log(poems$NPoems)
65 | poems$LogNumberOfMeanings = log(poems$NumberOfMeanings+1)
66 |
67 | \dontrun{
68 |
69 | p = poems[,c("Age", "LogNPoems", "LogMultipleChoiceRT", "NumberOfWordsIntoLine", "SentenceLength",
70 | "WordFrequencyInPoem", "RhymeFreqInPoem", "OnsetFreqInPoem", "WordLength",
71 | "NumberOfMorphemes",
72 | "RecFamilySize", "InflectionalEntropy", "LogLemmaFrequency", "LogWordFormFrequency",
73 | "LogNumberOfMeanings")]
74 | pc = prcomp(p,center=TRUE, scale=TRUE)
75 | round(pc$rotation[,1:7],2)
76 | # PC1 PC2 PC3 PC4 PC5 PC6 PC7
77 | #Age 0.00 0.01 0.00 0.03 0.61 0.49 -0.01
78 | #LogNPoems 0.00 -0.01 0.01 -0.01 -0.70 -0.02 0.00
79 | #LogMultipleChoiceRT 0.00 0.00 0.00 0.01 -0.37 0.87 -0.02
80 | #NumberOfWordsIntoLine 0.03 -0.19 -0.39 -0.56 0.01 0.02 -0.05
81 | #SentenceLength -0.09 -0.20 -0.40 -0.52 0.01 0.01 -0.11
82 | #WordFrequencyInPoem -0.30 -0.36 0.14 0.11 0.00 -0.01 -0.06
83 | #RhymeFreqInPoem -0.24 -0.54 0.15 0.07 0.01 0.00 0.11
84 | #OnsetFreqInPoem -0.20 -0.56 0.14 0.06 0.01 0.00 0.13
85 | #WordLength 0.41 -0.16 0.18 -0.08 0.00 0.00 0.15
86 | #NumberOfMorphemes 0.17 -0.13 0.24 -0.03 0.01 -0.01 -0.83
87 | #RecFamilySize -0.35 0.20 -0.02 -0.11 0.00 0.01 0.34
88 | #InflectionalEntropy 0.30 -0.19 -0.42 0.36 -0.01 -0.01 -0.02
89 | #LogLemmaFrequency -0.43 0.13 -0.21 0.18 -0.01 -0.01 -0.27
90 | #LogWordFormFrequency -0.45 0.16 -0.12 0.10 0.00 -0.01 -0.25
91 | #LogNumberOfMeanings 0.11 -0.15 -0.55 0.44 -0.01 -0.01 0.01
92 |
93 |
94 | poems$PC1 = pc$x[,1]
95 | poems$PC2 = pc$x[,2]
96 | poems$PC3 = pc$x[,3]
97 | poems$PC4 = pc$x[,4]
98 | poems$PC5 = pc$x[,5]
99 | poems$PC6 = pc$x[,6]
100 | poems$PC7 = pc$x[,7]
101 |
102 | library(lme4)
103 | poems.lmer = lmer(LogReadingTime ~
104 | PC1 + PC2 + PC3 + PC4 + PC5 + PC6 + PC7 +
105 | HasPunctuationMark*Sex + Trial + PositionBegMidEnd +
106 | (1|Poem) + (1|Word) + (1|Subject),
107 | #(1+LogWordFormFrequency+NumberOfMorphemes|Subject) ,
108 | data=poems, REML=FALSE)
109 | print(summary(poems.lmer), corr=FALSE)
110 |
111 | chf <- diag(c(diag(
112 | getME(poems.lmer, "Tlist")[[2]]),
113 | getME(poems.lmer, "Tlist")[[1]],
114 | getME(poems.lmer, "Tlist")[[3]]))
115 | chf[1:3, 1:3] <- getME(poems.lmer, "Tlist")[[2]]
116 |
117 | sv <- svd(chf)
118 | round(sv$d^2/sum(sv$d^2)*100, 1)
119 | }
120 | }
121 | \keyword{datasets}
122 |
--------------------------------------------------------------------------------
/man/rePCA.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2 (4.1.0): do not edit by hand
2 | % Please edit documentation in R/rePCA.R
3 | \name{rePCA}
4 | \alias{rePCA}
5 | \title{PCA of random-effects}
6 | \usage{
7 | rePCA(x)
8 | }
9 | \arguments{
10 | \item{x}{a merMod object}
11 | }
12 | \value{
13 | a \code{prcomplist} object
14 | }
15 | \description{
16 | PCA of random-effects variance-covariance estimates
17 | }
18 | \details{
19 | Perform a Principal Components Analysis (PCA) of the random-effects
20 | variance-covariance estimates from a fitted mixed-effects model
21 | }
22 | \author{
23 | Douglas Bates
24 | }
25 |
26 |
--------------------------------------------------------------------------------
/man/uighur.Rd:
--------------------------------------------------------------------------------
1 | \name{uighur}
2 | \alias{uighur1}
3 | \alias{uighur2}
4 | \docType{data}
5 | \title{Uighur reading data}
6 | %\description{}
7 | \format{
8 | The format is:
9 | chr "uighur1"
10 | }
11 | %\details{}
12 | %\source{}
13 | %\references{}
14 | \examples{
15 | str(uighur1)
16 | str(uighur2)
17 | }
18 | \keyword{datasets}
19 |
--------------------------------------------------------------------------------
/man/vietnamese.Rd:
--------------------------------------------------------------------------------
1 | \name{vietnamese}
2 | \alias{vietnamese}
3 | \docType{data}
4 | \title{vietnamese visual lexical decision from Pham and Baayen (2015)}
5 | \description{
6 | Data described in Pham and Baayen (2015)
7 | }
8 | \format{
9 | A data frame with 15021 observations on the following 6 variables.
10 | \describe{
11 | \item{\code{HeadWord}}{a factor specifying the word stimuli}
12 | \item{\code{RTinv}}{a numeric vector with transformed reaction times (-1000/RT)}
13 | \item{\code{MidLevelTone}}{a factor specifying whether the first syllabeme carries mid level tone (TRUE/FALSE)}
14 | \item{\code{LogFreq}}{the frequency of the compound}
15 | \item{\code{LogFreqSyl1}}{the frequency of the left syllabeme}
16 | \item{\code{LogFreySyl2}}{the frequency of the right syllabeme}
17 | }
18 | }
19 | \source{
20 | Pham, H. and Baayen, R. H. (2015) Vietnamese compounds show an anti-frequency effect in visual lexical decision.
21 | \emph{Language, Cognition, and Neuroscience},
22 | \bold{30.9}, pp. 1077-1095.
23 | }
24 | \references{
25 | Pham, H. and Baayen, R. H. (2015) Vietnamese compounds show an anti-frequency effect in visual lexical decision.
26 | \emph{Language, Cognition, and Neuroscience},
27 | \bold{30.9}, pp. 1077-1095.
28 | }
29 | \examples{
30 | data(vietnamese)
31 | library(mgcv)
32 | vietnamese.gam = bam(RTinv ~ MidLevelTone + s(LogFreq) + te(LogFreqSyl1, LogFreqSyl2), data=vietnamese, method="ML")
33 | summary(vietnamese.gam)
34 | plot(vietnamese.gam, pages=1)
35 | }
36 | \keyword{datasets}
37 |
--------------------------------------------------------------------------------
/vignettes/BS.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "RePsychLing Barr and Seyfeddinipur (2010)"
3 | author: "Reinhold Kliegl and Douglas Bates"
4 | date: "11 March, 2015"
5 | output:
6 | html_document: default
7 | pdf_document:
8 | highlight: tango
9 | keep_tex: yes
10 | word_document: default
11 | geometry: margin=1in
12 | fontsize: 12pt
13 | bibliography: RePsychLing.bib
14 | ---
15 |
19 |
20 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
21 | library(lme4)
22 | library(RePsychLing)
23 | library(knitr)
24 | opts_chunk$set(comment=NA)
25 | options(width=92,show.signif.stars = FALSE)
26 | ```
27 |
28 | ## Data from @barrseyfedd2010
29 |
30 | Some of the data from @barrseyfedd2010 are available as the data frame `bs10` in the `RePsychLing` package.
31 |
32 | ```{r bs10str}
33 | str(bs10)
34 | ```
35 |
36 | As with other data frames in this package, the subject and item factors are called `subj` and `item`. The response being modelled, `dif`, is the difference in two response times.
37 |
38 | The two experimental factors `S` and `F`, both at two levels, are represented in the -1/+1 encoding, as is their interaction, `SF`. The `S` factor is the speaker condition with levels -1 for the same speaker in both trials and +1 for different speakers. The `F` factor is the filler condition with levels -1 for `NS` and +1 for `FP`.
39 |
40 | ### Maximal linear mixed model (_maxLMM_)
41 |
42 | The maximal model has a full factorial design `1+S+F+SF` for the fixed-effects and for potentially correlated vector-valued random effects for `subj` and for `item`. We use the parameter estimates from an `lmm` fit using the [MixedModels package](https://github.com/dmbates/MixedModels.jl) package for [Julia](http://julialang.org), which can be much faster than fitting this model with `lmer`. (In addition to being faster, the fit from `lmm` produced a significantly lower deviance.)
43 |
44 | ```{r m0,warning=FALSE}
45 | m0 <- lmer(dif ~ 1+S+F+SF + (1+S+F+SF|subj) + (1+S+F+SF|item), bs10, REML=FALSE, start=thcvg$bs10$m0,
46 | control=lmerControl(optimizer="Nelder_Mead",optCtrl=list(maxfun=1L),
47 | check.conv.grad="ignore",check.conv.hess="ignore"))
48 | summary(m0)
49 | ```
50 |
51 | The model converges with warnings. Six of 12 correlation parameters are estimated at the +/-1 boundary.
52 |
53 | Notice that there are only 12 items. Expecting to estimate 4 variances and 6 covariances from 12 items is optimistic.
54 |
55 | ### PCA analysis of the _maxLMM_
56 |
57 | A summary of a principal components analysis (PCA) of the random-effects variance-covariance
58 | matrices shows
59 | ```{r sv_m0_s}
60 | summary(rePCA(m0))
61 | ```
62 |
63 | For both the by-subject and the by-item random effects the estimated variance-covariance matrices are singular. There are at least 2 dimensions with no variation in the subject-related random-effects and 3 directions with no variation for the item-related random-effects.
64 |
65 | Clearly the model is over-specified.
66 |
67 | ### Zero-correlation-parameter linear mixed model (_zcpLMM_)
68 |
69 | A zero-correlation-parameter model fits independent random effects for the intercept, the experimental factors and their interaction for each of the `subj` and `item` grouping factors.
70 | It can be conveniently specified using the `||` operator in the random-effects terms.
71 | ```{r m1}
72 | print(summary(m1 <- lmer(dif ~ 1+S+F+SF + (1+S+F+SF||subj) + (1+S+F+SF||item), bs10, REML=FALSE)), corr=FALSE)
73 | anova(m1, m0)
74 | ```
75 |
76 | The _zcpLMM_ fits significantly worse than the _maxLMM_. However, the results strongly suggest that quite a few of the variance components are not supported by the data.
77 |
78 | ### PCA for the _zcpLMM_
79 |
80 |
81 | ```{r pca1}
82 | summary(rePCA(m1))
83 | ```
84 |
85 | The random effect for filler, `F`, by subject has essentially zero variance and the random effect for the interaction, `SF`, accounts for less than 15% of the total variation in the random effects. There is no evidence for item-related random effects in `m1`.
86 |
87 | ### Iterative reduction of model complexity
88 |
89 | Remove all variance components estimated with a value of zero.
90 |
91 | ```{r m2}
92 | print(summary(m2 <- lmer(dif ~ 1+S+F+SF + (1+S+SF||subj), bs10, REML=FALSE)), corr=FALSE)
93 | ```
94 |
95 | Naturally, the fit for this model is equivalent to that for `m1` because it is only the variance components with zero estimates that are eliminated.
96 | ```{r m21anova}
97 | anova(m2,m1)
98 | ```
99 |
100 | Next we check whether the variance component for the interaction, `SF`, could reasonably be zero.
101 |
102 | ```{r m3}
103 | print(summary(m3 <- lmer(dif ~ 1+S+F+SF + (1+S||subj), bs10, REML=FALSE)), corr=FALSE)
104 | anova(m3, m2)
105 | ```
106 |
107 | Not quite significant, but could be considered. The fit is still worse than for the _maxLMM_ `m0`. We now reintroduce a correlation parameters in the vector-valued random effects for `subj`.
108 |
109 | ### Extending the reduced LMM with correlation parameters
110 |
111 | ```{r m4}
112 | print(summary(m4 <- lmer(dif ~ 1+S+F+SF + (1+S|subj), bs10, REML=FALSE)), corr=FALSE)
113 | anova(m3, m4, m0)
114 | ```
115 |
116 | Looks like we have evidence for a significant correlation parameter. Moreover, LMM `m4` fits as well as _maxLMM_.
117 |
118 | ### Summary
119 |
120 | LMM `m4` is the optimal model. It might be worth while to check the theoretical contribution of the correlation parameter.
121 |
122 | ## Versions of packages used
123 | ```{r versions}
124 | sessionInfo()
125 | ```
126 |
127 | # References
128 |
129 |
--------------------------------------------------------------------------------
/vignettes/GB.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "RePsychLing Gann and Barr (2014)"
3 | author: "Reinhold Kliegl and Douglas Bates"
4 | date: '2015-03-11'
5 | output:
6 | html_document: default
7 | pdf_document:
8 | highlight: tango
9 | keep_tex: yes
10 | word_document: default
11 | geometry: margin=1in
12 | fontsize: 12pt
13 | bibliography: RePsychLing.bib
14 | ---
15 |
19 |
20 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
21 | library(RePsychLing)
22 | library(lme4)
23 | library(knitr)
24 | opts_chunk$set(comment=NA)
25 | options(width=92,show.signif.stars = FALSE)
26 | ```
27 |
28 | ## Data from @Gann:Barr:2014
29 |
30 | These data, also used in the online supplement to @Barr:Levy:Scheepers:Tily:13, are available as `gb12` in the `RePsychLing` package.
31 |
32 | ```{r gbstr}
33 | str(gb12)
34 | summary(gb12)
35 | ```
36 |
37 | ### Maximal linear mixed model (_maxLMM_)
38 |
39 | We assume `P`, the partner, is a between-session factor and `F`, feedback, is a between-item factor (i.e., they are not included in RE terms). The model fit in the paper is:
40 |
41 | ```{r m0,warning=FALSE}
42 | m0 <- lmer(
43 | sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF+(1+T+F+TF|session)+(1+T+P+TP|item),
44 | gb12, REML=FALSE, start=thcvg$gb12$m0,
45 | control=lmerControl(optimizer="Nelder_Mead",optCtrl=list(maxfun=1L),
46 | check.conv.grad="ignore",check.conv.hess="ignore"))
47 | print(summary(m0),corr=FALSE)
48 | ```
49 |
50 | The model converges without problems, but two correlation parameters are estimated as 1.
51 |
52 | ### Principal components analysis for _maxLMM_
53 |
54 | ```{r m0PCA}
55 | summary(rePCA(m0))
56 | ```
57 |
58 | The PCA results indicate two dimensions with no variability in the random
59 | effects for session and another two dimensions in the random effects for item.
60 |
61 | ### Zero-correlation-parameter linear mixed model (_zcpLMM_)
62 |
63 | ```{r gbm02}
64 | m1 <-
65 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F+TF||session) + (1+T+P+TP||item),
66 | gb12, REML=FALSE)
67 | VarCorr(m1)
68 | anova(m1, m0)
69 | ```
70 |
71 | The _zcpLMM_ fits significantly worse than the _maxLMM_, but it reveals several variance components with values close to or of zero.
72 |
73 | ### Iterative reduction of model complexity
74 |
75 | Let's refit the model without small variance components.
76 |
77 | ```{r m2}
78 | m2 <-
79 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F||session) + (1+T||item),
80 | gb12, REML=FALSE)
81 | VarCorr(m2)
82 | anova(m2, m1, m0)
83 | ```
84 |
85 | Let's check the support of item-related variance components
86 | ```{r m3}
87 | m3 <-
88 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F||session) + (1|item),
89 | gb12, REML=FALSE)
90 | VarCorr(m3)
91 | anova(m3, m2)
92 | ```
93 |
94 | Marginally significant drop. (Deleting the intercept too leads to a significant drop in goodness of fit.)
95 |
96 | ### Extending the reduced LMM with correlation parameters
97 |
98 | Let's check correlation parameters for `item`
99 |
100 | ```{r m4,warning=FALSE}
101 | m4 <-
102 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+T+F|session) + (1|item),
103 | gb12, REML=FALSE)
104 | VarCorr(m4)
105 | anova(m3, m4)
106 | ```
107 |
108 | The correlation parameter is significant, but one correlation is 1.000, indicating a singular model. Let's remove the small correlation parameters.
109 |
110 | ### Pruning small correlation parameters
111 |
112 | ```{r m5}
113 | m5 <-
114 | lmer(sottrunc2 ~ 1+T+P+F+TP+TF+PF+TPF + (1+F|session) + (0+T|session) + (1|item),
115 | gb12, REML=FALSE)
116 | VarCorr(m5)
117 | anova(m5, m4)
118 | ```
119 |
120 | Now the model is clearly degenerate: The correlation is at the boundary (-1); `theta` returns a zero value for one of the variance components.
121 |
122 | ## Summary
123 |
124 |
125 | ## Versions of packages used
126 | ```{r versions}
127 | sessionInfo()
128 | ```
129 |
130 | ## References
131 |
--------------------------------------------------------------------------------
/vignettes/KB.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "RePsychLing Kronmüller and Barr (2007)"
3 | author: "Reinhold Kliegl"
4 | date: " `r Sys.Date()` "
5 | output: rmarkdown::html_vignette
6 | bibliography: RePsychLing.bib
7 | vignette: >
8 | %\VignetteEngine{knitr::knitr}
9 | %\VignetteIndexEntry{RePsychLing Kronmüller and Barr (2007)}
10 | \usepackage[utf8]{inputenc}
11 | ---
12 |
13 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
14 | library(lme4)
15 | library(RePsychLing)
16 | library(knitr)
17 | opts_chunk$set(comment=NA)
18 | options(width=92,show.signif.stars = FALSE)
19 | ```
20 |
21 | We apply the iterative reduction of LMM complexity to truncated response times of a 2x2x2 factorial psycholinguistic experiment (@Kronmuller:Barr:2007, Exp. 2; reanalyzed with an LMM in @Barr:Levy:Scheepers:Tily:13). The data are from 56 subjects who responded to 32 items. Specifically, subjects had to select one of several objects presented on a monitor with a cursor. The manipulations involved (1) auditory instructions that maintained or broke a precedent of reference for the objects established over prior trials, (2) with the instruction being presented by the speaker who established the precedent (i.e., an old speaker) or a new speaker, and (3) whether the task had to be performed without or with a cognitive load consisting of six random digits. All factors were varied within subjects and within items. There were main effects of Load, Speaker, and Precedent; none of the interactions were significant. Although standard errors of fixed-effect coefficents varied slightly across models, our reanalyses afforded the same statistical inference about the experimental manipulations as the original article, irrespective of LMM specification. The purpose of the analysis is to illustrate an assessment of model complexity as far as variance components and correlation parameters are concerned, neither of which were in the focus of the original publication.
22 |
23 | ## Data from @Kronmuller:Barr:2007
24 |
25 | The data are available as `kb07` in the `RePsychLing` package.
26 |
27 | ```{r kb07str}
28 | str(kb07)
29 | ```
30 |
31 | ### Maximal linear mixed model (_maxLMM_)
32 |
33 | Barr et al. (2012, supplement) analyzed Kronmüller and Barr (2007, Exp. 2) with the _maxLMM_ comprising 16 variance components (eight each for the random factors `subj` and `item`, respectively) (Footnote below output). This model takes a long time to fit using `lmer` because there are so many parameters and the likelihood surface is very flat. The `lmm` function from the [MixedModels](https://github.com/dmbates/MixedModels.jl) package for [Julia](http://julialang.org) is much faster fitting this particular model, providing the results
34 | ```{r m0}
35 | m0 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1+S+P+C+SP+SC+PC+SPC|subj) +
36 | (1+S+P+C+SP+SC+PC+SPC|item), kb07, REML=FALSE, start=thcvg$kb07$m0,
37 | control=lmerControl(optimizer="Nelder_Mead",optCtrl=list(maxfun=1L),
38 | check.conv.grad="ignore",check.conv.hess="ignore"))
39 | print(summary(m0),corr=FALSE)
40 | ```
41 | This fit converges and produces what look like reasonable parameter estimates (i.e., no variance components with estimates close to zero; no correlation parameters with values close to $\pm1$).
42 |
43 | We started this model fit at the converged parameter estimates to save time. Starting from the usual initial values, the model fit took nearly 40,000 iterations for the nonlinear optimizer to converge. Theparameter values look reasonable but are a local optimum. We use a better parameter value here.
44 |
45 | The slow convergence is due to a total of 2 x 36 = 72 parameters in the optimization. These parameters are all in the relative covariance factors. The more easily estimated nine fixed-effects parameters have been "profiled out" of the optimization.
46 |
47 | Footnote: The model formula reported in the supplement of Barr et al. (2012) specified only five variance components for the random factor item. However, `lmer()` automatically includes all lower-order terms of interactions specified for random-factor terms, resulting in the _maxLMM_ for this experimental design.
48 |
49 | ### Evaluation of singular value decomposition (svd) for _maxLMM_
50 |
51 | Considering that there are only 56 subjects and 32 items it is quite optimistic to expect to estimate 36 highly nonlinear covariance parameters for `subj` and another 36 for `item`.
52 | ```{r chf0}
53 | summary(rePCA(m0))
54 | ```
55 | The directions are the principal components for this covariance matrix. We see that there are seven singular values of zero, that is there is zero variability in seven directions. Overall, the svd analysis of this model returns only eight principal components with variances larger than one percent. Thus, the _maxLMM_ is clearly too complex.
56 |
57 | ### Zero-correlation-parameter linear mixed model (_zcpLMM_)
58 |
59 | As a first step of model reduction, we propose to start with a model including all 16 variance components, but no correlation parameters. Note that here we go through the motion to be consistent with the recommended strategy. The large number of components with zero or close to zero variance in _maxLMM_ already strongly suggests the need for a reduction of the number of variance components--as done in the next step. For this _zcpLMM_, we extract the vector-valued variables from the model matrix without the intercept column which is provided by the R formula. Then, we use the new double-bar syntax for `lmer()` to force correlation parameters to zero.
60 |
61 | ```{r m1}
62 | m1 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1+S+P+C+SP+SC+PC+SPC||subj) +
63 | (1+S+P+C+SP+SC+PC+SPC||item), kb07, REML=FALSE)
64 | print(summary(m1),corr=FALSE)
65 |
66 | anova(m1, m0)
67 | ```
68 |
69 | Nominally, the _zcpLMM_ fits significantly worse than the _maxLMM_, but note that the \chi^2 for the LRT (85) is smaller than twice the degrees of freedom for the LRT (56). Also the degrees of freedom are somewhat of an underestimate. According to our judgement, _zcpLMM_ could be preferred to _maxLMM_.
70 |
71 | ### Principal components analysis for _zcpLMM_
72 |
73 | ```{r rePCAm1}
74 | summary(rePCA(m1))
75 | ```
76 |
77 | The PCM analysis of _zcpLMM_ returns 12 of 16 components with variances different from zero. Thus, using this result as guide, the _zcpLMM_ is still too complex. Inspection of _zcpLMM_ variance components (see _zcpLMM_ `m1`) suggests a further reduction of model complexity with drop1-LRT tests, starting with the smallest variance components.
78 |
79 | ### Dropping non-significant variance components
80 |
81 | A second step of model reduction is to remove variance components that are not significant according to a likelihood ratio test (LRT). Starting with the smallest variance component (or a set of them) this step can be repeated until significant change in goodness of fit is indicated. For the present case, variance components for `SC` and `SPC` for `subj` and `S` and `SP` for `item` are estimated with zero values. We refit the LMM without these variance components.
82 |
83 | ```{r m2}
84 | m2 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1+S+P+C+SP+PC||subj) +
85 | (1+P+C+SC+PC+SPC||item), kb07, REML=FALSE)
86 | anova(m2, m1) # not significant: prefer m2 over m1
87 | ```
88 |
89 | Obviously, these four variance components are not supported by information in the data. So we drop the next four smallest variance components, vc1 and vc2 for `subj` and vc5 and vc7 for `item`.
90 |
91 | ```{r m3}
92 | m3 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1+C+SP+PC||subj) + (1+P+C+PC||item),kb07,REML=FALSE)
93 | anova(m3, m2) # not significant: prefer m3 over m2
94 | ```
95 |
96 | There is no significant drop in goodness of fit. Therefore, we continue with dropping vc3, vc4, and vc6 for `subj` and vc3 and vc6 for `item`.
97 |
98 | ```{r m4}
99 | m4 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1|item) + (0+P|item),kb07,REML=FALSE)
100 | anova(m4, m3) # not significant: prefer m4 over m3
101 | anova(m4, m1) # not significant: prefer m4 over m1 (no accumulation)
102 | ```
103 |
104 | As a final test, we refit the LMM without vc2 for `item`.
105 |
106 | ```{r m5}
107 | m5 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1|item), data=kb07, REML=FALSE)
108 | anova(m5, m4) # significant: prefer m4 over m5
109 | ```
110 |
111 | This time the LRT is significant. Therefore, we stay with LMM `m4` and test correlation parameters for this model.
112 |
113 | ### Extending the reduced LMM with a correlation parameter
114 |
115 | ```{r m6}
116 | m6 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1+P|item), kb07, REML=FALSE)
117 | print(summary(m6), corr=FALSE)
118 |
119 | anova(m4, m6) # significant: prefer m6 over m4
120 | anova(m6, m0) # not significant: prefer m6 over m0 (no accumulation)
121 | ```
122 |
123 | There is evidence for a reliable item-related negative correlation parameter between mean and precedence effect, that is there are reliable differences between items in the precedence effect. Finally, there is no significant difference between LMM `m6` and the _maxLMM_ `m0`. The final number of reliable dimensions is actually smaller than suggested by the PCA analysis of the _maxLMM_ `m0`.
124 |
125 | ### Profiling the parameters
126 |
127 | Confidence intervals for all parameters can be obtained
128 |
129 |
130 | ### Summary
131 | In our opinion, `m6` is the _optimal_ LMM for the data of this experiment. The general strategy of (1) starting with _maxLMM_, (2) followed by _zcpLMM_, (3) followed by iteratively dropping variance components until there is a significant decrease in goodness of model fit, (4) followed by inclusion of correlation parameters for the remaining variance components, and (5) using svd all along the way to check the principal dimensionality of the data for the respective intermediate models worked quite well again. Indeed, we also reanalyzed two additional experiments reported in the supplement of Barr et al. (2012). As documented in the `RePsychLing` package accompanying the present article, in each case, the _maxLMM_ was too complex for the information provided by the experimental data. In each case, the data supported only a very sparse random-effects structure beyond varying intercepts for subjects and items. Fortunately and interestingly, none of the analyses changed the statistical inference about fixed effects in these experiments. Obviously, this cannot be ruled out in general. If authors adhere to a strict criterion for significance, such as p < .05 suitably adjusted for multiple comparisons, there is always a chance that a t-value will fall above or below the criterion across different versions of an LMM.
132 |
133 | Given the degree of deconstruction (i.e., model simplification) reported for these models, one may wonder whether it might be more efficient to iteratively _increase_ rather the _decrease_ LMM complexity, that is to start with a minimal linear mixed model (_minLMM_), varying only intercepts of subject and item factors and adding variance components and correlation parameters to such a model. We will turn to this strategy in the next section.
134 |
135 | ## Versions of packages used
136 | ```{r versions}
137 | sessionInfo()
138 | ```
139 |
140 | ## References
--------------------------------------------------------------------------------
/vignettes/KWDYZ.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "RePsychLing Kliegl et al. (2011)"
3 | author: "Reinhold Kliegl"
4 | date: "2015-03-24"
5 | output: rmarkdown::html_vignette
6 | bibliography: RePsychLing.bib
7 | vignette: >
8 | %\VignetteEngine{knitr::knitr}
9 | %\VignetteIndexEntry{RePsychLing Kliegl et al. (2011)}
10 | \usepackage[utf8]{inputenc}
11 | ---
12 |
13 | ```{r preliminaries,echo=FALSE,include=FALSE,cache=FALSE}
14 | library(lme4)
15 | library(knitr)
16 | library(RePsychLing)
17 | opts_chunk$set(comment=NA)
18 | options(width=92,show.signif.stars=FALSE)
19 | ```
20 |
21 | This is a set of follow-up analyses of the data described in @Kliegl:Wei:Dambacher:Yan:Zhou:2011
22 |
23 | We are using the final set of data used in that paper, that is after filtering a few outlier responses, defining `sdif` contrasts for factor `tar` and corresponding vector-valued contrasts `spt`, `c2`, `c3` from the model matrix. The dataframe also includes transformations of the response time, `rt` (`lrt=log(rt)`, `srt=sqrt(rt)`, `rrt=1000/rt` (note change in effect direction), `prt=rt^0.4242424` (acc to boxcox); subj = factor(id)).
24 |
25 | ```{r strKWDYZ}
26 | str(KWDYZ)
27 | ```
28 |
29 | ## Models
30 |
31 | ### Maximal linear mixed model (_maxLMM_)
32 |
33 | The maximal model (_maxLMM_) reported in this paper is actually an overparameterized/degenerate model. Here we show how to identify the overparameterization and how we tried to deal with it.
34 |
35 |
36 | ```{r m0}
37 | summary(m0 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3|subj), KWDYZ, REML=FALSE))
38 | summary(rePCA(m0))
39 | ```
40 |
41 | The principal components analysis (PCA) of the estimated unconditional variance-covariance matrix indicates one dimension in the space of vector-valued random effects has no variability.
42 |
43 | That is, the model is degenerate.
44 |
45 | ### Evaluation of singular value decomposition (svd) for _maxLMM_
46 |
47 | The parameters are in the Cholesky factors of two relative covariance matrices, each of size 4 by 4. There are 10 parameters in each of these matices. To examine the structure of the relative covariance matrices for the random effects we generate a 4 by 4 lower triangular matrix from the first 10 elements. This matrix is the (lower) Cholesky factor of the relative covariance matrix for the random effects by `subj`.
48 |
49 | The singular values of the relative covariance factor are
50 |
51 | ```{r chf0}
52 | chf0 <- getME(m0,"Tlist")[[1]]
53 | zapsmall(chf0)
54 | ```
55 |
56 | To examine the rank of the relative covariance matrix we evaluate the singular value decomposition of `chf0`
57 |
58 | ```{r svd0}
59 | sv0 <- svd(chf0)
60 | sv0$d
61 | ```
62 |
63 | We see that that the last value is (close to) zero. These are the relative standard deviations in 4 orthogonal directions in the space of the random effects for `subj`. The directions are the principal components for this covariance matrix. In one direction there is zero variability. Finally, we get the percentage of variance associated with each component:
64 |
65 | Here is a bit more linear algebra on how these values are computed:
66 |
67 | ```{r linalg}
68 | (xx<-tcrossprod(chf0))
69 | sum(diag(xx))
70 | diag(xx)
71 | str(sv0)
72 | sv0$v
73 | zapsmall(sv0$v)
74 | sv0$u # last column is the singular combination of random effects
75 | ```
76 |
77 | In principle, the significance of model parameters can be determined with profiling or bootstrapping the model paramters to obtain confidence intervals [e.g., `confint(m0, method="profile")`] does not work for _maxLMM_. Bootstrapping the parameters [e.g., `confint(m0, method="boot")`] takes very long and yields strange values. Most likely, these are also consequences of the singularity of _maxLMM_.
78 |
79 | ### Zero-correlation parameter linear mixed model (zcppLMM)
80 |
81 | One option to reduce the complexity of the _maxLMM_ is to force correlation parameters to zero. This can be accomplished with the new double-bar syntax.
82 |
83 |
84 | ```{r m1}
85 | m1 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3||subj), KWDYZ, REML=FALSE)
86 | VarCorr(m1)
87 | summary(rePCA(m1))
88 | anova(m1, m0) # significant: too much of a reduction
89 | ```
90 |
91 | The PCA analysis reveals no exact singularity for the _zcpLMM_. This model, however, fits significantly worse than _maxLMM_. Thus, removing all correlation parameters was too much of a reduction in model complexity. Before checking invidual correlation parameters for inclusion, we check whether any of the variance components are not supported b the data.
92 |
93 | The following command takes time, but the results look fine:
94 |
95 | ```{r ci_p1, eval=FALSE}
96 | (m1.ci_profile <- confint(m1, method="profile"))
97 | ```
98 |
99 | Result:
100 | ```
101 | Computing profile confidence intervals ...
102 | 2.5 % 97.5 %
103 | .sig01 46.208468 66.139137
104 | .sig02 19.646992 29.614386
105 | .sig03 5.597126 15.166344
106 | .sig04 3.982159 13.692235
107 | .sigma 69.269014 70.415812
108 | (Intercept) 375.731344 403.724376
109 | c1 27.070830 40.478887
110 | c2 9.484679 18.518766
111 | c3 -1.485184 7.059293
112 | ```
113 |
114 |
115 | The following command takes time, but the results look fine:
116 |
117 | ```{r ci_b1, eval=FALSE}
118 | (m1.ci_boot <- confint(m1, method="boot"))
119 | ```
120 |
121 | Result:
122 | ```
123 | Computing bootstrap confidence intervals ...
124 | 2.5 % 97.5 %
125 | sd_(Intercept)|subj 46.1696994 65.177956
126 | sd_c1|subj 18.9972281 29.324149
127 | sd_c2|subj 4.4081808 14.810636
128 | sd_c3|subj 0.8622058 12.899966
129 | sigma 69.2213099 70.471296
130 | (Intercept) 375.0806542 404.386494
131 | c1 27.1196532 40.298967
132 | c2 9.1330003 18.326448
133 | c3 -1.8171621 7.315928
134 | ```
135 |
136 |
137 | ### Drop LRTs for vc's of maximal model
138 |
139 | ```{r m2.2}
140 | m2d <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2|subj), KWDYZ, REML=FALSE)
141 | VarCorr(m2d)
142 | summary(rePCA(m2d))
143 | ```
144 |
145 | Conclusion: Having both `subj.c1` and `subj.c3` as well as correlation parameters in the model generates singular covariance matrix.
146 |
147 | ### Using lrt=log(rt) or prt= rt^power (acc Box-Cox)
148 |
149 | ```{r m2.4}
150 | print(summary(m2i <- lmer(lrt ~ 1 + c1 + c2 + c3 + (1 + c1 + c2 + c3 | subj),
151 | REML=FALSE, data=KWDYZ)), corr=FALSE)
152 | summary(rePCA(m2i))
153 | print(summary(m2j <- lmer(prt ~ 1 + c1 + c2 + c3 + (1 + c1 + c2 + c3 | subj),
154 | REML=FALSE, data=KWDYZ)), corr=FALSE)
155 | summary(rePCA(m2j))
156 | ```
157 |
158 | Transformed dependent variables also yield degenerate models, indicated by the
159 | cumulative proportion of variance reaching 1.0 at the 3rd principal component.
160 |
161 | ## Package Versions
162 | ```{r versions}
163 | sessionInfo()
164 | ```
165 |
166 | ## References
--------------------------------------------------------------------------------
/vignettes/PCA.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Principal Components Analysis of LMM models"
3 | author: "Douglas Bates"
4 | date: "2015-03-06"
5 | output:
6 | html_document: default
7 | pdf_document:
8 | highlight: tango
9 | keep_tex: yes
10 | word_document: default
11 | geometry: margin=1in
12 | fontsize: 12pt
13 | bibliography: RePsychLing.bib
14 | ---
15 |
19 |
20 | ```{r preliminaries,include=FALSE,cache=FALSE}
21 | library(lme4)
22 | library(RePsychLing)
23 | library(knitr)
24 | opts_chunk$set(cache=FALSE,comment=NA)
25 | options(show.signif.stars=FALSE,width=92,digits = 5)
26 | ```
27 |
28 | In a linear mixed model (LMM) incorporating vector-valued random
29 | effects, say by-subject random effects for intercept and for slope,
30 | the _variance component_ parameters determine a variance-covariance
31 | matrix for these random effects.
32 |
33 | As described in @Bates:Maechler:Bolker:Walker:2015
34 | the parameters used in fitting the model are the entries in the
35 | Cholesky factor, $\Lambda$, of the relative variance-covariance
36 | matrix of the unconditional distribution of the random effects.
37 |
38 | Consider a _maximal model_ fit to the `KWDYZ` data from @Kliegl:Wei:Dambacher:Yan:Zhou:2011, available in this package,
39 | ```{r m0}
40 | summary(m0 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3|subj), KWDYZ, REML=FALSE))
41 | ```
42 | The parameter vector, $\theta$, for this model
43 | ```{r theta}
44 | zapsmall(getME(m0,"theta"))
45 | ```
46 | has a value that is effectively zero in the last position.
47 |
48 | (The `zapsmall` function, used here and in what follows, sets the format for printing a numeric vector or matrix so that very small elements do not cause a shift to scientific notation. In scientific notation it is more difficult to see at a glance which numbers are large and which are small.)
49 |
50 | These 10 parameter values are the values on and below the diagonal of a lower triangular Cholesky factor
51 | ```{r chf}
52 | zapsmall(chf <- getME(m0,"Tlist")[[1]])
53 | ```
54 | The $\theta$ vector elements fill the lower triangular matrix in _column major order_. That is, the first four elements of the vector are the first column, the next three are the elements on and below the diagonal in the second column, and so on.
55 |
56 | The _relative covariance_ matrix for the random effects is $\Lambda\Lambda'$, which can be evaluated as
57 | ```{r relvcov}
58 | tcrossprod(chf)
59 | ```
60 | (The expression `crossprod(X)` forms $X'X$ and `tcrossprod(X)` forms $XX'$.)
61 |
62 | To reproduce the covariance matrix `tcrossprod(chf)` must be scaled by $s^2$
63 | ```{r vcov}
64 | tcrossprod(getME(m0,"sigma")*chf)
65 | ```
66 | (Compare the diagonal entries of this result with the _variance components_ of the random effects listed in the model summary.)
67 |
68 | The (unconditional) correlation matrix of the random effects is obtained by scaling each row of $\Lambda$ to have unit length, then applying `tcrossprod`.
69 | ```{r rowlengths}
70 | (rowlengths <- sqrt(rowSums(chf*chf)))
71 | ```
72 | ```{r corr}
73 | tcrossprod(chf/rowlengths)
74 | ```
75 | (Compare the off-diagonal elements of this matrix with the correlations in the model summary.)
76 |
77 | ## Singularity in the Cholesky factor
78 |
79 | Some of the material in this section gets a bit technical. Don't be too concerned if parts seem unintelligible.
80 |
81 | Because the last column of `chf` is the zero vector, `chf` is rank-deficient. That is, although `chf` is a 4x4 matrix, the linear subspace formed by all possible linear combinations of the columns is 3-dimensional. The random-effects vectors that can be generated from this fitted model must lie in this 3-dimensional subspace. Thus there will be no variability in one direction of the space of random effects.
82 |
83 | The _singular value decomposition_ of `chf`
84 | ```{r chfsvd}
85 | (svd0 <- svd(chf,nv=0))
86 | ```
87 | returns the singular values, `d`, and the matrix of _left singular vectors_, `u`. In the language of principal components analysis (PCA), the columns of `u` are the component loadings. These columns have unit length and are mutually orthogonal.
88 | ```{r orthogonal}
89 | zapsmall(crossprod(svd0$u))
90 | ```
91 |
92 | The singular values, `svd0$d`, are on the standard deviation scale. To convert them to standard deviations on the scale of the response, multiply by the estimate of $\sigma$.
93 | ```{r stddevs}
94 | zapsmall(getME(m0,"sigma")*svd0$d)
95 | ```
96 |
97 | A more meaningful scaling, as used in PCA, is to consider the proportion of the overall variance accounted for by each component.
98 | ```{r propvar}
99 | vc <- svd0$d^2 # variances of principal components
100 | zapsmall(vc/sum(vc))
101 | ```
102 |
103 | The principal components are ordered so that the first component accounts for the largest proportion of the variance, the second component accounts for the second largest proportion, and so on. This ordering is enforced by the singular values which are defined to be non-negative values in decreasing order.
104 |
105 | The cumulative proportion of the variance shows how what proportion is in the subspace spanned by the first principal component, the first two components, the first three components and so on, indicating how many components we should retain.
106 | ```{r cumsum}
107 | cumsum(vc/sum(vc))
108 | ```
109 |
110 | We see that 100% of the variance is accounted for by the first three principal components, which is another way of saying that the covariance matrix is of rank 3. Furthermore, 96.7% of the variance in the random effects is in the first two prinipal components, indicating that it should be possible to reduce the model from three to two dimensions without too much of a drop in the quality of the fit.
111 |
112 | ## Using the `rePCA` function
113 |
114 | The `rePCA` (**r**andom-**e**ffects **P**rincipal **C**omponents **A**nalysis) function takes a object of class `lmerMod` (i.e. a model fit by `lmer`) and performs the steps decribed above to produce a list of `prcomp` objects.
115 | ```{r rePCAm0}
116 | prc <- rePCA(m0)
117 | class(prc)
118 | length(prc)
119 | names(prc)
120 | class(prc$subj)
121 | prc$subj
122 | summary(prc$subj)
123 | ```
124 |
125 | The `prcomplist` class has its own `summary` method, which simply applies `summary` to each element of the list.
126 |
127 | ## Multiple random-effects terms for the same grouping factor
128 |
129 | When several random-effects terms have the same grouping, the Cholesky factors from the terms are accumulated in a _block-diagonal_ matrix. For example, in the _zero correlation parameter_ model
130 | ```{r m1}
131 | VarCorr(m1 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2+c3||subj), KWDYZ, REML=FALSE))
132 | ```
133 | the `"Tlist"` consists of four 1x1 matrices
134 | ```{r m1Tlist}
135 | getME(m1,"Tlist")
136 | ```
137 | all named "subj". The _block diagonal_ matrix, which in this case is simply a diagonal matrix, is created by
138 | ```{r bdiag}
139 | bdiag(getME(m1,"Tlist"))
140 | ```
141 |
142 | The singular value decomposition of a diagonal matrix is trivial; `d` is the diagonal of the matrix and `u` is the identity.
143 | ```{r svdbdiag}
144 | svd(bdiag(getME(m1,"Tlist")),nv=0)
145 | ```
146 | ```{r m1rePCA}
147 | summary(rePCA(m1))
148 | ```
149 |
150 | The _block diagonal_ nature of the Cholesky factor is better illustrated with the results of the model
151 | ```{r m2}
152 | VarCorr(m2 <- lmer(rt ~ 1+c1+c2+c3 + (1+c1+c2|subj) + (0+c3|subj), KWDYZ, REML=FALSE))
153 | ```
154 | ```{r m2chf}
155 | (chf <- bdiag(getME(m2,"Tlist")))
156 | svd(chf,nu=0,nv=0)
157 | summary(rePCA(m2))
158 | ```
159 |
160 | ## Two or more grouping factors
161 |
162 | When a model incorporates random effects with repect to two or more grouping factors, such as this model fit to the `kb07` data from @Kronmuller:Barr:2007 and also discussed in @Barr:Levy:Scheepers:Tily:13
163 | ```{r m3}
164 | m3 <- lmer(RTtrunc ~ 1+S+P+C+SP+SC+PC+SPC + (1|subj) + (1+P|item), kb07, REML=FALSE)
165 | print(summary(m3),corr=FALSE)
166 | ```
167 | the `rePCA` function produces a list of `prcomp` objects, one for each grouping factor.
168 | ```{r m3rePCA}
169 | summary(rePCA(m3))
170 | ```
171 |
172 | ## Summary
173 |
174 | Principal components analysis (PCA) of the estimated covariance matrices for the random effects in a linear mixed model allows for simple assessment of the dimensionality of the random effects distribution. As shown in other vignettes in this `RePsychLing` package, the _maximal_ model in many analyses of data from Psychology and Linguistics experiments, is almost always shown by this analysis to be degenerate.
175 |
176 |
177 | ## Package versions
178 | ```{r versions}
179 | sessionInfo()
180 |
181 | ```
182 |
183 | ## References
--------------------------------------------------------------------------------
/vignettes/RePsychLing.bib:
--------------------------------------------------------------------------------
1 | @Article{barrseyfedd2010,
2 | author = {Dale J. Barr and Mandana Seyfeddinipur},
3 | title = {The role of fillers in listener attributions for speaker disfluency},
4 | journal = {Language and Cognitive Processes},
5 | year = 2010,
6 | volume = 25,
7 | number = 4,
8 | pages = {441-455}}
9 |
10 | @article{Bates:Maechler:Bolker:Walker:2015,
11 | title = {Fitting linear mixed-effects models using lme4},
12 | author = {Douglas Bates and Martin M\"{a}chler and Ben Bolker and Steven Walker},
13 | year = {2015},
14 | journal = {Journal of Statistical Software},
15 | pages = {in press},
16 | url = {http://arxiv.org/abs/1406.5823}
17 | }
18 |
19 | @article{Kronmuller:Barr:2007,
20 | title = {Perspective-free pragmatics: Broken precedents and the recovery-from-preemption hypothesis},
21 | author = {Kronm{\"u}ller, Edmundo and Barr, Dale J},
22 | journal = {Journal of Memory and Language},
23 | volume = {56},
24 | number = {3},
25 | pages = {436--455},
26 | year = {2007},
27 | publisher = {Elsevier}
28 | }
29 |
30 | @article{Barr:Levy:Scheepers:Tily:13,
31 | title = {Random effects structure for confirmatory hypothesis testing: Keep it maximal},
32 | author = {Barr, Dale J and Levy, Roger and Scheepers, Christoph and Tily, Harry J},
33 | journal = {Journal of Memory and Language},
34 | volume = {68},
35 | number = {3},
36 | pages = {255--278},
37 | year = {2013},
38 | publisher = {Elsevier}
39 | }
40 |
41 | @Article{Gann:Barr:2014,
42 | author = {Timothy M. Gann and Dale J. Barr},
43 | title = {Speaking from experience: audience design as expert performance},
44 | journal = {Language Cognition and Neuroscience},
45 | year = 2014,
46 | doi = {10.1080/01690965.2011.641388},
47 | volume = 29,
48 | number = 6,
49 | pages = {744-760}}
50 |
51 | @article{Kliegl:Wei:Dambacher:Yan:Zhou:2011,
52 | author = {Kliegl, R. and Wei, P. and Dambacher, M. and Yan, M. and Zhou, X.},
53 | year = {2011},
54 | title = {Experimental effects and individual differences in Linear Mixed Models: Estimating the relationship between spatial, object, and attraction effects in visual attention},
55 | journal = {Frontiers in Psychology},
56 | volume = {1},
57 | pages = {1--12}
58 | }
59 |
60 |
61 |
62 | @article{Kliegl:Kuschela:Laubrock:2014,
63 | author = {Kliegl, R. and Kuschela, J. and Laubrock, J.},
64 | year = {2015},
65 | title = {Object orientation and target size modulate the speed of visual attention},
66 | journal = {University of Potsdam}
67 | }
68 |
69 | @manual{stan-manual:2014,
70 | author = {{Stan Development Team}},
71 | year = {2014},
72 | title = {Stan Modeling Language Users Guide and Reference Manual,
73 | Version 2.5.0},
74 | url = {http://mc-stan.org/}
75 | }
76 |
77 | @unpublished{BatesEtAlParsimonious,
78 | Author = {Douglas Bates and Reinhold Kliegl and Shravan Vasishth and Harald Baayen},
79 | Note = {MS},
80 | Title = {Parsimonious mixed models},
81 | Year = {2015}}
82 |
83 | @unpublished{SorensenVasishth,
84 | Author = {Tanner Sorensen and Shravan Vasishth},
85 | Note = {MS},
86 | Title = {A tutorial on fitting Bayesian linear mixed models using Stan},
87 | Year = {2015}}
88 |
89 |
--------------------------------------------------------------------------------