Releases: henningte/mmgm
mmgm: Estimates the degree of decomposition of peat with a mixing model for gamma_MIRS
mmgm
Package mmgm provides a mixing model that allows to estimate the
degree of decomposition (
individual components from bulk estimates of
(Teickner et al. 2025).
Installation
You can install the development version of mmgm like so:
remotes::install_github("henningte/mmgm")Example
To estimate the mixing model, one needs to provide a list with two data
frames as elements. The first element, components contains information
on components, and the second element, mixtures, contains information
on the mixtures (peat samples):
library(mmgm)
library(cmdstanr)
#> Warning: package 'cmdstanr' was built under R version 4.3.3
#> This is cmdstanr version 0.8.1
#> - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
#> - CmdStan path: C:/Users/henni/.cmdstan/cmdstan-2.36.0
#> - CmdStan version: 2.36.0
#>
#> A newer version of CmdStan is available. See ?install_cmdstan() to install it.
#> To disable this check set option or environment variable cmdstanr_no_ver_check=TRUE.
# show structure of the example data
mmgm_example_data
#> $components
#> id_mixture id_component w
#> 1.1 1 1 0.5956979
#> 1.2 1 2 0.4043021
#> 2.1 2 1 0.2799653
#> 2.2 2 2 0.7200347
#> 3.1 3 1 0.5942072
#> 3.2 3 2 0.4057928
#> 4.1 4 1 0.4683780
#> 4.2 4 2 0.5316220
#> 5.1 5 1 0.1578412
#> 5.2 5 2 0.8421588
#>
#> $mixtures
#> # A tibble: 5 × 2
#> id_mixture degree_of_decomposition_2
#> * <int> <quantits>
#> 1 1 0.06227749
#> 2 2 0.01924212
#> 3 3 0.02330240
#> 4 4 0.03896350
#> 5 5 0.12042913To prepare estimation of the mixing model, this list is processed with
mmgm_make_stan_data():
stan_data <-
mmgm_make_stan_data(
x = mmgm_example_data,
id_model_gamma_mirs = 2,
priors = mmgm_make_default_priors(x = mmgm_example_data)
)Here, id_model_gamma_mirs defines the model that was used to predict
irpeatmodels
package (Teickner 2025)). priors defines the priors for the mixing
model and mmgm_make_default_priors() is a helper function that allows
to define default priors for a given list of data.
stan_data can be passed to mmgm_estimate_gamma_mirs_mixing_1() to
estimate the mixing model using CmdStan. Use the CmdStan
arguments
to adjust MCMC sampling:
stan_fit <-
mmgm_estimate_gamma_mirs_mixing_1(
stan_data = stan_data,
iter_warmup = 1000,
iter_sampling = 1000,
chains = 4,
sig_figs = 14,
seed = 7667
)
#> Running MCMC with 4 sequential chains...
#>
#> Chain 1 Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 1 Iteration: 100 / 2000 [ 5%] (Warmup)
#> Chain 1 Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: gamma_lpdf: Random variable[2] is 0, but must be positive finite! (in 'C:/Users/henni/AppData/Local/Temp/RtmpCCJ84p/model-df0ea5142c.stan', line 43, column 2 to column 40)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: beta_lpdf: Second shape parameter[2] is 0, but must be positive finite! (in 'C:/Users/henni/AppData/Local/Temp/RtmpCCJ84p/model-df0ea5142c.stan', line 48, column 4 to column 151)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Iteration: 300 / 2000 [ 15%] (Warmup)
#> Chain 1 Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 1 Iteration: 500 / 2000 [ 25%] (Warmup)
#> Chain 1 Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 1 Iteration: 700 / 2000 [ 35%] (Warmup)
#> Chain 1 Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 1 Iteration: 900 / 2000 [ 45%] (Warmup)
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: beta_lpdf: Second shape parameter[5] is 0, but must be positive finite! (in 'C:/Users/henni/AppData/Local/Temp/RtmpCCJ84p/model-df0ea5142c.stan', line 49, column 4 to column 110)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 1 Exception: beta_lpdf: Second shape parameter[5] is 0, but must be positive finite! (in 'C:/Users/henni/AppData/Local/Temp/RtmpCCJ84p/model-df0ea5142c.stan', line 48, column 4 to column 151)
#> Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 1
#> Chain 1 Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 1 Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 1 Iteration: 1100 / 2000 [ 55%] (Sampling)
#> Chain 1 Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 1 Iteration: 1300 / 2000 [ 65%] (Sampling)
#> Chain 1 Iteration: 1400 / 2000 [ 70%] (Sampling)
#> Chain 1 Iteration: 1500 / 2000 [ 75%] (Sampling)
#> Chain 1 Iteration: 1600 / 2000 [ 80%] (Sampling)
#> Chain 1 Iteration: 1700 / 2000 [ 85%] (Sampling)
#> Chain 1 Iteration: 1800 / 2000 [ 90%] (Sampling)
#> Chain 1 Iteration: 1900 / 2000 [ 95%] (Sampling)
#> Chain 1 Iteration: 2000 / 2000 [100%] (Sampling)
#> Chain 1 finished in 0.4 seconds.
#> Chain 2 Iteration: 1 / 2000 [ 0%] (Warmup)
#> Chain 2 Iteration: 100 / 2000 [ 5%] (Warmup)
#> Chain 2 Iteration: 200 / 2000 [ 10%] (Warmup)
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: beta_lpdf: Second shape parameter[3] is 0, but must be positive finite! (in 'C:/Users/henni/AppData/Local/Temp/RtmpCCJ84p/model-df0ea5142c.stan', line 49, column 4 to column 110)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: beta_lpdf: Second shape parameter[3] is 0, but must be positive finite! (in 'C:/Users/henni/AppData/Local/Temp/RtmpCCJ84p/model-df0ea5142c.stan', line 49, column 4 to column 110)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: beta_lpdf: First shape parameter[1] is 0, but must be positive finite! (in 'C:/Users/henni/AppData/Local/Temp/RtmpCCJ84p/model-df0ea5142c.stan', line 48, column 4 to column 151)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Iteration: 300 / 2000 [ 15%] (Warmup)
#> Chain 2 Iteration: 400 / 2000 [ 20%] (Warmup)
#> Chain 2 Iteration: 500 / 2000 [ 25%] (Warmup)
#> Chain 2 Iteration: 600 / 2000 [ 30%] (Warmup)
#> Chain 2 Iteration: 700 / 2000 [ 35%] (Warmup)
#> Chain 2 Iteration: 800 / 2000 [ 40%] (Warmup)
#> Chain 2 Iteration: 900 / 2000 [ 45%] (Warmup)
#> Chain 2 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
#> Chain 2 Exception: beta_lpdf: Second shape parameter[5] is 0, but must be positive finite! (in 'C:/Users/henni/AppData/Local/Temp/RtmpCCJ84p/model-df0ea5142c.stan', line 48, column 4 to column 151)
#> Chain 2 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
#> Chain 2 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
#> Chain 2
#> Chain 2 Iteration: 1000 / 2000 [ 50%] (Warmup)
#> Chain 2 Iteration: 1001 / 2000 [ 50%] (Sampling)
#> Chain 2 Iteration: 1100 / 2000 [ 55%] (Sampling)
#> Chain 2 Iteration: 1200 / 2000 [ 60%] (Sampling)
#> Chain 2 Iteration: 1300 / 2000 [ 65%] (Sampling)
#> Chain 2 Iteration: 140...