bayesiandemography / bage

https://bayesiandemography.github.io/bage/
Other
2 stars 0 forks source link

Issue with sparseMatrix density #48

Open lmorris2 opened 1 month ago

lmorris2 commented 1 month ago

When running the following emigration model (318 regions, 106 ages, 2 sexes, 12 times):

 mod_m9 <- mod_pois(count ~ age * region * sex + time,
   data = rates,
   exposure = ~ ifelse(count > 0 & midint_pop_est < 0.5, 0.5, midint_pop_est)
 ) %>%
   set_prior(age ~ RW(s = 1)) %>%
   set_prior(time ~ RW(s = 1)) %>%
   set_prior(sex ~ NFix(sd = 1)) %>%
   set_prior(region ~ N(s = 1)) %>%
   set_prior(age:region ~ RW(s = 1, along = "age")) %>%
   set_prior(age:sex ~ RW(s = 1, along = "age")) %>%
   set_prior(region:sex ~ N(s = 1)) %>%
   set_prior(age:region:sex ~ RW(s = 1, along = "age")) %>%
   set_n_draw(100) %>%
   fit()

I get the following error:

Error in ..subscript.2ary(x, l[[1L]], l[[2L]], drop = drop[1L]) : 
  x[i,j] too dense for [CR]sparseMatrix; would have more than 2^31-1 nonzero entries
14. ..subscript.2ary(x, l[[1L]], l[[2L]], drop = drop[1L])
13. .subscript.2ary(x, i, , drop = TRUE)
12. Y[rep(seq_len(ny), each = nx), ]
11. Y[rep(seq_len(ny), each = nx), ]
10. .sparse.interaction.2(r, rList[[i]], forceSparse = forceSparse, do.names = do.names, verbose = verbose)
9. .sparse.interaction.N(lapply(nmSplits, getR), do.names = TRUE, forceSparse = TRUE, verbose = verbose)
8. model.spmatrix(trms = t, mf = data, transpose = transpose, drop.unused.levels = drop.unused.levels, row.names = row.names, sep = sep, verbose = verbose)
7. Matrix::sparse.model.matrix(formula_term, data = data_term, contrasts.arg = contrasts_term, row.names = FALSE)
6. make_matrices_effect_outcome(data = data_ag, dimnames_terms = dimnames_terms)
5. make_vals_ag(mod)
4. fit_default(object)
3. fit.bage_mod(.)
2. fit(.)
1. mod_pois(count ~ age * region * sex + time, data = rates, exposure = ~ifelse(count > 0 & midint_pop_est < 0.5, 0.5, midint_pop_est)) %>% set_prior(age ~ RW(s = 1)) %>% set_prior(time ~ RW(s = 1)) %>% set_prior(sex ~ NFix(sd = 1)) %>% set_prior(region ~ N(s = 1)) %>% set_prior(age:region ~ ...

The same error occurs whether or not I use the "inner-outer" method for fit(), and it also happens if I change the priors on all of the parts involving age to Sp(). Equivalent count ~ age * region + sex + time models run without issue.

johnrbryant commented 1 month ago

That really is a huge model, given the number of parameters. Do you need the three-way interaction? I'd be tempted to try one that only had second-order terms. Something like

count ~ (age + region + sex)^2 + time

If that doesn't work, I'd try eliminating terms further.

Incidentally, we don't yet have SVD data that can be used for migration, but it's on the backlog. The would allow you to cut down the number of free parameters.