# build model
model <- gam(Voltage ~ s(Amplification, sp = 0.001), data = device)
# compute predictions with standard errors and rename columns to make plotting simpler
Amplifications <- data.frame(Amplification = seq(min(APD_data$Amplification),
max(APD_data$Amplification), length.out = 500))
predictions <- predict.gam(model, Amplifications, se.fit = TRUE)
predictions <- cbind(Amplifications, predictions)
predictions <- rename(predictions, Voltage = fit)
# plot data, model and standard errors
ggplot(device, aes(x = Amplification, y = Voltage)) +
+ geom_point() +
+ geom_ribbon(data geom_smooth= predictions,
aes(methodymin = "gam"Voltage - 1.96*se.fit, formulaymax = yVoltage ~+ s(x1.96*se.fit), se
fill = T"grey70", alpha = 0.5) +
geom_line(data = predictions, color = "blue")
log_model <- gam(Voltage ~ s(log(Amplification)), data = device)
# the rest of the code stays the same, except for log_model in place of model
Clearly Clearly something fishy is going on. Are these confidence intervals reliable? EDIT this is not simply a problem of the degree of smoothing, as it was suggested in an answer. Without the log-transform, the smoothing parameter is
> model$sp
s(Amplification)
5.03049e-07
With the log-transform, the smoothing parameter is indeed much bigger:
>log_model$sp
s(log(Amplification))
0.0005156608
But this is not the reason why the confidence intervals become so small. As a matter of fact, using an even bigger smoothing parameter sp = 0.001
, but avoiding any log-transform, oscillations are reduced (as in the log-transform case) but the standard errors are still huge with respect to the log-transform case:
smooth_model <- gam(Voltage ~ s(Amplification, sp = 0.001), data = device)
# the rest of the code stays the same, except for smooth_model in place of model
In general, if I log transform $X$ and/or $Y$, what happens to the confidence intervals? NOTE: If a general rule, or at least some rule of thumb, cannot be given in the general case, I'm ok with anit's not possible to answer which only explains if/how confidence intervals are affected, when log-transforming in the two specific cases I mention.
EDIT I'm feeling generous today ;-) if explaining the changequantitatively in confidence intervals for the GAMgeneral case is too difficult, I could stillwill accept an answer which only gives ais quantitative answer(i.e., it shows a formula) for the first case, (the exponential model) and gives at least an hand-waving argument for the GAMsecond case (GAM model).