I have the following data in a data.frame called t
DayNum MeanVolume StdDev StdErr
1 13 207.0500 41.00045 5.125057
2 15 142.7625 27.8
lm with log transformed y is not the same as glm with gaussian error distribution and log link (as to why check link in the comment by @Lyngbakr)
gz <- read.table("somet.txt")
gz <- as.data.frame(gz)
model_lm <- lm(log(MeanVolume) ~ DayNum, data = gz)
model_glm <- glm(MeanVolume ~ DayNum, data = gz, family = gaussian(link = "log"))
pred_lm <- exp(predict(model_lm))
pred_glm <- predict(model_glm, type = "response")
plot(MeanVolume ~ DayNum, data = gz, ylab = "Mean Volume (mm3)", xlim = c(0,120), ylim = c(0,1000))
arrows(gz$DayNum, gz$MeanVolume - gz$StdErr, gz$DayNum, gz$MeanVolume + gz$StdErr, length = 0.01, angle = 90, code = 3)
lines(gz$DayNum, pred_lm, col = "blue")
lines(gz$DayNum, pred_glm, col = "red")
legend("topleft", col = c("blue", "red"), lty = 1, legend = c("lm", "glm"))
as for the second part of the question:
library(ggplot2)
p = ggplot(data = gz, mapping = aes(x = DayNum, y=MeanVolume)) +
geom_line() +
geom_point(size = 3, color="blue") +
geom_smooth(method = "glm", method.args = list(family = gaussian(link = "log"))) +
labs(x = "Days", y = "Mean Volume (mm3)", title = "Data") +
geom_errorbar(aes(ymin = MeanVolume - StdErr, ymax = MeanVolume + StdErr), width=.2)
to extract the data from a ggplot one can use:
build = ggplot_build(p)
the data for the curve are in build$data[[3]]
p + geom_line(data = build$data[[3]], aes(x = x, y = y), lty = 2, color = "red", size = 1.5)
This data is the same as data in pred_glm
- well its a bit more dense (more data points). As far as I am aware there is no method to extract the coefficients from the ggplot just the predictions, but you can always build the glm model as described above.