title: Integrate function with vector argument in R
body: <p>I have a similar challenge to a previous post: <a href="https://stackoverflow.com/questions/12058405/how-to-pass-vector-to-integrate-function">How to pass vector to integrate function</a></p>
  
  <p>I have a function which I want to integrate the area under the curve.</p>
  
  <p>First, the [survival] function:</p>
  
  <pre><code>surv &lt;- function(x,score) exp(-0.0405*exp(score)*x) # probability of survival
  </code></pre>
  
  <p><code>score</code> is from a risk calculator and it adjusts the survival estimate. Patients have different scores so, for example:</p>
  
  <pre><code>score &lt;- c(0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1) # 7 different scores
  </code></pre>
  
  <p>Calculating the <code>surv</code> for all 7 patients is easy, if we have a specific time point <code>x</code> in mind:</p>
  
  <pre><code>surv(5, score) # Survival to year 5
  [1] 0.7161497 0.6914399 0.6651219 0.6371998 0.6077026 0.5766890 0.5442516
  </code></pre>
  
  <p>But to get the mean survival of a population or the expected survival of an individual, I need to calculate the area under the curve, where the curve is given by the function <code>surv</code>. I need to calculate the area under the limits of <code>x=0</code> and <code>x=Inf</code>. And I need to do this for all 7 (in this example) patients. </p>
  
  <p>The other stackoverflow post I referenced has a similar problem. It's not clear that the solution can help me. I present it below:</p>
  
  <pre><code>integrate(Vectorize(fun_integrate,vectorize.args='x'), upper = 3, lower = -3, vec = rnorm(100),subdivisions=10000)
  </code></pre>
  
  <p><code>fun_integrate</code> is the function to be integrated</p>
  
  <p><code>vectorize.args</code> is the arguments to be vectorized and passed to fun_integrate</p>
  
  <p><code>vec</code> is the vector of values that served as the argument to be passed into the fun_integrate</p>
  
  <p>I have no idea what subdivisions is but I assume it's not important.</p>
  
  <p>I try to execute this with the following:</p>
  
  <pre><code>integrate(Vectorize(surv, vectorize.args="score"), lower=0, upper=Inf, score=score)
  Error in integrate(Vectorize(surv, vectorize.args = "score"), lower = 0,  : 
    evaluation of function gave a result of wrong length
  </code></pre>
  
  <p>I have tried different modifications and nothing seems to give a result.</p>
  
  <p>Do you have any suggestions?</p>