繁体   English   中英

在GGPlot上绘制GLM回归方程和Rsquared

[英]Plot GLM Regression Equation and Rsquared on GGPlot

我有这样的问题:

样本数据:

library(tidyverse)
library(ggpubr)
a <- mtcars
reorganized <- a %>% gather (-mpg, key = "var", value = "value")

g <- ggplot(reorganized, aes(y = value, x = var)) +
  stat_smooth(method="glm", se=TRUE, fill=NA, 
              method.args = list(family = "binomial"), fullrange = F) +
  geom_smooth(method="glm", fill='red',
              method.args = list(family = "binomial")) +
  stat_regline_equation(
    aes(label =  paste(..eq.label.., ..adj.rr.label.., sep = "~~~~"))
  )+
  geom_point(aes(), alpha=2/10, shape=21, fill="blue", colour="black", size=0.2) +
  facet_wrap(~var, nrow=1)+
  theme_bw()

数据是reorganized数据框,有3列:mpg,var,value

使用此代码,我想要使用逻辑回归线的绘图方面,使用散点图的方程。 但是,公式显示不正确,它们只有y = b + ax的形式,有时Radj不在图中 在此输入图像描述

如何在此图中绘制逻辑回归的正确公式?

您似乎正在尝试将glm方法用于非分类变量。 这不行。 相反,您应该使用“黄土”或“lm”方法。 此外,要在每个方面内正确插入方程,您可以控制方程的位置和大小。 代码如下(geom_smooth默认为“loess”):

ggplot(reorganized, aes(y = value, x = mpg)) +
  geom_smooth(se=TRUE, fill=NA, method.args=list(family="gaussian"), fullrange = F) +
  stat_regline_equation( aes(label =  paste(..eq.label.., ..adj.rr.label.., 
                                             sep = "~~~~")),
                         label.x.npc = "left", label.y.npc = "top", size = 2)+
  geom_point(aes(), alpha=2/10, shape=21, fill="blue", colour="black", size=0.2) +
  facet_wrap(~var, nrow=2)+
  theme_bw()

它看起来应该是这样的

默认情况下,geom_smooth设置为黄土方法,公式为y~x。 这就是你在方程式上只有y和x的原因。 如果您想要一个不同的公式,您应该寻找如何在geom_smooth中自定义公式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM