首页 文章

如何根据glm模型绘制变量的预测概率

提问于
浏览
0

我想绘制作为glm模型一部分的每个变量,其中y轴是预测概率,x轴是变量水平或值 . 这是我为了做到这一点而尝试的代码:

数据:

dat <- read.table(text = "target apcalc    admit   num
     0        0        0         21
     0        0        1         24
     0        1        0         55
     0        1        1         72
     1        0        0         5
     1        0        1         31
     1        1        0         11
     1        1        1         3",  header = TRUE)

glm模型:

f<-glm(target ~ apcalc + admit +num, data = dat,family=binomial(link='logit'))

提供所需图表的循环:

for(i in 1:length(f$var.names)){
          plot(predict(f,i.var.names=i,newdata=dat,type='response'))
      }

我得到一个奇怪的情节作为输出(x轴上的“索引”和y轴上的“预测(f,i.var.names = i,newdata = dat,type ='response')” . 我该如何修复我的代码是为了得到想要的结果?(我还没有声誉,以便在这里展示它)

2 回答

  • 0

    下图用预测的概率绘制所有变量,

    f<-glm(target ~ apcalc + admit +num, data=dat,family=binomial(link="logit"))
    
    PredProb=predict(f,type='response') #predicting probabilities
    
    par(mfrow=c(2,2))
    for(i in names(dat)){
      plot(dat[,i],PredProb,xlab=i)
    }
    
  • 1

    在运行f <-glm(.....)部分时,f $ var.names给出NULL作为输出 . 那里肯定有一些错误 .

    f<-glm(target ~ apcalc + admit +num, data=dat,family=binomial("logit"))
    
    f
    
    Call:  glm(formula = target ~ apcalc + admit + num, family = binomial("logit"), 
        data = dat)
    
    Coefficients:
    (Intercept)       apcalc        admit          num  
         2.2690       3.1742       2.4406      -0.1721  
    
    Degrees of Freedom: 7 Total (i.e. Null);  4 Residual
    Null Deviance:      11.09 
    Residual Deviance: 5.172        AIC: 13.17
    
    f$var.names
    NULL
    

相关问题