【问题标题】:Plotting a Probability Equation绘制概率方程
【发布时间】:2012-02-10 19:24:11
【问题描述】:

我运行了一个 logit 模型并试图绘制概率曲线。我在这里发布问题而不是统计板,因为它更像是一个 R 问题而不是统计数据,或者至少我是这么认为的。

我的模型看起来像:

mod1 = glm(factor(status1) ~ our_bid1 + factor(state) + factor(type),
           data=mydat, family=binomial(link="logit"))
print(summary(mod1))

Status1 是一个因子有两个级别,our_bid 范围从 0 到 20,状态是 11 个级别(人口最多的 10 个和其他的一个),类型有三个级别。

为了得到预测的概率,我运行了以下代码

all.x1 <- expand.grid(status1=unique(status1), our_bid1=unique(our_bid1),
                      state=unique(state), type=unique(type))

y.hat.new1 <- predict(mod1, newdata=all.x1, type="response")

当我尝试绘制曲线时会出现问题。在给定模型的情况下,我正在尝试为我们的出价变化提供一般曲线。

plot(our_bid1<-000:1600, 
     predict(mod1, newdata=data.frame(our_bid1<-c(000:1600)), type="response"),
     lwd=5, col="blue", type="l")

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  variable lengths differ (found for 'factor(state)')
In addition: Warning message:
'newdata' had 1601 rows but variable(s) found have 29532 rows 

是否必须在 plot 命令中指定所有自变量?我做错了什么?

【问题讨论】:

    标签: r statistics glm


    【解决方案1】:
    • 这是不可重现的,如果是这样的话会更容易一些(即给我们一个 mydata 的工作示例)
    • 您确实需要指定所有独立(我更喜欢“预测”)变量的值
    • 您的规范混淆了 &lt;-=(虽然它们是在赋值上下文中,但它们完全可互换):你想要类似的东西

    (我这里需要一些文字,所以 SO 会正确格式化代码)

    bidvec <- 0:1600
    plot(bidvec,predict(mod1,
        newdata=data.frame(our_bid1=bidvec,
                           state=ref_state,type=ref_type),
                            type="response"), 
    lwd=5, col="blue", type="l")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 2013-06-29
      • 2018-01-24
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多