r - 将拟合线添加到多个 ggplot 输出
问题描述
我正在使用以下代码使用来自的逻辑模型绘制每个回归量X_train
与响应变量的关系won
glm()
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")
推荐阅读
- node.js - Lambda 从 S3 读取 jpg/vector 文件并使用 graphicsmagick 处理它们
- angularjs - 创建需要执行两个相互依赖的嵌套休息调用的服务功能的最佳和最干净的方法是什么?
- scala - 导入不会在范围内带来隐含
- macros - 为什么这些看似等效的宏之一会失败?
- cucumber - Cucumber JVM:避免 Picocontainer 对未标记执行的功能进行依赖注入
- c# - 无法在 Visual Studio 中使用 dotnet run 命令运行代码。“找不到要运行的项目”
- java - JAVA - 具有特殊字符的 LDAP 密码不起作用
- json - 将 aws cli dynamodb json 响应转换为常规 json
- php - 使用 /start 命令通过 PHP 引用 Telegram bot
- android - OnItemSelectedListener Spinner 重新创建循环 - Android