使用ggplot2绘制已经存在的线性模型 [英] Using ggplot2 to plot an already-existing linear model

查看:172
本文介绍了使用ggplot2绘制已经存在的线性模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一些数据,并且我创建了一个线性模型来拟合数据.然后,我使用ggplot2绘制数据,并想将线性模型添加到图中.据我所知,这是这样做的标准方法(使用内置的cars数据集):

Let's say that I have some data and I have created a linear model to fit the data. Then I plot the data using ggplot2 and I want to add the linear model to the plot. As far as I know, this is the standard way of doing it (using the built-in cars dataset):

library(ggplot2)
fit <- lm(dist ~ speed, data = cars)
summary(fit)
p <- ggplot(cars, aes(speed, dist))
p <- p + geom_point()
p <- p + geom_smooth(method='lm')
p

但是,以上内容违反了DRY原理(不要重复自己"):它涉及在对lm的调用中创建线性模型,然后在对geom_smooth的调用中重新创建线性模型.这对我来说似乎很微不足道,并且还为错误提供了空间.例如,如果我更改使用lm创建的模型,但是忘记更改使用geom_smooth创建的模型,则摘要和图解将不会属于同一模型.

However, the above violates the DRY principle ('don't repeat yourself'): it involves creating the linear model in the call to lm and then recreating it in the call to geom_smooth. This seems inelegant to me, and it also introduces a space for bugs. For example, if I change the model that is created with lm but forget to change the model that is created with geom_smooth, then the summary and the plot won't be of the same model.

有没有一种方法可以使用ggplot2绘制一个已经存在的线性模型,例如通过将lm对象本身传递给geom_smooth函数?

Is there a way of using ggplot2 to plot an already existing linear model, e.g. by passing the lm object itself to the geom_smooth function?

推荐答案

需要做的是创建一个新数据框,其中包含旧数据的观察值以及模型的预测值,然后使用ggplot2绘制该数据框

What one needs to do is to create a new data frame with the observations from the old one plus the predicted values from the model, then plot that dataframe using ggplot2.

library(ggplot2)

# create and summarise model
cars.model <- lm(dist ~ speed, data = cars)
summary(cars.model) 

# add 'fit', 'lwr', and 'upr' columns to dataframe (generated by predict)
cars.predict <- cbind(cars, predict(cars.model, interval = 'confidence'))

# plot the points (actual observations), regression line, and confidence interval
p <- ggplot(cars.predict, aes(speed,dist))
p <- p + geom_point()
p <- p + geom_line(aes(speed, fit))
p <- p + geom_ribbon(aes(ymin=lwr,ymax=upr), alpha=0.3)
p

这样做的最大好处是,如果更改了模型(例如cars.model <- lm(dist ~ poly(speed, 2), data = cars)),则图和摘要都将更改.

The great advantage of doing this is that if one changes the model (e.g. cars.model <- lm(dist ~ poly(speed, 2), data = cars)) then the plot and the summary will both change.

感谢Plamen Petrov使我意识到这里需要什么.正如他指出的那样,这种方法仅在为所讨论的模型定义predict时才有效;如果不是这样,就必须自己定义.

Thanks to Plamen Petrov for making me realise what was needed here. As he points out, this approach will only work if predict is defined for the model in question; if not, one has to define it oneself.

这篇关于使用ggplot2绘制已经存在的线性模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆