0

我正在尝试使用 ggplot 和真实数据集绘制二元回归线以供练习。问题:以公里为单位的距离是否是选择汽车作为前往足球场的交通工具的预测指标。

变量 A2 被二分法(1 = Auto(汽车)和 0 = kein Auto(无汽车)),现在称为 A2_auto

dataset %>%
mutate(A2_auto = car::recode(.$A2,
"1 = 1; 2:9 = 0", 
as.factor = FALSE)) -> dataset

dataset$A2_auto <- factor(dataset$A2_auto, labels = c("kein Auto",
                                                        "Auto"))
 

在我计算了决定系数(显着但非常低的奇数比)之后,我想用 ggplot 绘制回归曲线:

ggplot(data=dataset, aes(x=A21, y=A2_auto)) + 
  geom_point(alpha=.5) +
  stat_smooth(method="glm.fit", se=FALSE, method.args = list(family=binomial))

但我收到一条警告消息:

>`geom_smooth()` using formula 'y ~ x'
Warnmeldung:
Computation failed in `stat_smooth()`:
Unused Argument (data = data) 

散点图中没有回归线。不知道为什么:

二元回归图

这是数据框的结构:

'data.frame':   689 obs. of  3 variables:
 $ A2     : dbl+lbl [1:689] 1, 1, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 6, 7, 7, 3, 6, 6, 6, 6, 6, 6, 6...
   ..@ label        : chr "Mit welchem Verkehrsmittel legen Sie die größte Distanz zum Stadion zurück, wenn Sie ein Bundesliga-Heimspiel b"| __truncated__
   ..@ format.spss  : chr "F40.0"
   ..@ display_width: int 0
   ..@ labels       : Named num  1 2 3 4 5 6 7 8 9
   .. ..- attr(*, "names")= chr [1:9] "PKW" "Bahn (Fernverkehr)" "Bahn (Nahverkehr)" "Fernbus" ...
 $ A21    : num  1 1 1 1 1 1 1 1 1 1 ...
  ..- attr(*, "label")= chr "Distanz in km"
  ..- attr(*, "format.spss")= chr "F8.2"
  ..- attr(*, "display_width")= int 0
 $ A2_auto: Factor w/ 2 levels "kein Auto","Auto": 2 2 1 1 1 1 1 1 1 1 ...

谢谢您的帮助!

Edit1: 这是 dput(head(dataset,50)) 的输出:

structure(list(A2 = structure(c(1, 1, 6, 6, 6, 7, 7, 7, 7, 7, 
7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6, 7, 7, 7, 7, 7, 
7, 7, 7, 7, 7, 7, 7, 7, 7, 6, 7, 7, 3, 6, 6, 6, 6, 6, 6), label = "Mit welchem Verkehrsmittel legen Sie die größte Distanz zum Stadion zurück, wenn Sie ein Bundesliga-Heimspiel besuchen? - Selected Choice", format.spss = "F40.0", display_width = 0L, labels = c(PKW = 1, 
`Bahn (Fernverkehr)` = 2, `Bahn (Nahverkehr)` = 3, Fernbus = 4, 
`Fan-/Reisebus` = 5, ÖPNV = 6, Fahrrad = 7, `Zu Fuß` = 8, Sonstige = 9
), class = c("haven_labelled", "vctrs_vctr", "double")), A21 = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 
6, 6, 6, 6, 6, 6, 6), A2_auto = structure(c(2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("kein Auto", 
"Auto"), class = "factor")), row.names = c(NA, 50L), class = "data.frame")

当我将模型从 glm.fit 更改为 glm 时,会出现另一条警告消息:

ggplot(data=dataset, aes(x=A21, y=A2_auto)) + 
  geom_point(alpha=.5) +
  stat_smooth(method="glm", se=FALSE, method.args = list(family=binomial))

输出:

`geom_smooth()` using formula 'y ~ x'
Warnmeldungen:
1: glm.fit: algorithm did not converge 
2: Computation failed in `stat_smooth()`:
y values must be 0 <= y <= 1 

我还将变量二分为 0 和 1(没有因素),并且发生了同样的错误:

dataset %>%
  mutate(A2_auto = car::recode(.$A2,
  "1 = 1; 2:9 = 0", 
  as.factor = TRUE)) -> dataset
`geom_smooth()` using formula 'y ~ x'
Warnmeldungen:
1: glm.fit: algorithm did not converge 
2: Computation failed in `stat_smooth()`:
y values must be 0 <= y <= 1 

二分变量散点图

按照评论中的建议,我将尝试使用 mtcars 重现我的示例。

4

1 回答 1

0

我想我已经找到了解决方案。和数据集 mtcars 对比后,仔细看了一下我的数据集中的变量 A2_auto,发现这个变量毕竟不是数值型的。因此,我再次对其进行了转换并对其进行了二分法。此外,“glm”是评论中描述的正确方法。再次感谢评论中的建议!它现在起作用了。

于 2021-10-21T06:20:04.353 回答