repeated_median_line: Fit a level and trend to a time series using Repeated Median...

View source: R/initialize_states.R

repeated_median_lineR Documentation

Fit a level and trend to a time series using Repeated Median Regression

Description

Fit a level and trend to a time series using Repeated Median Regression

Usage

repeated_median_line(x)

Arguments

x

Numeric vector representing a time series

Value

A list of intercept, slope, fitted values, and residuals

References

https://en.wikipedia.org/wiki/Repeated_median_regression

Examples


y <- 1:100 + rnorm(100)
tulip:::repeated_median_line(y)

y <- 1:28 + rt(n = 28, df = 1)
y_idx <- 1:length(y)
lm_fit <- lm(y ~ 1 + y_idx)
rm_fit <- tulip:::repeated_median_line(y)
plot(y, pch = 19, cex = 0.5)
abline(a = 0, b = 1, lty = 2) # ideal fit
abline(
  a = lm_fit$coefficients[1],
  b = lm_fit$coefficients[2],
  col = "darkblue"
)
abline(
  a = rm_fit$intercept,
  b = rm_fit$slope,
  col = "darkorange"
)

y <- as.numeric(RobStatTM::resex)
y_idx <- 1:length(y)
lm_fit <- lm(y ~ 1 + y_idx)
rm_fit <- tulip:::repeated_median_line(y)
plot(y, pch = 19, cex = 0.5)
abline(
  a = lm_fit$coefficients[1],
  b = lm_fit$coefficients[2],
  col = "darkblue"
)
abline(a = rm_fit$intercept, b = rm_fit$slope, col = "darkorange")


timradtke/heuristika documentation built on April 24, 2023, 1:55 a.m.