Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
f0e84b1
Initial commit of attempt to implement relaxed_LASSO with linear regr…
Alexr626 Jun 19, 2023
3dd5196
Added custom functions to fit OLS on all lambdas from LASSO from firs…
Alexr626 Jun 26, 2023
c6a20f8
Deleted initial fitting procedures that resembled Post-LASSO or used …
Alexr626 Jun 26, 2023
d50d98f
(Hopefully) correct versions of relaxed LASSO implemented with correc…
Alexr626 Jun 26, 2023
f594328
Added comparisons of different procedure and relax = TRUE fit with no…
Alexr626 Jun 27, 2023
0512f77
Moving implementation of relaxed LASSO to helper_function.R
Alexr626 Jun 27, 2023
44277e3
Base code for single round of simulation.
Alexr626 Jun 29, 2023
3a5c970
Added option to fit initial LASSO using cv.glmnet to figure out why u…
Alexr626 Jun 30, 2023
4962c9e
Replaced cv.glmnet fit in inital LASSO with call to my function
Alexr626 Jun 30, 2023
1c9856c
Rewrote myRelaxed implementation to not rely on cv.lm for cross valid…
Alexr626 Jul 3, 2023
c39c776
Added parameter to fit cv.glmnet for lasso predictor selection instea…
Alexr626 Jul 3, 2023
aa6e3c8
Miscellaneous fixes
Alexr626 Jul 4, 2023
f0fc822
Miscellaneous
Alexr626 Jul 4, 2023
e445cdf
Cleaning/commenting
Alexr626 Jul 4, 2023
d898064
Refactored steps of one iteration of simulation into functions
Alexr626 Jul 5, 2023
b8e8dc6
Working on creating error handling for relaxed LASSO implementation
Alexr626 Jul 5, 2023
0980967
Committing commented out code for reference while debugging
Alexr626 Jul 5, 2023
3965dde
Refactoring order of code for simulation
Alexr626 Jul 5, 2023
33d771b
Saving commented out debug statements before deletion
Alexr626 Jul 6, 2023
17f6a44
Incredibly messy but working implementation of simulation. Only funct…
Alexr626 Jul 6, 2023
f17721c
Full working implementation of simulation complete
Alexr626 Jul 6, 2023
28f9d66
Fixed simulation such that MSE table now contains correct test-MSEs f…
Alexr626 Jul 7, 2023
f094dfd
Miscellaneous
Alexr626 Jul 10, 2023
183796f
Including .Rproj file
Alexr626 Jul 10, 2023
9ddc109
Created script version of simulation to run on Compute Canada
Alexr626 Jul 10, 2023
6152d16
Refactored code to include all simulation helper functions in helper …
Alexr626 Jul 10, 2023
9dea6b7
Adding simulation shell and resulting csvs and their directories
Alexr626 Jul 10, 2023
f006eac
Comparing different versions of current my relaxed LASSO implementation
Alexr626 Jul 11, 2023
e1e2c4e
git ignore files
Alexr626 Jul 11, 2023
6a64276
Initial commit of attempt to implement relaxed_LASSO with linear regr…
Alexr626 Jun 19, 2023
2d6f262
Added custom functions to fit OLS on all lambdas from LASSO from firs…
Alexr626 Jun 26, 2023
6e26780
Deleted initial fitting procedures that resembled Post-LASSO or used …
Alexr626 Jun 26, 2023
a45066f
(Hopefully) correct versions of relaxed LASSO implemented with correc…
Alexr626 Jun 26, 2023
8032c08
Added comparisons of different procedure and relax = TRUE fit with no…
Alexr626 Jun 27, 2023
e37c7c2
Moving implementation of relaxed LASSO to helper_function.R
Alexr626 Jun 27, 2023
a09d2cc
Base code for single round of simulation.
Alexr626 Jun 29, 2023
24b5678
Added option to fit initial LASSO using cv.glmnet to figure out why u…
Alexr626 Jun 30, 2023
98e8e8f
Replaced cv.glmnet fit in inital LASSO with call to my function
Alexr626 Jun 30, 2023
f2fa9c4
Rewrote myRelaxed implementation to not rely on cv.lm for cross valid…
Alexr626 Jul 3, 2023
b922dd4
Added parameter to fit cv.glmnet for lasso predictor selection instea…
Alexr626 Jul 3, 2023
112c9bf
Miscellaneous fixes
Alexr626 Jul 4, 2023
3b0f137
Miscellaneous
Alexr626 Jul 4, 2023
29c0e43
Cleaning/commenting
Alexr626 Jul 4, 2023
3b7cc0f
Refactored steps of one iteration of simulation into functions
Alexr626 Jul 5, 2023
ec0c021
Working on creating error handling for relaxed LASSO implementation
Alexr626 Jul 5, 2023
0b930ee
Committing commented out code for reference while debugging
Alexr626 Jul 5, 2023
5d25d0e
Refactoring order of code for simulation
Alexr626 Jul 5, 2023
cfbfcea
Saving commented out debug statements before deletion
Alexr626 Jul 6, 2023
00b8b3d
Incredibly messy but working implementation of simulation. Only funct…
Alexr626 Jul 6, 2023
3eaec70
Full working implementation of simulation complete
Alexr626 Jul 6, 2023
aec4e6c
Fixed simulation such that MSE table now contains correct test-MSEs f…
Alexr626 Jul 7, 2023
7c8367a
Miscellaneous
Alexr626 Jul 10, 2023
6844bb8
Including .Rproj file
Alexr626 Jul 10, 2023
3b8705b
Refactored code to include all simulation helper functions in helper …
Alexr626 Jul 10, 2023
c843d5f
Adding simulation shell and resulting csvs and their directories
Alexr626 Jul 10, 2023
30bb681
Comparing different versions of current my relaxed LASSO implementation
Alexr626 Jul 11, 2023
5918e5f
git ignore files
Alexr626 Jul 11, 2023
5e80937
adding session files
Alexr626 Jul 11, 2023
6be13f3
Merge branch 'relaxed_LASSO' of https://github.com/ntmv/MSc_criskcase…
Alexr626 Jul 11, 2023
376d051
Deleting unnecessary session log files
Alexr626 Jul 13, 2023
b2c6ec9
Removing .Rproj directory (main doesn't use .Rproj)
Alexr626 Jul 13, 2023
7caad61
Refactored repository structure and deleted repeated files
Alexr626 Jul 19, 2023
bbe5b7b
Adding results of latest simulation
Alexr626 Jul 19, 2023
c3b6c53
Refactoring directory pt 2
Alexr626 Jul 19, 2023
154a148
Scripts/markdown of current relaxed LASSO implementation and simulati…
Alexr626 Jul 19, 2023
32f80e5
Adding set.seed(NULL) before every return statement in each implement…
Alexr626 Jul 19, 2023
0e9b157
Added mse matrix that stores mse values calculated across all folds a…
Alexr626 Jul 20, 2023
85b451c
Ignore resulting csv files generated from simulations
Alexr626 Jul 26, 2023
50dc3e7
Adding initial implementation of casebase version of relaxed lasso
Alexr626 Jul 26, 2023
30c5fd7
Adding implementation of post lasso (old and new versions)
Alexr626 Jul 26, 2023
65859a9
Added initial rudimentary implementation of relaxed lasso and simulat…
Alexr626 Jul 28, 2023
d0fccf1
Edited implementation of post lasso
Alexr626 Jul 28, 2023
2419db8
Refactored simulation code into functions and added them to helper_fu…
Alexr626 Jul 28, 2023
c9b917d
Working simulation script in multinomial logistic for hazard modeling…
Alexr626 Jul 28, 2023
89d8d1b
Setting/deleting set seeds where necessary
Alexr626 Jul 28, 2023
3e5ec31
Cut down run time of relaxed lasso to less than 5 minutes (5 fold cv)
Alexr626 Jul 31, 2023
34a86a3
Miscellaneous additions to .gitignore
Alexr626 Aug 2, 2023
db68a54
Fixed places in simulation function where number of predictors and se…
Alexr626 Aug 2, 2023
2397e63
Created markdown file to test fitting functions in multinomial LR set…
Alexr626 Aug 2, 2023
3649117
Adding fix to mtool.multinom.cv such that lambda_max does not need to…
Alexr626 Aug 2, 2023
99e3834
Created markdown to investigate one iteration of simulation of MLR wi…
Alexr626 Aug 3, 2023
5c188dc
Deleted casebase investigation
Alexr626 Aug 3, 2023
4fb4c35
Created new script to simulate relaxed LASSO on non-survival data wit…
Alexr626 Aug 9, 2023
96df6be
Fixed relaxed implementation with non-survival data so that it uses f…
Alexr626 Aug 10, 2023
07604e6
Creating new version of relaxed in multinomial setting to fit unpenal…
Alexr626 Aug 12, 2023
a9b4241
Adding updated versions of relaxed implementation
Alexr626 Aug 12, 2023
74bffeb
Added code to fit non-survival multinomial data using relaxed_enet_nn…
Alexr626 Aug 14, 2023
1589ee9
Latest code updates on relaxed + figuring out differences between my …
Alexr626 Aug 16, 2023
3988614
Fixed Nirupama's implementation such that the correct amount of covar…
Alexr626 Aug 16, 2023
2e0c6e7
All versions of relaxed implementation attempts
Alexr626 Aug 21, 2023
f469d5d
Adding two scripts:
Alexr626 Aug 21, 2023
84ac269
Miscellaneous
Alexr626 Aug 21, 2023
b304e65
Deleting failed attempts at implementations of relaxed LASSO, fitting…
Alexr626 Aug 21, 2023
559e843
Organizing helper function files
Alexr626 Aug 21, 2023
a556d56
Fixed relaxed implementation in casebase setting and added script to …
Alexr626 Aug 22, 2023
e978f09
Miscellaneous
Alexr626 Aug 22, 2023
e56964e
Restructuring repository to improve organization and clarify workflow…
Alexr626 May 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
.DS_Store
../.DS_Store
.Rproj.user
.RData
.Rhistory
./sample_script/results
Binary file modified Papers/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions Papers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
52 changes: 50 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# 💡 Penalized Competing Risks Analysis using Case-base Sampling

📍 Repository with my code and my final report for my MSc. thesis
📍 Repository with my (Nirupama Tamvada) code and my final report for my MSc. thesis

🎯 We develop an alternative penalized cause-specific hazards model that extends on the `casebase` package for competing risks survival analysis of high-dimensional biological data

## 📂 Repository organization
## 📂 Repository organization

- `bash_script_template` - Template for bash scripts for Compute Canada. Creates individual bash scripts to be submitted to the cluster for one simulation run. Requires `runscripts` and `logs` folder to be setup

Expand Down Expand Up @@ -35,3 +35,51 @@ Genome-wide transcriptome profiling and advances in experimental technologies ha
💻 The optimization for the stochastic gradient descent in `mtool` was written by Dr. Yi Lian.

📌 The relaxed LASSO branch contains a relaxed LASSO implementation for the casebase penalized model (WIP) by Alex Romanus.



## 💡 Relaxed LASSO as a Penalization Method for Competing Risks Analysis using Case-Base Sampling

📍 relaxed_LASSO branch contains the addition of Relaxed LASSO penalization for fitting Competing Risks models using high-dimensional data

🎯 To address issues posed by fitting models to high-dimensional data, such as biased coefficient estimates, we developed a version of Relaxed LASSO regularization for efficient variable selection during model fitting

### 📂 Relaxed_LASSO-specific branch organization

- `practice` - Contains files created during development of relaxed LASSO method
- `development` - Files used to edit and check results of relaxed LASSO implementations for each data type (linear, multinomial, and Case-base) during development
- `simulation` - Mock simulation scripts and results

- `renv` - Virtual environment directory containing all packages needed to run relaxed LASSO project files (except mtool - more on that below)

- `simulation_final` - Scripts and results from final simulation used in report for each data type
- Note: Data in `final_results` sub-directories are results averaged over all iterations of each simulation for each data type

- `src`
- `final_relaxed_implementation.R` - Contains implementation of Relaxed LASSO used in final simulations in multinomial and Case-base data settings and its helper functions
- `practice_relaxed_implementation`- All versions of Relaxed LASSO implementations made during development and their helper functions
- simulation_helper_functions.R files - Helper functions intended for use during simulation, but replaced with scripts in final simulation runs (except in linear case)


### 💻 Instructions on running files from Relaxed LASSO project and installing repository packages
- To run any file and install the packages properly, please set your working directory to the project directory (casebase_relaxed_LASSO)

- Installing packages:
- Published packages - To install most of the packages needed for this project, use renv to install the virtual environment.

1) Install renv using the following command in your RStudio console:

install.packages("renv")

2) Use the following command to install all published packages:

renv::restore()

- mtool package - mtool is the only unpublished package required in this project, and hence must be downloaded from source. Use the following lines to install the package:

install_path = paste(getwd(), "/mtool_1.0.tar.gz", sep = "")
install.packages(install_path, repos = NULL, type = "source")

Finally, restart R to run the files using the required packages with:

.rs.restartR()
21 changes: 21 additions & 0 deletions bash_script_template/bash_scripts/linear_relaxed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

RUNSCRIPTS=~/projects/def-gcohenfr/ntamvada/simulations/runscripts/linear_relaxed
LOGS=~/projects/def-gcohenfr/ntamvada/simulations/logs/linear_relaxed

#SBATCH --time=2:00:00
#SBATCH --mem=128GB
#SBATCH --nodes=1
#SBATCH --cpus-per-task=8
#SBATCH --mail-user=<aromanus@gmail.com>
#SBATCH --output=${LOGS}/linear_relaxed.txt
#SBATCH --job-name="linear_relaxed"

module load StdEnv/2020 r/4.2.2

Rscript relaxed_simulation_script.R

">$RUNSCRIPTS/run.job
echo "Submitting $RUNSCRIPTS/${ID}.run.job to the cluster"
sbatch $RUNSCRIPTS/${ID}.run.job
done
13 changes: 13 additions & 0 deletions casebase_relaxed_LASSO.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX
80 changes: 80 additions & 0 deletions logs/mtool_error.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
✔ checking for file ‘/private/var/folders/wd/ykcm825s1_q8b6h4l_kczjx00000gn/T/RtmpIw5JYk/file50d37cb70d53/mtool/DESCRIPTION’
─ preparing ‘mtool’:
checking DESCRIPTION meta-information ... ✔ checking DESCRIPTION meta-information
─ cleaning src
─ installing the package to process help pages
-----------------------------------
─ installing *source* package ‘mtool’ ...
** using staged installation
** libs
clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/Rcpp/include' -I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/RcppArmadillo/include' -I/opt/R/arm64/include -I../inst/include/ -I../inst/include/decomp -I../inst/include/dictLearn -I../inst/include/linalg -I../inst/include/prox -DNDEBUG -DREMOVE_ -fPIC -falign-functions=64 -Wall -g -O2 -Wall -pedantic -c RcppExports.cpp -o RcppExports.o
clang++ -arch arm64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/Rcpp/include' -I'/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/RcppArmadillo/include' -I/opt/R/arm64/include -I../inst/include/ -I../inst/include/decomp -I../inst/include/dictLearn -I../inst/include/linalg -I../inst/include/prox -DNDEBUG -DREMOVE_ -fPIC -falign-functions=64 -Wall -g -O2 -Wall -pedantic -c main.cpp -o main.o
In file included from main.cpp:5:
In file included from ../inst/include/spams.h:25:
In file included from ../inst/include/dictLearn/dicts.h:36:
In file included from ../inst/include/prox/fista.h:24:
../inst/include/prox/project.h:2044:9: warning: variable 'num4' set but not used [-Wunused-but-set-variable]
long num4=0;
^
../inst/include/prox/project.h:2041:9: warning: variable 'num1' set but not used [-Wunused-but-set-variable]
long num1=0;
^
../inst/include/prox/project.h:2043:9: warning: variable 'num3' set but not used [-Wunused-but-set-variable]
long num3=0;
^
../inst/include/prox/project.h:2042:9: warning: variable 'num2' set but not used [-Wunused-but-set-variable]
long num2=0;
^
../inst/include/prox/project.h:2133:9: warning: variable 'num4' set but not used [ ../inst/include/prox/project.h:2133:9: warning: variable 'num4' set but not used [-Wunused-but-set-variable]
long num4=0;
^
../inst/include/prox/project.h:2130:9: warning: variable 'num1' set but not used [-Wunused-but-set-variable]
long num1=0;
^
../inst/include/prox/project.h:2131:9: warning: variable 'num2' set but not used [-Wunused-but-set-variable]
long num2=0;
^
../inst/include/prox/project.h:2132:9: warning: variable 'num3' set but not used [-Wunused-but-set-variable]
long num3=0;
^
8 warnings generated.
clang++ -arch arm64 -std=gnu++14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/opt/R/arm64/lib -o mtool.so RcppExports.o main.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.6.0/12.0.1 -L/opt/R/arm64/gfortran/lib -lgfortran -lemutls_w -lquadmath -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.6.0/12.0.1'
ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/lib'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mtool.so] Error 1
ERROR: compilation failed for package ‘mtool’
─ removing ‘/private/var/folders/wd/ykcm825s1_q8b6h4l_kczjx00000gn/T/RtmpiJ2JWV/Rinst7e1d419e1dc2/mtool’
-----------------------------------
ERROR: package installation failed

<system_command_status_error/rlib_error_3_0/rlib_error/error>
Error in `(function (command = NULL, args = character(), error_on_status = TRUE, …`:
! System command 'R' failed
---
Exit status: 1
stdout & stderr: <printed>
---
Backtrace:
1. devtools::]8;;ide:help:devtools::installinstall]8;;()
2. pkgbuild::build(pkg$path, dest_path = tempdir(), args = build_opts, …
3. withr::with_temp_libpaths(rcmd_build_tools(options$cmd, c(options$path, …
4. base::]8;;ide:help:base::forceforce]8;;(code)
5. pkgbuild::rcmd_build_tools(options$cmd, c(options$path, options$args), …
6. pkgbuild::with_build_tools({ …
7. base::withCallingHandlers(callr::rcmd_safe(..., env = env, spinner = FALSE, …
8. callr::rcmd_safe(..., env = env, spinner = FALSE, show = FALSE, …
9. callr:::run_r(options)
10. base::with(options, with_envvar(env, do.call(processx::run, c(list(bin, …
11. base::with.default(options, with_envvar(env, do.call(processx::run, …
12. base::]8;;ide:help:base::evaleval]8;;(]8;;ide:help:base::substitutesubstitute]8;;(expr), data, enclos = ]8;;ide:help:base::parent.frameparent.frame]8;;())
13. base::]8;;ide:help:base::evaleval]8;;(]8;;ide:help:base::substitutesubstitute]8;;(expr), data, enclos = ]8;;ide:help:base::parent.frameparent.frame]8;;())
14. callr:::with_envvar(env, do.call(processx::run, c(list(bin, args = real_cmdargs, …
15. base::]8;;ide:help:base::forceforce]8;;(code)
16. base::do.call(processx::run, c(list(bin, args = real_cmdargs, stdout_line_callback = real_callback(stdout), …
17. (function (command = NULL, args = character(), error_on_status = TRUE, …
18. base::throw(new_process_error(res, call = sys.call(), echo = echo, …
19. | base::]8;;ide:help:base::signalConditionsignalCondition]8;;(cond)
20. (function (e) …
21. ]8;;ide:help:base::asNamespaceasNamespace]8;;("callr")$err$throw(e)
Expand Down
2 changes: 2 additions & 0 deletions mtool/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
3 changes: 3 additions & 0 deletions mtool/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ License: GPL (>= 2)
Depends: Matrix
Imports: Rcpp (>= 1.0.5)
LinkingTo: Rcpp, RcppArmadillo



Binary file added mtool_1.0.tar.gz
Binary file not shown.
93 changes: 93 additions & 0 deletions practice/MNLogistic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Penalized Multinomial Logistic Regression
mtool.MNlogistic <- function(X, Y, offset, N_covariates,
regularization = 'l1', transpose = F,
lambda1, lambda2 = 0, lambda3 = 0,
learning_rate = 1e-4, tolerance = 1e-4,
niter_inner_mtplyr = 7, maxit = 100, ncores = -1,
group_id, group_weights,
groups, groups_var,
own_variables, N_own_variables) {
## Dimensions and checks
nx <- nrow(X)

if (!is.vector(Y)) {Y <- as.vector(Y)}
ny <- length(Y)

if (!is.vector(offset)) {offset <- as.vector(offset)}
noff <- length(offset)

if (nx == ny & nx == noff) {
n <- nx
} else {
stop('X, Y and offset have different number of observations.')
}

p <- ncol(X)

K <- length(unique(Y)) - 1

## regularization
pen1 <- c("l0", "l1", "l2", "linf", "l2-not-squared",
"elastic-net", "fused-lasso",
"group-lasso-l2", "group-lasso-linf",
"sparse-group-lasso-l2", "sparse-group-lasso-linf",
"l1l2", "l1linf", "l1l2+l1", "l1linf+l1", "l1linf-row-column",
"trace-norm", "trace-norm-vec", "rank", "rank-vec", "none")
pen2 <- c("graph", "graph-ridge", "graph-l2", "multi-task-graph")
pen3 <- c("tree-l0", "tree-l2", "tree-linf", "multi-task-tree")

if (regularization %in% pen1) { penalty <- 1 }
if (regularization %in% pen2) { penalty <- 2 }
if (regularization %in% pen3) { penalty <- 3 }
if (! regularization %in% c(pen1, pen2, pen3)) {
stop('The provided regularization is not supported.')
}

### check regularization-specific inputs
#### penalty = 1, call proximal(Flat), requires `group_id` in integer vector
if (penalty == 1) {
if (missing(group_id)) { group_id <- rep(0L, p) }
group_weights <- vector(mode = 'double')
groups <- matrix(NA)
groups_var <- matrix(NA)
own_variables <- vector(mode = 'integer')
N_own_variables <- vector(mode = 'integer')
}

#### penalty = 2, call proximalGraph
#### requires `groups` and `groups_var` in integer matrices and `group_weights` in double vector
if (penalty == 2) {
if (missing(groups)) { stop('Required input `groups` is missing.') }
if (missing(groups_var)) { stop('Required input `groups_var` is missing.') }
if (missing(group_weights)) { stop('Required input `group_weights` is missing.') }
group_id <- rep(0L, p)
own_variables <- vector(mode = 'integer')
N_own_variables <- vector(mode = 'integer')
}

#### penalty = 3, call proximalGraph
#### requires `own_variables` and `N_own_variables` in integer vectors, `group_weights` in double vector
#### and `groups` in integer matrix
if (penalty == 3) {
if (missing(groups)) { stop('Required input `groups` is missing.') }
if (missing(own_variables)) { stop('Required input `own_variables` is missing.') }
if (missing(N_own_variables)) { stop('Required input `N_own_variables` is missing.') }
if (missing(group_weights)) { stop('Required input `group_weights` is missing.') }
group_id <- rep(0L, p)
groups_var <- matrix(NA)
}

## call mtool main function
result <- MultinomLogistic(X = X, Y = Y, offset = offset, K = K, reg_p = p - N_covariates,
penalty = penalty, regul = regularization, transpose = transpose,
grp_id = group_id, etaG = group_weights,
grp = groups, grpV = groups_var,
own_var = own_variables, N_own_var = N_own_variables,
lam1 = lambda1, lam2 = lambda2, lam3 = lambda3,
learning_rate = learning_rate, tolerance = tolerance,
niter_inner = niter_inner_mtplyr * nx, maxit = maxit,
ncores = ncores)
nzc <- length(result$`Sparse Estimates`@i)
return(list(coefficients = result$`Sparse Estimates`,
no_non_zero = nzc))
}
93 changes: 93 additions & 0 deletions practice/MNLogistic_New.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Penalized Multinomial Logistic Regression (LASSO) with decreased learning rate
mtool.MNlogistic_new <- function(X, Y, offset, N_covariates,
regularization = 'l1', transpose = F,
lambda1, lambda2 = 0, lambda3 = 0,
learning_rate = 1e-3, tolerance = 1e-4,
niter_inner_mtplyr = 7, maxit = 100, ncores = -1,
group_id, group_weights,
groups, groups_var,
own_variables, N_own_variables) {
## Dimensions and checks
nx <- nrow(X)

if (!is.vector(Y)) {Y <- as.vector(Y)}
ny <- length(Y)

if (!is.vector(offset)) {offset <- as.vector(offset)}
noff <- length(offset)

if (nx == ny & nx == noff) {
n <- nx
} else {
stop('X, Y and offset have different number of observations.')
}

p <- ncol(X)

K <- length(unique(Y)) - 1

## regularization
pen1 <- c("l0", "l1", "l2", "linf", "l2-not-squared",
"elastic-net", "fused-lasso",
"group-lasso-l2", "group-lasso-linf",
"sparse-group-lasso-l2", "sparse-group-lasso-linf",
"l1l2", "l1linf", "l1l2+l1", "l1linf+l1", "l1linf-row-column",
"trace-norm", "trace-norm-vec", "rank", "rank-vec", "none")
pen2 <- c("graph", "graph-ridge", "graph-l2", "multi-task-graph")
pen3 <- c("tree-l0", "tree-l2", "tree-linf", "multi-task-tree")

if (regularization %in% pen1) { penalty <- 1 }
if (regularization %in% pen2) { penalty <- 2 }
if (regularization %in% pen3) { penalty <- 3 }
if (! regularization %in% c(pen1, pen2, pen3)) {
stop('The provided regularization is not supported.')
}

### check regularization-specific inputs
#### penalty = 1, call proximal(Flat), requires `group_id` in integer vector
if (penalty == 1) {
if (missing(group_id)) { group_id <- rep(0L, p) }
group_weights <- vector(mode = 'double')
groups <- matrix(NA)
groups_var <- matrix(NA)
own_variables <- vector(mode = 'integer')
N_own_variables <- vector(mode = 'integer')
}

#### penalty = 2, call proximalGraph
#### requires `groups` and `groups_var` in integer matrices and `group_weights` in double vector
if (penalty == 2) {
if (missing(groups)) { stop('Required input `groups` is missing.') }
if (missing(groups_var)) { stop('Required input `groups_var` is missing.') }
if (missing(group_weights)) { stop('Required input `group_weights` is missing.') }
group_id <- rep(0L, p)
own_variables <- vector(mode = 'integer')
N_own_variables <- vector(mode = 'integer')
}

#### penalty = 3, call proximalGraph
#### requires `own_variables` and `N_own_variables` in integer vectors, `group_weights` in double vector
#### and `groups` in integer matrix
if (penalty == 3) {
if (missing(groups)) { stop('Required input `groups` is missing.') }
if (missing(own_variables)) { stop('Required input `own_variables` is missing.') }
if (missing(N_own_variables)) { stop('Required input `N_own_variables` is missing.') }
if (missing(group_weights)) { stop('Required input `group_weights` is missing.') }
group_id <- rep(0L, p)
groups_var <- matrix(NA)
}

## call mtool main function
result <- MultinomLogistic(X = X, Y = Y, offset = offset, K = K, reg_p = p - N_covariates,
penalty = penalty, regul = regularization, transpose = transpose,
grp_id = group_id, etaG = group_weights,
grp = groups, grpV = groups_var,
own_var = own_variables, N_own_var = N_own_variables,
lam1 = lambda1, lam2 = lambda2, lam3 = lambda3,
learning_rate = learning_rate, tolerance = tolerance,
niter_inner = niter_inner_mtplyr * nx, maxit = maxit,
ncores = ncores)
nzc <- length(result$`Sparse Estimates`@i)
return(list(coefficients = result$`Sparse Estimates`,
no_non_zero = nzc))
}
Loading