Plot vs ggplot2 in R and how to extract fit parameters

情到浓时终转凉″ 提交于 2019-12-02 08:20:49

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!