基于逐步公式的_R_代码用于GLM和GLMM [英] Step-by-step formula based _R_ codes for GLM and GLMM

查看:450
本文介绍了基于逐步公式的_R_代码用于GLM和GLMM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用中来自lme4包中的glmglmer将广义线性模型( GLM )和广义线性混合模型( GLMM )拟合> R .作为统计专业的学生,​​我有兴趣按照逐步的公式基础 R 代码学习如何适合 GLM GLMM .如果您在这方面有任何资源和/或参考,我将不胜感激.预先感谢.

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

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

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

推荐答案

这可能有帮助
**泊松回归:GLM **
* 建议阅读:安妮特·J·多布森(Annette J. Dobson),第二版,第4章,第4.3和4.4节*

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

这篇关于基于逐步公式的_R_代码用于GLM和GLMM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆