我試圖在 R 中繪制一個簡單的邏輯回歸。
我正在按照本教程進行邏輯回歸并計算 P 值(
你有一個 glm,所以glance()fromtidy不會給你一個 p 值。使用示例:
library(ggplot2)
library(ggpmisc)
da = MASS::Pima.tr
da$label = as.numeric(da$type=="Yes")
model = glm(label ~ bmi,family=binomial,data=da)
summary(model)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -4.11156 0.92806 -4.430 9.41e-06 ***
bmi 0.10482 0.02738 3.829 0.000129 ***
你可以看到一瞥不會給你一個p值:
broom::glance(model)
# A tibble: 1 x 8
null.deviance df.null logLik AIC BIC deviance df.residual nobs
<dbl> <int> <dbl> <dbl> <dbl> <dbl> <int> <int>
1 256. 199 -120. 244. 251. 240. 198 200
您需要使用tidy()和評論中提到的@JonSpring,提供公式,如下所示:
ggplot(da,aes(x = bmi,y = label)) geom_point()
stat_smooth(method="glm",se=FALSE,method.args = list(family=binomial))
stat_fit_tidy(method = "glm",method.args = list(family=binomial,formula=y~x),
mapping = aes(label = sprintf("Coef = %.3g\np-value = %.3g",
after_stat(x_estimate),after_stat(x_p.value))))

uj5u.com熱心網友回復:
感謝您的所有幫助,但不幸的是沒有任何自動化作業,所以我想出了這個
require(cowplot)
require(ggplot2)
library(ggpmisc)
library(rms)
dataset=read.table('data.txt', header=TRUE)
model <- glm(variable ~ ancestry, data=dataset, family=binomial)
summary(model)
M1 <- glm(variable ~ ancestry, dataset, family = binomial)
M1
M1$null.deviance
M1$deviance
modelChi <- M1$null.deviance - M1$deviance
pseudo.R2 <- modelChi / M1$null.deviance
pseudo.R2
test <-lrm(variable ~ ancestry, dataset)
Chidf <- M1$df.null - M1$df.residual
chisq.prob <- 1 - pchisq(modelChi, Chidf)
chisq.prob
#plot logistic regression curve
all_variable <- ggplot(dataset, aes(x=ancestry, y=variable))
geom_point(alpha=.5, color=dataset$colorsite)
stat_smooth(method="glm", se=FALSE, method.args = list(family=binomial)) annotate("text", x=-Inf, y=Inf, hjust = 0, vjust = 2.5, label=paste("p-value: ",signif(chisq.prob, digits = 3),"\nR2: ",signif(pseudo.R2, digits = 3),sep="") )
ggtitle("Title not relevant to Stack Overflow")
ggsave("variable.pdf")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/361039.html
