Adjust a regular time series for an assumed linear drift over a known period while maintaining an assumed linear trend over the entire period.

adjust_linear(x, mask)

Arguments

x

A vector of data with missing values.

mask

A logical mask that identifies a subgroup of x to correct for drift. Values outside the subgroup are used to determine the overall linear trend. If no mask is specified, no overall trend is assumed.

Value

The vector x with corrected values for the subgroup specified by mask.

Examples

# no trend, only drift x = rnorm(100, 10) + 0.1 * (1:100) newx = adjust_linear(x) if (interactive()) { plot(x, type = 'l') lines(newx, col = 'red') } # trend and drift x = rnorm(100, 10) - 0.05 * (1:100) mask = seq_along(x) %in% 20:80 x[mask] = x[mask] - 0.1 * cumsum(mask)[mask] newx = adjust_linear(x, mask) if (interactive()) { plot(x, type = 'l') lines(newx, col = 'red') }