R Under development (unstable) (2024-06-17 r86768 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(epigrowthfit) > options(warn = 2L, error = if (interactive()) recover) > example("egf", package = "epigrowthfit"); o.1 <- m1; o.2 <- m2 egf> ## Simulate 'N' incidence time series exhibiting exponential growth egf> set.seed(180149L) egf> N <- 10L egf> f <- function(time, r, c0) { egf+ lambda <- diff(exp(log(c0) + r * time)) egf+ c(NA, rpois(lambda, lambda)) egf+ } egf> time <- seq.int(0, 40, 1) egf> r <- rlnorm(N, -3.2, 0.2) egf> c0 <- rlnorm(N, 6, 0.2) egf> data_ts <- egf+ data.frame(country = gl(N, length(time), labels = LETTERS[1:N]), egf+ time = rep.int(time, N), egf+ x = unlist(Map(f, time = list(time), r = r, c0 = c0))) egf> rm(f, time) egf> ## Define fitting windows (here, two per time series) egf> data_windows <- egf+ data.frame(country = gl(N, 1L, 2L * N, labels = LETTERS[1:N]), egf+ wave = gl(2L, 10L), egf+ start = c(sample(seq.int(0, 5, 1), N, TRUE), egf+ sample(seq.int(20, 25, 1), N, TRUE)), egf+ end = c(sample(seq.int(15, 20, 1), N, TRUE), egf+ sample(seq.int(35, 40, 1), N, TRUE))) egf> ## Estimate the generative model egf> m1 <- egf+ egf(model = egf_model(curve = "exponential", family = "pois"), egf+ formula_ts = cbind(time, x) ~ country, egf+ formula_windows = cbind(start, end) ~ country, egf+ formula_parameters = ~(1 | country:wave), egf+ data_ts = data_ts, egf+ data_windows = data_windows, egf+ se = TRUE) computing a Hessian matrix ... egf> ## Re-estimate the generative model with: egf> ## * Gaussian prior on beta[1L] egf> ## * LKJ prior on all random effect covariance matrices egf> ## (here there happens to be just one) egf> ## * initial value of 'theta' set explicitly egf> ## * theta[3L] fixed at initial value egf> m2 <- egf+ update(m1, egf+ formula_priors = list(beta[1L] ~ Normal(mu = -3, sigma = 1), egf+ Sigma ~ LKJ(eta = 2)), egf+ init = list(theta = c(log(0.5), log(0.5), 0)), egf+ map = list(theta = 3L)) computing a Hessian matrix ... > > > ## object ############################################################## > > o.2c <- coef(o.2, random = TRUE) > o.2c.e <- structure(as.double(o.2c), + len = c(beta = 2L, theta = 2L, b = 40L), + map = list(beta = NULL, theta = c(1L, 2L, NA), b = NULL), + class = "coef.egf") > stopifnot(identical(o.2c, o.2c.e)) > > > ## print ############################################################### > > vv <- withVisible(print(o.2c)) beta beta theta theta b b -3.10311755 6.42721459 -1.79201534 -0.56685809 -0.59279576 0.12022154 b b b b b b -0.73984082 -0.21830228 0.65420857 -1.23075773 1.04189983 -0.43572373 b b b b b b -0.13138401 -0.92817953 0.27140046 -1.32151717 -0.68686799 -1.49778693 b b b b b b -0.21594208 -1.02836446 -0.44008433 -0.45215250 -0.45694203 -0.62660402 b b b b b b 0.64146636 1.75115488 0.65667882 1.36907332 0.50463209 0.65624489 b b b b b b 1.70208514 1.50944790 0.23078312 0.58900493 -0.19751288 -0.06937314 b b b b b b -0.49636287 -0.22618172 -0.34614308 0.58402472 -1.00809601 1.14565468 b b 0.55412319 0.06316734 > stopifnot(exprs = { + identical(vv[["value"]], o.2c) + identical(vv[["visible"]], FALSE) + }) > > > ## as.list ############################################################# > > nms <- c("beta", "theta", "b") > o.2cl <- as.list(o.2c) > o.2cl.e <- split(o.2c, factor(labels(o.2c), levels = nms)) > for (s in nms) + attr(o.2cl.e[[s]], "map") <- attr(o.2c, "map")[[s]] > stopifnot(identical(o.2cl, o.2cl.e)) > > proc.time() user system elapsed 4.79 0.26 5.03