I am trying to develop slides for a course I am teaching in Org mode compiling to a latex beamer presentation. Overall most things are working fine. But for some reason I am having trouble getting two R plots aligned. Here is how the relevant slide looks currently. The problem should be obvious.
Here is the code for the presentation:
#+TITLE: my class
#+AUTHOR: me
#+EMAIL: [email protected]
#+BEAMER_HEADER: \institute[]{Penn State University}
#+DATE:
#+DESCRIPTION:
#+KEYWORDS:
#+LANGUAGE: en
#+BEAMER_THEME: Boadilla
#+BEAMER_HEADER:
#+latex_class: beamer
#+latex_compiler: pdflatex
#+LaTeX_HEADER: \usepackage{pdfpages}
#+OPTIONS: H:2 toc:t
#+BEAMER_HEADER: \AtBeginSection[]{\begin{frame}<beamer>\vfill\centering\usebeamerfont{title}\insertsectionhead\vfill\end{frame}}
#+BEAMER_HEADER: \usepackage{minted}
#+BEAMER_HEADER: \usemintedstyle{emacs}
#+STARTUP: beamer
#+begin_src R :exports none
library(tidyverse)
set.seed(123)
#+end_src
* Bayesian Statistics
** Metropolis Algorithm Example
\begin{align*}
y_i &\sim N(\beta x_i, \sigma^2) \\
\beta &\sim N(\mu, \tau^2)
\end{align*}
*** XXX :BMCOL:
:PROPERTIES:
:BEAMER_col: .55
:END:
#+begin_src R :exports code :session "*R*"
# Simulate some data
n <- 100
beta <- 4.1
sigma2 <- 1.3
x <- rnorm(n)
y <- rnorm(n, mean=beta*x,
sd=sqrt(sigma2))
#+end_src
*** XXX :BMCOL:
:PROPERTIES:
:BEAMER_col: .45
:END:
#+begin_src R :exports results :session "*R*" :results graphics file :file figures/foo.pdf
plot(x,y)
#+end_src
#+RESULTS:
** Metropolis Algorithm Example
\begin{align*}
y_i &\sim N(\beta x_i, \sigma^2) \\
\beta &\sim N(\mu, \tau^2)
\end{align*}
\tiny
#+begin_src R :exports code :session "*R*"
# For the moment we deal only with
# simple regression (x is univariate)
log_p_tilde <- function(beta, y, x, sigma2, mu, tau2){
loglik <- dnorm(beta, mean=mu,
sd=sqrt(tau2),
log = TRUE)
loglik <- loglik +
sum(dnorm(y, mean=beta*x,
sd=sqrt(sigma2),
log=TRUE))
return(loglik)
}
# Now the proposal function J(theta | theta^(s))
proposal <- function(beta_s, delta2){
return( rnorm(1, mean=beta_s, sd=delta2) )
}
#+end_src
\normalsize
** Metropolis Algorithm Example
*** XXX :BMCOL:
:PROPERTIES:
:BEAMER_col: .5
:END:
\tiny
#+begin_src R :exports code :session "*R*" :results silent
n_samples <- 4000
beta <- rep(NA, n_samples)
# Initialize chain
beta[1] <- 0
# Create partial function to make code easier to read
lpt <- function(beta) log_p_tilde(beta, y, x, 1.3,
# Choose priors
mu = 0, tau2 = 1)
# Run MCMC
for (s in 2:(n_samples+1)){
# Propose
beta_prop <- proposal(beta[s-1], delta2 = .1)
# Compute Acceptance Ratio (log-scale)
r <- lpt(beta_prop) - lpt(beta[s-1])
# Sample uniform
u <- runif(1, min=0, max=1)
# Accept/Reject
if (u <= exp(r)){
beta[s] <- beta_prop
} else {
beta[s] <- beta[s-1]
}
}
# Drop "Burn-in" rid of initial sample which we chose
beta <- beta[-(1:n_samples/2)]
#+end_src
\normalsize
*** XXX :BMCOL:
:PROPERTIES:
:BEAMER_col: .5
:END:
#+begin_src R :exports results :session "*R*" :results graphics file :file figures/beta-posterior-trace.pdf :width 3 :height 3
plot(beta)
#+end_src
#+begin_src R :exports results :session "*R*" :results graphics file :file figures/beta-posterior-density.pdf :width 3 :height 3
plot(density(beta))
#+end_src
I would greatly appreciate any help in debugging this issue!
EDIT: After reading this post Change emacs generated image size in org-mode export, I came up with the following solution (which does not work):
#+name: beta-posterior-trace
#+begin_src R :session "*R*" :file figures/beta-posterior-trace.pdf
plot(beta)
#+end_src
#+ATTR_LATEX: :width 0.3\linewidth
#+results: beta-posterior-trace
[[file:figures/beta-posterior-trace]]
#+name: beta-posterior-density
#+begin_src R :session "*R*" :file figures/beta-posterior-density.pdf
plot(density(beta))
#+end_src
#+ATTR_LATEX: :width 0.3\linewidth
#+results: beta-posterior-density
[[file:figures/beta-posterior-density]]
:exports results
on the last two source code blocks. Also, the.pdf
suffix is missing from the links. I would just delete the two links completely.