0

我运行了一个 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 命令中指定所有自变量?我究竟做错了什么?

4

1 回答 1

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

(我在这里需要一些文本,以便正确格式化代码)

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")
于 2012-02-10T19:45:57.040 回答