Skip to content

Clustered data

Cases are often grouped: several images per patient, several sentences per document, several patients per hospital. Cases in the same cluster tend to be similar, and so do the model's errors on them. Treating them as independent gives intervals that are too narrow.

Estimation

Pass the cluster labels of both samples:

result = fl.ppi_mean(
    y,
    preds_labeled,
    preds_unlabeled,
    clusters_labeled=patient_id_labeled,
    clusters_unlabeled=patient_id_unlabeled,
)

The variances become cluster-robust (sandwich) estimates. The labelled clusters should be a random sample of clusters, labelled completely or by a random subsample within each cluster. At least a few dozen clusters are needed for the normal approximation.

Planning

The Kish design effect approximates the variance inflation:

\[ \text{DEFF} = 1 + (\bar m - 1)\,\rho_{\text{ICC}}, \]

where \(\bar m\) is the mean cluster size and \(\rho_{\text{ICC}}\) the intra-cluster correlation.

params = params.with_design_effect(fl.design_effect(mean_cluster_size=5, icc=0.3))
fl.required_labels(0.03, params)

Known selection probabilities with clusters

ipw_ppi_mean also accepts clusters. Selection must then be made by cluster, with every case of a selected cluster labelled.