R 用不同分布拟合生存密度曲线

R 用不同分布拟合生存密度曲线,r,plot,distribution,survival-analysis,R,Plot,Distribution,Survival Analysis,我正在处理一些对数正态数据,当然,我想展示对数正态分布的结果比其他可能的分布有更好的重叠。基本上,我想用我的数据复制以下图表: 拟合密度曲线在对数(时间)上并置 链接图像的文本描述了拟合每个模型并获得以下参数的过程: 为此,我用上述分布拟合了四个天真的生存模型: survreg(Surv(time,event)~1,dist="family") 并提取了形状参数(α)和系数(β) 关于这个过程,我有几个问题: 1) 这样做对吗?我已经查看了几个R包,但找不到一个作为内置函数绘制密度曲线的包

我正在处理一些对数正态数据,当然,我想展示对数正态分布的结果比其他可能的分布有更好的重叠。基本上,我想用我的数据复制以下图表:

拟合密度曲线在
对数(时间)
上并置

链接图像的文本描述了拟合每个模型并获得以下参数的过程:

为此,我用上述分布拟合了四个天真的生存模型:

survreg(Surv(time,event)~1,dist="family")
并提取了形状参数(α)和系数(β)

关于这个过程,我有几个问题:

1) 这样做对吗?我已经查看了几个R包,但找不到一个作为内置函数绘制密度曲线的包,所以我觉得我一定忽略了一些显而易见的东西

2) 对数正态分布(μ和σ$^2$)对应的值是否只是截距的平均值和方差

3) 如何在R中创建类似的表?(可能这更像是一个堆栈溢出问题)我知道我可以手动
cbind
它们,但我更感兴趣的是从已安装的模型中调用它们
survreg
对象存储系数估计值,但调用
survreg.obj$coverties
会生成一个命名的数字向量(而不仅仅是一个数字)

4) 最重要的是,如何绘制类似的图形?我认为如果我只提取参数并在histrogram上绘制它们会相当简单,但到目前为止没有运气。这篇文章的作者说他根据参数估计了密度曲线,但我只得到了一个点估计——我遗漏了什么?我是否应该在绘图前根据分布手动计算密度曲线

我不知道如何在这种情况下提供mwe,但老实说,我只需要一个通用的解决方案,将多个密度曲线添加到生存数据中。另一方面,如果您认为这会有所帮助,请随意推荐一种mwe解决方案,我将尝试生产一种

谢谢你的意见

编辑:基于eclark的帖子,我取得了一些进展。我的参数是:

Dist = data.frame(
Exponential = rweibull(n = 10000, shape = 1, scale = 6.636684),
Weibull = rweibull(n = 10000, shape = 6.068786, scale = 2.002165),
Gamma = rgamma(n = 10000, shape = 768.1476, scale = 1433.986),
LogNormal = rlnorm(n = 10000, meanlog = 4.986, sdlog = .877)
)
然而,考虑到规模的巨大差异,我得到的是:

回到问题3,这就是我应该如何获得参数的方法吗? 目前我就是这样做的(很抱歉弄得一团糟):


我觉得我特别搞砸了对数正态分布,因为它不是标准的形状和系数串联,而是均值和方差;其思想是使用随机分布函数生成随机变量,然后用输出数据绘制密度函数,下面是一个您需要的示例:

require(ggplot2)
require(dplyr)
require(tidyr)

SampleData <- data.frame(Duration=rlnorm(n = 184,meanlog = 2.859,sdlog = .246)) #Asume this is data we have sampled from a lognormal distribution

#Then we estimate the parameters for different types of distributions for that sample data and come up for this parameters
#We then generate a dataframe with those distributions and parameters
Dist = data.frame(
  Weibull = rweibull(10000,shape = 1.995,scale = 22.386),
  Gamma = rgamma(n = 10000,shape = 4.203,scale = 4.699),
  LogNormal = rlnorm(n = 10000,meanlog = 2.859,sdlog = .246)
)

#We use gather to prepare the distribution data in a manner better suited for group plotting in ggplot2
Dist <- Dist %>% gather(Distribution,Duration)

#Create the plot that sample data as a histogram
G1 <- ggplot(SampleData,aes(x=Duration)) + geom_histogram(aes(,y=..density..),binwidth=5, colour="black", fill="white") 

#Add the density distributions of the different distributions with the estimated parameters
G2 <- G1 + geom_density(aes(x=Duration,color=Distribution),data=Dist)

plot(G2)
require(ggplot2)
需要(dplyr)
需要(三年)

谢谢你的回答。将数字插入配电发电机——一旦有人指出,这是非常明显的!但是,我相信您的代码显示的是插补分布的密度,而不是数据(比如
log(df$duration)
)。因此,我跳过了tidyr位,在运行intercept only模型后提取了形状和系数值,并使用
scale
缩放结果数据帧。不过,这并不完全正确。你能概括一下你的代码,使它与ggplot2一起工作吗?@sfsrc我不知道你的意思是什么,但做了一些调整,以反映直方图来自对数正态过程的一些样本数据。你能检查一下我在问题末尾添加的代码吗?你的代码可以很好地处理你输入的数据,但是我想我把参数和我的弄乱了。嗨,再次,基于Gregor的输入,我会接受这个作为解决方案,因为它满足了问题的这一部分。谢谢。您可能应该阅读
?survreg.distributions
,其中的注释是生存威布尔分布的参数化与
rweibull
中的不同。我也不相信你做的伽马参数化是正确的——这一点似乎比对数正态分布还要远。虽然绘制分布图是一个很好的问题,但如果你在估计参数/找到正确的参数化方面遇到困难,那么这可能是交叉验证中的一个新问题。看起来eclark的回答很好地解决了您的问题,即如何创建一个您想要的图形-现在您需要帮助了解您的分布情况。@Gregor我想您对eclark的贡献是正确的,我只是做了编辑,以便有人指出我的错误时,我能获得更多的洞察力。你的编辑提出了一个全新的问题。你应该只问一个新问题——如果你认为有帮助,你可以复制/粘贴这个问题并链接到它。
require(ggplot2)
require(dplyr)
require(tidyr)

SampleData <- data.frame(Duration=rlnorm(n = 184,meanlog = 2.859,sdlog = .246)) #Asume this is data we have sampled from a lognormal distribution

#Then we estimate the parameters for different types of distributions for that sample data and come up for this parameters
#We then generate a dataframe with those distributions and parameters
Dist = data.frame(
  Weibull = rweibull(10000,shape = 1.995,scale = 22.386),
  Gamma = rgamma(n = 10000,shape = 4.203,scale = 4.699),
  LogNormal = rlnorm(n = 10000,meanlog = 2.859,sdlog = .246)
)

#We use gather to prepare the distribution data in a manner better suited for group plotting in ggplot2
Dist <- Dist %>% gather(Distribution,Duration)

#Create the plot that sample data as a histogram
G1 <- ggplot(SampleData,aes(x=Duration)) + geom_histogram(aes(,y=..density..),binwidth=5, colour="black", fill="white") 

#Add the density distributions of the different distributions with the estimated parameters
G2 <- G1 + geom_density(aes(x=Duration,color=Distribution),data=Dist)

plot(G2)