How to Get Confidence Interval for Smooth.Spline

add confidence interval to splines from quantile regression

You're not actually using results from your model in the plot. So I'm not entirely sure what you're trying to do.

You can use predict to get quantile regression model-based predictions including confidence intervals, and plot those with geom_ribbon:

# Get model-based predictions
pred <- as.data.frame(predict(model, data.frame(x = mydata$x, age = mydata$age, sex = mydata$sex), interval = "confidence"));
pred$x <- mydata$x;

#plot
sp <- c(25,50,75);
ggplot(mydata, aes(x=x,y=y)) +
geom_point() +
geom_line(data = pred, aes(x = x, y = fit)) +
geom_ribbon(data = pred, aes(ymin = lower, ymax = higher, x = x), alpha = 0.4) +
geom_quantile(formula = y ~ ns(x, knots = sp), quantiles = 0.5, se = T);

Sample Image

overlay two qplots (smoother and confidence intervals) in R

You can have the same plot with ggplot

E.g: the first plot:

plotdata %>% ggplot( aes(x, y1)) + geom_point() + geom_smooth(method = "rlm")

and you can play from now on with the more flexible ggplot

How can I smoothen the edges of the shaded area in ggplot?

You can create splines and use them with geom_ribbon. Or just leave the shading and use ggalt::geom_xspline

library(tidyverse)

set.seed(10)
y <- runif(18, 0, 5) # Odds ratios
spread <- runif(18, 0, 2) # Confidence intervals
CI_hi <- y + spread/2 # The upper level of CIs
CI_lo <- y - spread/2 # Lower level of CIs
x <- seq(from=0.3, to=2.0, by=0.1) # x-values

foo <- data.frame(x,y, CI_hi, CI_lo)

foolong <- foo %>%
pivot_longer(names_to = 'CI', values_to = 'val', contains("CI"))
# use ggalt::geom_xspline

ggplot(data = NULL, aes(x=x, y=y)) +
geom_errorbar(aes(ymin=CI_lo, ymax=CI_hi), width=.0, color="deepskyblue3") +
geom_polygon(aes(x=append(x,rev(x)), y=append(CI_hi,rev(CI_lo))), alpha=0.2) +
ggalt::geom_xspline(data = foolong, aes(x = x, y = val, group = CI))

Sample Image

# make your own splines

spline_x <- spline(foo$x, foo$CI_hi)[["x"]]
spline_hi<- spline(foo$x, foo$CI_hi)[["y"]]
spline_lo<- spline(foo$x, foo$CI_lo)[["y"]]

foo_ribbon <- data.frame(spline_x, spline_hi, spline_lo)

ggplot(data = NULL, aes(x=x, y=y)) +
geom_errorbar(aes(ymin=CI_lo, ymax=CI_hi), width=.0, color="deepskyblue3") +
geom_ribbon(data = foo_ribbon,
aes(x = spline_x, y = NULL, ymin = spline_lo, ymax = spline_hi),
alpha= 0.2, color = 'grey')

Sample Image

Created on 2020-04-22 by the reprex package (v0.3.0)

Helpful thread: plotting smooth line through all data points

plotting custom confidence intervals around curve fits R

You can add the following code to your current code. The calculation of the error of the line is based on the error (assumed standard error) of the coefficients. You can change the calculation for the error of the line to something else, if desired. The order of plotting might need to be changed to make the polygons appear behind the lines.

# calculating the standard error of the line base on standard error of A,B,C
# could substitute another calculation
se.line.A <- ((x)/(0.02+x))*err.A
se.line.B <- ((x)/(0.02+x))*err.B
se.line.C <- ((x)/(0.02+x))*err.C

# library for polygons
library(graphics)

# plotting polygons
# colors can be changed
# polygons will be drawn over the existing lines
# may change the order of plotting for the shaded regions to be behind line
polygon(c(x,rev(x))
,c(A+se.line.A,rev(A-se.line.A))
,col='gray'
,density=100)

polygon(c(x,rev(x))
,c(B+se.line.B,rev(B-se.line.B))
,col='blue'
,density=100)

polygon(c(x,rev(x))
,c(C+se.line.C,rev(C-se.line.C))
,col='green'
,density=100)

Sample Image



Related Topics



Leave a reply



Submit