## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4
)

## ----quick-tour---------------------------------------------------------------
library(maskedhaz)

# A three-component series system with a mixed-distribution setup
model <- dfr_series_md(components = list(
  dfr_weibull(shape = 2, scale = 100),   # wear-out
  dfr_gompertz(a = 0.001, b = 0.02),     # aging
  dfr_exponential(0.005)                 # random failure
))

model

## ----data---------------------------------------------------------------------
set.seed(42)
rdata_fn <- rdata(model)
df <- rdata_fn(
  theta = c(2, 100, 0.001, 0.02, 0.005),  # matches the parameter layout
  n = 300,
  tau = 50,       # right-censoring time
  p = 0.4         # masking probability for non-failed components
)
head(df, 3)
table(df$omega)

## ----loglik-------------------------------------------------------------------
ll_fn <- loglik(model)
true_par <- c(2, 100, 0.001, 0.02, 0.005)
ll_fn(df, par = true_par)

## ----fit----------------------------------------------------------------------
solver <- fit(model)
result <- solver(df, par = c(1.5, 120, 0.0005, 0.015, 0.01))

## ----fit-compare--------------------------------------------------------------
rbind(truth = true_par, estimate = round(coef(result), 4))

## ----mle-methods-basic--------------------------------------------------------
# 95% Wald confidence intervals — note the Gompertz rows (3 and 4)
# cross zero. That's a known limitation of Wald intervals near
# a boundary: the asymptotic normal approximation is too symmetric
# for parameters bounded below by zero when the SE is comparable
# to the estimate.
confint(result)

## ----ecosystem-load-----------------------------------------------------------
library(algebraic.mle)   # se(), bias(), observed_fim(), mse(), ...
library(algebraic.dist)  # as_dist(), sampler(), expectation(), cdf(), ...

## ----algebraic-mle------------------------------------------------------------
se(result)             # standard errors (from algebraic.mle)
bias(result)           # first-order asymptotic bias — zero for Fisherian MLEs
observed_fim(result)   # observed Fisher information — the negative Hessian

## ----algebraic-dist-----------------------------------------------------------
# Convert to an explicit algebraic.dist object
mle_dist <- as_dist(result)   # from algebraic.dist
class(mle_dist)

# The expected value of the sampling distribution is the MLE itself
expectation(mle_dist)         # from algebraic.dist

# Draw 5 samples from the MLE's sampling distribution
set.seed(7)
samp <- sampler(mle_dist)     # from algebraic.dist
round(samp(5), 4)

## ----diagnostics--------------------------------------------------------------
ccp_fn <- conditional_cause_probability(model)
ccp_fn(t = c(25, 50), par = coef(result))

## ----diagnostics-marginal-----------------------------------------------------
cp_fn <- cause_probability(model)
set.seed(1)
cp_fn(par = coef(result), n_mc = 5000)

## ----assumptions--------------------------------------------------------------
assumptions(model)

