![cc](pix/cc-attrib-nc.png)
Licensed under the 
[Creative Commons attribution-noncommercial license](http://creativecommons.org/licenses/by-nc/3.0/).
Please share \& remix noncommercially, mentioning its origin.

```{r pkgs,message=FALSE}
library(lme4)
library(nlme)
library(dotwhisker)
library(broom)
library(broom.mixed)
library(cowplot)
library(r2glmm)
```

## List of possible topics

- effect size measures/$R^2$
- type III sums of squares (mostly philosophical)
- zero-inflation
- temporal autocorrelation

## type III sums of squares

There is a long-standing argument about the *principle of marginality*: when does it make sense to interpret/test the significance of a main effect in a model where that effect is also involved in an interaction?

- main effect parameter value/significance is always evaluated **for a particular value of all of the other parameters**
- when the interaction is present, that implies that the value/significance changes depending on where you test it.
- for an interaction with a continuous variable, the parameter will always be significant *eventually*
- the "average" condition might not make biological sense (what is halfway between male and female?)
- if marginality is violated, parameter effects/SSQ depend on how the model is parameterized/where the baseline (zero value) of each parameter lies

**the bottom line**

- need to choose a *sensible* "model center" [@schielzeth_simple_2010]
- center continuous variables by their mean, or by the minimum, or some sensible reference point
- center factor variables by using *sum-to-zero* contrasts (weights equally across levels), *or* see "Weights" in `?emmeans::emmeans`
- type-3 ANOVAs are available from `lmerTest`, `car::Anova`, `afex` ...

## Effect size measures

- I would generally recommend finding standardized effect sizes by scaling continuous predictors by their standard deviation as recommended by @schielzeth_simple_2010 (or by 2SD as suggested by @gelman_scaling_2008)
- R-squared measures:
     - generalized linear models: have to decide whether to include sampling variance or not, and what measure of variability to use
     - mixed models: have to decide how to account for/include variability at different levels
- overall metric like $\eta^2$:

> Unfortunately this is currently not possible.
>I believe that most of these problems are also discussed in a recent Psych Methods paper [@rights_quantifying_2018] ... The fact that calculating a global measure of model fit (such as R2) is already riddled with complications and that no simple single number can be found, should be a hint that doing so for a subset of the model parameters (i.e., main-effects or interactions) is even more difficult. Given this, I would not recommend to try finding a measure of standardized effect sizes for mixed models. [@singmann_compute_2018]

- see the relevant section of the [glmm FAQ](http://bbolker.github.io/mixedmodels-misc/glmmFAQ.html#how-do-i-compute-a-coefficient-of-determination-r2-or-an-analogue-for-glmms) for packages etc. ...

Example: using the `r2beta()` package with `method="nsj"` [nakagawa_general_2013;@johnson_extension_2014]

```{r grsq}
library(lme4)
load("../data/gopherdat2.RData")
Gdat <- transform(Gdat,fYear=factor(year))
gmod_lme4_L <- glmer(shells~prev+fYear+(1|Site),
                     offset=log(Area),
                     family=poisson,data=Gdat,
                     control=glmerControl(optimizer="bobyqa",
                                          check.conv.grad=.makeCC("warning",0.05)))
r2beta(gmod_lme4_L,method="nsj")
```

```{r dwplot1,echo=FALSE}
tfun <- function(title="",by_2sd=TRUE) {
    dwplot(gmod_lme4_L,effect="fixed",by_2sd=by_2sd)+ggtitle(title)+
        geom_vline(xintercept=0,lty=2)
}
plot_grid(tfun("unstandardized\ndwplot(...,by_2sd=FALSE)",FALSE),
          tfun("standardized (default)\ndwplot(by_2sd=TRUE)"))

```
    
## Temporal correlation

- Easier than spatial correlation!
- Diagnose (without trying to incorporate it in the model) via `nlme::ACF`
- use `correlation=` argument in `lme` or `MASS::glmmPQL`
- temporal correlation options in: `glmmTMB`, `brms`, `MCMCglmm`, `INLA` ...

```{r acf1}
m1 <- lme(Reaction~Days,
          random=~Days|Subject,
          sleepstudy)
plot(ACF(m1),alpha=0.05)
## this appears to fit, but makes the intercept/slope model singular
m1A <- update(m1,
              correlation=corAR1(),
              control=lmeControl(opt="optim"))
intervals(m1A)
plot(ACF(m1A,resType="normalized"),alpha=0.05)
```

## Spatial correlation

- use smooth terms (see E. Gonzalez's course ...)
- `glmmPQL`, `spaMM`, `INLA`, ... see @dormann_methods_2007 (now a bit out of date)

## Multivariate models

- via implicit melting: `MCMCglmm`
- via melting: `lme4` etc.

## References
