build_counts: Collapse CCES data to be analyzed in binomial model

View source: R/cces_collapse.R

build_countsR Documentation

Collapse CCES data to be analyzed in binomial model

Description

Currently only is compatible with question of type "yesno".

Usage

build_counts(
  formula,
  data,
  keep_vars = NULL,
  name_ones_as = "yes",
  name_trls_as = "n_response",
  multiple_qIDs = FALSE,
  verbose = TRUE
)

Arguments

formula

the model formula used to fit the multilevel regression model. Should be of the form y ~ x1 + x2 + (1|x3) where y is a binary variable and only categorical variables should be used in the random effects notation.

data

A cleaned CCES dataset, e.g. from ccc_std_demographics which is then combined with outcome and contextual data in cces_join_slim. By default it expects the LHS outcome to be named response, and expects the dataset to have that variable. This variable must be binary or it must be a character vector that can be coerced by yesno_to_binary into a binary variable.

keep_vars

Variables that will be kept as a cell variable, regardless of whether it is specified in a formula. Input as character vector.

name_ones_as

What to name the variable that represents the number of successes in the binomial

name_trls_as

What to name the variable that represents the number of trials in the binomial.

multiple_qIDs

Does the data contain multiple outcomes in long form and therefore require the counts to be built for each outcome? Defaults to FALSE.

verbose

Show warning messages? Defaults to TRUE

Value

A dataframe of cells. The following variables have fixed names and will be assumed by ccesMRPrun::fit_brms_binomial:

  • yes: the number of successes observed in the cell

  • n_response the number of non-missing responses, representing the number of trials.

Examples


library(dplyr)

ccc_samp_std <- ccc_samp %>%
  mutate(y = sample(c("For", "Against"), size = n(), replace = TRUE)) %>%
  ccc_std_demographics()

ccc_samp_out <- build_counts(y ~ age + gender + educ + (1|cd),
                             ccc_samp_std)

ccc_samp_out

# alternative options
build_counts(y ~ educ + (1|cd), ccc_samp_std,
             name_ones_as = "success", name_trls_as = "trials")
build_counts(y ~ educ + (1|cd), ccc_samp_std,
             keep_vars = "state")


kuriwaki/ccesMRPprep documentation built on July 27, 2023, 3:34 a.m.