| Title: | Calculate the Standardized Difference for Numeric, Binary and Category Variables in Apache Spark |
|---|---|
| Description: | Provides functions to compute standardized differences for numeric, binary, and categorical variables on Apache Spark DataFrames using 'sparklyr'. The implementation mirrors the methods used in the 'stddiff' package but operates on distributed data. See Zhicheng Du, Yuantao Hao (2022) <doi:10.32614/CRAN.package.stddiff> for reference. |
| Authors: | Alicja Januszkiewicz [aut, cre, cph] |
| Maintainer: | Alicja Januszkiewicz <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.0 |
| Built: | 2026-05-21 05:54:20 UTC |
| Source: | https://github.com/alicja-januszkiewicz/stddiff.spark |
Calculates standardized differences for binary variables using a Spark
DataFrame. Equivalent to stddiff::stddiff.binary but operates on Spark data.
stddiff.binary(data, gcol, vcol, verbose = FALSE)stddiff.binary(data, gcol, vcol, verbose = FALSE)
data |
A Spark DataFrame ( |
gcol |
Integer; column index of the binary grouping variable (e.g., treatment vs control). |
vcol |
Integer vector; column indices of the binary variables to analyze. |
verbose |
Logical; if TRUE, prints progress messages. Default is FALSE. |
Variables are encoded using lexicographic ordering since Spark does not have factor types. The first level alphabetically becomes 0, the second becomes 1.
The standardized difference is computed as:
A numeric matrix with one row per variable and columns:
p.c: Proportion in control group (first level alphabetically)
p.t: Proportion in treatment group (second level alphabetically)
missing.c: Number of missing values in control group
missing.t: Number of missing values in treatment group
stddiff: Standardized difference
stddiff.l: Lower bound of 95% confidence interval
stddiff.u: Upper bound of 95% confidence interval
stddiff.category, stddiff.numeric
sc <- sparklyr::spark_connect(master = "local") spark_df <- sparklyr::copy_to(sc, mtcars) result <- stddiff.binary( data = spark_df, gcol = 9, # column index of grouping variable vcol = c(8) # columns of binary variables ) sparklyr::spark_disconnect(sc)sc <- sparklyr::spark_connect(master = "local") spark_df <- sparklyr::copy_to(sc, mtcars) result <- stddiff.binary( data = spark_df, gcol = 9, # column index of grouping variable vcol = c(8) # columns of binary variables ) sparklyr::spark_disconnect(sc)
Calculates standardized differences for categorical variables using a Spark
DataFrame. Equivalent to stddiff::stddiff.category but operates on Spark data.
stddiff.category(data, gcol, vcol, verbose = FALSE)stddiff.category(data, gcol, vcol, verbose = FALSE)
data |
A Spark DataFrame ( |
gcol |
Integer; column index of the binary grouping variable. |
vcol |
Integer vector; column indices of the categorical variables to analyze. |
verbose |
Logical; if TRUE, prints progress messages. Default is FALSE. |
For categorical variables with K levels, the standardized difference is computed using a multivariate approach that accounts for all K-1 levels simultaneously (excluding the reference level). Category levels are sorted lexicographically; the first level alphabetically serves as the reference.
A numeric matrix with one row per category level and columns:
p.c: Proportion in control group
p.t: Proportion in treatment group
missing.c: Number of missing values in control group (first row only)
missing.t: Number of missing values in treatment group (first row only)
stddiff: Standardized difference (first row only)
stddiff.l: Lower CI bound (first row only)
stddiff.u: Upper CI bound (first row only)
Row names are formatted as "variable_name level_name".
stddiff.binary, stddiff.numeric
sc <- sparklyr::spark_connect(master = "local") spark_df <- sparklyr::copy_to(sc, as.data.frame(Titanic)) result <- stddiff.category( data = spark_df, gcol = 4, # column index of grouping variable vcol = c(1) # columns of categorical variables ) sparklyr::spark_disconnect(sc)sc <- sparklyr::spark_connect(master = "local") spark_df <- sparklyr::copy_to(sc, as.data.frame(Titanic)) result <- stddiff.category( data = spark_df, gcol = 4, # column index of grouping variable vcol = c(1) # columns of categorical variables ) sparklyr::spark_disconnect(sc)
Calculates standardized differences for continuous numeric variables using a
Spark DataFrame. Equivalent to stddiff::stddiff.numeric but operates on Spark data.
stddiff.numeric(data, gcol, vcol, verbose = FALSE)stddiff.numeric(data, gcol, vcol, verbose = FALSE)
data |
A Spark DataFrame ( |
gcol |
Integer; column index of the binary grouping variable. |
vcol |
Integer vector; column indices of the numeric variables to analyze. |
verbose |
Logical; if TRUE, prints progress messages. Default is FALSE. |
The standardized difference for continuous variables is computed as:
where represents means and represents variances.
This is equivalent to Cohen's d with pooled standard deviation.
A numeric matrix with one row per variable and columns:
mean.c: Mean in control group
sd.c: Standard deviation in control group
mean.t: Mean in treatment group
sd.t: Standard deviation in treatment group
missing.c: Number of missing values in control group
missing.t: Number of missing values in treatment group
stddiff: Standardized difference
stddiff.l: Lower bound of 95% confidence interval
stddiff.u: Upper bound of 95% confidence interval
stddiff.binary, stddiff.category
sc <- sparklyr::spark_connect(master = "local") spark_df <- sparklyr::copy_to(sc, mtcars) result <- stddiff.numeric( data = spark_df, gcol = 8, # column index of grouping variable vcol = c(1, 2, 5) # columns of numeric variables ) sparklyr::spark_disconnect(sc)sc <- sparklyr::spark_connect(master = "local") spark_df <- sparklyr::copy_to(sc, mtcars) result <- stddiff.numeric( data = spark_df, gcol = 8, # column index of grouping variable vcol = c(1, 2, 5) # columns of numeric variables ) sparklyr::spark_disconnect(sc)