knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "70%" )
The goal of detrendr is to estimate smooth quantile trends in evenly spaced series. Missing values are allowed. The methods implemented in this package are described here: https://arxiv.org/abs/1904.10582.
You can install the development version of detrendr from Github with:
``` {r load} library(devtools) install_github("halleybrantley/detrendr")
The \r{detrendr} functions are much faster when used with the Gurobi solver. Directions for obtaining a license and installing the gurobi package are available here: https://cran.r-project.org/web/packages/prioritizr/vignettes/gurobi_installation.html ## Examples Estimate the 5th and 20th quantile trends using a fixed value of the smoothing parameter $\lambda$. ```r library(detrendr) n <- 100 x <- seq(1, n, 1) y <- sin(x*2*pi/n) + rnorm(n, 0, .4) lambda <- 10 k <- 3 tau <- c(0.05, .2) trend <- get_trend(y, tau, lambda, k) plot(y~x, type="l", col="grey") lines(trend[,1]~x, col="red") lines(trend[,2]~x, col="blue")
Use eBIC criterion to choose smoothing parameter:
trend_fit <- get_trend_BIC(y, tau, k, plot_lambda = TRUE) trend <- trend_fit$trend plot(y~x, type="l", col="grey") lines(trend[,1]~x, col="red") lines(trend[,2]~x, col="blue")
Use ADMM algorithm for long time series:
library(ggplot2) library(splines) set.seed(987651) overlap <- 50 window_size <- 100 n <- window_size*3 - overlap*2 df.data <- generate_peaks(n) df.data$x <- seq(1, n, 1) tau <- c(0.05, 0.1) lambda <- length(df.data$y) max_iter <- 25 trend1 <- get_trend(df.data$y, tau, lambda, k=3) trend2 <- get_trend_windows(df.data$y, tau, lambda, k=3, window_size, use_gurobi = TRUE, # Change to FALSE if you do # not have gurobi installed overlap, max_iter=max_iter, update = 1, rho = 1, eps_abs = 0.01, scale=FALSE) df_no <- rbind(data.frame(x=df.data$x , method = "Single Fit", trend1), data.frame(x=df.data$x , method = "Windows", trend2)) ggplot(df.data, aes(x=x, y=y)) + geom_line(col="grey") + geom_line(data = df_no, aes(y=X1, col = "0.05", linetype = method))+ geom_line(data = df_no, aes(y=X2, col = "0.10", linetype = method))+ scale_color_brewer(palette = "Set1")+ labs(col="Quantile", linetype = "", x = "") + theme_bw()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.