我有兴趣使用交叉验证(留一法或 K 折)来测试我创建的几种不同的负二项式 GLM。我正在使用 glm.nb()
函数来自 MASS
运行负二项式回归。
我的问题是我是否可以使用 cv.glm()
来自 boot
来测试这个模型。我倾向于不,但想知道是否有人知道可以让我执行 K 折验证(或省略一个)的函数。或者,也许 cv.glm()
对负二项式完全有效。
这是来自在线示例的一些数据以提供帮助。我曾认为交叉验证结果( $delta
)应该在 0 和 1 之间,但下面的情况并非如此(并且可能表明出现了问题)
http://www.ats.ucla.edu/stat/r/dae/nbreg.htm
我发现了一些关于解释 cv.glm()
输出的问题。 ,但不是专门关于如何在 R 中使用负二项式模型进行交叉验证
require(foreign)
require(ggplot2)
require(MASS)
require(boot)
dat <- read.dta("http://www.ats.ucla.edu/stat/stata/dae/nb_data.dta")
dat <- within(dat, {
prog <- factor(prog, levels = 1:3, labels = c("General", "Academic","Vocational"))
id <- factor(id)
})
summary(m1 <- glm.nb(daysabs ~ math + prog, data = dat))
#This code will run, but is it valid for negative binomial GLM models?
cv.glm(dat,m1,K=10)
[我不确定这个问题是属于这里还是交叉验证...]
最佳答案
使用 cv.glm()
对任何 glm
都有效目的。它不使用任何更新公式来计算交叉验证分数,而是简单地删除一个组,为减少的数据集拟合模型,并计算预测误差。这样做多次以获得最终平均值。通过这种方式,留一法交叉验证(默认)比 K 折交叉验证的成本要高得多。
为什么它对任何 glm
都有效目的?它怎么知道它应该适合什么模型?好吧,你通过传入你的拟合模型来告诉它m1
.看一下:
m1$call
#glm.nb(formula = daysabs ~ math + prog, data = dat, init.theta = 1.032713156,
# link = log)
当
cv.glm
删除数据并为减少的数据集重新拟合模型,它使用这样的调用。所以你肯定每次都在拟合一个负二项式模型。I am trying to interpret the results from the
cv.glm()$delta
. In the example above, the model is predicting "Days absent from school", and the output of$delta
is ~42.1. Would I interpret this as "on average, this model has a prediction error of 42 days"?
默认情况下,
cv.glm()
返回 MSE,由 cost
定义争论:cost = function(y, yhat) mean((y - yhat)^2
所以 42.1 实际上是预测方差。您需要 RMSE,即
sqrt(42.1) = 6.5
,用于预测误差/标准偏差。这与您的响应变量具有相同的单位。
关于r - 计算具有负二项式响应的 GLM 的交叉验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38272150/