Last updated: 2019-03-31

Checks: 6 0

Knit directory: fiveMinuteStats/analysis/

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

Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(12345) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.

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:

Ignored files:
Ignored:    .Rhistory
Ignored:    .Rproj.user/
Ignored:    analysis/.Rhistory
Ignored:    analysis/bernoulli_poisson_process_cache/

Untracked files:
Untracked:  _workflowr.yml
Untracked:  analysis/CI.Rmd
Untracked:  analysis/gibbs_structure.Rmd
Untracked:  analysis/libs/
Untracked:  analysis/results.Rmd
Untracked:  analysis/shiny/tester/
Untracked:  docs/MH_intro_files/
Untracked:  docs/citations.bib
Untracked:  docs/figure/MH_intro.Rmd/
Untracked:  docs/hmm_files/
Untracked:  docs/libs/
Untracked:  docs/shiny/tester/

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 R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view them.

File Version Author Date Message
html 6053d26 stephens999 2018-04-23 Build site.
Rmd 1077c97 stephens999 2018-04-23 workflowr::wflow_publish(c(“analysis/index.Rmd”, “analysis/MH_intro.Rmd”,

## Pre-requisites

You should be familiar with Bayesian inference for a normal mean.

## The Normal Means problem

The “Normal means” problem is as follows: assume we have data $X_j \sim N(\theta_j, s_j^2) \quad (j=1,\dots,n)$ where the standard deviations $$s_j$$ are known, and the means $$\theta_j$$ are to be estimated.

It is easy to show that the maximum likelihood estimate of $$\theta_j$$ is $$X_j$$.

The idea here is that we can do better than the maximum likelihood estimates, by combining information across $$j=1,\dots,n$$.

## The Empirical Bayes approach

The Empirical Bayes (EB) approach to this problem assumes that the $$\theta_j$$ come from some underlying distribution $$g \in G$$ where $$G$$ is some appropriate family of distributions. Here, for simplicity, we will assume $$G$$ is the set of all normal distributions. That is, we assume $$\theta_j \sim N(\mu, V)$$ for some mean $$\mu$$ and variance $$V$$. Of course this assumption is somewhat inflexible, but it is a starting point. More flexible assumptions are possible, but we will stick with the simple normal assumption for now.

If we knew (or were willing to specify) $$\mu,V$$ then it would be easy to do Bayesian inference for $$\theta_j | X_j, \mu, V$$ like this. The idea behind the EB approach is to instead estimate $$\mu,V$$ from the data – specifically, by maximum likelihood estimation. It is called “Empirical Bayes” because you can think of estimating $$\mu,V$$ as “estimating the prior” on $$\theta_j$$ from the data.

### The likelihood

Notice that we can write $$X_j = \theta_j + N(0,s_j^2)$$ and $$\theta_j | \mu,V \sim N(\mu,V)$$. So using the fact that the sum of two normal distributions is normal we have: $X_j | \mu,V \sim N(\mu, V+ s_j^2).$

Assuming that the $$X_j$$ are independent, we can compute the log-likelihood using the following function. Notice that we parameterize in terms of $$\log(V)$$ rather than $$V$$ - this is to make the numerical optimization easier later. Specifically, the optimization over $$\log(V)$$ is
unconstrained, which is often easier to do than the constrained optimization ($$V>0$$).

#' @title the loglikelihood for the EB normal means problem
#' @param par a vector of parameters (mu,log(V))
#' @param x the data vector
#' @param s the vector of standard deviations
nm_loglik = function(par,x,s){
mu = par[1]
V = exp(par[2])
sum(dnorm(x,mu,sqrt(s^2+V),log=TRUE))
}

### Optimizing the likelihood

We use the R function optim to optimize this log-likelihood. (By default optim performs a minimization; here we set fnscale=-1 so that it will maximize the log-likelihood.) If we wanted to make the optimization more reliable we should compute the gradient of the log likelihood, but for now we will try with just providing it the function.

ebnm_normal = function(x,s){
par_init = c(0,0)
res = optim(par=par_init,fn = nm_loglik,method="BFGS",control=list(fnscale=-1),x=x,s=s)
return(res\$par)
}

Here, to illustrate we run this on a simulated example with $$\mu=1,V=7$$.

set.seed(1)
mu = 1
V = 7
n = 1000
t = rnorm(n,mu,sqrt(V))
s = rep(1,n)
x = rnorm(n,t,s)
res = ebnm_normal(x,s)
c(res[1],exp(res[2]))
[1] 0.952920 7.606758

TODO: complete this by computing the posterior distributions $$\theta_j | \mu_j, X_j, \hat{V}$$.

sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.1

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] workflowr_1.2.0 Rcpp_1.0.0      digest_0.6.18   rprojroot_1.3-2
[5] backports_1.1.3 git2r_0.24.0    magrittr_1.5    evaluate_0.12
[9] stringi_1.2.4   fs_1.2.6        whisker_0.3-2   rmarkdown_1.11
[13] tools_3.5.2     stringr_1.3.1   glue_1.3.0      xfun_0.4
[17] yaml_2.2.0      compiler_3.5.2  htmltools_0.3.6 knitr_1.21

This site was created with R Markdown