R/perc_diff.R
perc_diff.Rd
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) )
data_model | A data frame with at least the categorical and continuous variables from which to estimate the percentile differences |
---|---|
categorical_var | The bare unquoted name of the categorical variable. This variable SHOULD be an ordered factor. If not, will raise an error. |
continuous_var | The bare unquoted name of the continuous variable from which to estimate the percentiles |
weights | The bare unquoted name of the optional weight variable. If not specified, then estimation is done without weights |
percentiles | A numeric vector of two numbers specifying which percentiles to subtract |
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.
perc_diff
drops missing observations silently for calculating
the linear combination of coefficients.
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#> difference se #> -0.2316786 0.1357972#> difference se #> -0.05017358 0.11820274#> difference se #> 1 -0.05017358 0.1182027