将趋势添加到从β回归模型

时间:2018-04-10 11:59:24

标签: r ggplot2 regression prediction bayesian

我有下面的示例数据集和代码,非常感谢您帮助我们从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 个答案:

答案 0 :(得分:1)

您需要预测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())