R - 尝试绘制逻辑曲线,默认图并使用 'curve(predict' 不添加逻辑回归线
R - Trying to plot logistic curve, default plot and using 'curve(predict' does not add in logistic regression line
我正在尝试为数据集中的两个变量绘制逻辑回归。相关变量的结构-
str(df2)
$ Threatened : Factor w/ 2 levels "0","1": 1 2 1 1 1 1 2 1 2 2 ...
$ tl_mmlog : num 3.48 3.54 3.57 3.28 3.54 ...
当我尝试使用默认绘图函数绘制逻辑曲线时,没有绘制任何曲线。散点图以正确的方式创建。但是当我 运行 'curve(predict' 行时,逻辑回归线没有被添加。不会生成错误消息,只是不会出现任何行。此代码适用于我拥有的较小数据集,因此不确定它是否与我的数据集有关?有人知道为什么不画线吗?
逻辑回归代码-
attach(df2)
plot(x=tl_mmlog, y=Threatened)
fit2<-glm(Threatened~tl_mmlog, family=binomial)
curve(predict(fit2, data.frame(tl_mmlog=x), type="resp"), add=TRUE)
这是一些与您的数据相似的数据。我从 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)
我正在尝试为数据集中的两个变量绘制逻辑回归。相关变量的结构-
str(df2)
$ Threatened : Factor w/ 2 levels "0","1": 1 2 1 1 1 1 2 1 2 2 ...
$ tl_mmlog : num 3.48 3.54 3.57 3.28 3.54 ...
当我尝试使用默认绘图函数绘制逻辑曲线时,没有绘制任何曲线。散点图以正确的方式创建。但是当我 运行 'curve(predict' 行时,逻辑回归线没有被添加。不会生成错误消息,只是不会出现任何行。此代码适用于我拥有的较小数据集,因此不确定它是否与我的数据集有关?有人知道为什么不画线吗?
逻辑回归代码-
attach(df2)
plot(x=tl_mmlog, y=Threatened)
fit2<-glm(Threatened~tl_mmlog, family=binomial)
curve(predict(fit2, data.frame(tl_mmlog=x), type="resp"), add=TRUE)
这是一些与您的数据相似的数据。我从 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)