Last updated: 2025-12-29

Checks: 1 1

Knit directory: fiveMinuteStats/analysis/

This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown file has unstaged changes. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version d1623a0. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Unstaged changes:
    Modified:   Makefile
    Modified:   analysis/index.Rmd
    Modified:   analysis/integral.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/integral.Rmd) and HTML (docs/integral.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
html 5f62ee6 Matthew Stephens 2019-03-31 Build site.
Rmd 0cd28bd Matthew Stephens 2019-03-31 workflowr::wflow_publish(all = TRUE)
html 34bcc51 John Blischak 2017-03-06 Build site.
Rmd 5fbc8b5 John Blischak 2017-03-06 Update workflowr project with wflow_update (version 0.4.0).
Rmd 391ba3c John Blischak 2017-03-06 Remove front and end matter of non-standard templates.
html 8e61683 Marcus Davy 2017-03-03 rendered html using wflow_build(all=TRUE)
html c3b365a John Blischak 2017-01-02 Build site.
Rmd 67a8575 John Blischak 2017-01-02 Use external chunk to set knitr chunk options.
Rmd 5ec12c7 John Blischak 2017-01-02 Use session-info chunk.
Rmd a72861e stephens999 2016-05-02 add integral

See here for a PDF version of this vignette.

Prerequisites

Be familiar with basic probability and Bayesian calculations.

Overview

The goal here is simply to point out that everything you want to compute in Bayesian calculations is an integral.

Examples

Consider inference for a parameter \(\theta\) from data \(D\).

The posterior distribution of \(\theta\) is given by Bayes Theorem

\[p(\theta | D) = p(\theta) p(D | \theta)/ p(D)\]

First note that the denominator \(p(D)\) is an integral: \[p(D) = \int p(D | \theta) p(\theta) d\theta\].

Now suppose we want to estimate \(\theta\) by its posterior mean. This is \[E(\theta | D) = \int \theta p(\theta |D ) d\theta.\]

And if we want to find a 90% posterior credible interval for \(\theta\) then we want to find \(A\) and \(B\) such that \(\Pr(\theta \in [A,B] | D) = 0.9\). Note that the LHS of this is \[\Pr(\theta \in [A,B]|D) = \int I(\theta \in [A,B]) p(\theta | D) d\theta,\] where \(I(E)\) denotes the indicator function for the event \(E\), which takes the value 1 if \(E\) is true and \(0\) otherwise.

Examples: discrete

Of course, if \(\theta\) is discrete then the integrals above all become sums.

For example \[E(\theta | D) = \sum_n \theta_n \Pr(\theta=\theta_n | D)\] where \(\theta_1,\theta_2,\dots\) are the possible values for \(\theta\).

Summary

Pretty much all the things you want to compute when doing Bayesian inference are integrals (or sums) of one kind or another…

If you are computing 1-dimensional integrals then numerical methods are often useful. For example, Simpsons Rule, Gaussian Quadrature. These can also work in 2-dimensions, and maybe even 3 or 4.

Other simple methods that can work for low dimensions: naive Monte Carlo, and Importance Sampling. Also Laplace approximation.

For higher dimensions we usually resort to Markov Chain Monte Carlo.