【问题标题】:How to plot variable's predicted probability based on glm model如何基于 glm 模型绘制变量的预测概率
【发布时间】:2014-10-22 01:45:17
【问题描述】:

我想绘制作为 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 轴为“predict(f,i.var.names=i,newdata=dat,type='response')”。如何我可以修复我的代码以获得所需的结果吗? (我还没有声望在这里展示它)

【问题讨论】:

    标签: r loops plot glm


    【解决方案1】:

    在运行 f

    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
    

    【讨论】:

    • 我是在使用 $var.names 调用模型中的变量吗?我不确定,因此我的问题。
    【解决方案2】:

    这里用预测概率绘制所有变量,

    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)
    }
    

    【讨论】:

    • 非常感谢...510947
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多