I have a 2x2 factorial design: control vs enriched, and strain1 vs strain2. I wanted to make a linear model, which I did as follows:
anova(lmer(length ~ Strain + Insect + Strain:Insect + BW_final + (1|Pen), data = mydata))
Where length is one of the dependent variables I want to analyse, Strain and Insect as treatments, Strain:Insect as interaction effect, BW_final as covariate, and Pen as random effect.
As output I get this:
Sum Sq Mean Sq NumDF DenDF F value Pr(>F)
Strain 3.274 3.274 1 65 0.1215 0.7285
Insect 14.452 14.452 1 65 0.5365 0.4665
BW_final 45.143 45.143 1 65 1.6757 0.2001
Strain:Insect 52.813 52.813 1 65 1.9604 0.1662
As you can see, I only get 1 interaction term: Strain:Insect. However, I'd like to see 4 interaction terms: Strain1:Control, Strain1:Enriched, Strain2:Control, Strain2:Enriched.
Is there any way to do this in R?
Using summary instead of anova I get:
> summary(linearmer)
Linear mixed model fit by REML. t-tests use Satterthwaite's method [lmerModLmerTest]
Formula: length ~ Strain + Insect + Strain:Insect + BW_final + (1 | Pen)
Data: mydata_young
REML criterion at convergence: 424.2
Scaled residuals:
Min 1Q Median 3Q Max
-1.95735 -0.52107 0.07014 0.43928 2.13383
Random effects:
Groups Name Variance Std.Dev.
Pen (Intercept) 0.00 0.00
Residual 26.94 5.19
Number of obs: 70, groups: Pen, 27
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 101.646129 7.530496 65.000000 13.498 <2e-16 ***
StrainRoss 0.648688 1.860745 65.000000 0.349 0.729
Insect 0.822454 2.062696 65.000000 0.399 0.691
BW_final -0.005188 0.004008 65.000000 -1.294 0.200
StrainRoss:Insect -3.608430 2.577182 65.000000 -1.400 0.166
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Correlation of Fixed Effects:
(Intr) StrnRs Insect BW_fnl
StrainRoss 0.253
Insect -0.275 0.375
BW_final -0.985 -0.378 0.169
StrnRss:Ins 0.071 -0.625 -0.775 0.016
convergence code: 0
boundary (singular) fit: see ?isSingular```
So I'm an R novice attempting a GLMM and post hoc analysis... help! I've collected binary data on 9 damselflys under 6 light levels, 1=response to movement of optomotor drum, 0=no response. My data was imported into R with the headings 'Animal_ID, light_intensity, response'. Animal ID (1-9) repeated for each light intensity (3.36-0.61) (see below)
Using the following code (lme4 package), I've performed a GLMM and found a light level to have a significant effect on response:
d = data.frame(id = data[,1], var = data$Light_Intensity, Response = data$Response)
model <- glmer(Response~var+(1|id),family="binomial",data=d)
summary(model)
Returns
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) [glmerMod]
Family: binomial ( logit )
Formula: Response ~ var + (1 | Animal_ID)
Data: d
AIC BIC logLik deviance df.resid
66 72 -30 60 51
Scaled residuals:
Min 1Q Median 3Q Max
-3.7704 -0.6050 0.3276 0.5195 1.2463
Random effects:
Groups Name Variance Std.Dev.
Animal_ID (Intercept) 1.645 1.283
Number of obs: 54, groups: Animal_ID, 9
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.7406 1.0507 -1.657 0.0976 .
var 1.1114 0.4339 2.561 0.0104 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Correlation of Fixed Effects:
(Intr)
var -0.846
Then running:
m1 <- update(model, ~.-var)
anova(model, m1, test = 'Chisq')
Returns
Data: d
Models:
m1: Response ~ (1 | Animal_ID)
model: Response ~ var + (1 | Animal_ID)
Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
m1 2 72.555 76.533 -34.278 68.555
model 3 66.017 71.983 -30.008 60.017 8.5388 1 0.003477 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
I've installed the multcomp and lsmeans packages in an attempt at performing a Tukey post hoc to see where the difference is, but have run into difficulties with both.
Running:
summary(glht(m1,linfct=mcp("Animal_ID"="Tukey")))
Returns:
"Error in mcp2matrix(model, linfct = linfct) :
Variable(s) ‘Animal_ID’ have been specified in ‘linfct’ but cannot be found in ‘model’! "
Running:
lsmeans(model,pairwise~Animal_ID,adjust="tukey")
Returns:
"Error in lsmeans.character.ref.grid(object = new("ref.grid", model.info = list( :
No variable named Animal_ID in the reference grid"
I'm aware that I'm probably being very stupid here, but any help would be very much appreciated. My confusion is snowballing.
Also, does anyone have any suggestions as to how I might best visualize my results (and how to do this)?
Thank you very much in advance!
UPDATE:
New code-
Light <- c("3.36","3.36","3.36","3.36","3.36","3.36","3.36","3.36","3.36","2.98","2.98","2.98","2.98","2.98","2.98","2.98","2.98","2.98","2.73","2.73","2.73","2.73","2.73","2.73","2.73","2.73","2.73","2.15","2.15","2.15","2.15","2.15","2.15","2.15","2.15","2.15","1.72","1.72","1.72","1.72","1.72","1.72","1.72","1.72","1.72","0.61","0.61","0.61","0.61","0.61","0.61","0.61","0.61","0.61")
Subject <- c("1","2","3","4","5","6","7","8","9","1","2","3","4","5","6","7","8","9","1","2","3","4","5","6","7","8","9","1","2","3","4","5","6","7","8","9","1","2","3","4","5","6","7","8","9","1","2","3","4","5","6","7","8","9")
Value <- c("1","0","1","0","1","1","1","0","1","1","0","1","1","1","1","1","1","1","0","1","1","1","1","1","1","0","1","0","0","1","1","1","1","1","1","1","0","0","0","1","0","0","1","0","1","0","0","0","1","1","0","1","0","0")
data <- data.frame(Light, Subject, Value)
library(lme4)
model <- glmer(Value~Light+(1|Subject),family="binomial",data=data)
summary(model)
Returns:
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) [
glmerMod]
Family: binomial ( logit )
Formula: Value ~ Light + (1 | Subject)
Data: data
AIC BIC logLik deviance df.resid
67.5 81.4 -26.7 53.5 47
Scaled residuals:
Min 1Q Median 3Q Max
-2.6564 -0.4884 0.2193 0.3836 1.2418
Random effects:
Groups Name Variance Std.Dev.
Subject (Intercept) 2.687 1.639
Number of obs: 54, groups: Subject, 9
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.070e+00 1.053e+00 -1.016 0.3096
Light1.72 -7.934e-06 1.227e+00 0.000 1.0000
Light2.15 2.931e+00 1.438e+00 2.038 0.0416 *
Light2.73 2.931e+00 1.438e+00 2.038 0.0416 *
Light2.98 4.049e+00 1.699e+00 2.383 0.0172 *
Light3.36 2.111e+00 1.308e+00 1.613 0.1067
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Correlation of Fixed Effects:
(Intr) Lg1.72 Lg2.15 Lg2.73 Lg2.98
Light1.72 -0.582
Light2.15 -0.595 0.426
Light2.73 -0.595 0.426 0.555
Light2.98 -0.534 0.361 0.523 0.523
Light3.36 -0.623 0.469 0.553 0.553 0.508
Then running:
m1 <- update(model, ~.-Light)
anova(model, m1, test= 'Chisq')
Returns:
Data: data
Models:
m1: Value ~ (1 | Subject)
model: Value ~ Light + (1 | Subject)
Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
m1 2 72.555 76.533 -34.278 68.555
model 7 67.470 81.393 -26.735 53.470 15.086 5 0.01 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Finally, running:
library(lsmeans)
lsmeans(model,list(pairwise~Light),adjust="tukey")
Returns (it actually works now!):
$`lsmeans of Light`
Light lsmean SE df asymp.LCL asymp.UCL
0.61 -1.070208 1.053277 NA -3.1345922 0.9941771
1.72 -1.070216 1.053277 NA -3.1345997 0.9941687
2.15 1.860339 1.172361 NA -0.4374459 4.1581244
2.73 1.860332 1.172360 NA -0.4374511 4.1581149
2.98 2.978658 1.443987 NA 0.1484964 5.8088196
3.36 1.040537 1.050317 NA -1.0180467 3.0991215
Results are given on the logit (not the response) scale.
Confidence level used: 0.95
$`pairwise differences of contrast`
contrast estimate SE df z.ratio p.value
0.61 - 1.72 7.933829e-06 1.226607 NA 0.000 1.0000
0.61 - 2.15 -2.930547e+00 1.438239 NA -2.038 0.3209
0.61 - 2.73 -2.930539e+00 1.438237 NA -2.038 0.3209
0.61 - 2.98 -4.048866e+00 1.699175 NA -2.383 0.1622
0.61 - 3.36 -2.110745e+00 1.308395 NA -1.613 0.5897
1.72 - 2.15 -2.930555e+00 1.438239 NA -2.038 0.3209
1.72 - 2.73 -2.930547e+00 1.438238 NA -2.038 0.3209
1.72 - 2.98 -4.048874e+00 1.699175 NA -2.383 0.1622
1.72 - 3.36 -2.110753e+00 1.308395 NA -1.613 0.5897
2.15 - 2.73 7.347728e-06 1.357365 NA 0.000 1.0000
2.15 - 2.98 -1.118319e+00 1.548539 NA -0.722 0.9793
2.15 - 3.36 8.198019e-01 1.302947 NA 0.629 0.9889
2.73 - 2.98 -1.118326e+00 1.548538 NA -0.722 0.9793
2.73 - 3.36 8.197945e-01 1.302947 NA 0.629 0.9889
2.98 - 3.36 1.938121e+00 1.529202 NA 1.267 0.8029
Results are given on the log odds ratio (not the response) scale.
P value adjustment: tukey method for comparing a family of 6 estimates
Your model specifies Animal_ID as a random effect. The glht and lsmeans functions work only for fixed-effect comparisons.
I ran an experiment in which participants were asked to pass a story along a 4 person 'transmission chain', a bit like the game Chinese Whispers. Person 1 reads the story and re-writes it for person 2, who does the same and it continues until all four people in the chain have read and reproduced the story. I'm interested it whether positive or negative information 'survives' better in the reproductions. I have modeled this two ways: one approach was to code each item in the original story as being either present (1) or absent (0) in the reproductions and model this using a logistic model:
survival.logit <- glmer(Present ~ Posn.c*mood.c*Valence.c + (1+Valence.c|mood.c/Chain.) + (1|Item), data = Survival.Analysis_restructureddata, family = binomial, glmerControl(optimizer="bobyqa", check.conv.grad=.makeCC("warning", 2e-3)))
The other approach is to count the number of each type of statement that is lost across the chains and model this data using a poisson or negative binomial model.
survival.count <- glmer.nb(Loss_across.Chain ~ Posn.c*mood.c*Valence.c + (1 + Valence.c|mood.c/Chain), data = FinalData_forpoisson, control = glmerControl(optimizer = "bobyqa", check.conv.grad = .makeCC("warning", 0.05)))
The fixed factors in each model are:
Posn.c - position in the chain (centered)
mood.c - mood condition (a between-groups factor, centered)
Valence.c - valence of the item (positive or negative, centered)
Both models return similar results with one key exception - the interaction between position in the chain and valence is not significant in the logistic model but is highly significant in the negative binomial model. Why might this be the case?? Graphing the data suggests that there is indeed an interaction, such that positive information is lost at a faster rate than negative across the chain.
Any help would be greatly appreciated!
Edit: Please see below the model output for the both models:
Logistic:
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
Family: binomial ( logit )
Formula: Present ~ Posn.c * mood.c * Valence.c + (1 + Valence.c | mood.c/Chain.) + (1 | Item)
Data: Survival.Analysis_restructureddata
Control: glmerControl(optimizer = "bobyqa", check.conv.grad = .makeCC("warning", 0.002))
AIC BIC logLik deviance df.resid
5795.2 5895.4 -2882.6 5765.2 5873
Scaled residuals:
Min 1Q Median 3Q Max
-7.7595 -0.5744 0.1876 0.5450 5.5047
Random effects:
Groups Name Variance Std.Dev. Corr
Chain.:mood.c (Intercept) 7.550e-01 8.689e-01
Valence.c 1.366e+00 1.169e+00 0.47
Item (Intercept) 1.624e+00 1.274e+00
mood.c (Intercept) 3.708e-18 1.926e-09
Valence.c 7.777e-14 2.789e-07 1.00
Number of obs: 5888, groups: Chain.:mood.c, 92; Item, 16; mood.c, 2
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.43895 0.33331 1.317 0.1879
Posn.c -0.54789 0.03153 -17.378 <2e-16 ***
mood.c -0.23004 0.19436 -1.184 0.2366
Valence.c 1.64397 0.65245 2.520 0.0117 *
Posn.c:mood.c -0.07000 0.06141 -1.140 0.2543
Posn.c:Valence.c 0.06144 0.06301 0.975 0.3295
mood.c:Valence.c -0.05999 0.28123 -0.213 0.8311
Posn.c:mood.c:Valence.c 0.01498 0.12276 0.122 0.9029
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Correlation of Fixed Effects:
(Intr) Posn.c mood.c Vlnc.c Psn.:. Ps.:V. md.:V.
Posn.c -0.009
mood.c -0.001 0.009
Valence.c 0.025 -0.019 -0.002
Posn.c:md.c 0.001 0.007 -0.014 -0.001
Psn.c:Vlnc. -0.018 0.054 -0.002 -0.009 -0.024
md.c:Vlnc.c -0.002 -0.002 0.399 -0.001 -0.065 0.012
Psn.c:m.:V. -0.001 -0.024 -0.046 0.001 0.060 0.007 -0.019
Negative Binomial:
Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
Family: Negative Binomial(5.0188) ( log )
Formula: Loss_across.Chain ~ Posn.c * mood.c * Valence.c + (1 + Valence.c | mood.c/Chain)
Data: FinalData_forpoisson
Control: ..3
AIC BIC logLik deviance df.resid
1901.3 1970.4 -935.7 1871.3 721
Scaled residuals:
Min 1Q Median 3Q Max
-1.3727 -0.7404 -0.5037 0.4609 7.3896
Random effects:
Groups Name Variance Std.Dev. Corr
Chain:mood.c (Intercept) 1.989e-13 4.46e-07
Valence.c 3.589e-13 5.99e-07 1.00
mood.c (Intercept) 0.000e+00 0.00e+00
Valence.c 1.690e-14 1.30e-07 NaN
Number of obs: 736, groups: Chain:mood.c, 92; mood.c, 2
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.19375 0.04797 -4.039 5.37e-05 ***
Posn.c -0.61020 0.04124 -14.798 < 2e-16 ***
mood.c 0.04862 0.09597 0.507 0.61242
Valence.c -0.27487 0.09594 -2.865 0.00417 **
Posn.c:mood.c -0.04232 0.08252 -0.513 0.60803
Posn.c:Valence.c 0.38080 0.08247 4.617 3.89e-06 ***
mood.c:Valence.c 0.13272 0.19194 0.691 0.48929
Posn.c:mood.c:Valence.c 0.05143 0.16504 0.312 0.75534
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Correlation of Fixed Effects:
(Intr) Posn.c mood.c Vlnc.c Psn.:. Ps.:V. md.:V.
Posn.c 0.491
mood.c -0.014 0.007
Valence.c 0.030 -0.090 -0.036
Posn.c:md.c 0.007 -0.008 0.492 -0.021
Psn.c:Vlnc. -0.090 0.063 -0.021 0.491 -0.030
md.c:Vlnc.c -0.036 -0.021 0.027 -0.014 -0.091 0.007
Psn.c:m.:V. -0.021 -0.030 -0.091 0.007 0.060 -0.008 0.492
I have an example mixed lmer model with 8 predictors and I want to extract the names of the covariates, their coefficients, their standard errors and their p-values and place them into a matrix so I can write them out to a .csv.
I've extracted the first 3 into columns fine, but I can't figure out how to extract the p values. How do you do this? Is it a variation of vcov or getME()?
Here is what the model and summary look like:
mod <- lmer(outcome ~ predictor1 + etc...
summary(mod)
Generalized linear mixed model fit by the Laplace approximation
Formula: Freq ~ pm.lag0 + pm.lag1 + pm.lag2 + pm.lag3 + pm.lag4 + pm.lag5
+ temp13 + temp013 + rh13 + rh013 + (1 | county)
Data: dt
AIC BIC logLik deviance
3574 3636 -1775 3550
Random effects:
Groups Name Variance Std.Dev.
county (Intercept) 1.6131 1.2701
Number of obs: 1260, groups: county, 28
Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 2.9356504 0.2614892 11.227 < 2e-16 ***
pm.lag0 0.0012996 0.0005469 2.376 0.017494 *
pm.lag1 0.0005021 0.0005631 0.892 0.372568
pm.lag2 0.0009126 0.0005596 1.631 0.102893
pm.lag3 -0.0007073 0.0005678 -1.246 0.212896
pm.lag4 0.0031566 0.0005316 5.939 2.88e-09 ***
pm.lag5 0.0019598 0.0005359 3.657 0.000255 ***
temp13 -0.0028040 0.0007315 -3.833 0.000126 ***
temp013 -0.0023532 0.0009683 -2.430 0.015087 *
rh13 0.0058769 0.0009909 5.931 3.01e-09 ***
rh013 -0.0028568 0.0006070 -4.706 2.52e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Correlation of Fixed Effects:
(Intr) pm.lg0 pm.lg1 pm.lg2 pm.lg3 pm.lg4 pm.lg5 temp13 tmp013 rh13
pm.lag0 -0.025
pm.lag1 -0.032 -0.154
pm.lag2 -0.021 0.044 -0.179
pm.lag3 0.002 0.003 0.033 -0.176
pm.lag4 0.016 0.102 -0.016 0.041 -0.176
pm.lag5 0.008 0.027 0.090 -0.002 0.040 -0.186
temp13 -0.316 0.026 0.027 0.004 -0.019 -0.055 -0.035
temp013 0.030 -0.015 0.051 0.015 -0.015 0.002 -0.069 -0.205
rh13 -0.350 0.043 0.078 0.056 -0.012 -0.042 -0.030 0.430 0.055
rh013 0.193 -0.008 -0.021 0.011 0.030 0.101 -0.028 -0.278 0.025 -0.524
I've gone ahead here and left a space for the p-value column and entered a colname for it, so this sample of code isn't operational:
mixed.results <- mod
cbind(names(fixef(mod)),as.numeric(fixef(mod)),sqrt(diag(vcov(mod))), ???? )
mixed.results
colnames(mixed.results) <- c("Pred", "Coef", "St. Error", "Pr(>|z|)")
mixed.results
write.csv(mixed.results, file="mixedmod1.csv")
Thank you!
This is just coef(summary(model)), I believe:
gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
data = cbpp, family = binomial)
cc <- coef(summary(gm1))
str(cc)
# num [1:4, 1:4] -1.376 -1.058 -1.196 -1.638 0.205 ...
# - attr(*, "dimnames")=List of 2
# ..$ : chr [1:4] "(Intercept)" "period2" "period3" "period4"
# ..$ : chr [1:4] "Estimate" "Std. Error" "z value" "Pr(>|z|)"
cc[,4] ## or cc[,"Pr(>|z)"] to be more explicit
# (Intercept) period2 period3 period4
#1.907080e-11 1.996120e-41 4.634385e-43 4.657952e-47
I used the development version of lme4 but I think this has worked for a while.