R/ar1_cor.R

Defines functions dar1_cor.drho ar1_cor

Documented in ar1_cor dar1_cor.drho

#' Construct an AR(1) correlation matrix
#'
#' @param n number of events for each subject
#' @param rho autoregressive correlation
#'
#' @returns A n by n matrix
#' 
#' @export
#' @example inst/examples/ex_ar1_cor.R
ar1_cor <- function(n, rho) {
  ## Check if rho is between 0 and 1
  if (rho < 0 || rho > 1) stop("Error: rho must be between 0 and 1.")
  d <- c(n, n)
  rho^abs(.col(d) - .row(d)) / (1 - rho^2)
}


#' First derivative of AR(1) correlation matrix with respect to the auto-regressive coefficient
#'
#' @param n number of events for each subject
#' @param rho autoregressive correlation
#'
#' @returns A n by n inverse matrix
#' @export
dar1_cor.drho <- function(n, rho) {
  ## Check if rho is between 0 and 1
  if (rho < 0 || rho > 1) stop("Error: rho must be between 0 and 1.")
  d <- c(n, n)
  idx <- abs(.col(d) - .row(d))
  numerator <- idx * rho^(idx - 1) * (1 - rho^2) + 2 * rho * rho^(idx)
  return(numerator / (1 - rho^2)^2)
}

Try the FunSurv package in your browser

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

FunSurv documentation built on April 3, 2025, 9:13 p.m.