I have a regression model:
# Create Dataset ####
# simulate exponential relationship
set.seed(123)
# generate random x values between 0 and 60
x <- runif(400, 0, 60)
y <- 1 - exp(-0.1 * x) * rnorm(400, 0.7, 0.1)
data = data.frame(sr= x, fipar = y)
# Fit a GLM model #
glm_mod <- glm(fipar ~ log(sr), data = data, family = binomial)
Let's say that I want to predict this model on new data that has been collected over time:
newdat <- data.frame(sample = c(rep('A',3),rep('B',3),rep('C',3)),
sr = c(1,20,55,4,11,12,2,45,20),
timepoint = c(1,2,3,1,2,3,1,2,3))
newpreds <- predict(glm_mod,
newdata = newdat,
type = "response", se.fit = T)
newdat$predicted_fipar <- newpreds$fit
newdat$predicted_fipar_se <- newpreds$se.fit
From this model, I get standard errors and can plot errorbars at the timepoint where the predictions were made, i.e
newdat %>%
ggplot(aes(x=timepoint,y=predicted_fipar,group=sample)) +
geom_point(aes(color=sample)) +
geom_line() +
geom_errorbar(aes(ymin=predicted_fipar-predicted_fipar_se,
ymax=predicted_fipar+predicted_fipar_se,
color=sample),
width=0.1)
But let's say I want to know the error at timepoint 1.5. If the interpolation between points is linear, I think that it's possible to also interpolate the error linearly (not sure if this overlooks any statistical rules), i.e:
newdat %>%
ggplot(aes(x=timepoint,y=predicted_fipar,group=sample)) +
geom_point(aes(color=sample)) +
geom_line() +
geom_ribbon(aes(ymin=predicted_fipar-predicted_fipar_se,
ymax=predicted_fipar+predicted_fipar_se,
fill=sample),
alpha=0.4) +
theme(legend.position = 'none')
But what if the interpolation between predicted points is non-linear? Are there other methods that allow this? I was thinking I should first interpolate the independent variable over time (in this case sr
) and then make predictions from the model using the interpolated values. This way I can calculate the errors for the in-between points, i.e:
# Interpolate SR over time for each sample #
interpolated_sr <-
plyr::rbind.fill(
lapply(unique(newdat$sample),function(samp){
sampledat <- newdat %>%
filter(sample == samp)
sr_loess <- loess(sr ~ timepoint, data = sampledat)
topred <- data.frame(sample = samp,
timepoint = seq(1,3,0.1))
topred$sr <- predict(sr_loess, topred)
return(topred)
}))
interpolated_preds <-
predict(glm_mod,
newdata = interpolated_sr,
type = "response", se.fit = T)
interpolated_sr$predicted_fipar <- interpolated_preds$fit
interpolated_sr$predicted_fipar_se <- interpolated_preds$se.fit
interpolated_sr %>%
ggplot(aes(x=timepoint,y=predicted_fipar,group=sample)) +
geom_point(aes(color=sample)) +
geom_line() +
geom_ribbon(aes(ymin=predicted_fipar-predicted_fipar_se,
ymax=predicted_fipar+predicted_fipar_se,
fill=sample),
alpha=0.4) +
theme(legend.position = 'none')
Is this approach valid? I understand that by interpolating the independent variable over time I am introducing some additional error from the loess model (which in this case will be very small, but could be very big depending on the fit). Is this where error propagation comes into play?