简体   繁体   中英

Integrate function with vector argument in R

I have a similar challenge to a previous post: How to pass vector to integrate function

I have a function which I want to integrate the area under the curve.

First, the [survival] function:

surv <- function(x,score) exp(-0.0405*exp(score)*x) # probability of survival

score is from a risk calculator and it adjusts the survival estimate. Patients have different scores so, for example:

score <- c(0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1) # 7 different scores

Calculating the surv for all 7 patients is easy, if we have a specific time point x in mind:

surv(5, score) # Survival to year 5
[1] 0.7161497 0.6914399 0.6651219 0.6371998 0.6077026 0.5766890 0.5442516

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 surv . I need to calculate the area under the limits of x=0 and x=Inf . And I need to do this for all 7 (in this example) patients.

The other stackoverflow post I referenced has a similar problem. It's not clear that the solution can help me. I present it below:

integrate(Vectorize(fun_integrate,vectorize.args='x'), upper = 3, lower = -3, vec = rnorm(100),subdivisions=10000)

fun_integrate is the function to be integrated

vectorize.args is the arguments to be vectorized and passed to fun_integrate

vec is the vector of values that served as the argument to be passed into the fun_integrate

I have no idea what subdivisions is but I assume it's not important.

I try to execute this with the following:

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

I have tried different modifications and nothing seems to give a result.

Do you have any suggestions?

You're doing it in the wrong order. You need to create a function that calculates the integral, for a given score, and vectorize that.

surv <- function(x,score) exp(-0.0405*exp(score)*x) # probability of survival
area <- function(score) integrate(surv,lower=0,upper=Inf,score=score)$value
v.area <- Vectorize(area)

scores <- c(0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1)  # 7 different scores
v.area(scores)
# [1] 14.976066 13.550905 12.261366 11.094542 10.038757  9.083443  8.219039

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM