有没有办法计算R中GLM的估计值?

发布于 2025-02-12 07:06:25 字数 579 浏览 2 评论 0原文

我正在尝试获得R中的GLM模型的剩余系数估算。

这是我简单的循环版本实现:

data("mtcars")
res <- matrix(nrow = nrow(mtcars), ncol = 3L)
for (i in seq_len(nrow(mtcars))) {
  loo.data <- mtcars[-i, ]
  model <- glm(vs ~ mpg + disp, family = binomial, data = loo.data)
  res[i, ] <- model$coefficients
}

循环后,我得到了一个系数的矩阵,每行都包含一对一的估算。 res中的行数与观察总数相同。 res不必是矩阵格式。

是否有更清洁,也许更快地获得相同结果的方法?它将在其他C ++代码中使用。我在启动软件包中找到了cv.glm,但在我的情况下似乎不适用。使用RCPP 函数类可能是一个选项吗?在C ++中编写Fisher评分算法似乎只是浪费时间和精力。谢谢。

I'm trying to get the leave-one-out coefficient estimates for glm models in R. The context is not on cross validation, though.

Here's my simple loop version implementation:

data("mtcars")
res <- matrix(nrow = nrow(mtcars), ncol = 3L)
for (i in seq_len(nrow(mtcars))) {
  loo.data <- mtcars[-i, ]
  model <- glm(vs ~ mpg + disp, family = binomial, data = loo.data)
  res[i, ] <- model$coefficients
}

After the loop, I get a matrix of coefficients where each row consists of leave-one-out estimates. The number of rows in res is the same as the total number of observations. The res does not have to be a matrix format.

Is there a cleaner and perhaps faster way to get the same result? It will be used in other C++ code. I found cv.glm in boot package but it does not seem to be applicable in my case. Is using Rcpp Function class could be an option? Writing the Fisher scoring algorithm in C++ seems just waste of time and effort. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文