Last updated: 2022-04-14

Checks: 7 0

Knit directory: stat34800/analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20180411) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version b035721. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/

Untracked files:
    Untracked:  analysis/currency_analysis.Rmd
    Untracked:  analysis/haar.Rmd
    Untracked:  analysis/stocks_analysis.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/sudden_changes.Rmd) and HTML (docs/sudden_changes.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd b035721 Matthew Stephens 2022-04-14 workflowr::wflow_publish(“sudden_changes.Rmd”)

library(smooth)
Warning: package 'smooth' was built under R version 4.1.1
Loading required package: greybox
Warning: package 'greybox' was built under R version 4.1.1
Package "greybox", v1.0.5 loaded.
This is package "smooth", v3.1.6
library(genlasso)
Loading required package: Matrix
Loading required package: igraph
Warning: package 'igraph' was built under R version 4.1.1

Attaching package: 'igraph'
The following objects are masked from 'package:stats':

    decompose, spectrum
The following object is masked from 'package:base':

    union
library(smashr)

Introduction

I wanted to illustrate the idea that some smoothing techniques are better than others at dealing with changes that occur on different “scales” (eg a combination of smooth changes as sudden changes).

Simulated data

Here I set up \(E(y)=\mu\) as a function that varies smoothly except for a sudden change in the middle, and simulate some data with that mean:

t = seq(0,1,length=1024)
mu = (t-0.5)^2 - 0.2*(t>0.5)

y = rnorm(1024,mu,0.05)
plot(t,mu, type="l", col=2, lwd=2)
points(t,y)

Compare some smoothers

Simple moving average (sma)

I tried a sliding window of sizes 10 and 50:

y.sma.10 = sma(y,order=10) 
y.sma.50 = sma(y,order=50)
plot(t,mu, type="l", col=2, lwd=2, main="sma, order 10 (green) and 50 (blue)")

lines(t,y.sma.10$fitted,col=3,lwd=2)
lines(t,y.sma.50$fitted,col=4,lwd=2)

Trend-filtering

y.tf.0 = trendfilter(y,t,ord=0)
y.tf.0.cv = cv.trendfilter(y.tf.0) # performs 5-fold CV
Fold 1 ... Fold 2 ... Fold 3 ... Fold 4 ... Fold 5 ... 
y.tf.1 = trendfilter(y,t,ord=1)
y.tf.1.cv = cv.trendfilter(y.tf.1) # performs 5-fold CV
Fold 1 ... Fold 2 ... Fold 3 ... Fold 4 ... Fold 5 ... 
plot(t,mu, type="l", col=2, lwd=2, main="trendfilter (order 0 = green; order 1=blue)")

lines(t,predict(y.tf.0, y.tf.0.cv$lambda.min)$fit,col=3,lwd=2)
lines(t,predict(y.tf.1, y.tf.1.cv$lambda.min)$fit,col=4,lwd=2)

Wavelet with EB shrinkage

y.smash = smash(y,filter.number=1) 
plot(t,mu, type="l", col=2, lwd=2, main="Wavelet (haar)")
lines(t,y.smash,col=3,lwd=2)


sessionInfo()
R version 4.1.0 Patched (2021-07-20 r80657)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.2

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] smashr_1.2-9  genlasso_1.5  igraph_1.3.0  Matrix_1.3-4  smooth_3.1.6 
[6] greybox_1.0.5

loaded via a namespace (and not attached):
 [1] wavethresh_4.6.8  statmod_1.4.36    zoo_1.8-9         xfun_0.28        
 [5] ashr_2.2-54       lattice_0.20-45   vctrs_0.3.8       generics_0.1.2   
 [9] htmltools_0.5.2   yaml_2.2.1        utf8_1.2.2        rlang_0.4.12     
[13] pracma_2.3.8      mixsqp_0.3-43     jquerylib_0.1.4   nloptr_1.2.2.3   
[17] later_1.3.0       pillar_1.6.4      glue_1.5.0        lifecycle_1.0.1  
[21] stringr_1.4.0     workflowr_1.7.0   caTools_1.18.2    evaluate_0.14    
[25] knitr_1.36        fastmap_1.1.0     httpuv_1.6.3      invgamma_1.1     
[29] irlba_2.3.3       fansi_0.5.0       highr_0.9         Rcpp_1.0.7       
[33] promises_1.2.0.1  truncnorm_1.0-8   fs_1.5.0          texreg_1.38.6    
[37] digest_0.6.28     stringi_1.7.5     rprojroot_2.0.2   grid_4.1.0       
[41] bitops_1.0-7      tools_4.1.0       magrittr_2.0.2    tibble_3.1.6     
[45] crayon_1.4.2      whisker_0.4       pkgconfig_2.0.3   ellipsis_0.3.2   
[49] MASS_7.3-54       data.table_1.14.2 SQUAREM_2021.1    rmarkdown_2.11   
[53] httr_1.4.2        R6_2.5.1          git2r_0.29.0      compiler_4.1.0