stanova_lmer: Estimate ANOVA-type mixed models with rstanarm

Description Usage Arguments Note Examples

View source: R/stanova_lmer.R

Description

Estimate ANOVA-type mixed models with rstanarm

Usage

1
2
3
stanova_lmer(formula, data, check_contrasts = "contr.bayes", ...)

stanova_glmer(formula, data, family, check_contrasts = "contr.bayes", ...)

Arguments

formula

a formula describing the full mixed-model to be fitted. Passed to rstanarm::stan_g/lmer.

data

data.frame containing the data.

check_contrasts

character string (of length 1) denoting a contrast function or a contrast function which should be assigned to all character and factor variables in the model (as long as the specified contrast is not the global default). Default is contr.bayes. Set to NULL to disable the check.

...

further arguments passed to the rstanarm function used for fitting. Typical arguments are prior, prior_intercept, chain, iter, or core.

family

family argument passed to stan_glmer.

Note

These functions are only wrappers around stanova setting model_fun to "glmer" (and family = "gaussian" for stanova_lmer).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
### examples generally use stanova(.., model_fun = "glmer")
## this can be replaced by the stanova_lmer or stanove_glmer

data("Machines", package = "MEMSS")

## better formula would be: score ~ Machine + (Machine|Worker)
m_machines <- stanova(score ~ Machine + (1|Worker),
                      model_fun = "glmer",
                      data=Machines, chains = 2,
                      warmup = 250, iter = 750)
summary(m_machines) ## default: difference from intercept

summary(m_machines, diff_intercept = FALSE) ## alt: marginal means

out_array <- stanova_samples(m_machines)
str(out_array)

out_array2 <- stanova_samples(m_machines, dimension_chain = 2)
str(out_array2)

bayesplot::mcmc_trace(out_array2$`(Intercept)`)
bayesplot::mcmc_trace(out_array2$Machine)

out_df <- stanova_samples(m_machines, return = "data.frame")
str(out_df)

data("obk.long", package = "afex")

m2 <- stanova_lmer(value ~ treatment * phase + (1|id), obk.long,
                   chains = 2, iter = 500)

m2
summary(m2)


## with continuous variable
data(md_16.4, package = "afex")
md_16.4$cog <- scale(md_16.4$cog, scale=FALSE)


m_cont0 <- stanova(induct ~ cog + (cog|room:cond), md_16.4,
                   model_fun = "glmer", chains = 2, iter = 500)
summary(m_cont0)

# with interaction:
m_cont1 <- stanova(induct ~ cond*cog + (cog|room:cond), md_16.4,
                   model_fun = "glmer", chains = 2, iter = 500)
summary(m_cont1)

summary(m_cont1, diff_intercept = TRUE)

### glmer models

## binomial model
cbpp <- lme4::cbpp
cbpp$prob <- with(cbpp, incidence / size)
example_model <- stanova(prob ~ period + (1|herd),
                         data = cbpp, family = binomial,
                         weight = size, model_fun = "glmer",
                         chains = 2, cores = 1, seed = 12345, iter = 500)
summary(example_model)


## poisson model
data(Salamanders, package = "glmmTMB")
gm1 <- stanova(count~spp * mined + (1 | site), data = Salamanders,
               family = "poisson", model_fun = "glmer",
               chains = 2, cores = 1, seed = 12345, iter = 500)
summary(gm1)

bayesstuff/stanova documentation built on June 9, 2021, 6:18 p.m.