Spline regression in R language and draw a restricted cubic bar graph

Clinically, the dependent variable and the clinical outcome are sometimes not linearly related, and the regression model has an important assumption that the independent variable and the dependent variable are linearly related, so the nonlinear relationship model is limited by regression analysis. Therefore, a better solution is to fit the nonlinear relationship between the independent variable and the dependent variable. Restricted cubic spline (RCS) is one of the most common methods to analyze the nonlinear relationship.
Recently, a fan asked whether ordinal multi-categorical variables can be applied to restricted cube bar graphs? is allowed. We can show the difference of data by grouping visualization.
Need to use the three packages of splines, rms, ggplot2, you must download them first,
first load the package and import our data:

library(ggplot2)
library(splines)
library(rms)
be<-read.csv("E:/r/test/qztp2.csv",sep=',',header=TRUE)
names(be)

Insert picture description here
Build spline regression

model.spline <- lm(be$Gestational.week ~ rcs(be$HB))#建立样条回归
summary(model.spline)

Pay attention to this P value, less than 0.05 indicates a non-linear relationship.
Insert picture description here
Use anova(model.spline) to view the same,
Insert picture description here
and then use ggplot2 to draw the graph.

ggplot(be, aes(HB, Gestational.week)) +  
  geom_point()+
  stat_smooth(method = lm, formula = y ~ rcs(x,4)) ##绘制样条回归拟合效果图

Insert picture description here
We can also set the cut-off point and draw the picture by ourselves. It feels that there is no software that can set it by itself.

model.spline1 <- lm(be$Gestational.week ~ rcs(be$HB, c(20,100,126,150)))#建立样条回归,设置4个节点
ggplot(be, aes(HB, Gestational.week)) +  
  geom_point()+
  stat_smooth(method = lm, formula = y ~ rcs(x, c(20,100,126,150))) ##绘制样条回归拟合效果图

Insert picture description here
You can also further beautify the graphics

ggplot(be, aes(HB, Gestational.week,fill=Prenatal.hemorrhage,size=Prenatal.hemorrhage1)) +  
  geom_point(shape=21,size=4,col="black")+
  stat_smooth(method = lm, formula = y ~ rcs(x,4)) ##美化一下图形

Insert picture description here
Adding that we want to know the impact of the two classification indicators of bleeding and no bleeding on the result Y, we can add a grouping variable to perceive the amount of bleeding from the shade of the color. At present, the confidence intervals of the two variables almost overlap. Whether bleeding should be the result No effect, the aggregate depends on the model index.

ggplot(be, aes(HB, Gestational.week,group=Prenatal.hemorrhage,fill=Prenatal.hemorrhage1)) +  
  geom_point(shape=21,size=4,col="black")+
  stat_smooth(method = lm, formula = y ~ rcs(x,4)) ##分组表示

Insert picture description here
The colors of the two groups are a bit similar, it is not easy to distinguish, we can also modify it.
Insert picture description here
More exciting articles are available in the public account: zero-based research
Insert picture description here

Guess you like

Origin blog.csdn.net/dege857/article/details/113842472