将拟合线添加到多个 ggplot 输出
Adding fit line to multiple ggplot outputs
我正在利用以下代码绘制 X_train
中的每个回归变量,并使用来自 glm()
的逻辑模型针对响应变量 won
require(reshape2)
require(ggplot2)
regressors = melt(X_train[2:16], id.vars='won')
ggplot(regressors) +
geom_jitter(aes(value,won, colour=variable),) +
geom_smooth(aes(value,won, colour=variable), method="glm", method.args = list(family = "binomial")) +
facet_wrap(~variable, scales="free_x")
我认为 geom_smooth()
命令会将拟合逻辑回归线添加到每个图中,但我似乎无法让它工作。我也尝试将“data=X_train
”传递给命令。感谢任何建议,谢谢。
我认为你的 TRUE/FALSE 需要转换为 1/0。
这是一个包含 mtcars
数据的示例:
library(dplyr); library(ggplot2)
# doesn't work
regressors <- pivot_longer(mtcars, -mpg)
# still doesn't work
regressors <- pivot_longer(mtcars, -mpg) %>%
mutate(mpg = (mpg > median(mpg)))
# Warning messages:
# 1: Computation failed in `stat_smooth()`:
# y values must be 0 <= y <= 1
# ...
# works
regressors <- pivot_longer(mtcars, -mpg) %>%
mutate(mpg = (mpg > median(mpg)) %>% as.numeric())
ggplot(regressors) +
geom_jitter(aes(value,mpg, colour=name), height = 0.05) +
geom_smooth(aes(value,mpg, colour=name), method="glm", method.args = list(family = "binomial")) +
facet_wrap(~name, scales="free_x")
我正在利用以下代码绘制 X_train
中的每个回归变量,并使用来自 glm()
won
require(reshape2)
require(ggplot2)
regressors = melt(X_train[2:16], id.vars='won')
ggplot(regressors) +
geom_jitter(aes(value,won, colour=variable),) +
geom_smooth(aes(value,won, colour=variable), method="glm", method.args = list(family = "binomial")) +
facet_wrap(~variable, scales="free_x")
我认为 geom_smooth()
命令会将拟合逻辑回归线添加到每个图中,但我似乎无法让它工作。我也尝试将“data=X_train
”传递给命令。感谢任何建议,谢谢。
我认为你的 TRUE/FALSE 需要转换为 1/0。
这是一个包含 mtcars
数据的示例:
library(dplyr); library(ggplot2)
# doesn't work
regressors <- pivot_longer(mtcars, -mpg)
# still doesn't work
regressors <- pivot_longer(mtcars, -mpg) %>%
mutate(mpg = (mpg > median(mpg)))
# Warning messages:
# 1: Computation failed in `stat_smooth()`:
# y values must be 0 <= y <= 1
# ...
# works
regressors <- pivot_longer(mtcars, -mpg) %>%
mutate(mpg = (mpg > median(mpg)) %>% as.numeric())
ggplot(regressors) +
geom_jitter(aes(value,mpg, colour=name), height = 0.05) +
geom_smooth(aes(value,mpg, colour=name), method="glm", method.args = list(family = "binomial")) +
facet_wrap(~name, scales="free_x")