drop1 function for lmer - r

I constructed a mixed effect model with three fixed effects and one random effect.
mdl1 <- lmer(yld.res ~ veg + rep + rip + (1|state),
REML=FALSE,data=data2)
I want to get the most parsimonious model from the above model. To do this, I want to drop one independent variable at a time and see if it improved the fit of the model (by looking at the AICc value). But when I use drop1, it gives me the following error:
drop1(mdl1, test="F")
Error in match.arg(test) : 'arg' should be one of “none”, “Chisq”, “user”
I am not really sure how to go about this and would really appreciate any help.

If you just use drop1() with the default test="none" it will give you the AIC values corresponding to the model with each fixed effect dropped in turn.
Here's a slightly silly example (it probably doesn't make sense to test the model with a quadratic but no linear term):
library('lme4')
fm1 <- lmer(Reaction ~ Days + I(Days^2) + (Days | Subject), sleepstudy)
drop1(fm1)
## Single term deletions
##
## Model:
## Reaction ~ Days + I(Days^2) + (Days | Subject)
## Df AIC
## <none> 1764.3
## Days 1 1769.2
## I(Days^2) 1 1763.9
How badly do you need AICc rather than AIC? That could be tricky/require some hacking ...

Related

Residual modeling for mixed models: Any other package than nlme?

Aside from R function nlme::lme(), I'm wondering how else I can model the Level-1 residual variance-covariance structure?
ps. My search showed I could possibly use glmmTMB package but it seems it is not about Level-1 residuals but random-effects themselves (see below code).
glmmTMB::glmmTMB(y ~ times + ar1(times | subjects), data = data) ## DON'T RUN
nlme::lme (y ~ times, random = ~ times | subjects,
correlation = corAR1(), data = data) ## DON'T RUN
glmmTMB can effectively be used to model level-1 residuals, by adding an observation-level random effect to the model (and if necessary suppressing the level-1 variance via dispformula ~ 0. For example, comparing the same fit in lme and glmmTMB:
library(glmmTMB)
library(nlme)
data("sleepstudy" ,package="lme4")
ss <- sleepstudy
ss$times <- factor(ss$Days) ## needed for glmmTMB
I initially tried with random = ~Days|Subject but neither lme nor glmmTMB were happy (overfitted):
lme1 <- lme(Reaction ~ Days, random = ~1|Subject,
correlation=corAR1(form=~Days|Subject), data=ss)
m1 <- glmmTMB(Reaction ~ Days + (1|Subject) +
ar1(times + 0 | Subject),
dispformula=~0,
data=ss,
REML=TRUE,
start=list(theta=c(4,4,1)))
Unfortunately, in order to get a good answer with glmmTMB I did have to tweak the starting values ...

Dredge MuMIn: when using a dredge on a GLMM, does the null model include random effects?

I have a global model using a GLMM with 5 fixed effects with interactions, as well as two random effects.
x ~ a*b + a*c + a*d + a*e + (1|f) + (1|g)
I am using an information theoretic approach and have used the dredge() function in MuMIn on this global model.
The null model is ranked very low (95) which I had assumed meant that fixed effects are important to the system, as there are 94 possible model combinations which are better than random.
However, a colleague asked if the random effects were included in the null model, as otherwise, of course many models are better than null, because they take into account variation from the random effects, but the null model would not.
However, I assumed that the model would still have these random effects, just no fixed effects. Is this correct?
Yes.
Here's an experimental illustration that dredge() drops only fixed effects, leaving the null model as (intercept + all random effects)
library(lme4)
library(MuMIn)
m1 = lmer(Reaction~Days+(Days|Subject),sleepstudy,na.action=na.fail,REML=FALSE)
dd = dredge(m1)
Fixed term is "(Intercept)"
## Global model call: lmer(formula = Reaction ~ Days + (Days | Subject), data = sleepstudy,
## REML = FALSE, na.action = na.fail)
## ---
## Model selection table
## (Intrc) Days df logLik AICc delta weight
## 2 251.4 10.47 6 -875.970 1764.4 0.0 1
## 1 257.8 5 -887.738 1785.8 21.4 0
## Models ranked by AICc(x)
## Random terms (all models):
## ‘Days | Subject’
The first point here is that the end of the printed output explicitly shows the random effects included for all terms.
Fitting the null model manually and comparing AICc with the table above:
m0 <- update(m1, . ~ . - Days)
formula(m0)
## Reaction ~ (Days | Subject)
AICc(m0)
## [1] 1785.821

Different interaction p-values in R versus SPSS

I originally ran my data in SPSS because figuring out the lmer package took some time for me to learn. I spent a few weeks writing up a script in R, but my output in R is different than what I'm getting using SPSS.
I have 3 Fixed Effects: Group, Session, and TrialType.
When I ran a mixed model in SPSS, I got the interaction Group*Session p=.08 OR p=.02, depending on which covariance structure I used. This is partly the reason I wanted to use R, because I didn't have enough information to help me decide which structure to use.
Here are my models in R. I'm using Log Likelihood Test to get a p-value for this Group*Session interaction.
Mod2 = lmer(accuracy ~ group*session*trialtype + (trialtype|subject), REML=F, data=data,
control = lmerControl(optimizer = "optimx", optCtrl=list(method='L-BFGS-B'))))
Mod5 = lmer(accuracy ~ session + trialtype + group + session*trialtype + trialtype*group + (trialtype|subject),
data=data, REML=FALSE,
control = lmerControl(optimizer = "optimx", optCtrl=list(method='L-BFGS-B')))
anova(Mod2, Mod5)
Data: data
Models:
Mod5: accuracy ~ session + trialtype + group + session * trialtype +
Mod5: trialtype * group + (trialtype | subject)
Mod2: accuracy ~ group * session * trialtype + (trialtype | subject)
Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
Mod5 23 -961.32 -855.74 503.66 -1007.3
Mod2 27 -956.32 -832.38 505.16 -1010.3 2.9989 4 0.558
I'll also note that I added the lmerControl based on the 2 warning/error messages I was getting. When I added, this, I got the singular boundary warning message.
Is it possible that R is not recognizing a grouping variable in my data? I'm not sure how to identify this or correct it.
Here is my syntax from SPSS:
MIXED Acc BY Test TrialType Group
/CRITERIA=CIN(95) MXITER(100) MXSTEP(10) SCORING(1) SINGULAR(0.000000000001) HCONVERGE(0,
ABSOLUTE) LCONVERGE(0, ABSOLUTE) PCONVERGE(0.000001, ABSOLUTE)
/FIXED=Test TrialType Group Test*TrialType Test*Group TrialType*Group Test*TrialType*Group |
SSTYPE(3)
/METHOD=ML
/PRINT=COVB DESCRIPTIVES G SOLUTION
/RANDOM=INTERCEPT TrialType | SUBJECT(Subject) COVTYPE(CS)
/REPEATED=Test | SUBJECT(Subject) COVTYPE(ID).
The first thing to do to figure this out is to make sure the log-likelihood values for the fitted models are the same, as if the models aren't getting the same results, the test statistics wouldn't be expected to be the same. Even if the models are the same, in R you're using a chi-square statistic rather than an F, as is used in SPSS Statistics MIXED. The p values often would differ, though not usually by as much as from .02-.08 to .558. I suspect you haven't actually got strictly comparable results here.

What is "+0" in a simple mixed model code (R)?

I'm trying to compare a difference I encountered in mixed model analyses using the package lme4. Maybe my statistical background is not sharp enough but I just can't figure what the "+0" in the code is and what the resulting difference (to a model without +0) implies.
Here my example with the +0:
lmer(Yield ~ Treatment + 0 + (1|Batch) + (1|Irrigation), data = D)
in contrast to:
lmer(Yield ~ Treatment + (1|Batch) + (1|Irrigation), data = D)
Does anyone have a smart explanation for what the +0 is and what it does to the results?
Models with + 0 usually mean "without an overall intercept" (in the fixed effects). By default, models have an intercept included, you can also make that explicit using + 1.
Most discussions of regression modelling will recommend including an intercept, unless there's good reason to believe the outcome will be 0 when the predictors are all zero (maybe true of some physical processes?).
Compare:
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
fm2 <- lmer(Reaction ~ Days + 0 + (Days | Subject), sleepstudy)
summary(fm1)
summary(fm2)
paying attention to the fixed effects

How to extract fixed effects part of summary from lme4?

I wish to extract the fixed effects part of summary() as a data.frame. I am using lme4 to run the following model:
SleepStudy <- lmer(Reaction ~ Days + (1|Subject), data = sleepstudy)
summary(SleepStudy)
I know that I can extract the random effects section of summary by using the following:
SleepStudy_RE <- as.data.frame(VarCorr(SleepStudy))
Is there a similar line of code for the fixed effects, including the estimate, standard error, degrees of freedom and exact p-value?
Thank you.
coef(summary(fitted_model)) should do it.
library(lme4)
SleepStudy <- lmer(Reaction ~ Days + (1|Subject), data = sleepstudy)
coef(summary(SleepStudy))
## Estimate Std. Error t value
## (Intercept) 251.40510 9.7467163 25.79383
## Days 10.46729 0.8042214 13.01543
If you want p-values you need lmerTest (you need to re-fit the model):
library(lmerTest)
SleepStudy <- lmer(Reaction ~ Days + (1|Subject), data = sleepstudy)
coef(summary(SleepStudy))
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 251.40510 9.7467163 22.8102 25.79383 0
## Days 10.46729 0.8042214 161.0036 13.01543 0
I don't know why the p-values are exactly zero in this case; maybe something to take up with the lmerTest maintainers.
You may also be interested in the broom package.

Resources