gpt4 book ai didi

r - 生成/绘制对数正态生存函数

转载 作者:行者123 更新时间:2023-12-04 11:22:19 26 4
gpt4 key购买 nike

我在 SAS LIFEREG 中有一个加速故障时间模型,我想绘制它。因为 SAS 在绘图方面非常糟糕,我想实际重新生成 R 中曲线的数据并将它们绘制在那里。 SAS 提出了一个尺度(在指数分布固定为 1 的情况下)、一个截距和一个回归系数,用于暴露或未暴露人群。

有两条曲线,一条用于暴露人群,另一条用于未暴露人群。其中一个模型是指数分布,我已经生成了如下数据和图表:

intercept <- 5.00
effect<- -0.500
data<- data.frame(time=seq(0:180)-1)
data$s_unexposed <- apply(data,1,function(row) exp(-(exp(-intercept))*row[1]))
data$s_exposed <- apply(data,1,function(row) exp(-(exp(-(intercept+effect))*row[1])))

plot(data$time,data$s_unexposed, type="l", ylim=c(0,1) ,xaxt='n',
xlab="Days since Infection", ylab="Percent Surviving", lwd=2)
axis(1, at=c(0, 20, 40, 60, 80, 100, 120, 140, 160, 180))
lines(data$time,data$s_exposed, col="red",lwd=2)
legend("topright", c("ICU Patients", "Non-ICU Patients"), lwd=2, col=c("red","black") )

这给了我这个:

enter image description here

不是有史以来最漂亮的图表,但我真的不知道我对 ggplot2 的了解足以修饰它。但更重要的是,我有第二组数据来自对数正态分布,而不是指数分布,而我为它生成数据的尝试完全失败了 - 将 cdf 纳入正态分布等它超出了我的 R 技能。

任何人都能够使用相同的数字和 1 的比例参数指出我正确的方向?

最佳答案

对数正态模型在时间 t 的生存函数可以用 R 表示为 1 - plnorm() ,其中 plnorm()是对数正态累积分布函数。为了说明这一点,为了方便起见,我们首先将您的绘图放入一个函数中:

## Function to plot aft data
plot.aft <- function(x, legend = c("ICU Patients", "Non-ICU Patients"),
xlab = "Days since Infection", ylab="Percent Surviving", lwd = 2,
col = c("red", "black"), at = c(0, 20, 40, 60, 80, 100, 120, 140, 160, 180),
...)
{
plot(x[, 1], x[, 2], type = "l", ylim = c(0, 1), xaxt = "n",
xlab = xlab, ylab = ylab, col = col[2], lwd = 2, ...)
axis(1, at = at)
lines(x[, 1], x[, 3], col = col[1], lwd=2)
legend("topright", legend = legend, lwd = lwd, col = col)
}

接下来,我们将指定系数、变量和模型,然后生成指数和对数正态模型的生存概率:
## Specify coefficients, variables, and linear models
beta0 <- 5.00
beta1 <- -0.500
icu <- c(0, 1)
t <- seq(0, 180)
linmod <- beta0 + (beta1 * icu)
names(linmod) <- c("unexposed", "exposed")

## Generate s(t) from exponential AFT model
s0.exp <- dexp(exp(-linmod["unexposed"]) * t)
s1.exp <- dexp(exp(-linmod["exposed"]) * t)

## Generate s(t) from lognormal AFT model
s0.lnorm <- 1 - plnorm(t, meanlog = linmod["unexposed"])
s1.lnorm <- 1 - plnorm(t, meanlog = linmod["exposed"])

最后,我们可以绘制生存概率:
## Plot survival
plot.aft(data.frame(t, s0.exp, s1.exp), main = "Exponential model")
plot.aft(data.frame(t, s0.lnorm, s1.lnorm), main = "Log-normal model")

以及由此产生的数字:

Exponential model

Log-normal model

注意
plnorm(t, meanlog = linmod["exposed"])

是相同的
pnorm((log(t) - linmod["exposed"]) / 1) 

这是对数正态生存函数的规范方程中的 Φ:S(t) = 1 − Φ((ln(t) − µ)/σ)

我相信您知道,有许多 R 包可以处理具有左删失、右删失或间隔删失的加速失效时间模型,如 survival task view 中所列。 ,如果您碰巧偏爱 R 而不是 SAS。

关于r - 生成/绘制对数正态生存函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11103637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com