Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart refit #829

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Smart refit #829

wants to merge 6 commits into from

Conversation

bbolker
Copy link
Contributor

@bbolker bbolker commented Jun 17, 2022

this may or may not be finished, but I think it might be sufficiently useful to merge?

@mebrooks
Copy link
Contributor

Is the new function meant to be exported?

I suggest adding an example of how it should be used. I tried to make one (expecting it would work the same as refit and made it break in two different ways (details below). The error from the salamander model was Error in EvalDoubleFunObject(Fun, theta, do_simulate = do_simulate) : Wrong parameter length. and the error from the dispersion model was caught segfault.

library(glmmTMB)
 
(m1 <- glmmTMB(count ~ mined + (1|site),
   family=poisson, data=Salamanders))

Salamanders2 <- transform(Salamanders, count2=2*count)

refit(m1, Salamanders2$count2)

fast_refit(m1, Salamanders2$count2)

glmmTMB:::fast_refit.glmmTMB(m1, Salamanders2$count2)


## Dispersion model
sim1 <- function(nfac=40, nt=100, facsd=0.1, tsd=0.15, mu=0, residsd=1)
{
  dat <- expand.grid(fac=factor(letters[1:nfac]), t=1:nt)
  n <- nrow(dat)
  dat$REfac <- rnorm(nfac, sd=facsd)[dat$fac]
  dat$REt <- rnorm(nt, sd=tsd)[dat$t]
  dat$x <- rnorm(n, mean=mu, sd=residsd) + dat$REfac + dat$REt
  dat
}
set.seed(101)
d1 <- sim1(mu=100, residsd=10)
d2 <- sim1(mu=200, residsd=5)
d1$sd <- "ten"
d2$sd <- "five"
dat <- rbind(d1, d2)
m0 <- glmmTMB(x ~ sd + (1|t), dispformula=~sd, data=dat)

dat2 <- transform(dat, x2=2*x)
refit(m0, dat2$x2)

glmmTMB:::fast_refit.glmmTMB(m0, dat2$x2)

@bbolker
Copy link
Contributor Author

bbolker commented Jun 20, 2022

If it can be debugged and made robust (and if benchmarking suggests it's worth it), I'd like to roll it into refit.glmmTMB as an option.

@bbolker
Copy link
Contributor Author

bbolker commented Jun 21, 2022

Your examples above should work (or throw an informative error) now.

There was one simple/thinko bug, plus I didn't test for different length/storage mode of new response. If/when this gets included (whether on its own or as an option to refit), we have to emphasize to the user that it's up to them to figure out how to adjust the new response to match the processed/sanitized version of the response (storage mode and length must match: user has to sort out removal of incomplete cases by model.frame(), processing of weird (factor/matrix) binomial input, etc. themselves. We can add that later if we want to be nice.)

@bbolker
Copy link
Contributor Author

bbolker commented Jun 22, 2022

I've integrated this into refit now. https://github.com/glmmTMB/glmmTMB/blob/smart_refit/misc/refit_test.R tests it; for 100 parametric bootstrap samples, on my machine, I get 73 seconds ('fast') vs 125 seconds (not-fast) - seems worthwhile!

PS I didn't actually CHECK THE RESULTS -- seems fast but wrong! Will have to see what's going on ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants