At present, the new 2.8 version of the ggrcs package written by me has been launched on CRAN, and currently supports logistic regression (logistic regression), cox regression and multiple linear regression. Added singlercs function to draw separate rcs curves (restricted cubic splines).
You can use the code to install if you need it
install.packages("ggrcs")
If an old version is installed, you can upgrade it through Rstudio
so that you can upgrade to the latest version.
Some fans sent me private messages saying that the density function of the histogram is not needed, and they hope to create a function that draws rcs alone. Therefore, the singlercs function was added in version 2.9. Single means single and simple, that is, simply draws RCS, and the drawing method is basically the same as ggrcs, just without the histogram and left axis. Let's demonstrate it below. We only introduce new functions. For other functions, please refer to previous articles.
First import the package and data, here use the smoking data that comes with the ggrcs package
library(rms)
library(ggplot2)
library(scales)
library(ggrcs)
dt<-smoke
dd<-datadist(dt)
options(datadist='dd')
fit<- cph(Surv(time,status==1) ~ rcs(age,4)+gender, x=TRUE, y=TRUE,data=dt)
This is the smoking data that comes with the R package. Suppose we want to understand the relationship between age and smoking incidence
singlercs(data=dt,fit=fit,x="age")
Originally, I wanted to add a horizontal line at the beginning, but it is not convenient for you to modify it in the later stage. After thinking about it, I don’t want to add it. You can add it after generating a picture. This is also very convenient, and it is also convenient for you to modify the style of the line, which is flexible.
p<-singlercs(data=dt,fit=fit,x="age")
p+geom_hline(yintercept=1, linetype=2,linewidth=1)
You can also use my self-written cut.tab function to generate turning points
fit1 <-coxph(Surv(time,status==1) ~ age,data=dt)
cut.tab(fit1,"age",dt)
Then add the turning dotted line
p+geom_vline(aes(xintercept=38.449),colour="#BB0000", linetype="dashed")+
geom_hline(yintercept=1, linetype=2,linewidth=1)
Generate a table of the turning points, showing that the age turns here at 38.449 years, with an OR of 0.956 before and 1.055 after. A likelihood ratio test of less than 0.05 indicates that the curve is meaningful.
For the specific usage of the cut.tab function, please refer to " cox regression RCS threshold effect function cut.tab1.3 release "
Singlercs function can also be flexibly modified to change the color of lines and credible intervals
singlercs(data=dt,fit=fit,x="age",ribcol="green")
Change Credible Interval Transparency
singlercs(data=dt,fit=fit,x="age",ribcol="green",ribalpha=0.2)
Change axes and titles
singlercs(data=dt,fit=fit,x="age",histcol="blue",
histbinwidth=1,ribcol="green",ribalpha=0.5,xlab ="年龄",ylab="死亡率",title ='年龄与死亡率关系')
add p value
singlercs(data=dt,fit=fit,x="age",histcol="blue",
histbinwidth=1,ribcol="green",ribalpha=0.5,xlab ="年龄",
ylab="死亡率",title ='年龄与死亡率关系',P.Nonlinear=T,Pvalue="<0.05")
Adjust the position of the P value display
singlercs(data=dt,fit=fit,x="age",histcol="blue",
histbinwidth=1,ribcol="green",ribalpha=0.5,xlab ="年龄",
ylab="死亡率",title ='年龄与死亡率关系',P.Nonlinear=T,Pvalue="<0.05",xP.Nonlinear=25,yP.Nonlinear=10)
Let's introduce the classification (two) RCS curves. If you don't set the color, the default is red and green.
singlercs(data=dt,fit=fit,x="age",group="gender")
change color
singlercs(data=dt,fit=fit,x="age",group="gender",groupcol=c("red","blue"))
change transparency
singlercs(data=dt,fit=fit,x="age",group="gender",groupcol=c("red","blue"),ribalpha=0.5)
Change axes and titles
singlercs(data=dt,fit=fit,x="age",group="gender",groupcol=c("red","blue"),ribalpha=0.5,
xlab ="年龄",ylab="死亡率",title ='年龄与死亡率关系')
Add P value and modify the position of P value in the picture
singlercs(data=dt,fit=fit,x="age",group="gender",groupcol=c("red","blue"),ribalpha=0.5,
xlab ="年龄",ylab="死亡率",title ='年龄与死亡率关系',P.Nonlinear=T,Pvalue="<0.05",
xP.Nonlinear=25,yP.Nonlinear=7)
Change tab name
singlercs(data=dt,fit=fit,x="age",group="gender",groupcol=c("red","blue"),ribalpha=0.5,
xlab ="年龄",ylab="死亡率",title ='年龄与死亡率关系',P.Nonlinear=T,Pvalue="<0.05",
xP.Nonlinear=25,yP.Nonlinear=7,twotag.name= c("m","f"))
Well, the new version is introduced here, and I introduced the cox regression model here. The method of logistic regression and linear regression is also the same. It is worth mentioning that because there is no need to draw a density map, the rcs of linear regression drawn by the singlercs function is better than that of ggrcs. If using ggrcs to make a linear regression rcs curve is not ideal, you can consider it Use the singlercs function at once.
If you have any suggestions or if there is something wrong with the R package, you are welcome to let me know.