R/coxN.R

Defines functions coxN.GLMnet coxN.prodlim coxN.glm coxN.CauseSpecificCox coxN.phreg coxN.default coxN.coxph coxN.cph coxN

Documented in coxN coxN.CauseSpecificCox coxN.coxph coxN.cph coxN.default coxN.glm coxN.GLMnet coxN.phreg coxN.prodlim

### coxN.R --- 
#----------------------------------------------------------------------
## Author: Thomas Alexander Gerds
## Created: Apr 27 2025 (07:34) 
## Version: 
## Last-Updated: May  8 2025 (14:14) 
##           By: Thomas Alexander Gerds
##     Update #: 5
#----------------------------------------------------------------------
## 
### Commentary: 
## 
### Change Log:
#----------------------------------------------------------------------
## 
### Code:
## * coxN
#' @title Extract the number of observations from a Cox model
#' @description Extract the number of observations from a Cox model
#' @name coxN 
#' @param object The fitted Cox regression model object either
#'     obtained with \code{coxph} (survival package), \code{cph}
#'     (rms package), or \code{phreg} (mets package).
#' 
#' @author Brice Ozenne broz@@sund.ku.dk
#'

#' @rdname coxN
#' @export
coxN <- function(object){
  UseMethod("coxN") 
} 

## ** coxN.cph
#' @rdname coxN
#' @method coxN cph
#' @export
coxN.cph <- function(object){
    return(sum(object$n))
}

## ** coxN.coxph
#' @rdname coxN
#' @method coxN coxph
#' @export
coxN.coxph <- function(object){
  return(object$n)
}

## ** coxN.phreg
#' @rdname coxN
#' @method coxN default
#' @export
coxN.default <- function(object){
  return(stats::nobs(object))
}

#' @rdname coxN
#' @method coxN phreg
#' @export
coxN.phreg <- function(object){
  return(NROW(object$model.frame))
}

## ** coxN.CSC
#' @rdname coxN
#' @method coxN CauseSpecificCox
#' @export
coxN.CauseSpecificCox <- function(object){
  return(sapply(object$models,coxN))
}

## ** coxN.CSC
#' @rdname coxN
#' @method coxN glm
#' @export
coxN.glm <- function(object){
  return(stats::nobs(object))
}

## ** coxN.prodlim
#' @rdname coxN
#' @method coxN prodlim
#' @export
coxN.prodlim <- function(object){
  return(NROW(object$model.response))
}

## ** coxN.GLMnet
#' @rdname coxN
#' @method coxN GLMnet
#' @export
coxN.GLMnet <- function(object){
    if(object$cv){
        return(object$fit$glmnet.fit$nobs)
    } else{
        return(object$fit$nobs)
    }
}


######################################################################
### coxN.R ends here

Try the riskRegression package in your browser

Any scripts or data that you put into this service are public.

riskRegression documentation built on June 8, 2025, 11:33 a.m.