Skip to content

Commit

Permalink
rstan compatibility for n-mixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Clark committed Feb 19, 2024
1 parent 7db520e commit b754c2d
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 8 deletions.
28 changes: 20 additions & 8 deletions R/add_nmixture.R
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,23 @@ add_nmix_posterior = function(model_output,
samp_starts <- seq(1, NROW(samples), by = nsamples)
samp_ends <- seq(nsamples, NROW(samples), by = nsamples)
for(i in 1:nchains){

samps_df <- data.frame(samples[samp_starts[i]:samp_ends[i],])
colnames(samps_df) <- names
model_output@sim$samples[[i]] <-
dplyr::bind_cols(model_output@sim$samples[[i]],
samps_df)

if(is.list(model_output@sim$samples[[i]])){
old <- attributes(model_output@sim$samples[[i]])
oldnames <- attr(model_output@sim$samples[[i]], 'names')
model_output@sim$samples[[i]] <-
append(model_output@sim$samples[[i]], as.list(samps_df))
mostattributes(model_output@sim$samples[[i]]) <- old
attr(model_output@sim$samples[[i]], 'names') <-
c(oldnames, colnames(samps_df))
} else {
model_output@sim$samples[[i]] <-
dplyr::bind_cols(model_output@sim$samples[[i]],
samps_df)
}
}
model_output@sim$fnames_oi <- c(model_output@sim$fnames_oi,
names)
Expand Down Expand Up @@ -638,11 +650,11 @@ add_nmix_posterior = function(model_output,

# Add latent_ypreds to the posterior samples
model_output <- add_samples(model_output = model_output,
names = gsub('trend', 'latent_ypred', parnames),
samples = latentypreds[, indices],
nsamples = NROW(latentypreds) / nchains,
nchains = nchains,
parname = 'latent_ypred')
names = gsub('trend', 'latent_ypred', parnames),
samples = latentypreds[, indices],
nsamples = NROW(latentypreds) / nchains,
nchains = nchains,
parname = 'latent_ypred')
model_output@sim$dims_oi$latent_ypred <-
model_output@sim$dims_oi$trend

Expand Down
2 changes: 2 additions & 0 deletions R/mvgam.R
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,7 @@ mvgam = function(formula,
control = stan_control,
pars = NA,
refresh = 100,
save_warmup = FALSE,
...)
} else {
fit1 <- rstan::sampling(stan_mod,
Expand All @@ -2143,6 +2144,7 @@ mvgam = function(formula,
control = stan_control,
pars = NA,
refresh = 100,
save_warmup = FALSE,
...)
}
}
Expand Down
Binary file modified docs/articles/nmixtures_files/figure-html/unnamed-chunk-13-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/articles/nmixtures_files/figure-html/unnamed-chunk-13-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/RcppExports.o
Binary file not shown.
Binary file modified src/mvgam.dll
Binary file not shown.
Binary file modified src/trend_funs.o
Binary file not shown.
Binary file modified tests/testthat/Rplots.pdf
Binary file not shown.

0 comments on commit b754c2d

Please sign in to comment.