16.10 Making predictions

Let’s go ahead and wrap this all up by making some predictions from our model and plotting those over the raw data. This process is essentially identical to the process we used for linear models and GLM in R:

Make log-scale predictions

lpreds <- data.frame( predict(cray_model, se.fit = TRUE) )

Calculate confidence intervals:

lpreds$lwr = lpreds$fit - 1.96 * lpreds$se.fit
lpreds$upr = lpreds$fit + 1.96 * lpreds$se.fit

Now, convert the predictions back to the real scale for plotting:

rpreds <- apply(lpreds, 2, exp)

And smash the predictions back together with the data

cray_preds <- data.frame(cray, rpreds)

Finally, we can plot our predictions over the raw data on the real scale:

# Make the plot of our predictions against raw data
ggplot(cray_preds, aes(x = length, y = mass)) +
  geom_point(alpha = 0.05) +
  geom_ribbon(aes(x = length, ymin = lwr, ymax = upr),
              alpha = 0.2) +
  xlab("Carapace length (mm)") +
  ylab("Claw-free mass (g)") +
  theme_bw()