Getting p-values for mixed model run using lmer function - r

I've run some mixed models using lmer and they don't give p-values. I would like to know if there is a way to get p-values for these models. Someone suggested using the afex package. I've looked into this and am confused and overwhelmed. At https://rdrr.io/rforge/afex/man/mixed.html, for example, it gives what looks like very complicated and involved code; it's so overwhelming it makes me wonder if this is really what I need to do! Below is an example of a mixed model I run; I would like to get p-values for the fixed effects and the correlations of fixed effects. Any help would be appreciated!
Linear mixed model fit by REML ['lmerMod']
Formula: score ~ group + condition + (1 | subject) + (1 | token_set) + (1 | list)
Data: EN_JT_1
REML criterion at convergence: 744.9
Scaled residuals:
Min 1Q Median 3Q Max
-3.5860 -0.0364 0.2183 0.5424 1.6575
Random effects:
Groups Name Variance Std.Dev.
subject (Intercept) 0.006401 0.08000
token_set (Intercept) 0.001667 0.04083
list (Intercept) 0.000000 0.00000
Residual 0.084352 0.29043
Number of obs: 1704, groups: subject, 71; token_set, 24; list, 2
Fixed effects:
Estimate Std. Error t value
(Intercept) 0.99796 0.02425 41.156
groupHS -0.08453 0.02741 -3.084
groupSB -0.03103 0.03034 -1.023
conditionEN-GJT-D-ENG -0.10329 0.01990 -5.190
conditionEN-GJT-D-NNS -0.01288 0.02617 -0.492
conditionEN-GJT-D-NTR -0.19250 0.02596 -7.415
Correlation of Fixed Effects:
(Intr) gropHS gropSB cEN-GJT-D-E cEN-GJT-D-NN
groupHS -0.452
groupSB -0.409 0.361
cEN-GJT-D-E -0.410 0.000 0.000
cEN-GJT-D-NN -0.531 0.000 0.000 0.380
cEN-GJT-D-NT -0.535 0.000 0.000 0.383 0.700
optimizer (nloptwrap) convergence code: 0 (OK)
boundary (singular) fit: see ?isSingular

Related

Interpretation of an lmer output

I'm new here, I've tried to run a lmer model:
lmer = lmer(RI ~ SET + LOG_VP + (1|API) + (1|ODOUR), data = a)
Could someone help me interpret the output?
Linear mixed model fit by REML ['lmerMod']
Formula: RI ~ SET + LOG_VP + (1 | API) + (1 | ODOUR)
Data: a
REML criterion at convergence: -349.9
Scaled residuals:
Min 1Q Median 3Q Max
-2.6167 -0.4719 -0.0357 0.5053 8.4850
Random effects:
Groups Name Variance Std.Dev.
API (Intercept) 0.01431 0.11964
ODOUR (Intercept) 0.00415 0.06442
Residual 0.00778 0.08820
Number of obs: 238, groups: API, 34; ODOUR, 14
Fixed effects:
Estimate Std. Error t value
(Intercept) 0.15716 0.08792 1.787
SET 0.08180 0.05490 1.490
LOG_VP 0.03527 0.01968 1.792
Correlation of Fixed Effects:
(Intr) SET
SET -0.950
LOG_VP 0.083 -0.049
Thank you!
It depends on what your research question is, but
the response when both fixed effects are zero is is 0.15716
a 1 unit change in SET is associated with a 0.08180 change in RI
a 1 unit change in LOG_VP is associated with a 0.03527 change in RI
Variance at the API level is 0.01431
Variance at the ODOUR level is 0.00415
Residual (unit level) variance is 0.00778

How to extract only the random effects correlation parameters from an lmer model?

I am trying to extract random effect correlation parameters from an lmer output.
This is my model:
m <- lmer(RT ~ Condition + (1 + Condition| Participant), data)
Giving me the following output:
REML criterion at convergence: 6533.6
Scaled residuals:
Min 1Q Median 3Q Max
-3.4666 -0.6318 -0.0232 0.5696 4.1010
Random effects:
Groups Name Variance Std.Dev. Corr
Participant (Intercept) 0.045483 0.21327
Condition2 0.001271 0.03565 -0.43
Condition3 0.005774 0.07599 -0.04 -0.09
Condition4 0.003817 0.06178 -0.57 0.60 0.69
Residual 0.147445 0.38399
Number of obs: 6841, groups: Participant, 39
Fixed effects:
Estimate Std. Error t value
(Intercept) 1.57546 0.03537 44.544
Condition2 0.06677 0.01420 4.703
Condition3 -0.09581 0.01798 -5.328
Condition4 0.02710 0.01639 1.653
Correlation of Fixed Effects:
(Intr) Cndtn2 Cndtn3
Condition2 -0.334
Condition3 -0.157 0.307
Condition4 -0.476 0.508 0.571
However, I only want to extract specific correlation parameters of the random effects, say the correlation between Condition3 and Condition2 (-0.04). Does anyone know how to do that?
I tried using the VarCorr() function which only displays the results for the random effects, but still does not let me extract specific values from it. I would really appreciate any help!
#mfidino's answer is good. Alternatively
cc <- cov2cor(VarCorr(m1)$Subject)
cc["Days", "(Intercept)"]
or
cc <- attr(VarCorr(m1)$Subject, "corr")
cc["Days", "(Intercept)"]
The $Subject part is required because lmer models can have multiple random effects terms, so VarCorr is always returned as a list of covariance matrices (named according to the name of the corresponding grouping variable)
You want to use lme4::VarCorr to extract those values. Here is an example.
library(lme4)
data("sleepstudy")
sl <- sleepstudy
m1 <- lmer(
Reaction ~ Days + (Days | Subject),
data = sl
)
summary(m1)
Linear mixed model fit by REML ['lmerMod']
Formula: Reaction ~ Days + (Days | Subject)
Data: sl
REML criterion at convergence: 1743.6
Scaled residuals:
Min 1Q Median 3Q Max
-3.9536 -0.4634 0.0231 0.4634 5.1793
Random effects:
Groups Name Variance Std.Dev. Corr
Subject (Intercept) 612.10 24.741
Days 35.07 5.922 0.07
Residual 654.94 25.592
Number of obs: 180, groups: Subject, 18
Fixed effects:
Estimate Std. Error t value
(Intercept) 251.405 6.825 36.838
Days 10.467 1.546 6.771
Correlation of Fixed Effects:
(Intr)
Days -0.138
Here, we want to extract that correlation between (Intercept) and Days. We do that like so:
(ranef_vals <- data.frame(VarCorr(m1)))
grp var1 var2 vcov sdcor
1 Subject (Intercept) <NA> 612.100158 24.74065799
2 Subject Days <NA> 35.071714 5.92213766
3 Subject (Intercept) Days 9.604409 0.06555124
4 Residual <NA> <NA> 654.940008 25.59179572
The value we'd want here is on the third row in the sdcor column.
ranef_vals$sdcor[3]
[1] 0.06555124

Visualising crossed random effect for lme

I am new to mixed models and have some problems. I've got a model:
lmer(F2 ~ (phoneme|individual) + (1|word) + age + frequency + (1|zduration), data = nurse_female)
Linear mixed model fit by REML ['lmerMod']
Formula:
F2 ~ (phoneme | individual) + (1 | word) + age + frequency +
(1 | zduration)
Data: nurse_female
REML criterion at convergence: 654.4
Scaled residuals:
Min 1Q Median 3Q Max
-2.09203 -0.20332 0.03263 0.25273 1.37056
Random effects:
Groups Name Variance Std.Dev. Corr
zduration (Intercept) 0.27779 0.5271
word (Intercept) 0.04488 0.2118
individual (Intercept) 0.34181 0.5846
phonemeIr 0.54227 0.7364 -0.82
phonemeVr 1.52090 1.2332 -0.93 0.91
Residual 0.06326 0.2515
Number of obs: 334, groups:
zduration, 280; word, 116; individual, 23
Fixed effects:
Estimate Std. Error t value
(Intercept) 1.79167 0.32138 5.575
age -0.01596 0.00508 -3.142
frequencylow -0.37587 0.18560 -2.025
frequencymid -1.18901 0.27738 -4.286
frequencyvery high -0.68365 0.26564 -2.574
Correlation of Fixed Effects:
(Intr) age frqncyl frqncym
age -0.811
frequencylw -0.531 -0.013
frequencymd -0.333 -0.006 0.589
frqncyvryhg -0.356 0.000 0.627 0.389
The model predicts the normalised formant values of vowels such in NURSE for female speakers. Without getting too much into it, there are roughly 3 variants possible that I coded under phoneme as <Er, Ir, Vr>. Individual describes the speaker. I managed to plot the F2 variance of each speaker using random effects.
But how do I plot the model predictions for the F2 values for each speaker with phoneme on the x-axis (i.e. 3 marks for <Er, Ir, Vr>) and F2 on the y-axis?
I tried a few ways but none of them worked.
Thanks in advance. If you need further information/data just say so

R: Plotting Mixed Effect models plot results

I am working on linguistic data and try to investigate the realisation of the vowel in words such as NURSE. There are more less 3 categories that can be realised, which I coded as <Er, Ir, Vr>. I then measured Formant values (F1 and F2). Then I created an LME that predicts the F1 and F2 values with different fixed and random effects but the main effect is a cross random effect of phoneme (i.e. <Er, Ir, Vr>) and individual. An example model can be found below.
Linear mixed model fit by REML ['lmerMod']
Formula:
F2 ~ (phoneme | individual) + (1 | word) + age + frequency +
(1 | zduration)
Data: nurse_female
REML criterion at convergence: 654.4
Scaled residuals:
Min 1Q Median 3Q Max
-2.09203 -0.20332 0.03263 0.25273 1.37056
Random effects:
Groups Name Variance Std.Dev. Corr
zduration (Intercept) 0.27779 0.5271
word (Intercept) 0.04488 0.2118
individual (Intercept) 0.34181 0.5846
phonemeIr 0.54227 0.7364 -0.82
phonemeVr 1.52090 1.2332 -0.93 0.91
Residual 0.06326 0.2515
Number of obs: 334, groups:
zduration, 280; word, 116; individual, 23
Fixed effects:
Estimate Std. Error t value
(Intercept) 1.79167 0.32138 5.575
age -0.01596 0.00508 -3.142
frequencylow -0.37587 0.18560 -2.025
frequencymid -1.18901 0.27738 -4.286
frequencyvery high -0.68365 0.26564 -2.574
Correlation of Fixed Effects:
(Intr) age frqncyl frqncym
age -0.811
frequencylw -0.531 -0.013
frequencymd -0.333 -0.006 0.589
frqncyvryhg -0.356 0.000 0.627 0.389
The question is now, how would I go about plotting the mean F2 values for each individual and for each of 3 variants <Er, Ir, Vr>?
I tried plotting the random effects as a caterpillar plot and get the following, but I am not sure, if this is accurate or does what I want. If what I have done Is right, are there any other better ways of plotting it?
ranefs_nurse_female_F2 <- ranef(nurse_female_F2.lmer8_2)
dotplot(ranefs_nurse_female_F2)

gls() vs. lme() in the nlme package

In the nlme package there are two functions for fitting linear models (lme and gls).
What are the differences between
them in terms of the types of models
that can be fit, and the fitting
process?
What is the design
rational for having two functions to
fit linear mixed models where most
other systems (e.g. SAS SPSS) only
have one?
Update: Added bounty. Interested to know differences in the fitting process, and the rational.
From Pinheiro & Bates 2000, Section 5.4, p250:
The gls function is used to fit the
extended linear model, using either
maximum likelihood, or restricted
maximum likelihood. It can be veiwed
as an lme function without the
argument random.
For further details, it would be instructive to compare the lme analysis of the orthodont dataset (starting on p147 of the same book) with the gls analysis (starting on p250). To begin, compare
orth.lme <- lme(distance ~ Sex * I(age-11), data=Orthodont)
summary(orth.lme)
Linear mixed-effects model fit by REML
Data: Orthodont
AIC BIC logLik
458.9891 498.655 -214.4945
Random effects:
Formula: ~Sex * I(age - 11) | Subject
Structure: General positive-definite
StdDev Corr
(Intercept) 1.7178454 (Intr) SexFml I(-11)
SexFemale 1.6956351 -0.307
I(age - 11) 0.2937695 -0.009 -0.146
SexFemale:I(age - 11) 0.3160597 0.168 0.290 -0.964
Residual 1.2551778
Fixed effects: distance ~ Sex * I(age - 11)
Value Std.Error DF t-value p-value
(Intercept) 24.968750 0.4572240 79 54.60945 0.0000
SexFemale -2.321023 0.7823126 25 -2.96687 0.0065
I(age - 11) 0.784375 0.1015733 79 7.72226 0.0000
SexFemale:I(age - 11) -0.304830 0.1346293 79 -2.26421 0.0263
Correlation:
(Intr) SexFml I(-11)
SexFemale -0.584
I(age - 11) -0.006 0.004
SexFemale:I(age - 11) 0.005 0.144 -0.754
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-2.96534486 -0.38609670 0.03647795 0.43142668 3.99155835
Number of Observations: 108
Number of Groups: 27
orth.gls <- gls(distance ~ Sex * I(age-11), data=Orthodont)
summary(orth.gls)
Generalized least squares fit by REML
Model: distance ~ Sex * I(age - 11)
Data: Orthodont
AIC BIC logLik
493.5591 506.7811 -241.7796
Coefficients:
Value Std.Error t-value p-value
(Intercept) 24.968750 0.2821186 88.50444 0.0000
SexFemale -2.321023 0.4419949 -5.25124 0.0000
I(age - 11) 0.784375 0.1261673 6.21694 0.0000
SexFemale:I(age - 11) -0.304830 0.1976661 -1.54214 0.1261
Correlation:
(Intr) SexFml I(-11)
SexFemale -0.638
I(age - 11) 0.000 0.000
SexFemale:I(age - 11) 0.000 0.000 -0.638
Standardized residuals:
Min Q1 Med Q3 Max
-2.48814895 -0.58569115 -0.07451734 0.58924709 2.32476465
Residual standard error: 2.256949
Degrees of freedom: 108 total; 104 residual
Notice that the estimates of the fixed effects are the same (to 6 decimal places), but the standard errors are different, as is the correlation matrix.
Interesting question.
In principle the only difference is that gls can't fit models with random effects, whereas lme can. So the commands
fm1 <- gls(follicles ~ sin(2*pi*Time)+cos(2*pi*Time),Ovary,
correlation=corAR1(form=~1|Mare))
and
lm1 <- lme(follicles~sin(2*pi*Time)+cos(2*pi*Time),Ovary,
correlation=corAR1(form=~1|Mare))
ought to give the same result but they don't. The fitted parameters differ slightly.

Resources