# NOT RUN {
library(dynamichazard)
.lung <- lung[!is.na(lung$ph.ecog), ]
# standardize
.lung$age <- scale(.lung$age)
set.seed(43588155)
pf_fit <- PF_EM(
fixed = Surv(time, status == 2) ~ ph.ecog + age,
random = ~ age, model = "exponential",
data = .lung, by = 50, id = 1:nrow(.lung),
Q_0 = diag(1, 2), Q = diag(.5^2, 2), type = "VAR",
max_T = 800,
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-5, est_a_0 = FALSE, averaging_start = 100L,
n_threads = max(parallel::detectCores(logical = FALSE), 1)))
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
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$observation$score)
sapply(o3, function(x) sqrt(diag(solve(x$observation$neg_obs_info))))
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab