偏差图和残差图

时间:2020-04-26 21:14:52

标签: r plot cox-regression

我正在尝试在比例危害模型的数据(kidtran)上绘制偏差和得分残差图。 1-我尝试使用以下代码绘制带有协变量(种族,性别和性别,通过种族相互作用)的coxph模型的Deviance残差图:

library(KMsurv)
data(kidtran)
kidtran

####Deviance Residuals####
fit<-coxph(Surv(time, delta)~race+gender+race*gender, method="breslow", data=kidtran)
resid.dev<-resid(fit,type="deviance")
par(mfrow = C(1,1))
plot(resid.dev,xlab = "Risk Score", ylab = "Deviance Residual", lwd=1 )
title("Deviance Residual")
resid.dev
abline(h=2.5, col="blue")
abline(h=-2.5, col="blue")

它应该看起来像这样: enter image description here

这是我得到的结果。 enter image description here

除了运行后出现消息错误

par(mfrow = C(1,1))

C(1,1)中的错误:对象不能解释为一个因素

2-我确实绘制了每个协变量的得分残差图:

##score for interaction
fit<-coxph(Surv(time, delta)~race+gender+race*gender, method="breslow", data=kidtran)
resid.score<-resid(fit,type="score")
par(mfrow = C(1,2))
plot(resid.score)
resid.score
abline(h=2.5, col="blue")
abline(h=-2.5, col="blue")

##score for race
fit1<-coxph(Surv(time, delta)~race, method="breslow", data=kidtran)
resid.score<-resid(fit1,type="score")
par(mfrow = C(1,2))
plot(resid.score)
resid.score
abline(h=2.5, col="blue")
abline(h=-2.5, col="blue")

##score for gender
fit2<-coxph(Surv(time, delta)~gender, method="breslow", data=kidtran)
resid.score<-resid(fit2,type="score")
par(mfrow = C(1,2))
plot(resid.score)
resid.score
abline(h=2.5, col="blue")
abline(h=-2.5, col="blue")

结果应如下所示: enter image description here

enter image description here enter image description here

我得到了不同的结果。是我用于绘图的代码还是coxph代码的问题?

1 个答案:

答案 0 :(得分:2)

我的猜测是风险分数应该是预测值(如果我没有正确记住我的统计数据,则是危险比?),在上面的代码中,残差只是一个向量,所以:

##note it's a small c for par
par(mfrow = c(1,1))
plot(predict(fit),resid.dev,xlab = "Risk Score", 
ylab = "Deviance Residual", lwd=1 ,main="Deviance Residual")
abline(h=2.5, col="blue")
abline(h=-2.5, col="blue")

enter image description here

对于第二个图,您需要在x轴上绘制观察数:

fit<-coxph(Surv(time, delta)~race+gender+race*gender, method="breslow",data=kidtran)
resid.score<-resid(fit,type="score")
par(mfrow = c(1,3))
for(i in colnames(resid.score))
plot(1:nrow(resid.score),resid.score[,i],main=i,xlab="obs no")

enter image description here