简体   繁体   English

使用 Geom_Smooth 时没有绘制回归线

[英]Regression Line is Not Plotting When Using Geom_Smooth

I am doing the 5 day regression challenge on Kaggle but I am having trouble getting a poisson regression line to plot when using ggplot.我正在 Kaggle 上进行为期 5 天的回归挑战,但在使用 ggplot 时,我无法获得 plot 的泊松回归线。 I want to look at the effects of Rain fall on the number of bikes that cross the bridges in NYC.我想看看下雨对穿过纽约市桥梁的自行车数量的影响。

I have tried reversing the order of geom_point and geom_smooth but that hasn't worked.我试过颠倒 geom_point 和 geom_smooth 的顺序,但没有奏效。 If I use gaussian instead, that also doesn't work.如果我改用高斯,那也行不通。

Here's what I have written:这是我写的:

ggplot(data = bikes, mapping = aes(x = Precipitation, y = Total)) +
   geom_smooth(method = "glm", method.args = list(family = "poisson")) + 
   geom_point() +
   labs(title = 'Precipitation vs. Total Bike Crossings')

Inspect the data before plotting it:在绘制数据之前检查数据:

unique(bikes$Precipitation)
# [1] "0.01"     "0.15"     "0.09"     "0.47 (S)" "0"        "0.2"      "T"       
# [8] "0.16"     "0.24"     "0.05"

The regressor Precipitation is not numeric and some of its values when coerced to numeric will become NA values.回归量Precipitation不是数字,当强制转换为数字时,它的一些值将变为NA值。

If you coerce Precipitation first the regression line shows up.如果您先强制Precipitation ,则会出现回归线。

library(dplyr)
library(ggplot2)

bikes %>%
  mutate(Precipitation = as.numeric(Precipitation)) %>%
  na.omit() %>%
  ggplot(mapping = aes(x = Precipitation, y = Total)) +
  geom_smooth(method = "glm", 
              formula = y ~ x,
              method.args = list(family = "poisson")) + 
  geom_point() +
  labs(title = 'Precipitation vs. Total Bike Crossings')

在此处输入图像描述

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

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