Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.54 KB | None | 0 0
  1. # revised on 6 Nov 2013
  2. #A general and simple method for obtaining R2 from generalized linear mixed-effects models
  3. #Shinichi Nakagawa1,2 and Holger Schielzeth3
  4. #1 National Centre of Growth and Development, Department of Zoology, University of Otago, Dunedin, New Zealand
  5. #2 Department of Behavioral Ecology and Evolutionary Genetics, Max Planck Institute for Ornithology, Seewiesen, Germany
  6. #3 Department of Evolutionary Biology, Bielefeld University, Bielefeld, Germany
  7. #Running head: Variance explained by GLMMs
  8. #Correspondence:
  9. #S. Nakagawa; Department of Zoology, University of Otago, 340 Great King Street, Dunedin, 9054, New Zealand
  10. #Tel: +64 (0)3 479 5046
  11. #Fax: +64 (0)3 479 7584
  12. #e-mail: shinichi.nakagawa@otago.ac.nz
  13.  
  14. ####################################################
  15. # A. Preparation
  16. ####################################################
  17. # Note that data generation appears below the analysis section.
  18. # You can use the simulated data table from the supplementary files to reproduce exactly the same results as presented in the paper.
  19.  
  20. # Set the work directy that is used for rading/saving data tables
  21. # setwd("/Users/R2")
  22.  
  23. # load R required packages
  24. # If this is done for the first time, it might need to first download and install the package
  25. # install.packages("arm")
  26. library(arm)
  27. # install.packages("lme4")
  28. # the verson 1.0-5
  29. library(lme4)
  30.  
  31.  
  32. ####################################################
  33. # B. Analysis
  34. ####################################################
  35.  
  36. # 1. Analysis of body size (Gaussian mixed models)
  37. #---------------------------------------------------
  38.  
  39. # Clear memory
  40. rm(list = ls())
  41.  
  42. # Read body length data (Gaussian, available for both sexes)
  43. Data <- read.csv("BeetlesBody.csv")
  44.  
  45. # Fit null model without fixed effects (but including all random effects)
  46. m0 <- lmer(BodyL ~ 1 + (1 | Population) + (1 | Container), data = Data)
  47.  
  48. # Fit alternative model including fixed and all random effects
  49. mF <- lmer(BodyL ~ Sex + Treatment + Habitat + (1 | Population) + (1 | Container), data = Data)
  50.  
  51. # View model fits for both models
  52. summary(m0)
  53. summary(mF)
  54.  
  55. # Extraction of fitted value for the alternative model
  56. # fixef() extracts coefficents for fixed effects
  57. # mF@pp$X returns fixed effect design matrix
  58. Fixed <- fixef(mF)[2] * mF@pp$X[, 2] + fixef(mF)[3] * mF@pp$X[, 3] + fixef(mF)[4] * mF@pp$X[, 4]
  59.  
  60. # Calculation of the variance in fitted values
  61. VarF <- var(Fixed)
  62.  
  63. # An alternative way for getting the same result
  64. VarF <- var(as.vector(fixef(mF) %*% t(mF@pp$X)))
  65.  
  66. # R2GLMM(m) - marginal R2GLMM
  67. # Equ. 26, 29 and 30
  68. # VarCorr() extracts variance components
  69. # attr(VarCorr(lmer.model),'sc')^2 extracts the residual variance
  70. VarF/(VarF + VarCorr(mF)$Container[1] + VarCorr(mF)$Population[1] + attr(VarCorr(mF), "sc")^2)
  71.  
  72. # R2GLMM(c) - conditional R2GLMM for full model
  73. # Equ. 30
  74. (VarF + VarCorr(mF)$Container[1] + VarCorr(mF)$Population[1])/(VarF + VarCorr(mF)$Container[1] + VarCorr(mF)$Population[1] + (attr(VarCorr(mF), "sc")^2))
  75.  
  76. # AIC and BIC needs to be calcualted with ML not REML in body size models
  77. m0ML <- lmer(BodyL ~ 1 + (1 | Population) + (1 | Container), data = Data, REML = FALSE)
  78. mFML <- lmer(BodyL ~ Sex + Treatment + Habitat + (1 | Population) + (1 | Container), data = Data, REML = FALSE)
  79.  
  80. # View model fits for both models fitted by ML
  81. summary(m0ML)
  82. summary(mFML)
  83.  
  84.  
  85. # 2. Analysis of colour morphs (Binomial mixed models)
  86. #---------------------------------------------------
  87.  
  88. # Clear memory
  89. rm(list = ls())
  90. # Read colour morph data (Binary, available for males only)
  91. Data <- read.csv("BeetlesMale.csv")
  92.  
  93. # Fit null model without fixed effects (but including all random effects)
  94. m0 <- glmer(Colour ~ 1 + (1 | Population) + (1 | Container), family = "binomial", data = Data)
  95.  
  96. # Fit alternative model including fixed and all random effects
  97. mF <- glmer(Colour ~ Treatment + Habitat + (1 | Population) + (1 | Container), family = "binomial", data = Data)
  98.  
  99. # View model fits for both models
  100. summary(m0)
  101. summary(mF)
  102.  
  103. # Extraction of fitted value for the alternative model
  104. # fixef() extracts coefficents for fixed effects
  105. # mF@pp$X returns fixed effect design matrix
  106. Fixed <- fixef(mF)[2] * mF@pp$X[, 2] + fixef(mF)[3] * mF@pp$X[, 3]
  107.  
  108. # Calculation of the variance in fitted values
  109. VarF <- var(Fixed)
  110.  
  111. # An alternative way for getting the same result
  112. VarF <- var(as.vector(fixef(mF) %*% t(mF@pp$X)))
  113.  
  114. # R2GLMM(m) - marginal R2GLMM
  115. # see Equ. 29 and 30 and Table 2
  116. VarF/(VarF + VarCorr(mF)$Container[1] + VarCorr(mF)$Population[1] + pi^2/3)
  117.  
  118. # R2GLMM(c) - conditional R2GLMM for full model
  119. # Equ. 30
  120. (VarF + VarCorr(mF)$Container[1] + VarCorr(mF)$Population[1])/(VarF + VarCorr(mF)$Container[1] + VarCorr(mF)$Population[1] + pi^2/3)
  121.  
  122.  
  123. # 3. Analysis of fecundity (Poisson mixed models)
  124. #---------------------------------------------------
  125.  
  126. # Clear memory
  127. rm(list = ls())
  128.  
  129. # Read fecundity data (Poisson, available for females only)
  130. Data <- read.csv("BeetlesFemale.csv")
  131.  
  132. # Creating a dummy variable that allows estimating additive dispersion in lmer
  133. # This triggers a warning message when fitting the model
  134. Unit <- factor(1:length(Data$Egg))
  135.  
  136. # Fit null model without fixed effects (but including all random effects)
  137. m0 <- glmer(Egg ~ 1 + (1 | Population) + (1 | Container) + (1 | Unit), family = "poisson", data = Data)
  138.  
  139. # Fit alternative model including fixed and all random effects
  140. mF <- glmer(Egg ~ Treatment + Habitat + (1 | Population) + (1 | Container) + (1 | Unit), family = "poisson", data = Data)
  141.  
  142. # View model fits for both models
  143. summary(m0)
  144. summary(mF)
  145.  
  146. # Extraction of fitted value for the alternative model
  147. # fixef() extracts coefficents for fixed effects
  148. # mF@pp$X returns fixed effect design matrix
  149. Fixed <- fixef(mF)[2] * mF@pp$X[, 2] + fixef(mF)[3] * mF@pp$X[, 3]
  150.  
  151. # Calculation of the variance in fitted values
  152. VarF <- var(Fixed)
  153.  
  154. # An alternative way for getting the same result
  155. VarF <- var(as.vector(fixef(mF) %*% t(mF@pp$X)))
  156.  
  157. # R2GLMM(m) - marginal R2GLMM
  158. # see Equ. 29 and 30 and Table 2
  159. # fixef(m0) returns the estimate for the intercept of null model
  160. VarF/(VarF + VarCorr(mF)$Container[1] + VarCorr(mF)$Population[1] + VarCorr(mF)$Unit[1] + log(1 + 1/exp(as.numeric(fixef(m0)))))
  161.  
  162. # R2GLMM(c) - conditional R2GLMM for full model
  163. # Equ. 30
  164. (VarF + VarCorr(mF)$Container[1] + VarCorr(mF)$Population[1])/(VarF + VarCorr(mF)$Container[1] + VarCorr(mF)$Population[1] + VarCorr(mF)$Unit[1] + log(1 +
  165. 1/exp(as.numeric(fixef(m0)))))
  166.  
  167.  
  168. ####################################################
  169. # C. Data generation
  170. ####################################################
  171.  
  172. # 1. Design matrices
  173. #---------------------------------------------------
  174.  
  175. # Clear memory
  176. rm(list = ls())
  177.  
  178. # 12 different populations n = 960
  179. Population <- gl(12, 80, 960)
  180.  
  181. # 120 containers (8 individuals in each container)
  182. Container <- gl(120, 8, 960)
  183.  
  184. # Sex of the individuals. Uni-sex within each container (individuals are sorted at the pupa stage)
  185. Sex <- factor(rep(rep(c("Female", "Male"), each = 8), 60))
  186.  
  187. # Habitat at the collection site: dry or wet soil (four indiviudal from each Habitat in each container)
  188. Habitat <- factor(rep(rep(c("dry", "wet"), each = 4), 120))
  189.  
  190. # Food treatment at the larval stage: special food ('Exp') or standard food ('Cont')
  191. Treatment <- factor(rep(c("Cont", "Exp"), 480))
  192.  
  193. # Data combined in a dataframe
  194. Data <- data.frame(Population = Population, Container = Container, Sex = Sex, Habitat = Habitat, Treatment = Treatment)
  195.  
  196.  
  197. # 2. Gaussian response: body length (both sexes)
  198. #---------------------------------------------------
  199.  
  200. # simulation of the underlying random effects (Population and Container with variance of 1.3 and 0.3, respectively)
  201. PopulationE <- rnorm(12, 0, sqrt(1.3))
  202. ContainerE <- rnorm(120, 0, sqrt(0.3))
  203.  
  204. # data generation based on fixed effects, random effects and random residuals errors
  205. Data$BodyL <- 15 - 3 * (as.numeric(Sex) - 1) + 0.4 * (as.numeric(Treatment) - 1) + 0.15 * (as.numeric(Habitat) - 1) + PopulationE[Population] + ContainerE[Container] +
  206. rnorm(960, 0, sqrt(1.2))
  207.  
  208. # save data (to current work directory)
  209. write.csv(Data, file = "BeetlesBody.csv", row.names = F)
  210.  
  211.  
  212. # 3. Binomial response: colour morph (males only)
  213. #---------------------------------------------------
  214.  
  215. # Subset the design matrix (only males express colour morphs)
  216. DataM <- subset(Data, Sex == "Male")
  217.  
  218. # simulation of the underlying random effects (Population and Container with variance of 1.2 and 0.2, respectively)
  219. PopulationE <- rnorm(12, 0, sqrt(1.2))
  220. ContainerE <- rnorm(120, 0, sqrt(0.2))
  221.  
  222. # generation of response values on link scale (!) based on fixed effects and random effects
  223. ColourLink <- with(DataM, 0.8 * (-1) + 0.8 * (as.numeric(Treatment) - 1) + 0.5 * (as.numeric(Habitat) - 1) + PopulationE[Population] + ContainerE[Container])
  224.  
  225. # data generation (on data scale!) based on binomial distribution
  226. DataM$Colour <- rbinom(length(ColourLink), 1, invlogit(ColourLink))
  227.  
  228. # save data (to current work directory)
  229. write.csv(DataM, file = "BeetlesMale.csv", row.names = F)
  230.  
  231.  
  232. # 4. Poisson response: fecundity (females only)
  233. #---------------------------------------------------
  234.  
  235. # Subset the design matrix (only females express colour morphs)
  236. DataF <- Data[Data$Sex == "Female", ]
  237.  
  238. # random effects
  239. PopulationE <- rnorm(12, 0, sqrt(0.4))
  240. ContainerE <- rnorm(120, 0, sqrt(0.05))
  241.  
  242. # generation of response values on link scale (!) based on fixed effects, random effects and residual errors
  243. EggLink <- with(DataF, 1.1 + 0.5 * (as.numeric(Treatment) - 1) + 0.1 * (as.numeric(Habitat) - 1) + PopulationE[Population] + ContainerE[Container] + rnorm(480,
  244. 0, sqrt(0.1)))
  245.  
  246. # data generation (on data scale!) based on Poisson distribution
  247. DataF$Egg <- rpois(length(EggLink), exp(EggLink))
  248.  
  249. # save data (to current work directory)
  250. write.csv(DataF, file = "BeetlesFemale.csv", row.names = F)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement