代码之家  ›  专栏  ›  技术社区  ›  Krantz

向贝塔回归模型绘制的曲线添加趋势

  •  0
  • Krantz  · 技术社区  · 8 年前

    我有下面的示例数据集和代码,如果您能帮助将趋势添加到beta回归模型的曲线中,以便可视化 yield 对于变量的值 temp 超出范围的 200-450

    library(ggplot2)
    library(plotly)
    library(betareg)
    data("GasolineYield", package = "betareg")
    md <- betareg(yield ~ temp, data = GasolineYield,
                     link = "cloglog")
    
    ggplotly(
    ggplot(GasolineYield, aes(x = temp, y = yield)) +
    geom_point(size = 4, aes(fill = batch), shape = 21) +
    geom_line(aes(y = predict(md, GasolineYield)), col="red") +
    theme_classic())  
    

    使现代化

    结果我需要使用 pred.zoib 而不是 predict 因为我的数据 zero-and-one inflated ,而简单地将其中一个替换为另一个则不起作用,并会出现以下错误:

    Error in FUN(X[[i]], ...) : object 'yield' not found
    

    因此,我希望您能提供一些想法,帮助@Adela调整原始问题的答案,以实现相同的目标,但现在使用 zoib package 而不是 betareg package

    给出了一个示例代码:

    library(zoib)
    re.md <- zoib(yield ~ temp | 1 | 1, data=GasolineYield,
    joint = FALSE, random=1, EUID=GasolineYield$batch,
    zero.inflation = FALSE, one.inflation = FALSE,
    n.iter=3200, n.thin=15, n.burn=200)
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Adela    8 年前

    您需要预测值为的新数据 temp 超出范围 200-450

    请参见我的示例:

    # predict with new data
    df <- data.frame(yield = predict(md, data.frame(temp = seq(100, 600, 0.01))),
                     temp = seq(100, 600, 0.01))
    
    # plot
    ggplotly(
      ggplot() +
        geom_point(data = GasolineYield, 
                   aes(x = temp, y = yield, fill = batch),
                   size = 4, shape = 21) +
        xlim(100, 600) + 
        geom_line(data = df, aes(y = yield, x = temp), col="red") +
        theme_classic())
    
    推荐文章