brianstock / MixSIAR

A framework for Bayesian mixing models in R:
http://brianstock.github.io/MixSIAR/
94 stars 75 forks source link

Error: too many open devices #249

Closed amulcan closed 4 years ago

amulcan commented 4 years ago

I have been running MixSIAR to assess potential Pb sources in my study area sediments. I have 3 tracers, 4 sources, about 100 samples/mixtures, and no discrimination factors (trophic discrimination factors are not applicable for this study).

I run the model in "extreme" mode, and it seems to run without any problem. When it's done the output function creates about 60-65 posterior density plot pdfs then stops working and I receive the error "Error in (function (file = if (onefile) "Rplots.pdf" else "Rplot%03d.pdf", : too many open devices". This prevents the summary statistics and diagnostics files from being output, thus I cannot check if my model reached convergence.

Below is the script I am using and attached are examples of my data (randomized numbers). I appreciate any help! MixSIARexample.zip

library(MixSIAR)
library(tidyr)

mix.filename <- "digest_mixture.csv"
mix <- load_mix_data(
  filename = mix.filename,
  iso_names = c("Pb206.204", "Pb207.204", "Pb208.204"),
  factors = c("Site"),
  fac_random = c(TRUE),
  fac_nested = c(FALSE),
  cont_effects = NULL
)

source.filename <- "digest_sources.csv"
source <- load_source_data(
  filename = source.filename,
  source_factors = NULL,
  conc_dep = FALSE,
  data_type = "means",
  mix
)

discr.filename <- "digest_discrimination.csv"
discr <- load_discr_data(filename = discr.filename, mix)

plot_data(
  filename = "isospace_plot",
  plot_save_pdf = FALSE,
  plot_save_png = TRUE,
  mix,
  source,
  discr
)
plot_prior(alpha.prior = 1, source)

model_filename <- "MixSIAR_model.txt"
resid_err <- TRUE
process_err <- FALSE

write_JAGS_model(model_filename,
                 resid_err,
                 process_err,
                 mix,
                 source)
jags.1 <- run_model(
  run = "extreme",
  mix,
  source,
  discr,
  model_filename,
  alpha.prior = 1,
  resid_err,
  process_err
)

output_options <- list(
  summary_save = TRUE,
  summary_name = "summary_statistics",
  sup_post = TRUE,
  plot_post_save_pdf = FALSE,
  plost_post_name = "posterior_density",
  sup_pairs = FALSE,
  plot_pairs_save_pdf = FALSE,
  plot_pairs_name = "pairs_plot",
  sup_xy = TRUE,
  plot_xy_save_pdf = FALSE,
  plot_xy_name = "xy_plot",
  gelman = TRUE,
  heidel = FALSE,
  geweke = TRUE,
  diag_save = TRUE,
  diag_name = "diagnostics",
  indiv_effect = FALSE,
  plot_post_save_png = TRUE,
  plot_pairs_save_png = TRUE,
  plot_xy_save_png = TRUE,
  diag_save_ggmcmc = FALSE
)
output_JAGS(jags.1,
            mix,
            source,
            output_options)
brianstock commented 4 years ago

Sorry, this case is why I included the sup_post option... that should allow you to suppress the R plots and instead save them to png/pdf. When I run the above (changed run length to "test" since that shouldn't matter), the plots are suppressed successfully. But they also aren't saved to png like they should since plot_post_save_png = TRUE. That's a bug. I'll get on it.

Can you confirm that if you run the above code but change run length to "test", you get a whole bunch of posterior plots output to R?

Shouldn't matter, but there's a spelling error in 5th item of output_options: plost_post_name should be plot_post_name.

brianstock commented 4 years ago

I've added new output functions that allow you to suppress plots, save plots to file, or return plots for later modification. See new vignette Modify output.

Note that you'll need to install the Github version as these updates aren't on CRAN yet.

amulcan commented 4 years ago

Hi Brian,

Okay, thanks for your help with this. I'll try out the new output functions.

Best, Mandy