Plot regression line with confidence intervals

Apologies if this is covered elsewhere, but is there a way to plot confidence intervals around a regression line from the output of ‘ds.glm’ or ‘ds.glmSLMA’? I get the sense that a ‘newobj’ created on the server side from ‘ds.glmSLMA’ might have the elements to do this, but I’m unsure how to access and use this information in graphing functions.

Thank you for any help!

Hi Tina,

Sorry for the late response to this.

Do you have a simple linear regression model with only one continuous exposure and one continuous outcome?

There is a way to calculate the confidence intervals around a regression line. Here is a code on how I did it in R:

set.seed(1)

x <- rnorm(100, 0, 1)
y <- x + rnorm(100, 0, 0.5)

plot(x,y)
abline(lm(y~x))

lm.out <- lm(y~x)

newx = seq(min(x), max(x), by = 0.05)
conf_interval <- predict(lm.out, newdata=data.frame(x=newx), interval="confidence",
                         level = 0.95)
plot(x, y, xlab="x", ylab="y")
abline(lm.out, col="blue")
lines(newx, conf_interval[,2], col="blue", lty=2)
lines(newx, conf_interval[,3], col="blue", lty=2)


### USING FORMULAS

# Find length of y to use as sample size
n <- length(y) 

# Extract fitted coefficients from model object
b0 <- lm.out$coefficients[1]
b1 <- lm.out$coefficients[2]

# Calculate critical t-value
t.val <- qt(0.975, n - 2) 

# Fit linear model with extracted coefficients
y.fit <- b1 * x + b0

# Find the standard error of the regression line
se <- sqrt(sum((y - y.fit)^2) / (n - 2)) * sqrt(1 / n + (x - mean(x))^2 / sum((x - mean(x))^2))
# Note: the first sqrt is the MSE which is given as SSE/(n-2)

# Plot the fitted linear regression line and the computed confidence bands
plot(x, y, xlab="x", ylab="y")
lines(x, y.fit, col="red")
lines(sort(x), sort((y.fit + t.val * se)), col = 'red', lty = 2)
lines(sort(x), sort((y.fit - t.val * se)), col = 'red', lty = 2)

We can do something similar in DataSHIELD. However, the DataSHIELD graphical functions, like the scatterplots show anonymised values and not the actual values. So if you want to do it, you will get a scatterplot with anonymized x and y data and anonymised regression line and its CI. For more details on the anonymisatiion process you can have a look on this paper: Privacy preserving data visualizations | EPJ Data Science | Full Text

I am happy to discuss this further in a call if you like and I can show you how to calculate the CIs of the regression line in DataSHIELD.

Many thanks, Demetris