【问题标题】:Integrate function with vector argument in R将函数与 R 中的向量参数集成
【发布时间】:2014-08-17 17:21:31
【问题描述】:

我和之前的帖子有类似的挑战:How to pass vector to integrate function

我有一个函数,我想对曲线下的面积进行积分。

一、【生存】功能:

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

score 来自风险计算器,它会调整生存估计。患者有不同的分数,例如:

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

如果我们有一个特定的时间点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

但是为了得到一个群体的平均存活率或个体的预期存活率,我需要计算曲线下的面积,其中曲线由函数surv 给出。我需要计算x=0x=Inf 范围内的面积。我需要为所有 7 名(在本例中)患者执行此操作。

我引用的另一个 stackoverflow 帖子也有类似的问题。目前尚不清楚该解决方案是否可以帮助我。我在下面介绍它:

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

fun_integrate是要集成的功能

vectorize.args 是要矢量化并传递给 fun_integrate 的参数

vec 是作为要传递给 fun_integrate 的参数的值向量

我不知道细分是什么,但我认为它并不重要。

我尝试使用以下方法执行此操作:

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

我尝试了不同的修改,但似乎没有任何结果。

你有什么建议吗?

【问题讨论】:

    标签: r function vector integrate survival-analysis


    【解决方案1】:

    您的操作顺序错误。您需要创建一个函数来计算给定分数的积分,并将其向量化。

    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
    

    【讨论】:

    • 你刚刚拯救了白天和黑夜。干杯。
    猜你喜欢
    • 2021-08-06
    • 1970-01-01
    • 2020-11-08
    • 2021-07-03
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多