我知道如何将广义线性模型(GLMs)和广义线性混合模型(GLMs)与lme4软件包中的glm和glmer进行拟合。作为统计学专业的学生,我有兴趣学习如何拟合GLM和GLMME 29以下逐级公式基R码。如果你能指出这方面的任何资源和/或参考,我将不胜感激。提前谢谢。
EDiT
我想用公式一步一步地做、GLM、和GLMM,就像我们用矩阵方法做LM一样。是否有使用这种方法的R书或教程?谢谢
发布于 2011-08-03 22:43:37
Fox和Weisberg的“应用回归的R伙伴”在第8章有一个很好的指南,并以logistic回归为例。本书还介绍了如何使用S3和S4对象创建模型函数。特别是,它很好地回答了我最近提出的一个关于建模的问题-- What are the key components and functions for standard model objects in R?。
发布于 2013-01-31 04:49:16
这可能对有帮助
** Poisson回归: GLM**
*建议阅读:广义线性模型导论,Annette J. Dobson著,第2版,第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 programhttps://stackoverflow.com/questions/6929909
复制相似问题