从模型创建新数据集的麻烦

时间:2014-02-14 19:33:13

标签: r rows models poisson

我正在尝试创建一个类似于链接上的情节的情节:http://www.ats.ucla.edu/stat/r/dae/nbreg.htm

对于具有置信区间的预测结果的图。我正在使用的代码如下所示,我能找到的所有代码与上面引用的网站上列出的代码相同。

 summary(modelpoisson<-glmer(respiratory_morbidity~mdata2$prevpercent+mdata2$weightclass+mdata2$interpretation+(1|feedlot),family="poisson",offset=log(mdata2$starting_head)),data=mdata2)

    newdata2p <- data.frame(
      prevpercent = rep(seq(from = min(mdata2$prevpercent), to = max(mdata2$prevpercent), length.out = 20), 5),
      interpretation  = factor(rep(1:5, each = 20), levels = 1:5, labels =
                      levels(mdata2$interpretation )))

    newdata2p <- cbind(newdata2p, predict(modelpoisson, newdata2p,type="link", se.fit=TRUE))
    newdata2p <- within(newdata2, {
      respiratory_morbidity <- exp(fit)
      LL <- exp(fit - 1.96 * se.fit)
      UL <- exp(fit + 1.96 * se.fit)
    })

然而,当我运行此代码时,当我尝试创建数据框newdata2p时出现以下错误

有人能告诉我我做错了吗?

出现以下错误:

  newdata2p <- data.frame(
    + prevpercent = rep(seq(from = min(mdata2$prevpercent), to = max(mdata2$prevpercent), length.out = 20), 5),
    + interpretation  = factor(rep(1:5, each = 20), levels = 1:5, labels =
    + levels(mdata2$interpretation )))
    > View(newdata2p)
    > newdata2p <- cbind(newdata2p, predict(modelpoisson, newdata2p,type="link", se.fit=TRUE))
    Error in `[[<-.data.frame`(`*tmp*`, i, value = integer(0)) : 
      replacement has 0 rows, data has 100
    In addition: Warning messages:
    1: In predict.merMod(modelpoisson, newdata2p, type = "link", se.fit = TRUE) :
      unused arguments ignored
    2: 'newdata' had 100 rows but variables found have 134 rows 

谢谢!

0 个答案:

没有答案