piecewise_cors: Piecewise Correlations

Description Usage Arguments Details Value Examples

View source: R/piecewise_cors.R

Description

Independently for each variable of interest ... and .target, calculate the piecewise correlation. Observations included for each correlation calculation are segmented based on local extrema (minima / maxima) of the smoothed model fit.

Usage

1
2
3
4
5
6
7
8
piecewise_cors(
  data,
  .target,
  ...,
  custom_model_spec = NULL,
  fit_formula = ".target ~ s(...)",
  cor_function = "~cor(.x, method = 'spearman')[2,1]"
)

Arguments

data

Dataframe containing columns of interest.

.target

Column name of target (unquoted). Numeric with sufficient unique observations.

...

Column names of variables of interest (unquoted), or a tidy selection specification (see: https://dplyr.tidyverse.org/reference/select.html). Separate models are created for each variable passed into .... Numeric with sufficient unique observations.

custom_model_spec

NULL or model specification from the {parsnip} package. Default is NULL in which case the model specification will be:

parsnip::gen_additive_mod() %>%
  parsnip::set_engine("mgcv", method = "REML") %>%
  parsnip::set_mode("regression")

(At this point) must be a generalized additive model with {mgcv} engine. See for details on parameters: https://parsnip.tidymodels.org/reference/details_gen_additive_mod_mgcv.html

fit_formula

Character string. Pseudo representation of formula passed to parsnip::fit() of model specification. Default is ".target ~ s(...)". Note that .target and ... must be included in any custom fit_formula.

cor_function

Character string of a lambda function passed to purrr::map*() to calculate correlation between .target and variables of interest at piecewise segments of observations. Default is: "~cor(.x, method = 'spearman')[2,1]"

Should be written in tidyverse friendly shortcut lambda notation (like above), as opposed to traditional lambda function notation: "function(x) cor(x, method = 'spearman')[2,1]"

Must evaluate to a numeric vector of length one. Could pass in functions specification unrelated to correlation.

Details

If the smoothing model has no local minima / maxima, correlation is calculated across all observations.

Value

Named list of named list of models $mods and named list of dataframes $cors for each variable of interest specified in .... Each dataframe in .cors has the columns:

gtoe: (greater than or equal to) lt: (less than) data: list column containing dataframes of observations within segment n_obs: number of rows in data segment cor: value returned by cor_function for segment of observations

Examples

1
# See README

brshallo/piececor documentation built on Dec. 19, 2021, 11:49 a.m.