基于分步公式的 GLM 和 GLMM _R_ 代码

发布于 2024-11-27 21:00:29 字数 395 浏览 2 评论 0原文

我知道如何使用 glmglmerGLM) 和广义线性混合模型 (GLMM) > 来自 R 中的 lme4 包。作为一名统计专业的学生,​​我有兴趣学习如何按照分步公式库 R 代码拟合 GLMGLMM。如果您指出这方面的任何资源和/或参考资料,我将不胜感激。提前致谢。

编辑

我想使用公式逐步进行GLMGLMM,就像我们使用矩阵方法LM一样。有没有使用这种方法的R书籍或教程?谢谢

I know how to fit generalized linear models (GLMs) and generalized linear mixed models (GLMMs) with glm and glmer from lme4 package in R. Being a student of statistics, I'm interested in learning how to fit GLM and GLMM following step-by-step formula bases R codes. I'd highly appreciate if you point out any resource and/or reference in this regard. Thanks in advance.

EDiT

I'd like to do GLM and GLMM step by step using formula as we do LM using matrix approach. Is there any R book or tutorial available that use this approach? Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆啦不做梦 2024-12-04 21:00:29

Fox 和 Weisberg 撰写的“An R Companion to Applied Regression”在第 8 章提供了很好的指南,以逻辑回归为例。本书还介绍了如何使用 S3 和 S4 对象创建一般模型函数。特别是,它对我最近提出的有关建模的问题有很好的答案 - R 中标准模型对象的关键组件和功能是什么?

"An R Companion to Applied Regression" by Fox and Weisberg, has an excellent guide in chapter 8, with logistic regression as an example. The book also teaches a bit about how to create model functions in general with S3 and S4 objects. In particular, it has good answers to a recent question I'd asked about modeling -- What are the key components and functions for standard model objects in R?.

硬不硬你别怂 2024-12-04 21:00:29

这可能有帮助
** 泊松回归:GLM**
*建议阅读:《广义线性模型简介》,作者:Annette J. Dobson,第二版,第 4 章,第 4.3 和 4.4 节 *

library(MASS)
poisreg = function(n, b1, y, x1, tolerence) {  # n is the number of iteration   
  x0 = rep(1, length(x1))   
  x = cbind(x0, x1)  
  y = as.matrix(y)  
  w = matrix(0, nrow = (length(y)), ncol = (length(y)))  
  b0 = b1  
  result = b0
  for (i in 1:n) {  
    mu = exp(x %*% b0)     
    diag(w) = mu  
    eta = x %*% b0  
    z = eta + (y - mu) * (1/mu)   # dot product of (y - mu) & (1/mu)   
    xtwx = t(x) %*% w %*% x  
    xtwz = t(x) %*% w %*% z  
    b1 = solve(xtwx, xtwz)  
    if(sqrt(sum(b0 - b1)^2) > tolerence) (b0 <- b1)  
    result<- cbind(result,b1) # to get all the iterated values  
  }  
  result  
}
x1 <- c(-1,-1,0,0,0,0,1,1,1) # x1 is the explanatory variable 
y<- c(2,3,6,7,8,9,10,12,15)  # y is the dependent variable
b1 = c(1,2) # initial value  
poisreg (10, b1, y, x1, .001)   # Nicely converge after 10 iterations  
glm(y~x1, family=poisson(link="log"))   # check your result with the R GLM program

This may help
** Poisson regression: GLM**
*Suggested reading: An Introduction to generalized linear model, by Annette J. Dobson, 2nd Edition, Chapter 4, section 4.3 and 4.4 *

library(MASS)
poisreg = function(n, b1, y, x1, tolerence) {  # n is the number of iteration   
  x0 = rep(1, length(x1))   
  x = cbind(x0, x1)  
  y = as.matrix(y)  
  w = matrix(0, nrow = (length(y)), ncol = (length(y)))  
  b0 = b1  
  result = b0
  for (i in 1:n) {  
    mu = exp(x %*% b0)     
    diag(w) = mu  
    eta = x %*% b0  
    z = eta + (y - mu) * (1/mu)   # dot product of (y - mu) & (1/mu)   
    xtwx = t(x) %*% w %*% x  
    xtwz = t(x) %*% w %*% z  
    b1 = solve(xtwx, xtwz)  
    if(sqrt(sum(b0 - b1)^2) > tolerence) (b0 <- b1)  
    result<- cbind(result,b1) # to get all the iterated values  
  }  
  result  
}
x1 <- c(-1,-1,0,0,0,0,1,1,1) # x1 is the explanatory variable 
y<- c(2,3,6,7,8,9,10,12,15)  # y is the dependent variable
b1 = c(1,2) # initial value  
poisreg (10, b1, y, x1, .001)   # Nicely converge after 10 iterations  
glm(y~x1, family=poisson(link="log"))   # check your result with the R GLM program
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文