[英]Curve Plot logistic regression
這是我的模型。 Exam_taken 是二元變量(0,1),社會階層(1-10 尺度)和 GDP 是連續變量。
fit<-glm(Exam_taken~Gender+Social_class*GDP, data=final, family=binomial(link="probit")
summary(fit)
我需要繪制圖表。 目標 1) Social_class 和 Exam_taken 之間的關系; 目標 2) Social_class*GDP 在 Exam_taken 上的交互作用。
我遇到了兩個問題。
#exclude missing values
final=subset(final, final$Social_class!="NA")
final=subset(final, final$Exam_taken!="NA")
#graph
library(popbio)
logi.hist.plot(final$Social_class, final$Exam_taken, boxp=FALSE, type = "hist")
我收到一個錯誤“seq.default(min(independ),max(independ),len=100) 中的錯誤:'from' 必須是有限數”如何解決? 太感謝了
表示涉及三個因變量的回歸可能很困難,因為它實際上是一個四維結構。 但是,由於其中一個變量(性別)只有兩個級別,而社會類有 10 個離散級別,我們可以使用色階和分面來顯示模型。 首先,我們創建一個數據框,其中包含性別和社會階層的所有組合,每個 GDP 值從 1000 美元到 100,000 美元不等
pred_df <- expand.grid(Gender = c("Male", "Female"),
Social_class = 1:10,
GDP = 1:100 * 1000)
現在我們得到每種組合參加考試的概率:
pred_df$fit <- predict(fit, newdata = pred_df, type = "response")
然后我們可以像這樣繪制模型預測:
ggplot(pred_df, aes(GDP, fit, colour = Social_class, group = Social_class)) +
geom_line() +
facet_grid(Gender~.) +
scale_x_continuous(labels = scales::dollar, limits = c(0, 1e5)) +
labs(y = "Probability of taking exam",
color = "Social class") +
scale_color_viridis_c(breaks = 1:10) +
theme_minimal(base_size = 16) +
guides(color = guide_colorbar(barheight = unit(50, "mm")))
使用的數據
顯然,我們沒有您的數據,但我們可以根據您的描述和代碼提供線索,做出合理的復制。
set.seed(1)
final <- data.frame(Gender = rep(c("Male", "Female"), 100),
Social_class = sample(10, 200, TRUE),
GDP = 1000 * sample(20:60, 200, TRUE))
final$Exam_taken <- rbinom(200, 1,
c(0, 0.1) + 0.05 * final$Social_class +
final$GDP/1e5 - 0.2)
您可以使用sjPlot
包繪制模型的預測值。 如果保存plot_model()
函數的輸出,則可以使用ggplot2
修改其外觀。
這是可以向您展示此包的其他選項的眾多頁面之一: https ://cran.r-project.org/web/packages/sjPlot/vignettes/plot_model_estimates.html
library(sjPlot)
plot_model(fit, type = "int")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.