Package r2glmm. August 5, 2017

Similar documents
An R # Statistic for Fixed Effects in the Linear Mixed Model and Extension to the GLMM

Package polypoly. R topics documented: May 27, 2017

Package jmcm. November 25, 2017

Package gtheory. October 30, 2016

Package RTransferEntropy

Package HIMA. November 8, 2017

Package MultisiteMediation

Package dominanceanalysis

Package blme. August 29, 2016

Package SEMModComp. R topics documented: February 19, Type Package Title Model Comparisons for SEM Version 1.0 Date Author Roy Levy

Package Tcomp. June 6, 2018

Package noncompliance

Package idmtpreg. February 27, 2018

Package ShrinkCovMat

Package Delaporte. August 13, 2017

Package beam. May 3, 2018

Package effectfusion

Package darts. February 19, 2015

Package gwqs. R topics documented: May 7, Type Package. Title Generalized Weighted Quantile Sum Regression. Version 1.1.0

Package varband. November 7, 2016

Package OGI. December 20, 2017

Package SpatialVS. R topics documented: November 10, Title Spatial Variable Selection Version 1.1

Package covsep. May 6, 2018

Package matrixnormal

Package aspi. R topics documented: September 20, 2016

Package TwoStepCLogit

Package sscor. January 28, 2016

Package Grace. R topics documented: April 9, Type Package

Package gma. September 19, 2017

Package BGGE. August 10, 2018

Package invgamma. May 7, 2017

Package RI2by2. October 21, 2016

Package funrar. March 5, 2018

Package symmoments. R topics documented: February 20, Type Package

Package KMgene. November 22, 2017

Package robustrao. August 14, 2017

Package SK. May 16, 2018

Package leiv. R topics documented: February 20, Version Type Package

Package basad. November 20, 2017

Package rnmf. February 20, 2015

Package sbgcop. May 29, 2018

Package factorqr. R topics documented: February 19, 2015

Package SPreFuGED. July 29, 2016

Package metafuse. October 17, 2016

Package reinforcedpred

Package xdcclarge. R topics documented: July 12, Type Package

Package multivariance

Package SimSCRPiecewise

Package chemcal. July 17, 2018

Package supernova. February 23, 2018

Package EMVS. April 24, 2018

Package RATest. November 30, Type Package Title Randomization Tests

Package FinCovRegularization

Package depth.plot. December 20, 2015

Package locfdr. July 15, Index 5

Package thief. R topics documented: January 24, Version 0.3 Title Temporal Hierarchical Forecasting

Package platetools. R topics documented: June 25, Title Tools and Plots for Multi-Well Plates Version 0.1.1

Package msir. R topics documented: April 7, Type Package Version Date Title Model-Based Sliced Inverse Regression

Package CorrMixed. R topics documented: August 4, Type Package

Package bhrcr. November 12, 2018

Package NlinTS. September 10, 2018

Package SMFilter. December 12, 2018

Fitting mixed models in R

Package scio. February 20, 2015

Package CEC. R topics documented: August 29, Title Cross-Entropy Clustering Version Date

Package mmm. R topics documented: February 20, Type Package

Package metansue. July 11, 2018

Package bivariate. December 10, 2018

Package rarpack. August 29, 2016

Package generalhoslem

Package rarhsmm. March 20, 2018

Package AID. R topics documented: November 10, Type Package Title Box-Cox Power Transformation Version 2.3 Date Depends R (>= 2.15.

Package bigtime. November 9, 2017

Package EnergyOnlineCPM

Package msu. September 30, 2017

Package MACAU2. R topics documented: April 8, Type Package. Title MACAU 2.0: Efficient Mixed Model Analysis of Count Data. Version 1.

Package ICBayes. September 24, 2017

Package spcov. R topics documented: February 20, 2015

Package ebal. R topics documented: February 19, Type Package

Package rbvs. R topics documented: August 29, Type Package Title Ranking-Based Variable Selection Version 1.0.

Package rgabriel. February 20, 2015

Package RZigZag. April 30, 2018

Package icmm. October 12, 2017

Package cccrm. July 8, 2015

Package esaddle. R topics documented: January 9, 2017

Package testassay. November 29, 2016

Package rdefra. March 20, 2017

Package kgc. December 21, Version Date Title Koeppen-Geiger Climatic Zones

Package weathercan. July 5, 2018

Package scpdsi. November 18, 2018

Package convospat. November 3, 2017

Package timelines. August 29, 2016

Package spthin. February 20, 2015

Package EBMAforecast

Package dlagm. January 17, 2018

Package MicroMacroMultilevel

Package cvequality. March 31, 2018

Package sklarsomega. May 24, 2018

Package clogitboost. R topics documented: December 21, 2015

Package horseshoe. November 8, 2016

Transcription:

Type Package Package r2glmm August 5, 2017 Title Computes R Squared for Mixed (Multilevel) Models Date 2017-08-04 Version 0.1.2 The model R squared and semi-partial R squared for the linear and generalized linear mixed model (LMM and GLMM) are computed with confidence limits. The R squared measure from Edwards et.al (2008) <DOI:10.1002/sim.3429> is extended to the GLMM using penalized quasi-likelihood (PQL) estimation (see Jaeger et al. 2016 <DOI:10.1080/02664763.2016.1193725>). Three methods of computation are provided and described as follows. First, The Kenward-Roger approach. Due to some inconsistency between the 'pbkrtest' package and the 'glmmpql' function, the Kenward-Roger approach in the 'r2glmm' package is limited to the LMM. Second, The method introduced by Nakagawa and Schielzeth (2013) <DOI:10.1111/j.2041-210x.2012.00261.x> and later extended by Johnson (2014) <DOI:10.1111/2041-210X.12225>. The 'r2glmm' package only computes marginal R squared for the LMM and does not generalize the statistic to the GLMM; however, confidence limits and semi-partial R squared for fixed effects are useful additions. Lastly, an approach using standardized generalized variance (SGV) can be used for covariance model selection. Package installation instructions can be found in the readme file. Imports mgcv, lmertest, Matrix, pbkrtest, ggplot2, afex, stats, MASS, gridextra, grid, data.table, dplyr Suggests lme4, nlme, testthat License GPL-2 LazyData TRUE RoxygenNote 6.0.1 URL https://github.com/bcjaeger/r2glmm BugReports https://github.com/bcjaeger/r2glmm/issues NeedsCompilation no Author Byron Jaeger [aut, cre] 1

2 calc_sgv Maintainer Byron Jaeger <byron.jaeger@gmail.com> Repository CRAN Date/Publication 2017-08-05 10:26:17 UTC R topics documented: calc_sgv........................................... 2 cmp_r2........................................... 3 glmpql........................................... 4 is.compsym......................................... 5 make.partial.c........................................ 5 plot.r2........................................... 6 pqlmer............................................ 7 print.r2........................................... 8 r2beta............................................ 8 r2dt............................................. 10 Index 12 calc_sgv Compute the standardized generalized variance (SGV) of a blocked diagonal matrix. Compute the standardized generalized variance (SGV) of a blocked diagonal matrix. calc_sgv(nblocks = NULL, blksizes = NULL, vmat) nblocks blksizes vmat Number of blocks in the matrix. vector of block sizes The blocked covariance matrix The SGV of the covariance matrix vmat. library(matrix) v1 = matrix(c(1,0.5,0.5,1), nrow = 2) v2 = matrix(c(1,0.2,0.1,0.2,1,0.3,0.1,0.3,1), nrow = 3) v3 = matrix(c(1,0.1,0.1,0.1,1,0.2,0.1,0.2,1), nrow = 3) calc_sgv(nblocks = 3, blksizes = c(2,3,3), vmat = Matrix::bdiag(v1,v2,v3))

cmp_r2 3 cmp_r2 Compute R2 with a specified C matrix Compute R2 with a specified C matrix cmp_r2(c, x, SigHat, beta, method, obsperclust = NULL, nclusts = NULL) c x SigHat beta method obsperclust nclusts Contrast matrix for fixed effects Fixed effects design matrix estimated model covariance (matrix or scalar) fixed effects estimates the method for computing r2beta number of observations per cluster (i.e. subject) number of clusters (i.e. subjects) A vector with the Wald statistic (ncp), approximate Wald F statistic (F), numerator degrees of freedom (v1), denominator degrees of freedom (v2), and the specified r squared value (Rsq) library(nlme) library(lme4) library(mgcv) lmemod = lme(distance ~ age*sex, random = ~1 Subject, data = Orthodont) X = model.matrix(lmemod, data = Orthodont) SigHat = extract.lme.cov(lmemod, data = Orthodont) beta = fixef(lmemod) p = length(beta) obsperclust = as.numeric(table(lmemod$data[,'subject'])) nclusts = length(obsperclust) C = cbind(rep(0, p-1),diag(p-1)) partial.c = make.partial.c(p-1,p,2) cmp_r2(c=c, x=x, SigHat=SigHat, beta=beta, obsperclust = obsperclust, nclusts = nclusts, method = 'sgv') cmp_r2(c=partial.c, x=x, SigHat=SigHat, beta=beta, obsperclust = obsperclust, nclusts = nclusts, method = 'sgv')

4 glmpql glmpql Compute PQL estimates for fixed effects from a generalized linear model. Compute PQL estimates for fixed effects from a generalized linear model. glmpql(glm.mod, niter = 20, data = NULL) glm.mod niter data a generalized linear model fitted with the glm function. maximum number of iterations allowed in the PQL algorithm. The data used by the fitted model. This argument is required for models with special expressions in their formula, such as offset, log, cbind(sucesses, trials), etc. A glmpql object (i.e. a linear model using pseudo outcomes). # Load the datasets package for example code library(datasets) library(dplyr) # We'll model the number of world changing discoveries per year for the # last 100 years as a poisson outcome. First, we set up the data dat = data.frame(discoveries) %>% mutate(year = 1:length(discoveries)) # Fit the GLM with a poisson link function mod <- glm(discoveries~year+i(year^2), family = 'poisson', data = dat) # Find PQL estimates using the original GLM mod.pql = glmpql(mod) # Note that the PQL model yields a higher R Squared statistic # than the fit of a strictly linear model. This is attributed # to correctly modelling the distribution of outcomes and then # linearizing the model to measure goodness of fit, rather than # simply fitting a linear model summary(mod.pql)

is.compsym 5 summary(linfit <- lm(discoveries~year+i(year^2), data = dat)) r2beta(mod.pql) r2beta(linfit) is.compsym Checks if a matrix is Compound Symmetric. Checks if a matrix is Compound Symmetric. is.compsym(mat, tol = 1e-05) mat tol The matrix to be tested. a number indicating the smallest acceptable difference between off diagonal values. True if the matrix is compound symmetric. gcmat <- matrix(c(1,0.2,0.1,0.2,1,0.3,0.1,0.3,1), nrow = 3) csmat <- matrix(c(1,0.2,0.2,0.2,1,0.2,0.2,0.2,1), nrow = 3) is.compsym(csmat) make.partial.c Generate partial contrast matrices Generate partial contrast matrices make.partial.c(rows, cols, index)

6 plot.r2 rows cols index Number of rows in the contrast matrix Number of columns in the contrast matrix A number corresponding to the position of the fixed effect in the vector of fixed effect parameter estimates. A contrast matrix designed to test the fixed effect corresponding to index in the vector of fixed effects. make.partial.c(4, 5, 2) make.partial.c(4, 5, 3) make.partial.c(4, 5, 2:4) plot.r2 Visualize standardized effect sizes and model R squared Visualize standardized effect sizes and model R squared ## S3 method for class 'R2' plot(x, y = NULL, txtsize = 10, maxcov = 3, r2labs = NULL, r2mthd = "sgv", cor = TRUE,...) x y txtsize maxcov r2labs r2mthd cor An R2 object from the r2beta function. An R2 object from the r2beta function. The text size of the axis labels. Maximum number of covariates to include in the semi-partial plots. a character vector containing labels for the models. The labels are printed as subscripts on a covariance model matrix. The method used to compute R2 An argument to be passed to the r2dt function. Only relevant if comparing two R2 objects.... to be passed to plot

pqlmer 7 A visual representation of the model and semi-partial R squared from the r2 object provided. library(nlme) library(r2glmm) data(orthodont) # Linear mixed model lmemod = lme(distance ~ age*sex, random = ~1 Subject, data = Orthodont) r2 = r2beta(model=lmemod,partial=true,method='sgv') plot(x=r2) pqlmer pqlmer Fit a GLMM model with multivariate normal random effects using Penalized Quasi-Likelihood for mermod objects. pqlmer(formula, family, data, niter = 40, verbose = T) formula family data niter verbose The lme4 model formula. a family function of the error distribution and link function to be used in the model. the dataframe containing the variables in the model. Maximum number of iterations to perform. if TRUE, iterations are printed to console. A pseudo linear mixed model of class "lme". See Also glmmpql

8 r2beta # Compare lmer PQL with lme PQL library(mass) lmepql = glmmpql(y ~ trt + week + I(week > 2), random = ~ 1 ID, family = binomial, data = bacteria, verbose = FALSE) merpql= pqlmer(y ~ trt + week + I(week > 2) + (1 ID), family = binomial, data = bacteria, verbose = FALSE) summary(lmepql) summary(merpql) print.r2 Print the contents of an R2 object Print the contents of an R2 object ## S3 method for class 'R2' print(x,...) x an object of class R2... other arguments passed to the print function. r2beta r2beta Compute R Squared for Mixed Models Computes coefficient of determination (R squared) from edwards et al., 2008 and the generalized R squared from Jaeger et al., 2016. Currently implemented for linear mixed models with lmer and lme objects. For generalized linear mixed models, only glmmpql are supported. r2beta(model, partial = TRUE, method = "sgv", data = NULL)

r2beta 9 model partial method data a fitted mermod, lme, or glmmpql model. if TRUE, semi-partial R squared are calculated for each fixed effect in the mixed model. Specifies the method of computation for R squared beta: if method = sgv then the standardized generalized variance approach is applied. This method is recommended for covariance model selection. if method = kr, then the Kenward Roger approach is applied. This option is only available for lme models. if method = nsj,then the Nakagawa and Schielzeth approach is applied. This option is available for lmer and lme objects. if method = lm, the classical R squared from the linear model is computed. This method should only be used on glm and lm object. The data used by the fitted model. This argument is required for models with special expressions in their formula, such as offset, log, cbind(sucesses, trials), etc. A dataframe containing the model F statistic, numerator and denominator degrees of freedom, noncentrality parameter, and R squared statistic with 95 If partial = TRUE, then the dataframe also contains partial R squared statistics for all fixed effects in the model. References Edwards, Lloyd J., et al. "An R2 statistic for fixed effects in the linear mixed model." Statistics in medicine 27.29 (2008): 6137-6157. Nakagawa, Shinichi, and Holger Schielzeth. "A general and simple method for obtaining R2 from generalized linear mixed effects models." Methods in Ecology and Evolution 4.2 (2013): 133-142. Jaeger, Byron C., et al., "An R Squared Statistic for Fixed Effects in the Generalized Linear Mixed Model." Journal of Applied Statistics (2016). library(nlme) library(lme4) data(orthodont) # Linear mixed models mermod = lmer(distance ~ age*sex + (1 Subject), data = Orthodont) lmemod = lme(distance ~ age*sex, random = ~1 Subject, data = Orthodont) # The Kenward-Roger approach r2beta(mermod, method = 'kr') # Standardized Generalized Variance r2beta(mermod, method = 'sgv') r2beta(lmemod, method = 'sgv')

10 r2dt # The marginal R squared by Nakagawa and Schielzeth (extended by Johnson) r2beta(mermod, method = 'nsj') # linear and generalized linear models library(datasets) dis = data.frame(discoveries) dis$year = 1:nrow(dis) lmod = lm(discoveries ~ year + I(year^2), data = dis) glmod = glm(discoveries ~ year + I(year^2), family = 'poisson', data = dis) # Using an inappropriate link function (normal) leads to # a poor fit relative to the poisson link function. r2beta(lmod) r2beta(glmod) # PQL models # Currently only SGV method is supported library(mass) PQL_bac = glmmpql(y ~ trt + I(week > 2), random = ~ 1 ID, family = binomial, data = bacteria, verbose = FALSE) r2beta(pql_bac, method='sgv') r2dt R Squared Difference Test (R2DT). Test for a statistically significant difference in generalized explained variance between two candidate models. R Squared Difference Test (R2DT). Test for a statistically significant difference in generalized explained variance between two candidate models. r2dt(x, y = NULL, cor = TRUE, fancy = FALSE, onesided = TRUE, clim = 95, nsims = 2000, mu = NULL) x y An R2 object from the r2beta function. An R2 object from the r2beta function. If y is not specified, Ho: E[x] = mu is tested (mu is specified by the user).

r2dt 11 cor fancy onesided clim nsims mu if TRUE, the R squared statistics are assumed to be positively correlated and a simulation based approach is used. If FALSE, the R squared are assumed independent and the difference of independent beta distributions is used. This only needs to be specified when two R squared measures are being considered. if TRUE, the output values are rounded and changed to characters. if TRUE, the alternative hypothesis is that one model explains a larger proportion of generalized variance. If false, the alternative is that the amount of generalized variance explained by the two candidate models is not equal. Desired confidence level for interval estimates regarding the difference in generalized explained variance. number of samples to draw when simulating correlated non-central beta random variables. This parameter is only relevant if cor=true. Used to test Ho: E[x] = mu. A confidence interval for the difference in R Squared statistics and a p-value corresponding to the null hypothesis of no difference. library(nlme) library(lme4) library(r2glmm) data(orthodont) # Comparing two linear mixed models m1 = lmer(distance ~ age*sex+(1 Subject), Orthodont) m2 = lmer(distance ~ age*sex+(1+age Subject), Orthodont) m1r2 = r2beta(model=m1,partial=false) m2r2 = r2beta(model=m2,partial=false) # Accounting for correlation can make a substantial difference. r2dt(x=m1r2, y = m2r2, cor = TRUE) r2dt(x=m1r2, y = m2r2, cor = FALSE)

Index calc_sgv, 2 cmp_r2, 3 glmmpql, 7, 8 glmpql, 4 is.compsym, 5 lme, 8, 9 lmer, 8, 9 make.partial.c, 5 plot.r2, 6 pqlmer, 7 print.r2, 8 r2beta, 8 r2dt, 10 12