Computes the peak directional linear regression slope for upward or downward
trending response variable y
, respectively using rolling_slope()
within
a moving window.
Arguments
- y
A numeric vector of the response variable.
- x
A numeric vector of the predictor variable, defaults to using the index of
x = seq_along(y)
.- span
A numeric value defining the window in units of
x
for rolling local calculations.- align
Specifies the window alignment of
span
as "center" (the default), "left", or "right". Where "left" is forward looking, and "right" is backward looking by the windowspan
from the current sample.- na.rm
A logical indicating how missing data will be handled.
FALSE
(the default) will perform rolling slopes with complete case analysis and return the peak of rolling slopes where all localy
samples are valid.TRUE
will return the peak of rolling slopes where the local targety
sample is valid.
Value
A list of class mnirs.kinetics
with components L$...
:
x
The
x
position of the local peak slope.y
The
y
value predicted from linear regression at the local peak slope.slope
The slope value in units of
y/x
.x_fitted
x
values within the window given byspan
, corresponding to the local peak slope.y_fitted
Predicted
y
values from the linear regression model within the window given byspan
, corresponding to the local peak slope.
Details
Uses the least squares formula on complete case analysis to calculate
local slopes within a rolling window specified by span
along x
, in units
of x
. i.e. span = 10
would include samples within a 10-second window for
time series data.
The direction of y
(upward or downward) will determine whether a positive
or negative peak slope is returned. Peak positive slope will be returned for
an overall slope >= 0.
Examples
y <- c(1, 3, 2, 5, 8, 7, 9, 12, 11, 14)
peak_slope(y, span = 3)
#> $x
#> [1] 4
#>
#> $y
#> [1] 5
#>
#> $slope
#> [1] 3
#>
#> $x_fitted
#> [1] 3 4 5
#>
#> $y_fitted
#> [1] 2 5 8
#>
y <- c(14, 11, 12, 9, 7, 8, 5, 2, 3, 1)
peak_slope(y, span = 3)
#> $x
#> [1] 1
#>
#> $y
#> [1] 14
#>
#> $slope
#> [1] -3
#>
#> $x_fitted
#> [1] 1 2
#>
#> $y_fitted
#> [1] 14 11
#>