stanova_samples: Posterior samples for ANOVA-type models

Description Usage Arguments Examples

View source: R/stanova_samples.R

Description

Posterior samples for ANOVA-type models

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
stanova_samples(object, ...)

## S3 method for class 'stanova'
stanova_samples(
  object,
  diff_intercept = TRUE,
  terms,
  return = c("array", "matrix", "data.frame", "tibble", "tidybayes"),
  dimension_chain = 3L,
  ...
)

Arguments

object

Fitted model. Currently only support for stanova objects.

...

currently ignored.

diff_intercept

logical. If TRUE (the default) samples for factor levels represent the difference from the intercept. If FALSE, marginal means of factor levels are returned.

terms

optional character vector denoting a subset of model terms for which difference from intercept be returned.

return

character string denoting format in which samples should be returned. Allowed values are c("array", "matrix", "data.frame", "tibble", "tidybayes"), possibly abbreviated. "tibble" requires package tibble and returns the same object as data.frame wrapped into as_tibble(). "tidybayes" also returns essentially the same object, but renames the columns to follow tidybayes conventions.

dimension_chain

Scalar integer. If return = "array", determines the dimension of the chain. The default 3 means chain is the third dimension of the returned array. Value should be between 1 and 3.

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
fit_warp <- stanova(breaks ~ wool * tension, data = warpbreaks,
                    prior = rstanarm::R2(0.5),
                    model_fun = "lm",
                    chains = 2, iter = 500)

arr_warp <- stanova_samples(fit_warp)
str(arr_warp)

mat_warp <- stanova_samples(fit_warp, return = "matrix")
str(mat_warp)

df_warp <- stanova_samples(fit_warp, return = "data.frame")
str(df_warp)

tail(df_warp$`wool:tension`)

if (requireNamespace("tibble")) {
  tbl_warp <- stanova_samples(fit_warp, return = "tibble")
  tbl_warp
}

if (require("tidybayes") && require("tidyverse")) {
  tidy_warp <- stanova_samples(fit_warp, return = "tidybayes")
  tidy_warp

  tidy_warp$tension %>%
    group_by(variable) %>%
    median_qi(value) %>%
    ggplot(aes(y = variable, x = value, xmin = .lower, xmax = .upper)) +
    geom_pointinterval()

  tidy_warp$tension %>%
    ggplot(aes(y = variable, x = value)) +
    stat_halfeye()

  bind_rows(tidy_warp) %>%
    ggplot(aes(y = variable, x = value)) +
    stat_halfeye() +
    facet_wrap(vars(term), scales = "free")

   bind_rows(tidy_warp) %>%
    ggplot(aes(y = variable, x = value)) +
    stat_interval() +
    facet_wrap(vars(term), scales = "free")

   ## marginal means instead of differences from mean:
   stanova_samples(fit_warp, return = "tidybayes", diff_intercept = FALSE) %>%
     bind_rows() %>%
     ggplot(aes(y = variable, x = value)) +
     stat_halfeye() +
     facet_wrap(vars(term), scales = "free")
}

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