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