---
title: "Power analysis from scratch"
output: html_vignette
vignette: >
  %\VignetteIndexEntry{Power analysis from scratch}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

If pilot data is not available, `simr` can be used to create `lme4` objects from scratch as a starting point. This requires more paramters to be specified by the user. Values for these parameters might come from the literature or the user's own knowledge and experience.

```{r, message=FALSE, warning=FALSE}
library(simr)
```

```{r options, echo=FALSE, message=FALSE}
simrOptions(nsim=100, progress=FALSE)
```

### Covariates and parameters

First set up some covariates with `expand.grid`.

```{r}
x <- 1:10
g <- letters[1:3]

X <- expand.grid(x=x, g=g)
```

Specify some fixed and random parameters.

```{r}
b <- c(2, -0.1) # fixed intercept and slope
V1 <- 0.5 # random intercept variance
V2 <- matrix(c(0.5,0.05,0.05,0.1), 2) # random intercept and slope variance-covariance matrix
s <- 1 # residual standard deviation
```

### Build a model object

Use the `makeLmer` or `makeGlmer` function to build an artificial `lme4` object.

```{r}
model1 <- makeLmer(y ~ x + (1|g), fixef=b, VarCorr=V1, sigma=s, data=X)
print(model1)
model2 <- makeGlmer(z ~ x + (x|g), family="poisson", fixef=b, VarCorr=V2, data=X)
print(model2)
```

### Start the power analysis

Now we have "pilot" models, which can be used with `simr`.

```{r}
powerSim(model1, nsim=20)
powerSim(model2, nsim=20)
```
