Skip to contents

Computes the peak directional linear regression slope for upward or downward trending response variable y, respectively using rolling_slope() within a moving window.

Usage

peak_slope(
  y,
  x = seq_along(y),
  width,
  align = c("center", "left", "right"),
  na.rm = FALSE
)

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).

width

A numeric scalar defining the window width in units of x for rolling calculations.

align

Specifies the window alignment of width as "center" (the default), "left", or "right". Where "left" is forward looking, and "right" is backward looking by the window width 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 local y samples are valid. TRUE will return the peak of rolling slopes where the local target y 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

The range of x values given by width corresponding to the local peak slope.

y_fitted

The range of y values given by width predicted from linear regression, corresponding to the local peak slope.

Details

Uses the least squares formula on complete case analysis to calculate local slopes within a rolling window along x specified by width in units of x. i.e. x = 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, width = 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, width = 3)
#> $x
#> [1] 1
#> 
#> $y
#> [1] 14
#> 
#> $slope
#> [1] -3
#> 
#> $x_fitted
#> [1] 1 2
#> 
#> $y_fitted
#> [1] 14 11
#>