Last updated: 2020-03-08

Checks: 7 0

Knit directory: 2019-feature-selection/

This reproducible R Markdown analysis was created with workflowr (version 1.6.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(20190522) 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:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    .Ruserdata/
    Ignored:    .drake/
    Ignored:    .vscode/
    Ignored:    analysis/rosm.cache/
    Ignored:    data/
    Ignored:    inst/Benchmark for Filter Methods for Feature Selection in High-Dimensional  Classification Data.pdf
    Ignored:    inst/study-area-map/._study-area.qgs
    Ignored:    inst/study-area-map/study-area.qgs~
    Ignored:    log/
    Ignored:    renv/library/
    Ignored:    renv/staging/
    Ignored:    reviews/
    Ignored:    rosm.cache/

Untracked files:
    Untracked:  code/06-modeling/project/

Unstaged changes:
    Modified:   .Rprofile
    Modified:   _drake.R
    Modified:   analysis/_site.yml
    Modified:   analysis/report-defoliation.Rmd
    Modified:   code/02-hyperspectral-processing.R
    Modified:   code/03-sentinel-processing.R
    Modified:   code/06-modeling/paper/02-param-sets.R
    Modified:   code/06-modeling/paper/03-learner.R
    Modified:   code/06-modeling/paper/04-filter-wrapper.R
    Modified:   code/06-modeling/paper/07-tune-wrapper.R
    Modified:   code/06-modeling/paper/08-train.R
    Modified:   code/071-benchmark-matrix-buffer2.R
    Modified:   code/09-reports.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 b7e12c1 pat-s 2020-03-08 wflow_publish(knitr_in(“analysis/inspect-glmnet-models.Rmd”), view =

Last update:

date()
[1] "Sun Mar  8 18:49:29 2020"

Inspect fitted models during CV

Inspect Ridge regression on VI task in detail because the error is enourmus.

First extract the models.

models = benchmark_models_new_penalized_buffer2[[8]][["results"]][["vi_buffer2"]][["Ridge-CV"]][["models"]]

Then look at the fold performances

benchmark_models_new_penalized_buffer2[[8]][["results"]][["vi_buffer2"]][["Ridge-CV"]][["measures.test"]][["rmse"]]
[1] 4.131716e+07 6.053429e+01 6.886208e+01 7.046982e+01

We see a high error on Fold 1 (= Laukiz 2). The others are also quite high but not “out of bounds”.

Because this models used the internal optimization of the lambda sequence (cv.glmnet), let’s look at the value which was chosen for prediction (parameter s which defaults to s="lambda.1se"):

purrr::map_dbl(models, ~ .x[["learner.model"]][["lambda.1se"]])
[1] 4.211054e+08 4.370350e+16 3.622142e+16 4.163667e+16

It seems that the lambda.1se value for Fold 1 is way smaller than for the other 3 folds. However, all values seem to be quite high.

Let’s look at the full lambda sequence

purrr::map_int(models, ~ length(.x[["learner.model"]][["lambda"]]))
[1]   5 100 100 100

Interestingly, the lambda length of fold 1 is not 100 (default) but only 5.

Train/predict manually for Fold 1 via {glmnet}

To inspect further, let’s refit a {glmnet} model directly on the training data of Fold 1 and inspect what glmnet::cv.glmnet estimates for the lambda sequence:

train_inds_fold1 = benchmark_models_new_penalized_buffer2[[8]][["results"]][["vi_buffer2"]][["Ridge-CV"]][["pred"]][["instance"]][["train.inds"]][[1]]

obs_train_f1 = as.matrix(task_new_buffer2[[2]]$env$data[train_inds_fold1, getTaskFeatureNames(task_new_buffer2[[2]])])
target_f1 = getTaskTargets(task_new_buffer2[[2]])[train_inds_fold1]

Fit cv.glmnet

set.seed(1)
modf1 = glmnet::cv.glmnet(obs_train_f1, target_f1, nfolds = 5)

modf1$lambda.1se
[1] 0.8509291

Ok, a value of 0.85 is very different to what happened during the CV (4.211054e+08).

Predict on Laukiz 2 now.

pred_inds_fold1 = benchmark_models_new_penalized_buffer2[[8]][["results"]][["vi_buffer2"]][["Ridge-CV"]][["pred"]][["instance"]][["test.inds"]][[1]]

obs_pred_f1 = as.matrix(task_new_buffer2[[2]]$env$data[pred_inds_fold1, getTaskFeatureNames(task_new_buffer2[[2]])])

pred = predict(modf1, newx = obs_pred_f1)

Calculate the error

truth = task_new_buffer2[[2]]$env$data[pred_inds_fold1, "defoliation"]

mlr:::measureRMSE(truth, pred)
[1] 52.42488

Ok, RMSE of 52. Still not great but at least not out of bounds.

Train/predict manually for Fold 1 via {mlr}

Now let’s fit the same subset (= Fold 1) via mlr

lrn = makeLearner("regr.cvglmnet", nfolds = 5)
task_f1 = subsetTask(task_new_buffer2[[2]], train_inds_fold1)

set.seed(1)
mod = train(lrn, task_f1)

Check lambda sequence and lambda.1se:

mod$learner.model$lambda
  [1] 4.9839093118 4.5411525452 4.1377290693 3.7701446232 3.4352153661
  [6] 3.1300403011 2.8519761478 2.5986144474 2.3677607022 2.1574153674
 [11] 1.9657565324 1.7911241401 1.6320056082 1.4870227281 1.3549197274
 [16] 1.2345523933 1.1248781613 1.0249470858 0.9338936117 0.8509290772
 [21] 0.7753348833 0.7064562693 0.6436966414 0.5865124060 0.5344082604
 [26] 0.4869329034 0.4436751264 0.4042602510 0.3683468845 0.3356239625
 [31] 0.3058080548 0.2786409100 0.2538872196 0.2313325788 0.2107816301
 [36] 0.1920563710 0.1749946123 0.1594485733 0.1452836015 0.1323770068
 [41] 0.1206169984 0.1099017167 0.1001383511 0.0912423360 0.0831366183
 [46] 0.0757509903 0.0690214811 0.0628898030 0.0573028462 0.0522122193
 [51] 0.0475738296 0.0433475017 0.0394966291 0.0359878575 0.0327907954
 [56] 0.0298777515 0.0272234944 0.0248050342 0.0226014233 0.0205935752
 [61] 0.0187640987 0.0170971479 0.0155782844 0.0141943526 0.0129333654
 [66] 0.0117844009 0.0107375072 0.0097836166 0.0089144671 0.0081225304
 [71] 0.0074009472 0.0067434674 0.0061443964 0.0055985451 0.0051011858
 [76] 0.0046480105 0.0042350941 0.0038588600 0.0035160495 0.0032036934
 [81] 0.0029190861 0.0026597625 0.0024234765 0.0022081816 0.0020120128
 [86] 0.0018332711 0.0016704084 0.0015220139 0.0013868024 0.0012636027
 [91] 0.0011513477 0.0010490651 0.0009558691 0.0008709523 0.0007935793
 [96] 0.0007230799 0.0006588435 0.0006003136 0.0005469834 0.0004983909
mod$learner.model$lambda.1se
[1] 0.8509291

Check for equality between {mlr} and {glmnet} directly

all.equal(modf1$lambda.1se, mod$learner.model$lambda.1se)
[1] TRUE

Ok, so {mlr} works correctly when fitting the model for a single fold. Hence, somethings goes wrong during CV?


sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS:   /opt/spack/opt/spack/linux-centos7-x86_64/gcc-9.2.0/r-3.6.1-j25wr6zcofibs2zfjwg37357rjj26lqb/rlib/R/lib/libRblas.so
LAPACK: /opt/spack/opt/spack/linux-centos7-x86_64/gcc-9.2.0/r-3.6.1-j25wr6zcofibs2zfjwg37357rjj26lqb/rlib/R/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] tidyselect_0.2.5  glmnet_3.0-2      Matrix_1.2-15     mlr_2.17.0.9001  
[5] ParamHelpers_1.12 drake_7.10.0     

loaded via a namespace (and not attached):
 [1] storr_1.2.1       shape_1.4.4       xfun_0.5         
 [4] purrr_0.3.3       lattice_0.20-38   splines_3.6.1    
 [7] colorspace_1.4-0  vctrs_0.2.1       htmltools_0.3.6  
[10] yaml_2.2.0        XML_3.98-1.17     survival_2.43-3  
[13] rlang_0.4.4       R.oo_1.23.0       later_1.0.0      
[16] pillar_1.4.3      txtq_0.1.4        glue_1.3.1       
[19] R.utils_2.8.0     foreach_1.4.4     stringr_1.4.0    
[22] munsell_0.5.0     gtable_0.2.0      workflowr_1.6.0  
[25] R.methodsS3_1.7.1 codetools_0.2-16  evaluate_0.13    
[28] knitr_1.23        parallelMap_1.4   httpuv_1.4.5.1   
[31] parallel_3.6.1    Rcpp_1.0.3        promises_1.0.1   
[34] backports_1.1.5   scales_1.0.0      filelock_1.0.2   
[37] checkmate_1.9.1   fs_1.3.1          fastmatch_1.1-0  
[40] ggplot2_3.2.1     digest_0.6.23     stringi_1.3.1    
[43] BBmisc_1.11       dplyr_0.8.3       rprojroot_1.3-2  
[46] grid_3.6.1        tools_3.6.1       magrittr_1.5     
[49] base64url_1.4     lazyeval_0.2.1    tibble_2.1.3     
[52] crayon_1.3.4      whisker_0.3-2     pkgconfig_2.0.3  
[55] zeallot_0.1.0     data.table_1.12.6 iterators_1.0.10 
[58] assertthat_0.2.1  rmarkdown_1.13    R6_2.4.1         
[61] igraph_1.2.4.1    git2r_0.26.1      compiler_3.6.1