根据survreg预测绘制生存曲线
我对生存分析比较陌生,曾被一些标准的电信公司客户流失数据示例使用,下面的示例名为“电信公司”:根据survreg预测绘制生存曲线,r,ggplot2,survival-analysis,R,Ggplot2,Survival Analysis,我对生存分析比较陌生,曾被一些标准的电信公司客户流失数据示例使用,下面的示例名为“电信公司”: telco <- read.csv(text = "State,Account_Length,Area_Code,Intl_Plan,Day_Mins,Day_Calls,Day_Charge,Eve_Mins,Eve_Calls,Eve_Charge,Night_Mins,Night_Calls,Night_Charge,Intl_Mins,Intl_Calls,Intl_Charge,Cus
telco <- read.csv(text = "State,Account_Length,Area_Code,Intl_Plan,Day_Mins,Day_Calls,Day_Charge,Eve_Mins,Eve_Calls,Eve_Charge,Night_Mins,Night_Calls,Night_Charge,Intl_Mins,Intl_Calls,Intl_Charge,CustServ_Calls,Churn
IN,65,415,no,129.1,137,21.95,228.5,83,19.42,208.8,111,9.4,12.7,6,3.43,4,TRUE
RI,74,415,no,187.7,127,31.91,163.4,148,13.89,196,94,8.82,9.1,5,2.46,0,FALSE
IA,168,408,no,128.8,96,21.9,104.9,71,8.92,141.1,128,6.35,11.2,2,3.02,1,FALSE
MT,95,510,no,156.6,88,26.62,247.6,75,21.05,192.3,115,8.65,12.3,5,3.32,3,FALSE
IA,62,415,no,120.7,70,20.52,307.2,76,26.11,203,99,9.14,13.1,6,3.54,4,FALSE
NY,161,415,no,332.9,67,56.59,317.8,97,27.01,160.6,128,7.23,5.4,9,1.46,4,TRUE")
…以获得每个客户的预测寿命
我正在努力解决的问题是如何想象这一点的生存曲线。有没有一种方法(最好是在ggplot2中)可以从我掌握的数据中实现这一点?这里有一个
base
R版本,它绘制了预测生存曲线。我更改了公式
,因此每行的曲线不同
> # change setup so we have one covariate
> telcosurvreg = survreg(
+ Surv(Account_Length, Churn) ~ Eve_Charge, dist = "gaussian", data = telco)
> telcosurvreg # has more than an intercept
Call:
survreg(formula = Surv(Account_Length, Churn) ~ Eve_Charge, data = telco,
dist = "gaussian")
Coefficients:
(Intercept) Eve_Charge
227.274695 -3.586121
Scale= 56.9418
Loglik(model)= -12.1 Loglik(intercept only)= -12.4
Chisq= 0.54 on 1 degrees of freedom, p= 0.46
n= 6
>
> # find linear predictors
> vals <- predict(telcosurvreg, newdata = telco, type = "lp")
>
> # use the survreg.distributions object. See ?survreg.distributions
> x_grid <- 1:400
> sur_curves <- sapply(
+ vals, function(x)
+ survreg.distributions[[telcosurvreg$dist]]$density(
+ (x - x_grid) / telcosurvreg$scale)[, 1])
>
> # plot with base R
> matplot(x_grid, sur_curves, type = "l", lty = 1)
#更改设置,使我们有一个协变量
>telcosurvreg=survreg(
+Surv(账户长度,客户流失)~Eve\u费用,dist=“gaussian”,data=telco)
>telcosurvreg#不仅仅是一个截获
电话:
survreg(公式=Surv(账户长度、客户流失)~Eve费用,数据=电信公司,
dist=“高斯”)
系数:
(截获)Eve_冲锋
227.274695 -3.586121
比例=56.9418
Loglik(型号)=-12.1 Loglik(仅限截距)=-12.4
在1个自由度上,Chisq=0.54,p=0.46
n=6
>
>#寻找线性预测值
>瓦尔斯
>#使用survreg.distributions对象。见?survreg
>x_栅格曲线
>#以R为底的绘图
>matplot(x网格,表面曲线,type=“l”,lty=1)
结果如下
可能会对您有所帮助。该帖子犯的错误与此问题相同。不要在survreg函数之外构建Surv对象。
> # change setup so we have one covariate
> telcosurvreg = survreg(
+ Surv(Account_Length, Churn) ~ Eve_Charge, dist = "gaussian", data = telco)
> telcosurvreg # has more than an intercept
Call:
survreg(formula = Surv(Account_Length, Churn) ~ Eve_Charge, data = telco,
dist = "gaussian")
Coefficients:
(Intercept) Eve_Charge
227.274695 -3.586121
Scale= 56.9418
Loglik(model)= -12.1 Loglik(intercept only)= -12.4
Chisq= 0.54 on 1 degrees of freedom, p= 0.46
n= 6
>
> # find linear predictors
> vals <- predict(telcosurvreg, newdata = telco, type = "lp")
>
> # use the survreg.distributions object. See ?survreg.distributions
> x_grid <- 1:400
> sur_curves <- sapply(
+ vals, function(x)
+ survreg.distributions[[telcosurvreg$dist]]$density(
+ (x - x_grid) / telcosurvreg$scale)[, 1])
>
> # plot with base R
> matplot(x_grid, sur_curves, type = "l", lty = 1)