Skip to contents

This function wraps the custom approach for fitting distributions to doubly censored data using fitdistrplus and primarycensoreddist.

Usage

fitdistdoublecens(
  censdata,
  distr,
  pwindow = 1,
  D = Inf,
  dprimary = stats::dunif,
  dprimary_name = NULL,
  dprimary_args = list(),
  ...
)

Arguments

censdata

A data frame with columns 'left' and 'right' representing the lower and upper bounds of the censored observations. Unlike fitdistrplus::fitdistcens() NA is not supported for either the upper or lower bounds.

distr

A character string naming the distribution to be fitted.

pwindow

Primary event window

D

Maximum delay (truncation point). If finite, the distribution is truncated at D. If set to Inf, no truncation is applied. Defaults to Inf.

dprimary

Function to generate the probability density function (PDF) of primary event times. This function should take a value x and a pwindow parameter, and return a probability density. It should be normalized to integrate to 1 over [0, pwindow]. Defaults to a uniform distribution over [0, pwindow]. Users can provide custom functions or use helper functions like dexpgrowth for an exponential growth distribution. See primary_dists.R for examples.

dprimary_name

A string specifying the name of the primary event distribution function. If NULL, the function name is extracted using .extract_function_name(). Used to determine if a analytical solution exists for the primary censored distribution. Must be set if dprimary is passed a pre-assigned variable rather than a function name.

dprimary_args

List of additional arguments to be passed to dprimary. For example, when using dexpgrowth, you would pass list(min = 0, max = pwindow, r = 0.2) to set the minimum, maximum, and rate parameters

...

Additional arguments to be passed to fitdistrplus::fitdist().

Value

An object of class "fitdist" as returned by fitdistrplus::fitdist.

Details

This function temporarily assigns and then removes functions from the global environment in order to work with fitdistr. Users should be aware of this behaviour, especially if they have existing functions with the same names in their global environment.

See also

Modelling wrappers for external fitting packages pcd_as_cmdstan_data(), pcd_cmdstan_model()

Examples

# Example with normal distribution
set.seed(123)
n <- 1000
true_mean <- 5
true_sd <- 2
pwindow <- 2
swindow <- 2
D <- 10
samples <- rprimarycensoreddist(
  n, rnorm,
  mean = true_mean, sd = true_sd,
  pwindow = pwindow, swindow = swindow, D = D
)

delay_data <- data.frame(
  left = samples,
  right = samples + swindow
)

fit_norm <- fitdistdoublecens(
  delay_data,
  distr = "norm",
  start = list(mean = 0, sd = 1),
  D = D, pwindow = pwindow
)

summary(fit_norm)
#> Fitting of the distribution ' .pcens_dist ' by maximum likelihood 
#> Parameters : 
#>      estimate Std. Error
#> mean 5.007126 0.07883554
#> sd   2.020160 0.06962184
#> Loglikelihood:  -1398.874   AIC:  2801.747   BIC:  2811.563 
#> Correlation matrix:
#>           mean        sd
#> mean 1.0000000 0.3248076
#> sd   0.3248076 1.0000000
#>