如何模拟 beta 回归 (betareg) 的数据点?

How to simulate a data point from a beta-regression (betareg)?

提问人:Alex San 提问时间:11/15/2023 最后编辑:Stéphane LaurentAlex San 更新时间:11/15/2023 访问量:66

问:

R 初学者在这里!

我对 R 中的“betareg”模型分别对 beta 回归有疑问。

我想模拟一个值(temp 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)

感谢您查看我的问题:)

R 统计数据

评论

0赞 gregor-fausto 11/15/2023
欢迎来到 Stack Overflow!感谢您提供代码!您尝试添加到绘图中的点被截断。我会改成.一些点也可以绘制在彼此的顶部。添加一些像这样的抖动。根据需要更改标准偏差。plot(GasolineYield$temp, GasolineYield$yield, xlab = "Temperature (Fahrenheit)", ylab = "Yield", main = "Beta-Regression Model")plot(GasolineYield$temp, GasolineYield$yield, xlab = "Temperature (Fahrenheit)", ylab = "Yield", main = "Beta-Regression Model", ylim=c(0,1))points(rep(300, 10)+rnorm(10,mean=0,sd=2),...
0赞 Alex San 11/15/2023
谢谢!我会把它传送到我拥有的其他一些情节中并玩弄它......

答:

0赞 Stéphane Laurent 11/15/2023 #1

打印模型显示:

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

参数为 ,参数 是 Beta 分布的均值,由 给出。因此,对于特定温度,您可以得到 和 如下所示:phishape1+shape2mushape1/(shape1+shape2)logit(mu) = intercept + slope*tempshape1shape2

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

预测的响应是均值,因此您可以检查与该函数获得的值相同。mupredict

评论

0赞 Alex San 11/15/2023
非常感谢!这奏效了。现在试图把我的头包裹起来。