绘制概率方程

时间:2012-02-10 19:24:11

标签: r statistics glm

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

1 个答案:

答案 0 :(得分:3)

  • 这是不可复制的,如果它是更容易的(即给我们一个mydata的工作示例)
  • 您需要指定所有独立(我更喜欢“预测变量”)变量的值
  • 您的规范混淆了<-=(它们不是相当可互换,尽管它们位于分配的上下文中):您想要类似
  • 的内容

(我需要一些文字,所以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")
相关问题