Search code examples
rsplinenon-linear-regression

How to extract values of regression curve in R?


How to extract the x- and y-values of a regression curve in R?

Generate some example data and combine in data frame:

x <- c(54,  54,  54,  54,  54,  54,  54,  72,  72,  72,  90,  90,  90,  90,  90,  90,  90,  90,  90,  72,  72,  72, 72, 72,  54,  54,  54,  54,  54,  54,  54,  72,  90,  90,  90,  90,  90,  90,  90,  90, 108, 126, 144, 144, 144, 144)
y <- c(15, 15, 15,  7.50,  7.50,  7.50, 25, 15, 15,  7.50,  0,  0,  0,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3, 15,  0, 25, 15,  7.50,  7.50,  0,  7.50,  7.50,  7.50,  7.50,  7.50,  0,  3,  3,  3,  3, 15,  3, 15,  3,  3,  7.50)
df_xy <- as.data.frame(cbind(x, y))
df_xy

Visualize the data:

plot(df_xy[,2], df_xy[,1])

enter image description here

Regression analysis (code from here)

Generate training and test data:

library(tidyverse)
library(caret)
set.seed(123)
training.samples <- df_xy[,1] %>%
  createDataPartition(p = 0.8, list = FALSE)
train.data  <- df_xy[training.samples, ]
test.data <- df_xy[-training.samples, ]

Apply spline regression:

library(splines)
knots <- quantile(train.data$y, p = c(0.25, 0.5, 0.75))
model <- lm (x ~ bs(y, knots = knots), data = train.data)

Visualize:

ggplot(train.data, aes(y, x) ) +
  geom_point() +
  stat_smooth(method = lm, formula = y ~ splines::bs(x, df =3))

enter image description here

Is there a way to extract the x- and respective y-values of that curve (blue line)? Furthermore, might there be a base R option to visualize the spline regression?

Addition to the question:

Which uncertainty/error bars are shown in this display? And how can this be specified in the respective code?

How can the x- and respective y-values of the uncertainty band (grey area) be extracted?


Solution

  • If you save the plot as gg1, then

    ggplot_build(gg1)$data[[2]][,c("x","y")]
    

    will get you the x and y coordinates of the curve.