cluster_size: Compute cluster sizes in multilevel designs

View source: R/cluster_size.R

cluster_sizeR Documentation

Compute cluster sizes in multilevel designs

Description

This function counts the number of unique observations within each of the user-defined clusters in the context of multilevel (i.e., clustered or nested) data with an arbitrary number of hierarchical levels (L). For example, when students at L1 are nested within classrooms at L2, which are in turn nested within schools at L3, the function computes the cluster size at L2 (i.e., number of students per classroom) and the cluster sizes at L3 (i.e., number of students per school and number of classrooms per school). By default, cluster sizes are returned broken down by L1 unit. Instead of returning cluster sizes broken down by L1 unit, it is also possible to compute summary statistics by supplying the respective summary functions to the .fns argument. Total sample sizes at each hierarchical level can be included via the .totalargument.

Usage

cluster_size(data, ..., .fns, .total = FALSE)

Arguments

data

A data frame.

...

<dynamic-dots> The names or column numbers of the identifiers depicting the cluster structure in ascending order (e.g., students, classrooms, schools: ⁠id_student, id_classroom, id_school⁠). At least two identifiers must be specified. Note that if .fns is not supplied, the first identifier will be the first column in the new data frame.

.fns

Optional. A list of function/s to compute summary statistics across cluster sizes (e.g., median cluster size: .fns = list("mdn" = median))

.total

Optional. A logical indicating whether the number of unique observations at each hierarchical level should be summed up. If TRUE, the total sample size at each hierarchical level is included in the new data frame. The default is FALSE.

Value

A data frame.

Examples


# compute cluster sizes in a three-level design
# (e.g., students within classrooms within schools)
cluster_size(studach, id_stu, id_cla, id_sch)

# compute median cluster sizes in a two-level design
# (e.g., students within schools) and include total
# and include total sample sizes at each hierarchical level
cluster_size(studach, id_stu, id_sch,
             .fns = list("mdn" = median),
             .total = TRUE)

sophiestallasch/multides documentation built on Oct. 20, 2024, 5:14 a.m.