【问题标题】:How to plot the S-curve for my logistic regression model?如何为我的逻辑回归模型绘制 S 曲线?
【发布时间】:2019-03-29 21:59:17
【问题描述】:

我正在尝试将 S 曲线拟合到我的逻辑回归模型,但我所拥有的只是这条略微弯曲的线,而不是 S 形曲线。我的代码有什么需要更改的地方吗?

#Log Regression model
logmodel<-glm(y~emp.var.rate,data=Dataset,family=binomial)
summary(logmodel)

#plot
plot(emp.var.rate,y)
x<-seq(from=min(emp.var.rate),to=max(emp.var.rate))
hat.beta<-coef(logmodel)
hat.beta
lines(x, (1 + exp(-hat.beta[1] - hat.beta[2]*x))^(-1), col="blue")

> hat.beta
 (Intercept) emp.var.rate 
  -2.2207829   -0.5203776

【问题讨论】:

  • 我假设您的预测概率不涵盖 0 和 1 之间的全部范围,因此您不会得到完整的 S 曲线。预测值的最小值和最大值是多少?
  • 但是使用这个代码“x
  • 这是从您观察到的数据的最小值到最大值。您的观察范围不足以使 s 曲线的渐近线变得明显。试试x &lt;- seq(from = min(emp.var.rate) - 3 * diff(range(emp.var.rate)), to = max(emp.var.rate) + 3 * diff(range(emp.var.rate)), length.out = 1000)。根据需要展开更多。 (但也要意识到,如果你这样做,你的推断远远超出了你的数据范围......)
  • 谢谢。现在它延伸得更远了,但仍然根本看不到 S...这与我的数据有关吗?是不是因为数据不平衡?
  • 这并没有错。如果你没有得到 S 曲线也没关系,这就是你的数据。不要将x 扩展到观察值之外,这会超出观察到的数据。

标签: r


【解决方案1】:

我有一个不错的功能,几年前我就用过。它打印 S 形预测~线性化并在模型中按颜色和按重量的元素绘制模型不匹配:

library(dplyr); library(ggplot2);

#create your model
m1 <- glm(C(prog25) ~ C(sex) + ageB + gfrB +  mapB + C(M) + C(E) + C(S) + C(T) + C(CRESC) + C(cat0) + tlr4l , data=df10, family=binomial(link=logit))

#extract data for plot
df10$predicted <- predict(m1, type="response")
df10$residuals <- residuals(m1, type = "response")
df10$linearized <- m1$linear.predictors
df10$weights <- m1$weights

#reinterpret as numeric
df10$prog25 <- as.numeric(df10$prog25)

#assign id to know the model "mismatched"
df10$id <- seq(1,nrow(df10))
mism <- df10 %>% filter(prog25 != round(predicted))
for (i in 1:nrow(df10)) {
            if (!is.na(match(df10$id[i],mism$id))) m <- 1
            else m <- 0
            df10$mismatched[i] <- m
            }
#then remove ID
df10$id <- NULL

#define a function to plot the model
gra.tot <- function(dat, varLin, varY, varP, group, fitModel, devModel, dfModel, devNull, dfNull, aicModel, nameX, nameY) {
  ggplot(dat, aes(x = varLin, y = varY)) +
  geom_point(aes(size=varP, color=as.factor(group)), alpha=.3) +
  scale_colour_manual(name="mismatched", values = c("grey30", "red3")) +
  geom_text(x= min(varLin) + (0.1 * (max(varLin) - min(varLin))) , y=0.5,          hjust=0, label=paste( 'predicted ~ linearized', "\nmismatched: ",sum(group),"/",length(fitModel), '\nres. deviance: ', round(devModel,2) , ' (df: ', round(dfModel,2),')',  '\nnull deviance: ', round(devNull,2), ' (df: ', round(dfNull,2),')', '\nAIC: ', round(aicModel,2) ), col='grey40', size=3, fontface='italic') +
  xlab(as.character(nameX)) +
  ylab(as.character(nameY)) +
  theme_bw()
}

#plot the model
tot <- gra.tot(df10, df10$linearized, df10$predicted, df10$weights, df10$mismatched, m1$fitted, m1$deviance, m1$df.residual, m1$null.deviance, m1$df.null, m1$aic, 'linearized_predictors', 'logit_risk')

这应该会给你这样的输出:

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2016-07-26
    • 2015-02-19
    • 2014-03-16
    • 2016-08-09
    • 2018-10-13
    • 2021-11-23
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多