`vignettes/mixedmodels-statistics.Rmd`

`mixedmodels-statistics.Rmd`

This vignettes demontrates those functions of the *sjstats*-package that deal especially with mixed effects models. *sjstats* provides following functions:

Befor we start, we fit a simple linear mixed model:

```
library(sjstats)
library(lme4)
# load sample data
data(sleepstudy)
# fit linear mixed model
m <- lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy)
set.seed(2018)
sleepstudy$mygrp <- sample(1:45, size = 180, replace = TRUE)
m2 <- lmer(Reaction ~ Days + (1 | mygrp) + (1 | Subject), sleepstudy)
```

The first two functions, `deff()`

and `smpsize_lmm()`

, can be used to approximately calculate the sample size in the context of power calculation. Calculating the sample size for simple linear models is pretty straightforward, however, for (linear) mixed models, statistical power is affected through the change of the variance of test statistics. This is what *Hsieh et al. (2003)* call a *design effect* (or variance inflation factor, VIF). Once this design effect is calculated, the sample size calculated for a standard design can be adjusted accordingly.

`deff()`

computes this design effect for linear mixed models with two-level design. It requires the approximated average number of observations per grouping cluster (i.e. level-2 unit) and the assumed intraclass correlation coefficient (ICC) for the multilevel-model. Typically, the minimum assumed value for the ICC is *0.05*.

```
# Design effect for two-level model with 30 observations per
# cluster group (level-2 unit) and an assumed intraclass
# correlation coefficient of 0.05.
deff(n = 30)
#> [1] 2.45
# Design effect for two-level model with 24 observation per cluster
# group and an assumed intraclass correlation coefficient of 0.2.
deff(n = 24, icc = 0.2)
#> [1] 5.6
```

`smpsize_lmm()`

combines the functions for power calculation from the **pwr**-package and design effect `deff()`

. It computes an approximated sample size for linear mixed models (two-level-designs), based on power-calculation for standard design and adjusted for design effect for 2-level-designs.

```
# Sample size for multilevel model with 30 cluster groups and a small to
# medium effect size (Cohen's d) of 0.3. 27 subjects per cluster and
# hence a total sample size of about 802 observations is needed.
smpsize_lmm(eff.size = .3, k = 30)
#> $`Subjects per Cluster`
#> [1] 27
#>
#> $`Total Sample Size`
#> [1] 802
# Sample size for multilevel model with 20 cluster groups and a medium
# to large effect size for linear models of 0.2. Five subjects per cluster and
# hence a total sample size of about 107 observations is needed.
smpsize_lmm(eff.size = .2, df.n = 5, k = 20, power = .9)
#> $`Subjects per Cluster`
#> [1] 5
#>
#> $`Total Sample Size`
#> [1] 107
```

There are more ways to perform power calculations for multilevel models, however, most of these require very detailed knowledge about the sample characteristics and performing simulation studys. `smpsize_lmm()`

is a more pragmatic alternative to these approaches.

Most functions to fit multilevel and mixed effects models only allow to specify frequency weights, but not design (i.e. *sampling* or *probability*) weights, which should be used when analyzing complex samples and survey data.

`scale_weights()`

implements an algorithm proposed by *Aaparouhov (2006)* and *Carle (2009)* to rescale design weights in survey data to account for the grouping structure of multilevel models, which then can be used for multilevel modelling.

To calculate a weight-vector that can be used in multilevel models, `scale_weights()`

needs the data frame with survey data as `x`

-argument. This data frame should contain 1) a *cluster ID* (argument `cluster.id`

), which represents the *strata* of the survey data (the level-2-cluster variable) and 2) the probability weights (argument `pweight`

), which represents the design or sampling weights of the survey data (level-1-weight).

`scale_weights()`

then returns the original data frame, including two new variables: `svywght_a`

, where the sample weights `pweight`

are adjusted by a factor that represents the proportion of cluster size divided by the sum of sampling weights within each cluster. The adjustment factor for `svywght_b`

is the sum of sample weights within each cluster devided by the sum of squared sample weights within each cluster (see *Carle (2009)*, Appendix B, for details).

```
data(nhanes_sample)
scale_weights(nhanes_sample, SDMVSTRA, WTINT2YR)
#> # A tibble: 2,992 x 9
#> total age RIAGENDR RIDRETH1 SDMVPSU SDMVSTRA WTINT2YR svywght_a svywght_b
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 2.2 1 3 2 31 97594. 1.57 1.20
#> 2 7 2.08 2 3 1 29 39599. 0.623 0.525
#> 3 3 1.48 2 1 2 42 26620. 0.898 0.544
#> 4 4 1.32 2 4 2 33 34999. 0.708 0.550
#> 5 1 2 2 1 1 41 14746. 0.422 0.312
#> 6 6 2.2 2 4 1 38 28232. 0.688 0.516
#> 7 350 1.6 1 3 2 33 93162. 1.89 1.46
#> 8 NA 1.48 2 3 1 29 82276. 1.29 1.09
#> 9 3 2.28 2 4 1 41 24726. 0.707 0.523
#> 10 30 0.84 1 3 2 35 39895. 0.760 0.594
#> # ... with 2,982 more rows
```

Aaparouhov T. 2006. *General Multi-Level Modeling with Sampling Weights.* Communications in Statistics—Theory and Methods (35): 439–460

Carle AC. 2009. *Fitting multilevel models in complex survey data with design weights: Recommendations.* BMC Medical Research Methodology 9(49): 1-13

Hsieh FY, Lavori PW, Cohen HJ, Feussner JR. 2003. *An Overview of Variance Inflation Factors for Sample-Size Calculation.* Evaluation & the Health Professions 26: 239–257. doi: 10.1177/0163278703255230