Skip to content

Commit

Permalink
Fix memory issues with Tweedie #302
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson committed Mar 29, 2024
1 parent ef801ee commit 616e8cd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
68 changes: 68 additions & 0 deletions scratch/memory-tweedie.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
library(sdmTMB)

# match glmmTMB:
ctl <- sdmTMBcontrol(newton_loops = 0, multiphase = FALSE)

N <- 3e4
set.seed(1)
dat <- data.frame(
y = fishMod::rTweedie(N, 1, 1, 1.5)
)

bench::mark({m2 <- sdmTMB(
y ~ 1,
data = dat,
spatial = 'off',
family = tweedie("log"),
control = ctl
)}, iterations = 1)

tidy(m2)

bench::mark({m1 <- glmmTMB::glmmTMB(
y ~ 1,
data = dat,
family = glmmTMB::tweedie("log")
)}, iterations = 1)


N <- 3e4
set.seed(1)
dat <- data.frame(
y = rnorm(N, 0, 1)
)

bench::mark({m2 <- sdmTMB(
y ~ 1,
data = dat,
spatial = 'off',
control = sdmTMBcontrol(newton_loops = 1, multiphase = FALSE)
)}, iterations = 1)

bench::mark({m1 <- glmmTMB::glmmTMB(
y ~ 1,
data = dat
)}, iterations = 1)


##############

N <- 3e4
set.seed(1)
dat <- data.frame(
y = rgamma(N, 0.2, 0.2/3)
)

bench::mark({m2 <- sdmTMB(
y ~ 1,
data = dat,
spatial = 'off',
family = Gamma(link = "log"),
control = sdmTMBcontrol(newton_loops = 1, multiphase = FALSE)
)}, iterations = 1)

bench::mark({m1 <- glmmTMB::glmmTMB(
y ~ 1,
data = dat,
family = Gamma(link = "log"),
)}, iterations = 1)
3 changes: 2 additions & 1 deletion src/sdmTMB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,6 @@ Type objective_function<Type>::operator()()
}
case tweedie_family: {
tweedie_p = invlogit(thetaf) + Type(1.0);
ADREPORT(tweedie_p);
// FIXME! move this out of loop!!!!!!!!
if (!sdmTMB::isNA(priors(12))) {
error("Priors not enabled for Tweedie p currently");
Expand Down Expand Up @@ -1380,6 +1379,8 @@ Type objective_function<Type>::operator()()
REPORT(phi);
}

if (family(0) == tweedie_family) ADREPORT(tweedie_p); // #302

REPORT(epsilon_st_A_vec); // spatio-temporal effects; vector
REPORT(b_rw_t); // time-varying effects
REPORT(omega_s_A); // spatial effects; n_s length vector
Expand Down

0 comments on commit 616e8cd

Please sign in to comment.