为CPLM包创建R Squared功能

问题描述 投票:1回答:1

对于我的研究生研究,我正在使用CPLM软件包(特别是cpglmm函数)来计算数据集中的零膨胀数据(Tweedie复合Poisson分布),以查看登录繁殖鸟类密度的影响。这不是广泛使用的包,如lme4,nlme等。因此,可以在这些更常用的包上使用的模型验证方法不能在cpglmm上使用。

我目前正处于描述模型拟合的阶段,我正在尝试计算边际和条件的R平方值。不幸的是我不能使用r2glmm包或MuMln来计算R平方值,因为它们不支持cpglmm。因此,我必须通过一个例子找到here手动计算这些值(例子见附录6 cpglmm寄生虫模型,第33页)。这是该示例中的脚本:

# Fit null model without fixed effects (but including all random effects)
parmodCPr <- cpglmm(Parasite ~ 1 + (1 | Population) + (1 | Container), data = DataAll)

# Fit alternative model including fixed and all random effects
parmodCPf <- cpglmm(Parasite ~ Sex + Treatment + Habitat + (1 | Population) +
                  (1 | Container), data = DataAll)

# Calculation of the variance in fitted values
VarF <- var(as.vector(model.matrix(parmodCPf) %*% fixef(parmodCPf)))

# getting the observation-level variance Null model
phiN <- parmodCPr@phi # the dispersion parameter
pN <- parmodCPr@p # the index parameter
mu <- exp(fixef(parmodCPr) + 0.5 * (VarCorr(parmodCPr)$Population[1] + VarCorr(parmodCPr)$Container[1]))
VarOdN <- phiN * mu^(pN - 2) # the delta method

# Full model
phiF <- parmodCPf@phi # the dispersion parameter
pF <- parmodCPf@p # the index parameter
VarOdF <- phiF * mu^(pF - 2) # the delta method

# R2[GLMM(m)] - marginal R2[GLMM]; using the delta method observation-level variance
R2glmmM <- VarF/(VarF + sum(as.numeric(VarCorr(parmodCPf))) + VarOdF)

# R2[GLMM(c)] - conditional R2[GLMM] for full model
R2glmmC <- (VarF + sum(as.numeric(VarCorr(parmodCPf))))/(VarF + sum(as.numeric(VarCorr(parmodCPf))) +
                                                       VarOdF)

我希望能够做的是使用此代码在R中编写一个函数,输出边缘和条件R平方值(RglmmM和RglmmC)以及我的模型作为输入。我非常感谢对这个问题的任何帮助。希望我提供了足够的信息。

谢谢。

r regression mixed-models poisson tweedie
1个回答
0
投票

相信我明白了。这是我写的一个例子:

R2glmm <- function(model){

  # Calculation of the variance in fitted values
  VarALT <- var(as.vector(model.matrix(model) %*% fixef(model)))

  # getting the observation-level variance Null model
  phiNULL <- NULLmodel$phi # the dispersion parameter
  pNULL <- NULLmodel$p # the index parameter
  mu <- exp(fixef(NULLmodel) + 0.5 * (VarCorr(NULLmodel)$YEAR[1])) 
  VarOdNULL <- phiNULL * mu^(pNULL - 2) # the delta method

  # Alternate model
  phiALT <- model$phi # the dispersion parameter
  pALT <- model$p # the index parameter
  VarOdALT <- phiALT * mu^(pALT - 2) # the delta method

  # R2[GLMM(m)] - marginal R2[GLMM]; using the delta method observation-level variance
  R2glmmM <- VarALT/(VarALT + sum(as.numeric(VarCorr(model))) + VarOdALT)

  # R2[GLMM(c)] - conditional R2[GLMM] for full model
  R2glmmC <- (VarALT + sum(as.numeric(VarCorr(model))))/(VarALT + sum(as.numeric(VarCorr(model))) + VarOdALT)

  return(c(R2glmmM, R2glmmC))

  }

包含ALT的变量指的是备用模型。 “model”表示您需要运行该函数的任何cpglmm模型。

希望这有助于某人。多年来一直在研究这个问题及其他相关问题。

© www.soinside.com 2019 - 2024. All rights reserved.