这里是R初学者!
我对 R 中的 beta 回归和“betareg”模型有疑问。
我想模拟一个值(临时 300)的 beta 回归的数据点。 我应该通过首先模拟 temp=300 的 10 个数据点来测试结果转换,然后将它们添加到我的绘图和回归拟合线中。这些模拟的 10 个数据点应该分散开来 垂直于回归线...
从 beta 分布生成随机样本的函数 rbeta 不是用于拟合模型的函数... beta 分布有不同的参数化。我需要将从 betareg 得出的估计值转换为 rbeta 中使用的参数。
我是 r 的初学者,不知道如何从 betareg 模型中提取和操作正确的估计,以输入 rbeta 来生成数据点...
这是我的代码,但我很确定这不是最严格的方法......
library(betareg)
data("GasolineYield")
model <- betareg(yield ~ temp, data = GasolineYield)
new_temp <- seq(min(GasolineYield$temp), max(GasolineYield$temp), length.out = 100)
preds <- predict(model, newdata = data.frame(temp = new_temp), type = "response")
predicted_values <- predict(model, newdata = data.frame(temp = new_temp), type = "response")
plot(GasolineYield$temp, GasolineYield$yield, xlab = "Temperature (Fahrenheit)", ylab = "Yield", main = "Beta-Regression Model")
lines(new_temp, preds, col = "red", lwd = 2)
specific_temp <- 300
new_data <- data.frame(temp = specific_temp)
mu <- predicted_values
phi <- exp(predicted_values)^2
simulated_yield <- rbeta(n = 1, shape1 = mu * phi, shape2 = (1 - mu) * phi)
points(specific_temp, simulated_yield, col = "red", pch = 16)
simulated_yield_300 <- rbeta(n = 10, shape1 = predicted_values * phi, shape2 = (1 - predicted_values) * phi)
points(rep(300, 10), simulated_yield_300, col = "red", pch = 16)
感谢您查看我的问题:)
打印模型显示:
library(betareg)
data("GasolineYield")
model <- betareg(yield ~ temp, data = GasolineYield)
# Call:
# betareg(formula = yield ~ temp, data = GasolineYield)
#
# Coefficients (mean model with logit link):
# (Intercept) temp
# -3.908231 0.007308
#
# Phi coefficients (precision model with identity link):
# (phi)
# 26.64
参数
phi
为 shape1+shape2
,参数 mu
,即 Beta 分布的平均值 shape1/(shape1+shape2)
,由 logit(mu) = intercept + slope*temp
给出。因此,对于特定温度,您会得到 shape1
和 shape2
,如下所示:
specific_temp <- 300
logit_mu <- -3.908231 + 0.007308 * specific_temp
phi <- 26.64 # this is shape1+shape2
mu <- 1 / (1 + exp(-logit_mu)) # this is shape1/(shape1+shape2)
shape1 <- mu * phi
shape2 <- phi - shape1