Last updated: 2020-02-01

Checks: 7 0

Knit directory: SMF/

This reproducible R Markdown analysis was created with workflowr (version 1.5.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(20190719) 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 version displayed above was the version of the Git repository at the time these results were generated.

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:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    analysis/.DS_Store
    Ignored:    data/.DS_Store

Untracked files:
    Untracked:  analysis/LDANMF.Rmd
    Untracked:  analysis/SMFrealdata.Rmd
    Untracked:  analysis/bigpicture.Rmd
    Untracked:  analysis/poissonmeanscle.Rmd
    Untracked:  data/external_data/

Unstaged changes:
    Modified:   analysis/SmoothMFpoisson2.Rmd
    Modified:   analysis/index.Rmd
    Modified:   analysis/poissonmean.Rmd
    Modified:   analysis/ptfopt.Rmd
    Modified:   analysis/tforder.Rmd
    Modified:   code/simulations.R

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 R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
Rmd bce77fb Dongyue Xie 2020-02-02 wflow_publish(“analysis/outlierWavelet.Rmd”)

Introduction

Investigate how the outlier will influecne the wavelet smoothing and how to automatically account for outliers.

Generate outliers from Huber’s contamination model:

set.seed(12345)
n = 256
x = seq(-2*pi,2*pi,length.out = n)
f = 1.5*sin(x) + sin(2*x)
e_z = rbinom(n,1,0.05)
e = e_z*rnorm(n,0,1) + (1-e_z)*rnorm(n,0,0.2)
y = f+e
library(smashr)
out = smash.gaus(y)
plot(y,col='grey80')
lines(out)

plot(y,col='grey80')
lines(ti.thresh(y))

Try robust scatter plot smoothing

plot(y,col='grey80')
lines(runmed(y,11))

Let’s see the outliers

plot(x,y)
points(x[which(e_z==1)],y[which(e_z==1)],pch=16,col=4)

Running mad estimate of variance:

library(wavethresh)
Loading required package: MASS
WaveThresh: R wavelet software, release 4.6.8, installed
Copyright Guy Nason and others 1993-2016
Note: nlevels has been renamed to nlevelsWT
library(caTools)
J = log2(n)
x.w = wd(y, filter.number=1,family='DaubExPhase', type = "station")
win.size = 25
sigma.est = runmad(accessD(x.w, J - 1), win.size, endrule = "func")


plot(x,sigma.est,col='grey80')
points(x[which(e_z==1)],sigma.est[which(e_z==1)],pch=16,col=4)

plot(abs(y[which(e_z==1)] - f[which(e_z==1)]),sigma.est[which(e_z==1)])
abline(0,1)

# reduce window size

win.size = 10
sigma.est = runmad(accessD(x.w, J - 1), win.size, endrule = "func")
Warning in runmed(x, k): 'k' must be odd! Changing 'k' to 11
plot(x,sigma.est,col='grey80')
points(x[which(e_z==1)],sigma.est[which(e_z==1)],pch=16,col=4)

plot(abs(y[which(e_z==1)] - f[which(e_z==1)]),sigma.est[which(e_z==1)])
abline(0,1)

plot(y,col='grey80')
lines(smash.gaus(y,sigma.est))

# 

win.size = 3
sigma.est = runmad(accessD(x.w, J - 1), win.size, endrule = "func")


plot(x,sigma.est,col='grey80')
points(x[which(e_z==1)],sigma.est[which(e_z==1)],pch=16,col=4)

plot(abs(y[which(e_z==1)] - f[which(e_z==1)]),sigma.est[which(e_z==1)])
abline(0,1)

plot(x,y,col='grey80')
lines(x,smash.gaus(y,sigma.est))
lines(x,f,col=4)

Summary 1

can not achieve robustness via change running MAD window size

Let’s look at the finest level NDWT coefficients

plot(x,accessD(x.w, J - 1))
points(x[which(e_z==1)],accessD(x.w, J - 1)[which(e_z==1)],pch=16,col=4)

Let’s directly use the absolute deviation as the sigma.est. Much better….

plot(y,col='grey80')
lines(smash.gaus(y,abs(accessD(x.w, J - 1)-median(accessD(x.w, J - 1)))))

plot(y,col='grey80')
lines(ti.thresh(y,abs(accessD(x.w, J - 1)-median(accessD(x.w, J - 1)))))

Let’s increase the number of outliers

set.seed(12345)
e_z = rbinom(n,1,0.1)
e = e_z*rnorm(n,0,1) + (1-e_z)*rnorm(n,0,0.2)
y = f+e
x.w = wd(y, filter.number=1,family='DaubExPhase', type = "station")
plot(y,col='grey80')
lines(smash.gaus(y,abs(accessD(x.w, J - 1)-median(accessD(x.w, J - 1)))))

plot(y,col='grey80')
lines(ti.thresh(y,abs(accessD(x.w, J - 1)-median(accessD(x.w, J - 1)))))

Let’s increase the variance of outliers

set.seed(12345)
e_z = rbinom(n,1,0.1)
e = e_z*rnorm(n,0,3) + (1-e_z)*rnorm(n,0,0.2)
y = f+e
x.w = wd(y, filter.number=1,family='DaubExPhase', type = "station")
plot(y,col='grey80')
lines(smash.gaus(y,abs(accessD(x.w, J - 1)-median(accessD(x.w, J - 1)))))

plot(y,col='grey80')
lines(ti.thresh(y,abs(accessD(x.w, J - 1)-median(accessD(x.w, J - 1)))))

Let’s increase the variance of outliers and variance of random errors

set.seed(12345)
e_z = rbinom(n,1,0.1)
e = e_z*rnorm(n,0,4) + (1-e_z)*rnorm(n,0,1)
y = f+e
x.w = wd(y, filter.number=1,family='DaubExPhase', type = "station")
plot(y,col='grey80')
lines(smash.gaus(y,abs(accessD(x.w, J - 1)-median(accessD(x.w, J - 1)))))

plot(y,col='grey80')
lines(ti.thresh(y,abs(accessD(x.w, J - 1)-median(accessD(x.w, J - 1)))))

Pseudo-data approach

http://stat.snu.ac.kr/heeseok/html/paper/robusttech.pdf

smooth_outlier = function(y,maxiter=10,tol=1e-4){
  f_hat = ti.thresh(y)
  niter = 1
  while(niter<=maxiter){
    niter = niter + 1
    y_tilde = f_hat + tanh(y-f_hat)/2
    f_hat_new = ti.thresh(y_tilde)
    if(norm(f_hat-f_hat_new,'2')<=tol){
      break
    }else{
      f_hat = f_hat_new
    }
  }
  f_hat_new
}

plot(y,col='grey80')
lines(smooth_outlier(y))

Too slow. Does not work well.


sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/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] caTools_1.17.1.2 wavethresh_4.6.8 MASS_7.3-51.4    smashr_1.2-7    

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2        compiler_3.6.1    later_1.0.0      
 [4] git2r_0.26.1      workflowr_1.5.0   bitops_1.0-6     
 [7] iterators_1.0.12  tools_3.6.1       digest_0.6.21    
[10] evaluate_0.14     lattice_0.20-38   rlang_0.4.0      
[13] Matrix_1.2-17     foreach_1.4.7     yaml_2.2.0       
[16] parallel_3.6.1    xfun_0.10         stringr_1.4.0    
[19] knitr_1.25        fs_1.3.1          rprojroot_1.3-2  
[22] grid_3.6.1        data.table_1.12.6 glue_1.3.1       
[25] R6_2.4.0          rmarkdown_1.16    mixsqp_0.1-97    
[28] ashr_2.2-38       magrittr_1.5      whisker_0.4      
[31] backports_1.1.5   promises_1.1.0    codetools_0.2-16 
[34] htmltools_0.4.0   httpuv_1.5.2      stringi_1.4.3    
[37] doParallel_1.0.15 pscl_1.5.2        truncnorm_1.0-8  
[40] SQUAREM_2017.10-1