Linear mixed model with repeated Anova issue - r

I have a question related to repeated Anova with Linear mixed model. I have found a nice example posted very long time ago on another website (https://stats.stackexchange.com/questions/237512/how-to-perform-post-hoc-test-on-lmer-model) but I am not sure if this method is correct for my case.
library (lme4)
library (lmerTest)
Result<- lmer (Value~Type+ (1|Material), data = data)
summary (Result)
library(multcomp)
summary(glht(Results, linfct = mcp(Material= "Tukey")), test = adjusted("holm"))
The person who gave this example mentioned: "After you've fit your lmer model you can do ANOVA, MANOVA, and multiple comparison procedures on the model object". Briefly, I have repeated measurements of 6 groups and I want to see which material differs from which material. When I run this code I see the results I am looking for but I would like to ask if this method can be used for repeated anova or should I use different library? in that case I would be happy if you can edit the code. Thanks in advance.
Best Regards.

Related

How to do crossvalidation on a two-part regression model?

I am currently working on regression analysis to model EQ-5D-5L health data. This data is inflated at the upper bound i.e. 1, and one of the approaches I use to model is with two-part models. By doing that, I combine a logistic model with binary data (1 or not 1), and continuous data.
The issue comes when trying to cross-validate (K-fold) the two-part models: I cannot find a way to include both "parts" of the model in the caret package in R, and I have not been able to find anybody that has solved the problem for me.
When I generate the predictions from the two-part model, it is essentially the coefficients from the two separate models that are multiplied together. So the models are developed separately, as they model different things from the same variable (binary and continuous outcome), but joined together when used to predict values.
Could it be possible to somehow cross-validate each part of the model separately, and get some kind of useful answer out of it?
Hope you guys can help.

R: mixed model with heteroscedastic data -> only lm function works?

This question asks the same question, but hasn't been answered. My question relates to how to specify the model with the lm() function and is therefore a programming (not statistical) question.
I have a mixed design (2 repeated and 1 independent predictors). Participants were first primed into group A or B (this is the independent predictor) and then they rated how much they liked 4 different statements (these are the two repeated predictors).
There are many great online resources how to model this data. However, my data is heterscedastic. So I like to use heteroscedastic-consistent covariance matrices. This paper explains it well. The sandwich and lmtest packages are great. Here is a good explanation how to do it for a indpendent design in R with lm(y ~ x).
It seems that I have use lm, else it wont work?
Here is the code for a regression model assuming that all variances are equal (which they are not as Levene's test comes back significant).
fit3 <- nlme:::lme(DV ~ repeatedIV1*repeatedIV2*independentIV1, random = ~1|participants, df) ##works fine
Here is the code for an indepedent model correcting for heteroscedasticity, which works.
fit3 <- lm(DV ~ independentIV1)
library(sandwich)
vcovHC(fit3, type = 'HC4', sandwich = F)
library(lmtest)
coef(fit3, vcov. = vcovHC, type = 'HC4')
So my question really is, how to specify my model with lm?
Alternative approaches in R how to fit my model accounting for heteroscedasticity are welcome too!
Thanks a lot!!!
My impression is that your problems come from mixing various approaches for various aspects (repeated measurements/correlation vs. heteroscedasticity) that cannot be mixed so easily. Instead of using random effects you might also consider fixed effects, or instead of only adjusting the inference for heteroscedasticity you might consider a Gaussian model and model both mean and variance, etc. For me, it's hard to say what is the best route forward here. Hence, I only comment on some aspects regarding the sandwich package:
The sandwich package is not limited to lm/glm only but it is in principle object-oriented, see vignette("sandwich-OOP", package = "sandwich") (also published as doi:10.18637/jss.v016.i09.
There are suitable methods for a wide variety of packages/models but not
for nlme or lme4. The reason is that it's not so obvious for which mixed-effects models the usual sandwich trick actually works. (Disclaimer: But I'm no expert in mixed-effects modeling.)
However, for lme4 there is a relatively new package
called merDeriv (https://CRAN.R-project.org/package=merDeriv) that
supplies estfun and bread methods so that sandwich covariances can be
computed for lmer output etc. There is also a working paper associated
with that package: https://arxiv.org/abs/1612.04911

post hoc test for a two way mixed model anova

I am doing a repeated measures anova with a mixed model. I would like to run a post hoc test to see the p-values of the interaction TREAT*TIME, but I only managed to use the following ghlt Tukey test which do not give me the interaction I am looking for.
library(multcomp)
library(nlme)
oi<-lme(total ~ TREAT * TIME, data=TURN, random = ~1|NO_UNIT)
anova(oi)
summary(glht(oi, linfct=mcp(TIME="Tukey", TREAT="Tukey")))
what I would be looking for is something like:
summary(glht(oi, linfct=mcp(TIME="Tukey",TREAT="Tukey",TREAT*TIME="Tukey")))
Use snk.test(model, term="TREAT*TIME", among="TREAT", within="TIME") from the package GAD if you have a balanced model and summary( lsmeans( oi, pairwise ~ TIME*TREAT), infer=TRUE) from lsmeans if your model is unbalanced
I have also had this problem.
It appears that a straight-forward post hoc test for two way ANOVAs does not exist.
However, you may like to try bootstrapping, which is a form of robust estimation for a two-way ANOVA. I found the following link very helpful.
http://rcompanion.org/rcompanion/d_08a.html
It contains a step-by-step tutorial using the rcompanion,WRS2, psych, and multcompView packages to perform your bootstrapped ANOVA and follow up with a post hoc. Good luck.
For a mixed model you can find an alternative with the aov_ez() function from the afex package instead of lme(), and then performe post hoc analysis using lsmeans().
You will find a detailed tutorial here:
https://www.psychologie.uni-heidelberg.de/ae/meth/team/mertens/blog/anova_in_r_made_easy.nb.html

Repeated measures ANOVA

What is the generic code to perform a repeated measures ANOVA?
I am currently using the code:
summary(aov(Chlo~Site,data=alldata)).
There are three different sites (Site) and four expirmental variables that I am testing individually (Chlo, SST, DAC and PAR). I am also assessing any differences in these variables and year (between 2003 and 2012):
summary(aov(Chlo~Year,data=year))
Any help would be appreciated!
In general you should avoid performing multiple calls with aov and rather use a mixed effects linear model.
You can find several examples in this post by Paul Gribble
I often use the nlme package, such as:
require(nlme)
model <- lme(dv ~ myfactor, random = ~1|subject/myfactor, data=mydata)
Depending on the situation you may run in more complex situations, I would suggest to have a look at the very clear book by Julian Faraway "Extending the Linear Model with R: Generalized Linear, Mixed Effects and Nonparametric Regression Models".
Also, you may want to ask on CrossValidated if you have more specific statistical questions.
The trick using aov function is that you just need to add Error term. As one of the guides says: The Error term must reflect that we have "treatments nested within subjects".
So, in your case, if Site is a repeated measure you should use:
summary(aov(Chlo ~ Site + Error(subject/Site), data=alldata))

Model fitting: glm vs glmmPQL

I am fitting a model regarding absence-presence data and I would like to check whether the random factor is significant or not.
To do this, one should compare a glmm with a glm and check with the LR-test which one is most significant, if I understand correct.
But if I perform an ANOVA(glm,glmm) , I get an analysis of Deviance Table and no output that compares the models.
How do I get the output that I desire, thus comparing both models?
Thanks in advance,
Koen
Somewhere you got the wrong impression about using anova() for this. Below re was fit using glmmPQL() in MASS package. fe was fit using glm() from base:
> anova(re,fe)
#Error in anova.glmmPQL(re, fe) : 'anova' is not available for PQL fits
That message appears to be the sole reason anova.glmmPQL() was created.
See this thread for verification and vague explanation:
https://stat.ethz.ch/pipermail/r-help/2002-July/022987.html
simply anova does not work with glmmPQL you need to use glmer from lme4 package to be able to use anova.

Resources