Skip to content

Commit

Permalink
version 0.1-1
Browse files Browse the repository at this point in the history
  • Loading branch information
Walter K Kremers authored and cran-robot committed Dec 14, 2022
0 parents commit c5f0566
Show file tree
Hide file tree
Showing 47 changed files with 5,347 additions and 0 deletions.
28 changes: 28 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Title: Relaxed Lasso Model for Data Which Might Have Long Run Times
Using 'glmnet'
Package: glmnetr
Version: 0.1-1
Date: 2022-12-10
Depends: R (>= 3.4.0)
Suggests: R.rsp
VignetteBuilder: R.rsp
Imports: glmnet, survival, Matrix
ByteCompile: Yes
Authors@R: c(person(c("Walter", "K"), "Kremers",
role=c("aut", "cre"),
email="kremers.walter@mayo.edu",
comment = c(ORCID = "0000-0001-5714-3473") ))
Author: Walter K Kremers [aut, cre] (<https://orcid.org/0000-0001-5714-3473>)
Maintainer: Walter K Kremers <kremers.walter@mayo.edu>
Description:
For some datasets, for example when the design matrix is not of full rank, 'glmnet' may have very long run times when fitting the relaxed lasso model, in particular when fitting a Cox based model, making it difficult to get solutions either from glmnet() or cv.glmnet(). In this package, 'glmnetr', we provide a workaround and solve for the non penalized relaxed model where gamma=0 for model structures analogue to R functions like glm() or coxph() of the survival package. If you are not fitting relaxed lasso models, or if you are able to get convergence using 'glmnet', then this package may not be of much benefit to you. Note, while this package may allow one to fit relaxed lasso models that have difficulties converging using 'glmnet', this package does not afford the full function and versatility of 'glmnet'.
In addition to fitting the relaxed lasso model this package also includes the function cv.glmnetr() to perform a cross validation to identify hyper-parameters for a lasso fit, much like the cv.glmnet() function of the 'glmnet' package. Additionally, the package includes the function nested.glmnetr() to perform a nested cross validation to assess the fit of a cross validated derived lasso model fit. If though you are fitting not a relaxed lasso model but an elastic-net model, then the R-packages 'nestedcv' <https://cran.r-project.org/package=nestedcv>, 'glmnetSE' <https://cran.r-project.org/package=glmnetSE> or others may provide greater functionality when performing a nested CV.
As with the 'glmnet' package, this package passes most relevant output to the output object and tabular and graphical summaries can be generated using the summary and plot functions. Use of the 'glmnetr' has many similarities to the 'glmnet' package and it is recommended that the user of 'glmnetr' first become familiar with the 'glmnet' package <https://cran.r-project.org/package=glmnet>, with the "An Introduction to 'glmnet'" and "The Relaxed Lasso" being especially helpful in this regard.
License: GPL-3
NeedsCompilation: no
Copyright: Mayo Foundation for Medical Education and Research
RoxygenNote: 7.2.2
Encoding: UTF-8
Packaged: 2022-12-14 04:55:24 UTC; KREMERS
Repository: CRAN
Date/Publication: 2022-12-14 12:10:02 UTC
46 changes: 46 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
b0449858e65c8715025b6e1d85deea10 *DESCRIPTION
2a76af39933b57cbdbc0cf13ac10cff7 *NAMESPACE
5c46759bfc58d26c720cd915d6cbd8c3 *R/aicreg_221210.R
7e6e50b2e8d2b7d2cda36453e10fa350 *R/cv.glmnetr_221210.R
e8c3bd2e17ef03c645776f9a162a1412 *R/cv.stepreg_221210.R
8cea1c89d17d588d693b55dd6bef6a1e *R/nested.glmnetr_221210.R
a4c5b1ae7e3250afb05963affacf3d48 *R/plot.cv.glmnetr_221210.R
d1fa231910e74d66e015b5e7f756d941 *R/stepreg_221210.R
f4687bfaaa216fefb0e6cc1e15a6761f *R/summary.cv.glmnetr_221210.R
8dd59260970752d3ab7a2cf2eb1061ee *R/summary.nested.glmnetr_221210.R
f5d149cab5f8ce4d60cf1646f104a8c1 *build/vignette.rds
3e2fa930b4a5fa9cae38a9031452b001 *inst/doc/Using_glmnetr_221210.pdf
21f21b35db3650c28c96a2a3d81e6229 *inst/doc/Using_glmnetr_221210.pdf.asis
0f391666e2c999322e290f2b6eff039a *inst/doc/Using_stepreg_221210.pdf
11d2a2dbc54b3dc80a7e6389c7d5a9eb *inst/doc/Using_stepreg_221210.pdf.asis
6ca50f4d9614426d53d8120d4c027c5f *man/aicreg.Rd
17c182f1e5a4c37139874e346c117b9b *man/best.preds.Rd
485b50a94c4291cb27e9ce22f28dc527 *man/cox.sat.dev.Rd
a7f947deebcd5a1b8944d07530b17255 *man/cv.glmnetr.Rd
d84018224c03d0bd3c9cdc8962d73312 *man/cv.stepreg.Rd
7742477f221e66885b215f2ab966ff17 *man/difftime1.Rd
2119bad8b8f02a9f8b3b72cec7e79c0c *man/difftime2.Rd
a71175d4ec61e43c3e41bcfd18e0171d *man/getlamgam.Rd
3929c4c86970c3f066f3c6fbd4d7fdd9 *man/glmnetr.Rd
3636b0461e2f3b8e02c77015bc130bf2 *man/glmnetr.compcv.Rd
58eee208132d1ebfa6b33e6cac29030f *man/glmnetr.compcv0.Rd
df2f5e039d39825329273852c43d1927 *man/glmnetr.simdata.Rd
51c9b247c287611b6bffd687e43ba75e *man/glmnetr_devratio.Rd
393e08556131f6403eeac6aca2dfe86a *man/glmnetrll_1fold.Rd
a597569ce9ae9bea632361f8f086d9ec *man/nested.glmnetr.Rd
d142c711b4d4f9d48e565c07e79a6016 *man/plot.cv.glmnetr.Rd
836056de9e509b3f2b90465de92cc446 *man/plot.glmnetr.Rd
9da56afccdcbf0b2a3cf60546e8f38bf *man/plot.nested.glmnetr.Rd
f9a3f3171c8b3f819b23f0a931ac9507 *man/predict.cv.glmnetr.Rd
e7dbc6b99f8a8b405643cd65850537f4 *man/predict.glmnetr.Rd
57b0e6eec57cd481aed126bb449299ce *man/predict.nested.glmnetr.Rd
983cbd56398dd0b9ba9bff45df8c702b *man/preds_1.Rd
3893835b9edf7599e904e038bd20ae8c *man/stepreg.Rd
e956b8609acd1d2f510b76e75219e229 *man/summary.cv.glmnetr.Rd
0475a3e17a2fe9bafbe59266ab5312c6 *man/summary.cv.stepreg.Rd
78d37f69662be66ed5b915d2947b6cc9 *man/summary.nested.glmnetr.Rd
5a785a4600b976e27b14fa878207ad73 *man/summary.stepreg.Rd
b6e4c057355f8987468a0526ff7a18d1 *vignettes/Using_glmnetr_221210.Rmd
21f21b35db3650c28c96a2a3d81e6229 *vignettes/Using_glmnetr_221210.pdf.asis
c3c30a39b346cf887dd894ee83484796 *vignettes/Using_stepreg_221210.Rmd
11d2a2dbc54b3dc80a7e6389c7d5a9eb *vignettes/Using_stepreg_221210.pdf.asis
51 changes: 51 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Generated by roxygen2: do not edit by hand

S3method(plot,cv.glmnetr)
S3method(plot,glmnetr)
S3method(plot,nested.glmnetr)
S3method(predict,cv.glmnetr)
S3method(predict,glmnetr)
S3method(predict,nested.glmnetr)
S3method(summary,cv.glmnetr)
S3method(summary,cv.stepreg)
S3method(summary,nested.glmnetr)
S3method(summary,stepreg)
export(aicreg)
export(best.preds)
export(cox.sat.dev)
export(cv.glmnetr)
export(cv.stepreg)
export(difftime2)
export(glmnetr)
export(glmnetr.compcv)
export(glmnetr.simdata)
export(nested.glmnetr)
export(stepreg)
importFrom(Matrix,rankMatrix)
importFrom(glmnet,cv.glmnet)
importFrom(graphics,abline)
importFrom(graphics,axis)
importFrom(graphics,lines)
importFrom(stats,binomial)
importFrom(stats,cor)
importFrom(stats,cov)
importFrom(stats,deviance)
importFrom(stats,formula)
importFrom(stats,glm)
importFrom(stats,glm.control)
importFrom(stats,glm.fit)
importFrom(stats,lm)
importFrom(stats,logLik)
importFrom(stats,pchisq)
importFrom(stats,predict)
importFrom(stats,rbeta)
importFrom(stats,residuals)
importFrom(stats,rnorm)
importFrom(stats,runif)
importFrom(stats,t.test)
importFrom(stats,var)
importFrom(survival,Surv)
importFrom(survival,concordance)
importFrom(survival,concordancefit)
importFrom(survival,coxph)
importFrom(survival,coxph.control)
101 changes: 101 additions & 0 deletions R/aicreg_221210.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#' Identify model based upon AIC criteria from a stepreg() putput
#'
#' @param xs predictor input - an n by p matrix, where n (rows) is sample size, and p (columns)
#' the number of predictors. Must be in matrix form for complete data, no NA's, no Inf's, etc.,
#' and not a data frame.
#' @param start start time, Cox model only - class numeric of length same as number of patients (n)
#' @param y_ output vector: time, or stop time for Cox model, Y_ 0 or 1 for binomal (logistic), numeric for gaussian.
#' Must be a vector of length same as number of sample size.
#' @param event event indicator, 1 for event, 0 for census, Cox model only.
#' Must be a numeric vector of length same as sample size.
#' @param steps_n number of steps done in stepwise regression fitting
#' @param family model family, "cox", "binomial" or "gaussian"
#' @param object A stepreg() output. If NULL it will be derived.
#' @param time Indicate whether or not to update progress in the console. Default of
#' 0 suppresses these updates. The option of 1 provides these updates. In fitting
#' clinical data with non full rank design matrix we have found some R-packages to
#' take a vary long time or seemingly be caught in infinite loops. Therefore we allow
#' the user to track the package and judge whether things are moving forward or
#' if the process should be stopped.
#'
#' @return The identified model in form of a glm() or coxph() output object, and an
#' entry of the stepreg() output object.
#'
#' @export
#'
#' @examples
#' set.seed(18306296)
#' sim.data=glmnetr.simdata(nrows=100, ncols=100, beta=c(0,1,1))
#' # this gives a more intersting case but takes longer to run
#' xs=sim.data$xs
#' # this will work numerically
#' xs=sim.data$xs[,c(2,3,50:55)]
#' y_=sim.data$yt
#' event=sim.data$event
#' cox.aic.fit = aicreg(xs, NULL, y_, event, family="cox", steps_n=40)
#' summary(cox.aic.fit)
#'
#' y_=sim.data$yt
#' norm.aic.fit = aicreg(xs, NULL, y_, NULL, family="gaussian", steps_n=40)
#' summary(norm.aic.fit)
#'
aicreg = function(xs, start, y_, event, steps_n=steps_n, family=family, object=NULL, time=0) {
if (!is.null(object)) {
# stepreg.fit.all.best = as.matrix( cv.stepreg.fit.all$stepreg.fit.all.best )
if (inherits(object,"cv.stepreg")) {
stepreg.fit.all.best = object$stepreg.fit.all.best
} else if (inherits(object,"nested.glmnetr")) {
stepreg.fit.all.best = object$stepreg.fit.all.best
}
} else {
if (time >= 1) { cat(paste0("\n", " ########## Derive stepwise model on all data ################################################" , "\n")) }
stepreg.fit.all = stepreg(xs, start, y_, event, steps_n=steps_n, family=family)
class(stepreg.fit.all) = "data.frame"
stepreg.fit.all.best = stepreg.fit.all[(stepreg.fit.all$best==1) , ]
}
aic = 2*stepreg.fit.all.best[,1] - 2*stepreg.fit.all.best[,5] ;# plot(c(1:steps_n),aic)
if (family == "binomial") { aic = aic + 2
} else if (family == "gaussian") { aic = aic + 4 }
best.aic.all.index = which.min( aic )
aic.fit.all.d = stepreg.fit.all.best[best.aic.all.index,]
temp_ = colnames(aic.fit.all.d)
aic.fit.all.n = as.numeric(aic.fit.all.d)
names(aic.fit.all.n) = temp_
if ((length(temp_)-7)%%2 == 1) {
intercept = TRUE
nvars = (length(temp_)-8)/2
} else {
intercept = FALSE
nvars = (length(temp_)-7)/2
}
# temp_[8:(7+nvars)]
if (!intercept) { aic.fit.all.vars = temp_[(nvars+8):(length(temp_))][aic.fit.all.n[8:(nvars+7)]==1]
} else { aic.fit.all.vars = ( temp_[(nvars+9):(length(temp_))][aic.fit.all.n[8:(nvars+7)]==1] ) }
# if (family=="cox") {
# beta = stepreg.fit.all.best [ best.aic.all.index , (nvars+8):(2*nvars+7) ] ## get beta, the regression estimate
# beta = matrix(beta, nrow=nvars, ncol=1)
# } else {
# beta = stepreg.fit.all.best [ best.aic.all.index , (nvars+8):(2*nvars+8) ] ## get beta, the regression estimate
# beta = matrix(beta, nrow=(nvars+1), ncol=1)
# }
if (family=="cox") {
if (is.null(start)) {
dataf = as.data.frame( cbind( y_, event, xs ) )
form1 = formula( paste("Surv(" , colnames(dataf)[1] , ", " , colnames(dataf)[2] , ") ~ ", paste(aic.fit.all.vars, collapse = " + " ) ) )
} else {
dataf = as.data.frame( cbind( start, y_, event, xs ) )
form1 = formula( paste("Surv(" , colnames(dataf)[1] , ", " , colnames(dataf)[2], ", " , colnames(dataf)[3] , ") ~ ", paste(aic.fit.all.vars, collapse = " + " ) ) )
}
aic.fit.all = coxph(form1, dataf)
} else {
dataf = as.data.frame( cbind( y_, xs ) )
form1 = formula( paste("y_ ~ " , paste(aic.fit.all.vars, collapse = " + " ) ) )
aic.fit.all = glm(form1, family=family, data=dataf)
}
aic.fit.all$stepaic
aic.fit.all$stepaic = aic
aic.fit.all$aic.fit.n = aic.fit.all.n
aic.fit.all$stepreg.fit.aic = stepreg.fit.all.best
summary(aic.fit.all)
return(aic.fit.all)
}

0 comments on commit c5f0566

Please sign in to comment.