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.
As an example, I'm running several survival models, each one has a different set of covariates.
First model
> coxph = coxph(Surv(time, event) ~ tr.ma, data = first.data, method = "breslow")
> summary(coxph)
Call:
coxph(formula = Surv(time, event) ~ tr.ma, data = first.data,
method = "breslow")
n= 49, number of events= 46
(1 observation deleted due to missingness)
coef exp(coef) se(coef) z Pr(>|z|)
tr.ma 0.09520 1.09988 0.03906 2.437 0.0148 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
exp(coef) exp(-coef) lower .95 upper .95
tr.ma 1.1 0.9092 1.019 1.187
Concordance= 0.694 (se = 0.058 )
Rsquare= 0.109 (max possible= 0.997 )
Likelihood ratio test= 5.68 on 1 df, p=0.01718
Wald test = 5.94 on 1 df, p=0.0148
Score (logrank) test = 5.93 on 1 df, p=0.01485
Second model
> coxph = coxph(Surv(time, event) ~ tr.ma + contag_per, data = first.data, method = "breslow")
> summary(coxph)
Call:
coxph(formula = Surv(time, event) ~ tr.ma + contag_per, data = first.data,
method = "breslow")
n= 49, number of events= 46
(1 observation deleted due to missingness)
coef exp(coef) se(coef) z Pr(>|z|)
tr.ma 0.09801 1.10297 0.04231 2.317 0.020518 *
contag_per -1.97309 0.13903 0.51683 -3.818 0.000135 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
exp(coef) exp(-coef) lower .95 upper .95
tr.ma 1.103 0.9066 1.01521 1.1983
contag_per 0.139 7.1929 0.05049 0.3828
Concordance= 0.752 (se = 0.058 )
Rsquare= 0.348 (max possible= 0.997 )
Likelihood ratio test= 20.98 on 2 df, p=2.78e-05
Wald test = 19.33 on 2 df, p=6.357e-05
Score (logrank) test = 20.73 on 2 df, p=3.147e-05
The problem
I want to get exp(coef), Pr(>|z|) from the output of each model and gather them into a table formatted in LaTex to make a concise report. Something like:
Model covariates exp(coef) p-value
----------------------------------------
1 tr.ma 1.099 0.0148
2 tr.ma 1.103 0.0205
contag_per 0.139 0.00013
...
It would be fantastic if I can automate that, since I have about 25 models, and having to manually retrieving and entering the numbers in a table would be quite inconvenient and error-prone. Thanks.
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