简体   繁体   English

在R中将函数与向量参数集成

[英]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. score来自风险计算器,它调整生存估计。 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很容易:

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 . 但是为了得到一个群体或个人的预期生存的平均生存,我需要的曲线,其中曲线由函数给出下计算面积surv I need to calculate the area under the limits of x=0 and x=Inf . 我需要在x=0x=Inf的限制下计算面积。 And I need to do this for all 7 (in this example) patients. 我需要为所有7名(在这个例子中)患者做这件事。

The other stackoverflow post I referenced has a similar problem. 我引用的另一个stackoverflow帖子也有类似的问题。 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_integrate是要集成的功能

vectorize.args is the arguments to be vectorized and passed to fun_integrate vectorize.args是要向量化并传递给fun_integrate的参数

vec is the vector of values that served as the argument to be passed into the fun_integrate vec是值的向量,用作要传递给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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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