RCS-LRM

Authors

[Editor] Hu Zheng;

[Contributors]

Note

Hiplot website

This page is the tutorial for source code version of the Hiplot RCS-LRM plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:

https://hiplot.cn/basic/rcs-lrm?lang=en

Nonlinear regression analysis.

Setup

  • System Requirements: Cross-platform (Linux/MacOS/Windows)

  • Programming language: R

  • Dependent packages: rms; survival; ggplot2; stringr

# Install packages
if (!requireNamespace("rms", quietly = TRUE)) {
  install.packages("rms")
}
if (!requireNamespace("survival", quietly = TRUE)) {
  install.packages("survival")
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
  install.packages("ggplot2")
}
if (!requireNamespace("stringr", quietly = TRUE)) {
  install.packages("stringr")
}

# Load packages
library(rms)
library(survival)
library(ggplot2)
library(stringr)

Data Preparation

# Load data
data <- read.delim("files/Hiplot/152-rcs-lrm-data.txt", header = T)

# Convert data structure
data <- na.omit(data)
ex <- set::not(colnames(data), c("main", "group"))
ex <- str_c(ex, collapse = "+")
dd <<- datadist(data)
options(datadist = "dd")
for (i in 3:5) {
  fit <- lrm(as.formula(paste0("group~rcs(main,nk=i,inclx = T)+", ex, collapse = "+")), data = data, x = TRUE)
  tmp <- AIC(fit)
  if (i == 3) {
    AIC <- tmp
    nk <<- 3
  }
  if (tmp < AIC) {
    AIC <- tmp
    nk <<- i
  }
}
fit <- lrm(as.formula(paste0("group~rcs(main,nk=nk,inclx = T)+", ex, collapse = "+")), data = data, x = TRUE)
dd$limits$main[2] <- median(data$main)
fit <- update(fit)
orr <- Predict(fit, main, fun = exp, ref.zero = TRUE)

# View data
head(data)
  main   X2 X3 group
1  100 0.90  0     1
2   90 0.65  1     1
3  400 1.36  0     1
4  200 0.83  0     1
5  300 1.38  0     1
6  200 0.69  0     1

Visualization

# RCS-LRM
p <- ggplot() +
  geom_line(data = orr, aes(main, yhat), linetype = "solid", size = 1, alpha = 1,
            colour = "#FF0000") +
  geom_ribbon(data = orr, aes(main, ymin = lower, ymax = upper), alpha = 0.6, 
              fill = "#FFC0CB") +
  geom_hline(yintercept = 1, linetype = 2, size = 0.5) +
  geom_vline(xintercept = dd$limits$main[2], linetype = 2, size = 0.5) +
  labs(x = "main", y = "Odds Ratio(95%CI)") +
  theme_bw() +
  theme(text = element_text(family = "Arial"),
        plot.title = element_text(size = 12,hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        axis.text.x = element_text(angle = 0, hjust = 0.5,vjust = 1),
        legend.position = "right",
        legend.direction = "vertical",
        legend.title = element_text(size = 10),
        legend.text = element_text(size = 10))
 
p
FigureΒ 1: RCS-LRM