Provided a vector of y values, this function returns either the plain
or per-capita difference or derivative between sequential values
Usage
calc_deriv(
y,
x = NULL,
return = "derivative",
percapita = FALSE,
x_scale = 1,
blank = NULL,
subset_by = NULL,
window_width = NULL,
window_width_n = NULL,
window_width_frac = NULL,
window_width_n_frac = NULL,
trans_y = "linear",
na.rm = TRUE,
warn_ungrouped = TRUE,
warn_logtransform_warnings = TRUE,
warn_logtransform_infinite = TRUE,
warn_window_toosmall = TRUE
)Arguments
- y
Data to calculate difference or derivative of
- x
vectorofxvalues provided as a simplenumeric.- return
One of
c("difference", "derivative")for whether the differences inyshould be returned, or the derivative ofywith respect tox- percapita
When
percapita = TRUE, the per-capita difference or derivative is returned- x_scale
numericto scalexby in derivative calculationSet
x_scaleto the ratio of the units ofxto the desired units. E.g. ifxis in seconds, but the desired derivative is in units of /minute, setx_scale = 60(since there are 60 seconds in 1 minute).- blank
y-value associated with a "blank" where the density is 0. Is required whenpercapita = TRUE.If a
vectorofblankvalues is specified,blankvalues are assumed to be in the same order asunique(subset_by)- subset_by
An optional
vectoras long asy.ywill be split by the unique values of thisvectorand the derivative for each group will be calculated independently of the others.This provides an internally-implemented approach similar to group_by and mutate
- window_width, window_width_n, window_width_frac, window_width_n_frac
Set how many data points are used to determine the slope at each point.
When all are
NULL,calc_derivcalculates the difference or derivative of each point with the next point, appendingNAat the end.When one or multiple are specified, a linear regression is fit to all points in the window to determine the slope.
window_width_nspecifies the width of the window in number of data points.window_widthspecifies the width of the window in units ofx.window_width_n_fracspecifies the width of the window as a fraction of the total number of data points.When using multiple window specifications at the same time, windows are conservative. Points included in each window will meet all of the
window_width,window_width_n, andwindow_width_n_frac.A value of
window_width_n = 3orwindow_width_n = 5is often a good default.- trans_y
One of
c("linear", "log")specifying the transformation ofy-values.'log'is only available when calculating per-capita derivatives using a fitting approach (when non-default values are specified forwindow_widthorwindow_width_n).For per-capita growth expected to be exponential or nearly-exponential,
"log"is recommended, since exponential growth is linear when log-transformed. However, log-transformations must be used with care, sincey-values at or below 0 will become undefined and results will be more sensitive to incorrect values ofblank.- na.rm
logicalwhetherNA's should be removed before analyzing- warn_ungrouped
logicalwhether warning should be issued whencalc_derivis being called on ungrouped data andsubset_by = NULL.- warn_logtransform_warnings
logicalwhether warning should be issued whenlog(y)produced warnings.- warn_logtransform_infinite
logicalwhether warning should be issued whenlog(y)produced infinite values that will be treated asNA.- warn_window_toosmall
logicalwhether warning should be issued when only one data point is in the window set bywindow_width_n,window_width, orwindow_width_n_frac, and soNAwill be returned.
Value
A vector of values for the plain (if percapita = FALSE)
or per-capita (if percapita = TRUE) difference
(if return = "difference") or derivative
(if return = "derivative") between y values. vector
will be the same length as y, with NA values
at the ends
Details
For per-capita derivatives, trans_y = 'linear' and
trans_y = 'log' approach the same value as time resolution
increases.
For instance, let's assume exponential growth $N = e^rt$ with per-capita growth rate $r$.
With trans_y = 'linear', note that $dN/dt = r e^rt = r N$.
So we can calculate per-capita growth rate as $r = dN/dt * 1/N$.
With trans_y = 'log', note that $log(N) = log(e^rt) = rt$.
So we can calculate per-capita growth rate as the slope of a linear
fit of $log(N)$ against time, $r = log(N)/t$.