title: Integrate over an integral in R
body: <p>I want to solve the following in R:</p>
  
  <p>&int;<sub>0</sub><sup><em>H</em></sup> [&pi;(<i>t</i>) &int;<sub><i>t</i></sub><sup><em>H</em></sup> <i>A</i>(<i>x</i>) dx ] dt </p>
  
  <p>Where &pi;(t) is the prior and A(x) is the A function defined below. </p>
  
  <pre><code>prior &lt;- function(t) dbeta(t, 1, 24)
  A     &lt;- function(x) dbeta(x, 1, 4)
  expected_loss &lt;- function(H){
    integrand     &lt;- function(t) prior(t) * integrate(A, lower = t, upper = H)$value
    loss          &lt;- integrate(integrand, lower = 0, upper = H)$value
    return(loss)
  } 
  </code></pre>
  
  <p>Since &pi;(t), A(x) > 0, expected_loss(.5) should be less than expected_loss(1). But this is not what I get:</p>
  
  <pre><code>&gt; expected_loss(.5)
  [1] 0.2380371
  &gt; expected_loss(1)
  [1] 0.0625
  </code></pre>
  
  <p>I'm not sure what I'm doing wrong.</p>