Last updated: 2020-05-28

Checks: 7 0

Knit directory: stat34800/analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.6.1). 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 65cf780. 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:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    .sos/
    Ignored:    exams/
    Ignored:    temp/

Untracked files:
    Untracked:  analysis/hmm_final2019.Rmd
    Untracked:  analysis/neanderthal.Rmd
    Untracked:  analysis/pca_cell_cycle.Rmd
    Untracked:  data/reduced.chr12.90-100.data.txt
    Untracked:  data/reduced.chr12.90-100.snp.txt
    Untracked:  homework/fdr.aux
    Untracked:  homework/fdr.log
    Untracked:  tempETA_1_parBayesC.dat
    Untracked:  temp_ETA_1_parBayesC.dat
    Untracked:  temp_mu.dat
    Untracked:  temp_varE.dat
    Untracked:  tempmu.dat
    Untracked:  tempvarE.dat

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/svd_zip.Rmd) and HTML (docs/svd_zip.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 65cf780 Matthew Stephens 2020-05-28 workflowr::wflow_publish(“svd_zip.Rmd”)
html f2098f0 stephens999 2018-05-21 Build site.
Rmd d63b9eb stephens999 2018-05-21 workflowr::wflow_publish(“svd_zip.Rmd”)
html f300de6 stephens999 2018-04-19 Build site.
Rmd bb95415 stephens999 2018-04-19 wflow_publish(“analysis/svd_zip.Rmd”)

Introduction

Here I run both SVD and non-negative matrix factorization on the zipcode training data.

SVD

Here we read in the “zipcode” training data, and extract the 2s and 3s.

z = read.table("../data/zip.train.txt")
sub = (z[,1] == 2) | (z[,1]==3)
z23 = as.matrix(z[sub,])

Now we run svd (excluding the first column which are the labels)

z23.svd = svd(z23[,-1])

Plot the first two two singular vectors, colored by group, we see the second sv separates the groups reasonably well.

plot(z23.svd$u[,1],z23.svd$u[,2],col=z23[,1])

Version Author Date
f300de6 stephens999 2018-04-19

And a histogram suggests a mixture of two Gaussians might be a reasonable start:

hist(z23.svd$u[,2],breaks=seq(-0.07,0.07,length=20))

Version Author Date
f300de6 stephens999 2018-04-19

Plot the images corresponding to first two eigenvectors

We put the eigenvector in the form of an image before plotting. Note that I have not been very careful with the limits

n = sqrt(length(z23.svd$v[,1]))
plot_zipcode_image = function(v){
  n = sqrt(length(v))
  m = matrix(v,nrow=n)
  m = apply(m,1,rev) # I had to experiment here to get the images to appear the "right way up"
  m = apply(m,1,rev) # note that applying rev twice does not result in no change...
  m = apply(m,2,rev)
  image(m)
}
plot_zipcode_image(z23.svd$v[,1])

plot_zipcode_image(z23.svd$v[,2])

Try non-negative matrix factorization

You will need to install NNLM to run this.

#library(devtools)
#devtools::install_github('linxihui/NNLM')

library(NNLM)
summary(as.vector(z23[1,]))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
-1.0000 -1.0000 -1.0000 -0.4028  0.3190  3.0000 
z23_nonneg = z23[,-1] + 1
z23.nmf = nnmf(z23_nonneg,k = 2)
plot_zipcode_image(z23.nmf$H[1,])

plot_zipcode_image(z23.nmf$H[2,])

plot(z23.nmf$W[,1],z23.nmf$W[,2],col=z23[,1])

Try doing 6 and 3 instead - you see it separates them very nicely.

sub = (z[,1] == 6) | (z[,1]==3)
z36 = as.matrix(z[sub,])
summary(as.vector(z36[1,]))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
-1.0000 -1.0000 -0.9870 -0.2932  0.7530  6.0000 
z36_nonneg = z36[,-1] + 1
z36.nmf = nnmf(z36_nonneg,k = 2)
plot_zipcode_image(z36.nmf$H[1,])

plot_zipcode_image(z36.nmf$H[2,])

plot(z36.nmf$W[,1],z36.nmf$W[,2],col=z36[,1])

Try doing all the digits. Here the results are messier. It is possible this is a local optimum problem as nmf is sensitive to initialization.

z_nonneg = as.matrix(z[,-1]) + 1
z.nmf = nnmf(z_nonneg,k = 12,max.iter = 100)
par(mfcol=c(3,4),mai=rep(0.2,4))
for(i in 1:12){
  plot_zipcode_image(z.nmf$H[i,])
}

Try running multiple times. Here I plot the mse trace for 3 different runs.

z.nmf2 = nnmf(z_nonneg,k = 12,max.iter = 100)
z.nmf3 = nnmf(z_nonneg,k = 12,max.iter = 100)
par(mfcol=c(1,1),mai=rep(0.5,4))
plot(z.nmf$mse,type="l")
lines(z.nmf2$mse,col=2)
lines(z.nmf3$mse,col=3)


sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.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] NNLM_0.4.4

loaded via a namespace (and not attached):
 [1] workflowr_1.6.1 Rcpp_1.0.4.6    rprojroot_1.3-2 digest_0.6.25  
 [5] later_1.0.0     R6_2.4.1        backports_1.1.5 git2r_0.26.1   
 [9] magrittr_1.5    evaluate_0.14   stringi_1.4.6   rlang_0.4.5    
[13] fs_1.3.2        promises_1.1.0  whisker_0.4     rmarkdown_2.1  
[17] tools_3.6.0     stringr_1.4.0   glue_1.4.0      httpuv_1.5.2   
[21] xfun_0.12       yaml_2.2.1      compiler_3.6.0  htmltools_0.4.0
[25] knitr_1.28