brianstock / MixSIAR

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

Run JAGS model warning message: In FUN(X[[i]], ...) : Failed to set trace monitor for ilr.global... #242

Open averynm opened 4 years ago

averynm commented 4 years ago

Hello, I am trying to run the following code in MixSIAR (sourced from the examples and modified just to suit my analysis and files), and when I do, I get the warning listed below. I have raw mixture data from multiple consumers and raw source data, and have 2 fixed and 1 continuous effect in my model.

loading my mixture data

mix <- load_mix_data(filename = "mix.filename.csv", iso_names=c("d13C","d15N"), factors=c("Species", "Year"), fac_random=c(FALSE, FALSE), fac_nested=c(FALSE, FALSE), cont_effects="Julian.day")

Load source data

source <- load_source_data(filename="lump2.preyconc.csv", source_factors=NULL, conc_dep=TRUE, data_type="raw", mix)

Load discrimination/TDF data

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

Define model structure and write JAGS model file

model_filename <- "MixSIAR_model2.txt" resid_err <- TRUE process_err <- TRUE write_JAGS_model(model_filename, resid_err, process_err, mix, source)

Run the JAGS model ("test" first, then "short")

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

I then get the following output and warning:

module glm loaded Compiling model graph Declaring variables Resolving undeclared variables Allocating nodes Graph information: Observed stochastic nodes: 161 Unobserved stochastic nodes: 40 Total graph size: 6874

Initializing model

|++++++++++++++++++++++++++++++++++++++++++++++++++| 100% |**| 100% Warning message: In FUN(X[[i]], ...) : Failed to set trace monitor for ilr.global Monitor already exists and cannot be duplicated

The model appears to run properly and I get most of the output (except for a few parts which I may post about separately ), and the results make sense based on my data. Is this warning something I should be concerned about? I don't understand what it actually means, so any explanation would be much appreciated.

brianstock-NOAA commented 4 years ago

Hmm, sorry I've never actually seen that warning before, and I don't know what it means...

Does it run if you use the same files but only include 1 factor + Julian day in the model?

averynm commented 3 years ago

Yes, if I only include Year as a fixed effect and Recapture Date as continuous, I get the same output. However, I get different warnings and errors depending on what particular combination of factors I use, and I think it has something to do with whether or not recap date is treated as a continuous or fixed effect. For context, my data are isotope values of Arctic and Common Tern plasma and various prey items, with terns captured during two field seasons.

If I lump species and just use Year (Fixed) and Recapture Date (Fixed), I don't get any errors when I run the run_model() function, but when I run the output_JAGS() function, I get a new set of warnings, listed below; I also get these warnings with models of Species (FE) + Year (FE) or by splitting the dataset by Species, turning recap date into a categorical variable (Early/late) and running separate models for each species (Arctic & Common Tern) of Year (FE) + Recap date (FE)

warnings() Warning messages: 1: In cor(X, use = "pairwise.complete.obs") : the standard deviation is zero 2- 12: In cor(X, use = "pairwise.complete.obs") : the standard deviation is zero [condensed repeated lines 2-12 for clarity] 13: Removed 600 rows containing missing values (geom_bar). 14: In cor(X, use = "pairwise.complete.obs") : the standard deviation is zero 15: In cor(X, use = "pairwise.complete.obs") : the standard deviation is zero 16: In cor(X, use = "pairwise.complete.obs") : the standard deviation is zero 17: Removed 150 rows containing missing values (geom_bar). 18 - 26: In cor(X, use = "pairwise.complete.obs") : the standard deviation is zero [condensed lines 18-26 for clarity] 27: Removed 450 rows containing missing values (geom_bar). 28: In cor(as.matrix(X[, -c(1, 2), drop = FALSE])) : the standard deviation is zero 29: Removed 8 rows containing missing values (geom_point). 30: Removed 24 rows containing missing values (geom_point).

If I run separate models for each species of Year (FE) + Recap date (continuous) I get additional warnings when running the output:

Warning messages: 1-10: In df$x[seq(1 + n.draws (src - 1), src n.draws)] <- as.matrix(p.ind[, ... : [condensed lines 1-10] number of items to replace is not a multiple of replacement length 11: tbl_df() is deprecated as of dplyr 1.0.0. Please use tibble::as_tibble() instead. This warning is displayed once every 8 hours. Call lifecycle::last_warnings() to see where this warning was generated. 12-23: In cor(X, use = "pairwise.complete.obs") : the standard deviation is zero [condensed lines 12-23 24: Removed 600 rows containing missing values (geom_bar). 25: In cor(as.matrix(X[, -c(1, 2), drop = FALSE])) : the standard deviation is zero 26: Removed 4 rows containing missing values (geom_point). 27: Removed 12 rows containing missing values (geom_point).

and if I run a model with only Recapture date as a continuous factor, I get only the first warning listed above regarding replacement length.

If I run a model with only Recapture date as a fixed factor, I get only the warnings that are the same as lines 12-27 in the warnings produced by the Year (FE) + Recap Date (CE) model.

According to the diagnostics issued with output, the models that converge the best are: Year (FE) + Recap (FE) Year (FE) + Recap (FE) , split by species; this is the modeling approach that I think may end up working the best for my question and data.

Models of Year (FE) + Recap (CE) , split by species, did not converge well at all, which to me also suggests that perhaps recapture date is best treated as a fixed factor rather than a continuous one.

The output from the models differs depending on whether or not recapture date is treated as continuous or fixed when looking at the diet of each species. The biggest differences seem to be the relative proportions of benthic omnivores throughout the season for Common Terns, and for Arctic Terns, in the relative proportions of hake; the model with recapture date as a continuous effect estimates a higher proportion of hake early in the season, whereas in the model with recapture date as a fixed effect, hake is only the fourth-most abundant prey item. Here's a few plots of Arctic Tern diet by Julian Day (from the model with recap as continuous) and a few posterior density plots from the model with recap day as a fixed effect. I've also attached a few files with more output and isotope biplots for context.

Does it seem logical to stick with the models for each species with recap date as fixed effect? I just want to make sure that I'm not missing something with the warnings that I'm getting. Any suggestions greatly appreciated!

image image image image

ARTE Yr_recapFE output.docx ARTE_recapCE_output.docx COTE Yr_RecapFE output.docx COTE_Year_RecapCE output.docx

hopelee266 commented 3 years ago

I am having the same issue. With the following mix, source, and discr data:

mix <- load_mix_data(filename = mix.filename, iso_names = c("Thr", "Ile", "Phe", "Leu", "Val", "Lys"), factors = c("Site", "Season"), fac_random = c(FALSE, FALSE), fac_nested = c(FALSE, FALSE), cont_effects = "Date_num")

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

discr <- load_discr_data(filename = discr.filename, mix)

I get the following error message: Failed to set trace monitor for ilr.global Monitor already exists and cannot be duplicated

Similarly, I get most of the output. However, when attempting to use output_posteriors to modify posterior density plots, I get the error message: Error in plot_continuous_var(jags.1, mix, source, output_options) : object 'g' not found

If I change the mix to the following (one variable in the data frame which includes both variables), I no longer get the errors:

mix <- load_mix_data(filename = mix.filename, iso_names = c("Thr", "Ile", "Phe", "Leu", "Val", "Lys"), factors = "Site_Season", fac_random = FALSE, fac_nested = NULL, cont_effects = "Date_num")

bennhanns commented 1 year ago

Hi, when run a model very similar hopelee266I's I also get the object 'g' error:

Error in plot_continuous_var(jags.1, mix, source, output_options) : object 'g' not found

what does this mean?

I currently running a models with only a continuous effect and only cat factors to what that changes.

tiredofmakingnewusernames commented 1 year ago

Hi MixSIAR folks, Was the issue raised by averynm ever resolved? I have an identical model structure, with two fixed effects and one continuous covariate, and I receive the same warning message: "Failed to set trace monitor for ilr.global. Monitor already exists and cannot be duplicated". I can remove one fixed effect and not receive any warnings. I notice when outputting the posteriors from the run with both fixed effects still in I won't get the posterior plot showing the diet proportions as a function of the covariate, but I do get this plot with just one fixed effect. I'm not sure if the underlying numbers necessary for such a plot are still being calculated, or if that warning indicates this aspect is failing?