rm(list = ls())
library(rms)
head(lung)
dd <- datadist(lung)
options(datadist='dd')
lung$status1 <- lung$status-1
S <- Surv(lung$time,lung$status1==1)
fit1a<- cph(S ~ rcs(age,3),data=lung)
fit1a<- cph(S ~ rcs(age,3)+ph.karno,data=lung)#矫正混杂的模型
dd$limits$age[2] <- 60 #这里是设置参考点,也就是HR为1的点,常见的为中位数或者临床有意义的点
fit1a=update(fit1a)
anova(fit1a)
Pre0 <-rms::Predict(fit1a,age,fun=exp,type="predictions",ref.zero=T,conf.int = 0.95,digits=2)
par(mar=c(3,4,1,5),new=T)
#直方图
hist(lung$age,
axes = F,
xlab = "",
ylab = "",
xlim = c(0,100),
breaks = 20,#直方图的间隔,可以调小
# col = "#dba880"灰褐色
col = "pink",#直方图柱子的颜色
density = NULL, #用线条填充柱子
#angle =45,
main="",
# border = "#92523c"
border = "red",#直方图的边缘颜色
freq = F)
axis(4)
par(new=T)
plot(Pre0[,1],axes=T,Pre0$yhat,type='l',lty=1,lwd=2,
col= "#177cb0",
xlim = c(0,120),
ylim=c(0,5),xlab='age',ylab='HR (95%CI)')+theme_bw()
lines(Pre0[,1],Pre0$lower,type='l',lty=2,lwd=2,col="#94d2ef")
lines(Pre0[,1],Pre0$upper,type='l',lty=2,lwd=2,col="#94d2ef")
points(x=dd$limits$age[2], ##散点X轴坐标
y=1, ####散点y轴坐标
pch=19,##pch是点的形状,数字从1-25代表不同的点形状。
col='black')
abline(#v=90,
h=1,
col='black',lty=2,lwd=1.2)
mtext('freqency (%)',side=4,line = 3,outer = F)