将函数与R中的向量参数积分

时间:2022-01-28 16:00:35

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:

如果我们心中有一个特定的时间点x,计算所有7个病人的手术次数是很容易的:

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.

但要得到总体的平均生存值或个体的预期生存值,我需要计算曲线下的面积,曲线由函数surv给出。我需要计算x=0和x=Inf的极限下的面积。我需要为所有7个病人做这个。

The other * 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

fun_integration是要集成的函数

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

vectorize。args是要向量化并传递给fun_integration的参数

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

vec是作为要传递到fun_integration中的参数的值的向量

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?

你有什么建议吗?

1 个解决方案

#1


4  

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

#1


4  

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