draw_mcc: Draw a (matched) case-control (CC) sample from a cohort (or...

View source: R/draw_mcc.R

draw_mccR Documentation

Draw a (matched) case-control (CC) sample from a cohort (or cross-sectional data)

Description

Draw a (matched) case-control (CC) sample from a cohort (or cross-sectional data)

Usage

draw_mcc(
  cohort,
  y_name,
  n_cases = NULL,
  n_per_case,
  match_var_names = NULL,
  weight_name = "ip_weight"
)

Arguments

cohort

Cohort (or cross-sectional) data. A data.frame or a matrix with column names.

y_name

Name of the outcome variable in cohort. Cases are required to be indicated by integer 1. It is recommended to code the outcome variable as an integer vector, where 1=case and 0=non-case.

n_cases

Number of cases to draw. Default is NULL, in which case all cases will be selected.

n_per_case

Number of controls to draw for each case.

match_var_names

Name(s) of the match variable(s) in cohort used when drawing a matched CC. A string vector. Default is NULL, i.e., the controls will be sampled randomly.

weight_name

Name to assign to the column for the inverse probability weight, which will be created as the last column in the (matched) CC data returned. Default is "ip_weight".

Value

Returns a data.frame of the (matched) CC sample, with an additional column for the inverse probability weight for each subject, i.e., 1 for cases and the inverse probability of the sampling fraction for each control.

Author(s)

Yilin Ning, Anastasia Lam, Marie Reilly

Examples

library(SamplingDesignTools)
# Load cohort data
data("cohort_2") # Ignoring that we have the event time in this simulated cohort
head(cohort_2)
# Draw simple 1:2 case-control sample (i.e., with randomly selected controls):
dat_scc <- draw_mcc(cohort = cohort_2, y_name = "y", n_per_case = 2)
head(dat_scc)
# Draw simple 1:2 case-control sample, matched on age group and gender:
dat_mcc <- draw_mcc(cohort = cohort_2, y_name = "y", n_per_case = 2,
                    match_var_names = c("age_cat", "gender"),
                    weight_name = ".w")
head(dat_mcc)

nyilin/SamplingDesignTools documentation built on Nov. 20, 2022, 8:07 a.m.