提问人:sabc04 提问时间:11/10/2023 更新时间:11/10/2023 访问量:19
如何在 R 中使用逻辑回归模型绘制线性图?
How can I plot for linearity using a logistic regression model in R?
问:
我想确定我的连续变量和二元结果变量是否具有线性关系。我该怎么做?我尝试了几种不同的情节,但没有一个能很好地显示这种关系。我的数据如下所示:
anxiety physical_activity
<dbl> <dbl>
1 1 240
2 0 210
3 0 90
4 0 60
5 1 160
6 0 300
7 0 0
8 0 40
9 0 30
10 1 60
我的模型如下所示:
model <- glm(anxiety ~ physical_activity_hours, data=data, family=binomial(), na.action = na.exclude)
我试过这个情节:
# Extracting residuals from the model
residuals <- resid(model)
# Creating the data frame for plotting
plot_data <- data.frame(physical_activity_hours = data$physical_activity_hours, residuals = residuals)
# Generating the plot
ggplot(plot_data, aes(x = physical_activity_hours, y = residuals)) +
geom_point(alpha = 0.5) + # Plot the raw data points with some transparency
stat_smooth(span = 0.5, se = FALSE) + # Add a loess smoothed line; adjust span as needed
theme_minimal() + # Use a minimal theme for a clean plot
labs(x = "Physical Activity Hours", y = "Residuals", title = "Residuals vs. Physical Activity with Moving Average")
而这个情节:
> data %>%
+ mutate(anxiety = as.numeric(anxiety)) %>%
+ pivot_longer(cols = c("physical_activity_hours"), names_to = "predictors") %>%
+ ggplot(aes(x = value, y = anxiety)) +
+ geom_point(size = 0.5, alpha = 0.5) +
+ geom_smooth(method = "loess") +
+ facet_wrap(~predictors, scales = "free_x")
他们俩都不是很好。
我做错了什么?
答: 暂无答案
评论