# PF_forward_filter

##### Forward Particle Filter

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, fixed_effects,
control = PF_control(...), seed = NULL, trace = 0, G, theta, J, K,
psi, phi, type = "RW", Fmat, ...)

# S3 method for data.frame
PF_forward_filter(x, N_fw, N_first, formula,
model = "logit", by, max_T, id, a_0, Q_0, Q, fixed_effects,
control = PF_control(...), seed = NULL, trace = 0, fixed = NULL,
random = NULL, G, theta, J, K, psi, phi, type = "RW", Fmat,
order = 1, ...)

##### 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 with the logistic link function,`'cloglog'`

for binary outcomes with the inverse cloglog link function, 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.

- fixed_effects
values for the fixed parameters.

- control
see

`PF_control`

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

- G, theta, J, K, psi, phi
parameters for a restricted

`type = "VAR"`

model. See the vignette mentioned in 'Details' of`PF_EM`

and the examples linked to in 'See Also'.- type
type of state model. Either

`"RW"`

for a [R]andom [W]alk or "VAR" for [V]ector [A]uto[R]egression.- Fmat
starting value for \(F\) when

`type = "VAR"`

. See 'Details' in`PF_EM`

.- formula
`coxph`

like formula with`Surv(tstart, tstop, event)`

on the left hand site of`~`

.- fixed
two-sided

`formula`

to be used with`random`

instead of`formula`

. It is of the form`Surv(tstart, tstop, event) ~ x`

or`Surv(tstart, tstop, event) ~ - 1`

for no fixed effects.- random
one-sided

`formula`

to be used with`fixed`

instead of`formula`

. It is of the form`~ z`

.- order
order of the random walk.

##### 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.`data.frame`

: Forward particle filter with`data.frame`

data input as`x`

instead of`data`

. Can be used with`fixed`

and`random`

argument.

##### Warning

The function is still under development so the output and API may change.

##### Examples

```
# 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 <- logLik(pf_fit))
# 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,
a_0 = pf_fit$a_0, fixed_effects = -0.5370051,
control = ctrl, max_T = 30, seed = pf_fit$seed)
all.equal(c(end_log_like), c(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,
a_0 = pf_fit$a_0, fixed_effects = -0.5370051,
control = ctrl, max_T = 30, seed = pf_fit$seed)
all.equal(c(end_log_like), c(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(c(end_log_like), c(logLik(fw_ps)))
# }
```

*Documentation reproduced from package dynamichazard, version 0.6.5, License: GPL-2*