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
Related
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
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
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)
I'm an R noob, I hope you can help me:
I'm trying to analyse a dataset in R, but I'm not sure how to interpret the output of summary(glmer(...)) and the documentation isn't a big help:
> data_chosen_stim<-glmer(open_chosen_stim~closed_chosen_stim+day+(1|ID),family=binomial,data=chosenMovement)
> summary(data_chosen_stim)
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
Family: binomial ( logit )
Formula: open_chosen_stim ~ closed_chosen_stim + day + (1 | ID)
Data: chosenMovement
AIC BIC logLik deviance df.resid
96.7 105.5 -44.4 88.7 62
Scaled residuals:
Min 1Q Median 3Q Max
-1.4062 -1.0749 0.7111 0.8787 1.0223
Random effects:
Groups Name Variance Std.Dev.
ID (Intercept) 0 0
Number of obs: 66, groups: ID, 35
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.4511 0.8715 0.518 0.605
closed_chosen_stim2 0.4783 0.5047 0.948 0.343
day -0.2476 0.5060 -0.489 0.625
Correlation of Fixed Effects:
(Intr) cls__2
clsd_chsn_2 -0.347
day -0.916 0.077
I understand the GLM behind it, but I can't see the weights of the independent variables and their error bounds.
update: weights.merMod already has a type argument ...
I think what you're looking for weights(object,type="working").
I believe these are the diagonal elements of W in your notation?
Here's a trivial example that matches up the results of glm and glmer (since the random effect is bogus and gets an estimated variance of zero, the fixed effects, weights, etc etc converges to the same value).
Note that the weights() accessor returns the prior weights by default (these are all equal to 1 for the example below).
Example (from ?glm):
d.AD <- data.frame(treatment=gl(3,3),
outcome=gl(3,1,9),
counts=c(18,17,15,20,10,20,25,13,12))
glm.D93 <- glm(counts ~ outcome + treatment, family = poisson(),
data=d.AD)
library(lme4)
d.AD$f <- 1 ## dummy grouping variable
glmer.D93 <- glmer(counts ~ outcome + treatment + (1|f),
family = poisson(),
data=d.AD,
control=glmerControl(check.nlev.gtr.1="ignore"))
Fixed effects and weights are the same:
all.equal(fixef(glmer.D93),coef(glm.D93)) ## TRUE
all.equal(unname(weights(glm.D93,type="working")),
weights(glmer.D93,type="working"),
tol=1e-7) ## TRUE
I am attempting to estimate a multilevel model. My code is:
fullModel2 <- lmer(pharmexp_2001 ~ gdp_1000_gm + health_exp_per_cap_1000_gm + life_exp +
labour_cost_1000_gm + (year_gm|lowerID), data=adat, REML=F)
which results in the following model:
Linear mixed model fit by maximum likelihood ['lmerMod']
Formula: pharmexp_2001 ~ gdp_1000_gm + health_exp_per_cap_1000_gm + life_exp +
labour_cost_1000_gm + (year_gm | lowerID)
Data: adat
AIC BIC logLik deviance df.resid
1830.2 1859.9 -906.1 1812.2 191
Scaled residuals:
Min 1Q Median 3Q Max
-2.5360 -0.6853 -0.0842 0.4923 4.0051
Random effects:
Groups Name Variance Std.Dev. Corr
lowerID (Intercept) 134.6851 11.6054
year_gm 0.4214 0.6492 -1.00
Residual 487.5324 22.0801
Number of obs: 200, groups: lowerID, 2
Fixed effects:
Estimate Std. Error t value
(Intercept) -563.7924 75.4125 -7.476
gdp_1000_gm -0.9050 0.2051 -4.413
health_exp_per_cap_1000_gm 37.5394 6.3943 5.871
life_exp 8.8571 0.9498 9.326
labour_cost_1000_gm -1.3573 0.4684 -2.898
Correlation of Fixed Effects:
(Intr) g_1000 h____1 lif_xp
gdp_1000_gm -0.068
hl____1000_ 0.374 -0.254
life_exp -0.996 0.072 -0.393
lbr_c_1000_ -0.133 -0.139 -0.802 0.142
I know that it's a problem that the correlation is -1 by random effects, but I have a bigger problem. I have to plot my results, but only I need 2 lines: when lowerID=0 and when lowerID=1. So I want to plot pharmaexp_2001 on the y-axis against year on the x-axis, but I need only 2 lines (by lowerID). I know that I have to use predict.merMod, but how can I plot these results, plotting only these two lines? Currently my plot has 21 lines (because I analyse pharmaceutical expenditure in 21 countries).
Welcome to the site, #Eszter Takács!
You only need to specify the two IDs in newdata. Here is an example based on sleepstudy data in R. I assume you want to plot the predicted values on the y-axis. Just replace the code with your data and variables, you will obtain the predicted values for lowerID==0 and lowerID==1. Then you can use your code to plot the two lines for the two IDs.
> (fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy, REML=F))
Linear mixed model fit by maximum likelihood ['lmerMod']
Formula: Reaction ~ Days + (Days | Subject)
Data: sleepstudy
AIC BIC logLik deviance
1763.9393 1783.0971 -875.9697 1751.9393
Random effects:
Groups Name Std.Dev. Corr
Subject (Intercept) 23.781
Days 5.717 0.08
Residual 25.592
Number of obs: 180, groups: Subject, 18
Fixed Effects:
(Intercept) Days
251.41 10.47
> newdata = sleepstudy[sleepstudy$Subject==308 | sleepstudy$Subject==333,]
> str(p <- predict(fm1,newdata)) # new data, all RE
Named num [1:20] 254 274 293 313 332 ...
- attr(*, "names")= chr [1:20] "1" "2" "3" "4" ...