CRAN status R-CMD-check

Introduction

stddiff.spark provides Spark-compatible implementations of the standardized difference calculations from the stddiff package. The interface is identical to stddiff, so you can swap your existing calls in-place without changing your workflow.

Functions automatically dispatch to the stddiff package when non-Spark data is supplied, so the same code works seamlessly on both local R data frames and Spark DataFrames.

Ordering of groups and levels

Spark DataFrames have no factor type, so values are ordered the way stddiff’s default as.factor() conversion orders them: numbers in numeric order, strings alphabetically, dates and date-times in time order, and FALSE before TRUE.

The ordering only decides how results are labeled. The standardized differences and their confidence intervals are the same either way: the numeric and binary differences are absolute values, and the categorical difference is a Mahalanobis distance, which doesn’t depend on which group comes first or which level is the reference.

[!Note] To report a different group as the control group, recode the group column so that group sorts first. For example, if the groups are “drug” and “placebo”, “drug” sorts first and is reported as the control group. To make placebo the control group instead:

library(dplyr)
spark_df <- spark_df %>%
  mutate(group = ifelse(group == "placebo", 0L, 1L))

Placebo is now 0, which sorts before 1, so it is reported in the .c columns and the drug group in the .t columns.

Differences from stddiff

On Spark data the results replicate stddiff, including its behaviour on unusual input, except:

[!Note] Like stddiff, stddiff.binary() doesn’t check that variables are binary. With more than two distinct values the results aren’t meaningful: the proportions can exceed 1, and the standardized difference can be NaN, with R’s “NaNs produced” warning.

Installation

CRAN

install.packages("stddiff.spark")

GitHub

# install.packages("remotes") # if you don’t have it
remotes::install_github("alicja-januszkiewicz/stddiff.spark")

Usage

library(sparklyr)
library(dplyr)
library(stddiff.spark)

# connect to Spark
sc <- spark_connect(master = "local")

# create example local data
my_data <- data.frame(
  treatment = c(1, 0, 1, 0, 1, 0),
  age       = c(34, 28, 45, 30, 50, 33),
  bmi       = c(22.1, 24.3, 27.8, 23.5, 28.2, 25.0),
  weight    = c(70, 65, 85, 68, 90, 72)
)

# copy data to Spark
spark_df <- copy_to(sc, my_data, overwrite = TRUE)

# compute standardized differences for numeric variables
stddiff.numeric(spark_df, gcol = 1, vcol = 2:4)

# disconnect Spark
spark_disconnect(sc)

Requirements