直接用ggplots绘制一个统计模型 [英] Directly plot a statistical model with ggplots

查看:235
本文介绍了直接用ggplots绘制一个统计模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

visreg 包允许直接绘制一个统计模型,我发现这个统计模型非常方便,可以检查是否有任何错误,并检查我们是否正确理解估计的含义。我很乐意将 visreg 的功能与 ggplot 的难以置信的灵活性结合起来。我希望能够在ggplot代码行中直接调用模型。这是否可行(最终通过直接修改 visreg 函数)?




<例如:

  require(visreg)
require(ggplot2)
y = c(rnorm( 40,10,1),rnorm(20,11,1),rnorm(5,1​​2,1))
x =因子(c(rep(1,40),rep(2,20),rep 3,5)))#这一行已经改变!
dt = data.frame(x = x,y = y)
m = lm(y〜x,data = dt)

我希望能够在 ggplot m c $ c>函数来表示我的统计模型 m 。使用 visreg ,它只是:

  visreg(m)


下面的代码不是我正在寻找的,因为它不直接调用对象 m
pre $ g $ p $ ggplot(dt,aes(x,y))+
geom_boxplot(aes(group = x),alpha = 0.5)+ geom_jitter()

解决方案

Just FYI, visreg 现在支持 ggplot2 输出:

  visreg(m,gg = TRUE)

产生


The package visreg allows to plot directly a statistical model which I find very convenient to check if anything gone wrong and to check if we understand correctly what the estimates mean. I would love to combine the functionality of visreg with the incredible flexibility of ggplot. I'd like to be able to directly call the model in a ggplot code line. Is this feasible (eventually by directly modifying the visreg function)?


For example:

require(visreg)
require(ggplot2)
y = c(rnorm(40,10,1), rnorm(20,11,1), rnorm(5,12,1))
x=factor(c(rep(1,40), rep(2,20), rep(3,5))) # this line has changed!
dt=data.frame(x=x, y=y)
m = lm(y~x, data=dt)

I'd like to be able to directly call the object m in a ggplot function in order to represent my statistical model m. With visreg, it simply is:

visreg(m)

The below code is NOT what I am looking for as it does not directly call the object m

 ggplot(dt, aes(x,y)) + 
          geom_boxplot(aes(group=x), alpha=0.5)+geom_jitter()

解决方案

Just FYI, visreg now supports ggplot2 output:

visreg(m, gg=TRUE)

produces

这篇关于直接用ggplots绘制一个统计模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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