I can extract Fixed effects from the nlme summary using summary(fm1). But struggling how to get Random effects: portion.
fm1 <- lme(distance ~ age, Orthodont, random = ~ age | Subject)
summary(fm1)
Linear mixed-effects model fit by REML
Data: Orthodont
AIC BIC logLik
454.6367 470.6173 -221.3183
Random effects:
Formula: ~age | Subject
Structure: General positive-definite, Log-Cholesky parametrization
StdDev Corr
(Intercept) 2.3270340 (Intr)
age 0.2264278 -0.609
Residual 1.3100397
Fixed effects: distance ~ age
Value Std.Error DF t-value p-value
(Intercept) 16.761111 0.7752460 80 21.620377 0
age 0.660185 0.0712533 80 9.265333 0
Correlation:
(Intr)
age -0.848
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-3.223106086 -0.493761144 0.007316631 0.472151121 3.916033210
Number of Observations: 108
Number of Groups: 27
Any help will be highly appreciated. Thanks
Use ranef(fm1) to extract for each subject.
Updated to give code for extraction from summary table:
>VarCorr(fm1)
Subject = pdLogChol(age)
Variance StdDev Corr
(Intercept) 5.41508758 2.3270341 (Intr)
age 0.05126955 0.2264278 -0.609
Residual 1.71620400 1.3100397
> temp <- VarCorr(fm1)
> temp[,2]
(Intercept) age Residual
"2.3270341" "0.2264278" "1.3100397"
> temp[1,2]
[1] "2.3270341"
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 estimate the interrater reliability using ICC from a random-intercept mixed-effects linear regression model. But how to get the confidence interval around the ICC.
elmer <- lmer(A_total ~ (1|Subject), data = data)
summary(elmer)
Linear mixed model fit by REML ['lmerMod']
Formula: A_total ~ 1 + (1 | Subject)
Data: data_A
REML criterion at convergence: 184.7
Scaled residuals:
Min 1Q Median 3Q Max
-1.41507 -0.29387 0.02918 0.45656 1.27346
Random effects:
Groups Name Variance Std.Dev.
Subject (Intercept) 3663.0 60.52
Residual 116.3 10.79
Number of obs: 20, groups: Kodnamn, 10
Fixed effects:
Estimate Std. Error t value
(Intercept) 337.35 19.29 17.49
I calculate ICC to be 3663.0 / (3663.0 + 116.3). But, how can I get the confidence interval around this ICC?
Let consider this simple model consisting in two independent random effects:
$$y_{ijk} = \mu + \delta1_i + \delta2_j + \epsilon_{i,j,k}$$
where \delta1_i and \delta2_j are independent random effect (i.e. \delta1_i \sim N(0,\sigma_1^2) iid, \delta2_i \sim N(0,\sigma_1^2) iid, both independent).
Defining and inferring the variance components of such a model is straightforward using lme4::lmer (sorry the example below is likely to have no physical meaning) :
library(lme4)
library(nlme)
data(Orthodont,package="nlme")
lmer(data=Orthodont,distance~1+(1|Sex)+(1|Subject))
#Linear mixed model fit by REML ['lmerMod']
#Formula: distance ~ 1 + (1 | Sex) + (1 | Subject)
# Data: Orthodont
#REML criterion at convergence: 510.3937
#Random effects:
# Groups Name Std.Dev.
# Subject (Intercept) 1.596
# Sex (Intercept) 1.550
# Residual 2.220
#Number of obs: 108, groups: Subject, 27; Sex, 2
#Fixed Effects:
#(Intercept)
# 23.83
How can this be modeled easily using nlme::lme ? Specifying the random effect using a list of random effect implicitly introduces interaction depending from the ordering of the random effects , which can be verified here (changing order of specification, change results):
VarCorr(lme(data=Orthodont,distance~1,random=list(~1|Subject,~1|Sex)))
# Variance StdDev
#Subject = pdLogChol(1)
#(Intercept) 1.875987 1.369667
#Sex = pdLogChol(1)
#(Intercept) 1.875987 1.369667
#Residual 4.929784 2.220312
VarCorr(lme(data=Orthodont,distance~1,random=list(~1|Sex,~1|Subject)))
# Variance StdDev
#Sex = pdLogChol(1)
#(Intercept) 2.403699 1.550387
#Subject = pdLogChol(1)
#(Intercept) 2.546702 1.595839
#Residual 4.929784 2.220312
I checked my linear regression model (WMAN = Species, WDNE = sea surface temp) and found auto-correlation so instead, I am trying generalized least squares with the following script;
library(nlme)
modelwa <- gls(WMAN ~WDNE, data=dat,
correlation = corAR1(form=~MONTH),
na.action=na.omit)
summary(modelwa)
I compared both models;
> library(MuMIn)
> model.sel(modelw,modelwa)
Model selection table
(Intrc) WDNE class na.action correlation df logLik AICc delta
modelwa 31.50 0.1874 gls na.omit crAR1(MONTH) 4 -610.461 1229.2 0.00
modelw 11.31 0.7974 lm na.excl 3 -658.741 1323.7 94.44
weight
modelwa 1
modelw 0
Abbreviations:
na.action: na.excl = ‘na.exclude’
correlation: crAR1(MONTH) = ‘corAR1(~MONTH)’
Models ranked by AICc(x)
I believe the results suggest I should use gls as the AIC is lower.
My problem is, I have been reporting F-value/R²/p-value, but the output from the gls does not have these?
I would be very grateful if someone could assist me in interpreting these results?
> summary(modelwa)
Generalized least squares fit by REML
Model: WMAN ~ WDNE
Data: mp2017.dat
AIC BIC logLik
1228.923 1240.661 -610.4614
Correlation Structure: ARMA(1,0)
Formula: ~MONTH
Parameter estimate(s):
Phi1
0.4809973
Coefficients:
Value Std.Error t-value p-value
(Intercept) 31.496911 8.052339 3.911524 0.0001
WDNE 0.187419 0.091495 2.048401 0.0424
Correlation:
(Intr)
WDNE -0.339
Standardized residuals:
Min Q1 Med Q3 Max
-2.023362 -1.606329 -1.210127 1.427247 3.567186
Residual standard error: 18.85341
Degrees of freedom: 141 total; 139 residual
>
I have now overcome the problem of auto-correlation so I can use lm()
Add lag1 of residual as an X variable to the original model. This can be done using the slide function in DataCombine package.
library(DataCombine)
econ_data <- data.frame(economics, resid_mod1=lmMod$residuals)
econ_data_1 <- slide(econ_data, Var="resid_mod1",
NewVar = "lag1", slideBy = -1)
econ_data_2 <- na.omit(econ_data_1)
lmMod2 <- lm(pce ~ pop + lag1, data=econ_data_2)
This script can be found here