Calculate percentile differences from an ordered categorical variable and a continuous variable.

perc_diff(
data_model,
categorical_var,
continuous_var,
weights = NULL,
percentiles = c(90, 10)
)

perc_diff_df(
data_model,
categorical_var,
continuous_var,
weights = NULL,
percentiles = c(90, 10)
)

## Arguments

data_model A data frame with at least the categorical and continuous variables from which to estimate the percentile differences The bare unquoted name of the categorical variable. This variable SHOULD be an ordered factor. If not, will raise an error. The bare unquoted name of the continuous variable from which to estimate the percentiles The bare unquoted name of the optional weight variable. If not specified, then estimation is done without weights A numeric vector of two numbers specifying which percentiles to subtract

## Value

perc_diff returns a vector with the percentile difference and its associated standard error. perc_diff_df returns the same but as a data frame.

## Details

perc_diff drops missing observations silently for calculating the linear combination of coefficients.

## Examples



set.seed(23131)
N <- 1000
K <- 20

toy_data <- data.frame(id = 1:N,
score = rnorm(N, sd = 2),
type = rep(paste0("inc", 1:20), each = N/K),
wt = 1)

# perc_diff(toy_data, type, score)
# type is not an ordered factor!

toy_data$type <- factor(toy_data$type, levels = unique(toy_data\$type), ordered = TRUE)

perc_diff(toy_data, type, score, percentiles = c(90, 10))
#> difference         se
#> -0.3205555  0.1465449 perc_diff(toy_data, type, score, percentiles = c(50, 10))
#> difference         se
#> -0.2316786  0.1357972
perc_diff(toy_data, type, score, weights = wt, percentiles = c(30, 10))
#>  difference          se
#> -0.05017358  0.11820274 # Results as data frame
perc_diff_df(toy_data, type, score, weights = wt, percentiles = c(30, 10))
#>    difference        se
#> 1 -0.05017358 0.1182027