dynamichazard (version 0.6.0)

PF_forward_filter: Forward Particle Filter

Description

Functions to only use the forward particle filter. Useful for log-likelihood evaluation though there is an \(O(d^2)\) variance of the estimate where \(d\) is the number of time periods. The number of particles specified in the control argument has no effect.

The function does not alter the .Random.seed to make sure the same rng.kind is kept after the call. See PF_EM for model details.

Usage

PF_forward_filter(x, N_fw, N_first, ...)

# S3 method for PF_EM PF_forward_filter(x, N_fw, N_first, seed, ...)

# S3 method for formula PF_forward_filter(x, N_fw, N_first, data, model = "logit", by, max_T, id, a_0, Q_0, Q, R, fixed_effects, control = PF_control(...), seed = NULL, trace = 0, type = "RW", Fmat, ...)

Arguments

x

an PF_EM or formula object.

N_fw

number of particles.

N_first

number of time zero particles to draw.

...

optional way to pass arguments to control.

seed

.GlobalEnv$.Random.seed to set. Not seed as in set.seed function. Can be used with the .Random.seed returned by PF_EM.

data

data.frame or environment containing the outcome and covariates.

model

either 'logit' for binary outcomes or 'exponential' for piecewise constant exponential distributed arrival times.

by

interval length of the bins in which parameters are fixed.

max_T

end of the last interval interval.

id

vector of ids for each row of the in the design matrix.

a_0

vector \(a_0\) for the initial coefficient vector for the first iteration (optional). Default is estimates from static model (see static_glm).

Q_0

covariance matrix for the prior distribution.

Q

initial covariance matrix for the state equation.

R

\(R\) matrix in the model. See PF_EM.

fixed_effects

values for the fixed parameters.

control
trace

argument to get progress information. Zero will yield no info and larger integer values will yield incrementally more information.

type

type of state model. Either "RW" for a [R]andom [W]alk or "VAR" for [V]ector [A]uto[R]egression.

Fmat

\(F\) matrix in the model. See PF_EM.

Value

An object of class PF_clouds.

Methods (by class)

  • PF_EM: Forward particle filter using the estimates of an PF_EM call.

  • formula: Forward particle filter with formula input.

Examples

Run this code
# NOT RUN {
# head-and-neck cancer study data. See Efron, B. (1988) doi:10.2307/2288857
is_censored <- c(
  6, 27, 34, 36, 42, 46, 48:51, 51 + c(15, 30:28, 33, 35:37, 39, 40, 42:45))
head_neck_cancer <- data.frame(
  id = 1:96,
  stop = c(
    1, 2, 2, rep(3, 6), 4, 4, rep(5, 8),
    rep(6, 7), 7, 8, 8, 8, 9, 9, 10, 10, 10, 11, 14, 14, 14, 15, 18, 18, 20,
    20, 37, 37, 38, 41, 45, 47, 47,
    2, 2, 3, rep(4, 4), rep(5, 5), rep(6, 5),
    7, 7, 7, 9, 10, 11, 12, 15, 16, 18, 18, 18, 21,
    21, 24, 25, 27, 36, 41, 44, 52, 54, 59, 59, 63, 67, 71, 76),
  event = !(1:96 %in% is_censored),
  group = factor(c(rep(1, 45 + 6), rep(2, 45))))

# fit model
set.seed(61364778)
ctrl <- PF_control(
  N_fw_n_bw = 500, N_smooth = 2500, N_first = 2000,
  n_max = 1, # set to one as an example
  n_threads = max(parallel::detectCores(logical = FALSE), 1),
  eps = .001, Q_tilde = as.matrix(.3^2), est_a_0 = FALSE)
pf_fit <- suppressWarnings(
  PF_EM(
    survival::Surv(stop, event) ~ ddFixed(group),
    data = head_neck_cancer, by = 1, Q_0 = 1, Q = 0.1^2, control = ctrl,
    max_T = 30))

# the log-likelihood in the final iteration
(end_log_like <- tail(pf_fit$log_likes, 1))

# gives the same
fw_ps <- PF_forward_filter(
  survival::Surv(stop, event) ~ ddFixed(group), N_fw = 500, N_first = 2000,
  data = head_neck_cancer, by = 1, Q_0 = 1, Q = 0.1^2, Fmat = 1, R = 1,
  a_0 = pf_fit$a_0, fixed_effects = -0.5370051,
  control = ctrl, max_T = 30, seed = pf_fit$seed)
all.equal(end_log_like, logLik(fw_ps))

# will differ since we use different number of particles
fw_ps <- PF_forward_filter(
  survival::Surv(stop, event) ~ ddFixed(group), N_fw = 1000, N_first = 3000,
  data = head_neck_cancer, by = 1, Q_0 = 1, Q = 0.1^2, Fmat = 1, R = 1,
  a_0 = pf_fit$a_0, fixed_effects = -0.5370051,
  control = ctrl, max_T = 30, seed = pf_fit$seed)
all.equal(end_log_like, logLik(fw_ps))

# will differ since we use the final estimates
fw_ps <- PF_forward_filter(pf_fit, N_fw = 500, N_first = 2000)
all.equal(end_log_like, logLik(fw_ps))
# }

Run the code above in your browser using DataCamp Workspace