将具有模拟数据点的 beta 回归模型转换为 DHARMa 对象

Converting a beta-regression model with simulated data points to an DHARMa-object

提问人:Lucca 提问时间:11/17/2023 最后编辑:jpsLucca 更新时间:11/17/2023 访问量:12

问:

我是 R 的初学者,正在努力将带有模拟数据点的 beta 回归模型转换为 DHARMa 对象。我使用了 betareg 包的 gasoyield 数据集,并用这段代码模拟了 200 个数据点,现在想将其转换为 DHARMA 对象。

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", ylim=c(0,1))
lines(new_temp, preds, col = "red", lwd = 2)
specific_temp <- 300


new_data <- data.frame(temp = specific_temp)
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

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)+rnorm(10,mean=0,sd=1),simulated_yield_300, col = "red", pch = 16)


# Set the number of simulations
num_simulations <- 200

# Create an empty matrix to store simulated data sets
simulated_data <- matrix(NA, nrow = length(new_temp), ncol = num_simulations)

# Simulate data sets
for (i in 1:num_simulations) {
  # Simulate yields for each temperature with increased variability
  simulated_yield <- rbeta(n = 1, shape1 = mu * phi, shape2 = (1 - mu) * phi)
  
  # Add more variability by adjusting the standard deviation
  simulated_yield_with_variation <- simulated_yield + rnorm(1, mean = 0, sd = 0.2)
  
  # Store simulated yields in the matrix
  simulated_data[, i] <- simulated_yield_with_variation
}

# Plot the original data and the fitted line
plot(GasolineYield$temp, GasolineYield$yield, xlab = "Temperature (Fahrenheit)", ylab = "Yield", ylim = c(-1, 1.5), main = "Beta-Regression Model")

# Add the fitted line
lines(new_temp, preds, col = "red", lwd = 2)

感谢您的帮助!!

R 回归

评论


答: 暂无答案