# \donttest{
library(dynamichazard)
.lung <- lung[!is.na(lung$ph.ecog), ]
# standardize
.lung$age <- scale(.lung$age)
# fit model
set.seed(43588155)
pf_fit <- PF_EM(
fixed = Surv(time, status == 2) ~ ph.ecog + age,
random = ~ 1, model = "exponential",
data = .lung, by = 50, id = 1:nrow(.lung),
Q_0 = as.matrix(1), Q = as.matrix(.5^2), type = "VAR",
max_T = 800, Fmat = as.matrix(.5),
control = PF_control(
N_fw_n_bw = 250, N_first = 2000, N_smooth = 500, covar_fac = 1.1,
nu = 6, n_max = 1000L, eps = 1e-4, averaging_start = 200L,
n_threads = 2))
# compute score and observed information matrix
comp_obj <- PF_get_score_n_hess(pf_fit)
comp_obj$set_n_particles(N_fw = 10000L, N_first = 10000L)
comp_obj$run_particle_filter()
(o1 <- comp_obj$get_get_score_n_hess())
# O(N^2) method with lower variance as a function of time
comp_obj <- PF_get_score_n_hess(pf_fit, use_O_n_sq = TRUE)
comp_obj$set_n_particles(N_fw = 2500L, N_first = 2500L)
(o2 <- comp_obj$get_get_score_n_hess())
# approximations may have large variance
o3 <- replicate(10L, {
runif(1)
pf_fit$seed <- .Random.seed
comp_obj <- PF_get_score_n_hess(pf_fit)
comp_obj$set_n_particles(N_fw = 10000L, N_first = 10000L)
comp_obj$run_particle_filter()
comp_obj$get_get_score_n_hess()
}, simplify = FALSE)
sapply(o3, function(x) x$score)
sapply(o3, function(x) sqrt(diag(solve(x$obs_info))))
# }
Run the code above in your browser using DataLab