IDEAS home Printed from https://ideas.repec.org/a/eee/ecomod/v501y2025ics0304380024003107.html
   My bibliography  Save this article

Demystifying Monte Carlo methods in R: A guide from Metropolis–Hastings to Hamiltonian Monte Carlo with biological growth equation examples

Author

Listed:
  • Mestry, Dipali Vasudev
  • Bhowmick, Amiya Ranjan

Abstract

Hamiltonian Monte Carlo (HMC) has emerged as a cutting-edge and versatile Markov Chain Monte Carlo (MCMC) method, widely adopted across various disciplines due to its superior computational efficiency compared to other MCMC techniques. Over the past few decades, HMC has gained significant traction. However, its implementation can pose challenges for practitioners, as it relies on concepts rooted in Hamiltonian dynamics from classical mechanics. Despite the development of modern Bayesian computation tools like Stan, which facilitate the application of HMC, the underlying mechanics may remain opaque to beginners. This article seeks to provide a clear and accessible introduction to HMC. We begin by reviewing the Metropolis–Hastings (MH) algorithm and its limitations, illustrated with simulated data. We then methodically explain the HMC algorithm through step-by-step simulation examples, showcasing its implementation in R software. Finally, we present a series of ecological case studies spanning a broad range of applications, including both single-species and multispecies dynamics. These studies demonstrate the implementation of HMC using the rstan package in R, applied to both simulated and real-world data. By adopting this pedagogical approach, we aim to help newcomers better understand and apply HMC to their research domains with confidence.

Suggested Citation

  • Mestry, Dipali Vasudev & Bhowmick, Amiya Ranjan, 2025. "Demystifying Monte Carlo methods in R: A guide from Metropolis–Hastings to Hamiltonian Monte Carlo with biological growth equation examples," Ecological Modelling, Elsevier, vol. 501(C).
  • Handle: RePEc:eee:ecomod:v:501:y:2025:i:c:s0304380024003107
    DOI: 10.1016/j.ecolmodel.2024.110922
    as

    Download full text from publisher

    File URL: http://www.sciencedirect.com/science/article/pii/S0304380024003107
    Download Restriction: Full text for ScienceDirect subscribers only

    File URL: https://libkey.io/10.1016/j.ecolmodel.2024.110922?utm_source=ideas
    LibKey link: if access is restricted and if your library uses this service, LibKey will redirect you to where you can use your library subscription to access this item
    ---><---

    As the access to this document is restricted, you may want to search for a different version of it.

    References listed on IDEAS

    as
    1. Carpenter, Bob & Gelman, Andrew & Hoffman, Matthew D. & Lee, Daniel & Goodrich, Ben & Betancourt, Michael & Brubaker, Marcus & Guo, Jiqiang & Li, Peter & Riddell, Allen, 2017. "Stan: A Probabilistic Programming Language," Journal of Statistical Software, Foundation for Open Access Statistics, vol. 76(i01).
    2. Sturtz, Sibylle & Ligges, Uwe & Gelman, Andrew, 2005. "R2WinBUGS: A Package for Running WinBUGS from R," Journal of Statistical Software, Foundation for Open Access Statistics, vol. 12(i03).
    3. Samuel Thomas & Wanzhu Tu, 2021. "Learning Hamiltonian Monte Carlo in R," The American Statistician, Taylor & Francis Journals, vol. 75(4), pages 403-413, October.
    4. White, Philip A. & Gelfand, Alan E. & Frye, Henry & Silander, John A., 2024. "Good modelling practice in ecology, the hierarchical Bayesian perspective," Ecological Modelling, Elsevier, vol. 496(C).
    5. Chan, Jennifer So-Kuen & Ng, Kok-Haur & Ragell, Rachel, 2019. "Bayesian return forecasts using realised range and asymmetric CARR model with various distribution assumptions," International Review of Economics & Finance, Elsevier, vol. 61(C), pages 188-212.
    6. Fatma Hachicha & Yosra Ghabri & Khaled Guesmi & Ramzi Benkraiem, 2022. "Bayesian stochastic volatility predictability of cryptocurrencies with the algorithm of Metropolis Hasting," Post-Print hal-04193188, HAL.
    7. Cheng, Haotian & Ng'ombe, John N. & Lambert, Dayton M., 2024. "A Bayesian generalized rank ordered logit model," Journal of choice modelling, Elsevier, vol. 50(C).
    8. Alexander Shapiro & Jos Berge, 2002. "Statistical inference of minimum rank factor analysis," Psychometrika, Springer;The Psychometric Society, vol. 67(1), pages 79-94, March.
    9. Bhowmick, Amiya Ranjan & Saha, Bapi & Chattopadhyay, Joydev & Ray, Santanu & Bhattacharya, Sabyasachi, 2015. "Cooperation in species: Interplay of population regulation and extinction through global population dynamics database," Ecological Modelling, Elsevier, vol. 312(C), pages 150-165.
    10. Wentao Xu & Cong Jiang & Lei Yan & Lingqi Li & Shuonan Liu, 2018. "An Adaptive Metropolis-Hastings Optimization Algorithm of Bayesian Estimation in Non-Stationary Flood Frequency Analysis," Water Resources Management: An International Journal, Published for the European Water Resources Association (EWRA), Springer;European Water Resources Association (EWRA), vol. 32(4), pages 1343-1366, March.
    11. Fang, Guanqi & Pan, Rong & Hong, Yili, 2020. "Copula-based reliability analysis of degrading systems with dependent failures," Reliability Engineering and System Safety, Elsevier, vol. 193(C).
    Full references (including those not matched with items on IDEAS)

    Most related items

    These are the items that most often cite the same works as this one and are cited by the same works as this one.
    1. Marco Gramatica & Peter Congdon & Silvia Liverani, 2021. "Bayesian modelling for spatially misaligned health areal data: A multiple membership approach," Journal of the Royal Statistical Society Series C, Royal Statistical Society, vol. 70(3), pages 645-666, June.
    2. Saberzadeh, Zahra & Razmkhah, Mostafa & Amini, Mohammad, 2023. "Bayesian reliability analysis of complex k-out-of-n: â„“ systems under degradation performance," Reliability Engineering and System Safety, Elsevier, vol. 231(C).
    3. Karim, Md Aktar Ul & Bhagat, Supriya Ramdas & Bhowmick, Amiya Ranjan, 2022. "Empirical detection of parameter variation in growth curve models using interval specific estimators," Chaos, Solitons & Fractals, Elsevier, vol. 157(C).
    4. Igor Ferreira Batista Martins & Hedibert Freitas Lopes, 2023. "Stochastic volatility models with skewness selection," Papers 2312.00282, arXiv.org.
    5. Edgar Santos‐Fernandez & Julie Vercelloni & Aiden Price & Grace Heron & Bryce Christensen & Erin E. Peterson & Kerrie Mengersen, 2024. "Increasing Trust in New Data Sources: Crowdsourcing Image Classification for Ecology," International Statistical Review, International Statistical Institute, vol. 92(1), pages 43-61, April.
    6. Cappelen, Alexander W. & Sørensen, Erik Ø. & Tungodden, Bertil & Xu, Xiaogeng, 2025. "Risk taking on behalf of others: Does the timing of uncertainty revelation matter?," Discussion Paper Series in Economics 13/2025, Norwegian School of Economics, Department of Economics.
    7. Nowak, Piotr Bolesław, 2016. "The MLE of the mean of the exponential distribution based on grouped data is stochastically increasing," Statistics & Probability Letters, Elsevier, vol. 111(C), pages 49-54.
    8. Francis,David C. & Kubinec ,Robert, 2022. "Beyond Political Connections : A Measurement Model Approach to Estimating Firm-levelPolitical Influence in 41 Economies," Policy Research Working Paper Series 10119, The World Bank.
    9. Martinovici, A., 2019. "Revealing attention - how eye movements predict brand choice and moment of choice," Other publications TiSEM 7dca38a5-9f78-4aee-bd81-c, Tilburg University, School of Economics and Management.
    10. Yongping Bao & Ludwig Danwitz & Fabian Dvorak & Sebastian Fehrler & Lars Hornuf & Hsuan Yu Lin & Bettina von Helversen, 2022. "Similarity and Consistency in Algorithm-Guided Exploration," CESifo Working Paper Series 10188, CESifo.
    11. Torsten Heinrich & Jangho Yang & Shuanping Dai, 2020. "Growth, development, and structural change at the firm-level: The example of the PR China," Papers 2012.14503, arXiv.org.
    12. Lachaud, Michée A. & Bravo-Ureta, Boris E., 2022. "A Bayesian statistical analysis of return to agricultural R&D investment in Latin America: Implications for food security," Technology in Society, Elsevier, vol. 70(C).
    13. Jiang, Deyin & Chen, Tianyu & Xie, Juanzhang & Cui, Weimin & Song, Bifeng, 2023. "A mechanical system reliability degradation analysis and remaining life estimation method——With the example of an aircraft hatch lock mechanism," Reliability Engineering and System Safety, Elsevier, vol. 230(C).
    14. van Kesteren Erik-Jan & Bergkamp Tom, 2023. "Bayesian analysis of Formula One race results: disentangling driver skill and constructor advantage," Journal of Quantitative Analysis in Sports, De Gruyter, vol. 19(4), pages 273-293, December.
    15. Camilo Alberto Cárdenas-Hurtado & Aaron Levi Garavito-Acosta & Jorge Hernán Toro-Córdoba, 2018. "Asymmetric Effects of Terms of Trade Shocks on Tradable and Non-tradable Investment Rates: The Colombian Case," Borradores de Economia 1043, Banco de la Republica de Colombia.
    16. Xin Xu & Yang Lu & Yupeng Zhou & Zhiguo Fu & Yanjie Fu & Minghao Yin, 2021. "An Information-Explainable Random Walk Based Unsupervised Network Representation Learning Framework on Node Classification Tasks," Mathematics, MDPI, vol. 9(15), pages 1-14, July.
    17. Yu, Jun, 2012. "A semiparametric stochastic volatility model," Journal of Econometrics, Elsevier, vol. 167(2), pages 473-482.
    18. Anastasiou, Andreas, 2017. "Bounds for the normal approximation of the maximum likelihood estimator from m-dependent random variables," Statistics & Probability Letters, Elsevier, vol. 129(C), pages 171-181.
    19. Spilker Finn & Ötting Marius, 2024. "No cheering in the background? Individual performance in professional darts during COVID-19," Journal of Quantitative Analysis in Sports, De Gruyter, vol. 20(3), pages 219-234.
    20. Xiaoyue Xi & Simon E. F. Spencer & Matthew Hall & M. Kate Grabowski & Joseph Kagaayi & Oliver Ratmann & Rakai Health Sciences Program and PANGEA‐HIV, 2022. "Inferring the sources of HIV infection in Africa from deep‐sequence data with semi‐parametric Bayesian Poisson flow models," Journal of the Royal Statistical Society Series C, Royal Statistical Society, vol. 71(3), pages 517-540, June.

    Corrections

    All material on this site has been provided by the respective publishers and authors. You can help correct errors and omissions. When requesting a correction, please mention this item's handle: RePEc:eee:ecomod:v:501:y:2025:i:c:s0304380024003107. See general information about how to correct material in RePEc.

    If you have authored this item and are not yet registered with RePEc, we encourage you to do it here. This allows to link your profile to this item. It also allows you to accept potential citations to this item that we are uncertain about.

    If CitEc recognized a bibliographic reference but did not link an item in RePEc to it, you can help with this form .

    If you know of missing items citing this one, you can help us creating those links by adding the relevant references in the same way as above, for each refering item. If you are a registered author of this item, you may also want to check the "citations" tab in your RePEc Author Service profile, as there may be some citations waiting for confirmation.

    For technical questions regarding this item, or to correct its authors, title, abstract, bibliographic or download information, contact: Catherine Liu (email available below). General contact details of provider: http://www.journals.elsevier.com/ecological-modelling .

    Please note that corrections may take a couple of weeks to filter through the various RePEc services.

    IDEAS is a RePEc service. RePEc uses bibliographic data supplied by the respective publishers.