diff --git a/NAMESPACE b/NAMESPACE index 92dcff7c..0fae7061 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -14,6 +14,7 @@ S3method(vcov,cyclopsFit) export(Multitype) export(aconfint) export(cacheCyclopsModelForJava) +export(clearCyclopsModelCache) export(convertToCyclopsData) export(convertToTimeVaryingCoef) export(coverage) @@ -38,6 +39,7 @@ export(getNumberOfRows) export(getNumberOfStrata) export(getUnivariableCorrelation) export(getUnivariableSeparability) +export(gradient) export(isInitialized) export(listGPUDevices) export(meanLinearPredictor) diff --git a/R/ModelFit.R b/R/ModelFit.R index f5a39020..a88037aa 100644 --- a/R/ModelFit.R +++ b/R/ModelFit.R @@ -511,6 +511,24 @@ logLik.cyclopsFit <- function(object, ...) { out } +#' @title Extract gradient +#' +#' @description +#' \code{gradient} returns the current gradient wrt the regression parameters of +#' the log-likelihood of the fit in a Cyclops model fit object +#' +#' @param object A Cyclops model fit object +#' +#' @export +gradient <- function(object) { + + .checkInterface(object$cyclopsData, testOnly = TRUE) + gradient <- .cyclopsGetLogLikelihoodGradient(object$interface) + names(gradient) <- names(coef(object)) + + return(gradient) +} + #' @method print cyclopsFit #' @title Print a Cyclops model fit object diff --git a/man/cyclopsLibraryFileName.Rd b/man/cyclopsLibraryFileName.Rd new file mode 100644 index 00000000..c4a3ed79 --- /dev/null +++ b/man/cyclopsLibraryFileName.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ModelFit.R +\name{cyclopsLibraryFileName} +\alias{cyclopsLibraryFileName} +\title{Get full path to installed Cyclops library} +\usage{ +cyclopsLibraryFileName() +} +\value{ +String +} +\description{ +\code{cyclopsLibraryFileName} returns the full path to the installed Cyclops .so/.dll file +} diff --git a/man/gradient.Rd b/man/gradient.Rd new file mode 100644 index 00000000..9977e0f4 --- /dev/null +++ b/man/gradient.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ModelFit.R +\name{gradient} +\alias{gradient} +\title{Extract gradient} +\usage{ +gradient(object) +} +\arguments{ +\item{object}{A Cyclops model fit object} +} +\description{ +\code{gradient} returns the current gradient wrt the regression parameters of +the log-likelihood of the fit in a Cyclops model fit object +} diff --git a/src/cyclops/CyclicCoordinateDescent.cpp b/src/cyclops/CyclicCoordinateDescent.cpp index 77f76f5e..9c93bc24 100644 --- a/src/cyclops/CyclicCoordinateDescent.cpp +++ b/src/cyclops/CyclicCoordinateDescent.cpp @@ -1386,6 +1386,7 @@ double CyclicCoordinateDescent::getLogLikelihoodGradient(int index) { checkAllLazyFlags(); double gradient, hessian; + computeNumeratorForGradient(index); modelSpecifics.computeGradientAndHessian(index, &gradient, &hessian, useCrossValidation); return gradient; diff --git a/tests/testthat/test-gradient.R b/tests/testthat/test-gradient.R new file mode 100644 index 00000000..7bfa364e --- /dev/null +++ b/tests/testthat/test-gradient.R @@ -0,0 +1,15 @@ +library("testthat") +library("survival") + +suppressWarnings(RNGversion("3.5.0")) + +test_that("gradient", { + + data <- Cyclops::createCyclopsData(Surv(stop, event) ~ (rx - 1) + size, data = bladder, modelType = "cox") + + fit <- Cyclops::fitCyclopsModel(data) + + gradientAtMode <- gradient(fit) +}) + +