这里有一些与您的数据相似的数据。我从 R 中包含的名为 iris 的数据集创建了它,并使用 dput 创建了一种易于导入 R 的格式:
df2 <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6,
5, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 6.3, 5.8, 7.1, 6.3, 6.5,
7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8), Species = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("0",
"1"), class = "factor")), row.names = c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 101L, 102L, 103L, 104L,
105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L
), class = "data.frame")
str(df2)
# 'data.frame': 30 obs. of 2 variables:
# $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
# $ Species : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
现在计算分析并绘制图:
fit2 <- glm(Species~Sepal.Length, df2, family=binomial)
with(df2, plot(Sepal.Length, Species))
请注意,y 轴的范围是 1 到 2,因为这是数值因子值(而不是字符因子级别)的值。但是predict 函数将使用 0 到 1 的范围,因此它不会出现在您的图表上,除非您在绘图前为每个值添加 1。最好将因子转换为数值,使第一个值为 0,第二个值为 1:
df2$Species <- as.numeric(as.character(df2$Species))
fit2 <- glm(Species~Sepal.Length, df2, family=binomial)
with(df2, plot(Sepal.Length, Species))
现在绘图范围从 0 到 1。接下来我们添加曲线,但我们必须包括曲线的值范围:
minmax <- range(df2$Sepal.Length)
curve(predict(fit2, data.frame(Sepal.Length=x), type="resp"), minmax[1], minmax[2], add=TRUE)