Skip to contents

Compute total count and proportion of responses for each level per combination of factors defined as a formula: <response_var> ~ <fixed_factor1> + <fixed_factor2> + ... (1|<random_factor1>) + (1|<random_factor2>) + .... The <response_var> must be a factor, as the function fills in missing response level counts with zeros.

Usage

count_responses(df, formula, resample = FALSE, predictions = NULL)

Arguments

df

Table with data

formula

Formula that specifies outcome variable, fixed and random effects.

resample

logical or formula. resample = TRUE : resampled with replacement over entire table. resample = ~ <var1> + <var2> + ... resample over grouped data.

predictions

vector or NULL. Optional values that replace original response values. Useful for applying the same counting procedure for model predictions.

Value

tibble with columns for all fixed factors and response variable, as well as columns N (total count) and P (proportion of responses for this level).

Details

The data is first grouped by all fixed and random factors and responses are counted per factor level. Any missing combinations are filled in with zero counts. Next, function computes proportion of each response level for all fixed and random factors combinations. Finally, it groups data only by fixed factors and compute total counts and averagere proportion of responses per factor level.

Optionally, the data can be resampled with replacement before counting. To resample over entire table set resample = TRUE. Alternatively, you can resample within data group by using a formula: resample = ~ <var1> + <var2> + .... Here, the data is first grouped and then resampled.

Examples

data(art)

counts <- count_responses(art, Response ~ Group + PaintingKind + Scale)

# with resampling over entire table
count_sample <- count_responses(art, Response ~ Group + PaintingKind + Scale, resample = TRUE)

# with resampling per group
count_sample <- count_responses(art, Response ~ Group + PaintingKind + Scale, resample = ~ Group)