2011-08-03 39 views
1

_R_码我知道如何适应广义线性模型(GLMS)和广义线性混合模型(GLMMs参数)与glm和在glmer从lme4包r 。作为统计学的学生,我有兴趣学习如何适合GLMGLMM下面的分步公式基地R代码。如果您在这方面指出任何资源和/或参考,我将非常感激。提前致谢。步骤一步基于公式GLM和GLMM

编辑

我想做GLMGLMM一步使用公式,我们使用矩阵法做LM一步。可以使用这种方法的书或教程是否有任何R?谢谢

+0

您的意思是说您想要学习如何编写代码以适合GLM(M)? –

+3

我认为答案部分是“McCullagh and Nelder”。阅读,告诉你所有的算法。虽然首先从简单的线性高斯东西开始。 – Spacedman

+0

@Spacedman的确。当然R的来源。 –

回答

2

这可有助于
**泊松回归:GLM **
* 推荐阅读:介绍广义线性模型,是Annette J.多布森,第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 program 
+0

+1:谢谢@overwhelmed您的不错答案。你的回答可能是一个好的开始。再次感谢。 – MYaseen208

+0

有没有办法改变这个代码的二项式实现,而不是泊松? –