R Under development (unstable) (2013-09-09 r63889) -- "Unsuffered Consequences" Copyright (C) 2013 The R Foundation for Statistical Computing Platform: i686-pc-linux-gnu (32-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library("testthat") > library("lme4") Loading required package: lattice Loading required package: Matrix > > testLevel <- if (nzchar(s <- Sys.getenv("LME4_TEST_LEVEL"))) + as.numeric(s) else 1 > > context("fitting glmer models") > test_that("glmer", { + expect_warning(glmer(z~ 1|f, family=binomial, method="abc"),"Use the nAGQ argument") + expect_warning(glmer(z~ 1|f, family=binomial, method="Laplace"),"Use the nAGQ argument") + expect_warning(glmer(z~ 1|f, sparseX=TRUE),"has no effect at present") + expect_that(gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd), + data = cbpp, family = binomial), is_a("glmerMod")) + expect_that(gm1@resp, is_a("glmResp")) + expect_that(gm1@pp, is_a("merPredD")) + expect_equal(ge1 <- unname(fixef(gm1)), c(-1.39854982537216, -0.992335519118859, + -1.12867532780426, -1.58030423764517), + tol=5e-4) + expect_equal(c(VarCorr(gm1)[[1]]), 0.41245527438386, tol=6e-4) + ### expect_that(family(gm1), equals(binomial())) + ### ?? binomial() has an 'initialize' component ... and the order is different + expect_equal(deviance(gm1), 184.052674598026, tol=1e-5) + expect_equal(sigma(gm1), 1) + expect_equal(extractAIC(gm1), c(5, 194.052674598026), tol=1e-5) + + expect_equal(theta <- unname(getME(gm1, "theta")), 0.642226809144453, tol=6e-4) + ###expect_that(X <- getME(gm1, "X"), is_equivalent_to(array(1, c(1, 30)))) + expect_that(Zt <- getME(gm1, "Zt"), is_a("dgCMatrix")) + expect_equal(dim(Zt), c(15L, 56L)) + expect_equal(length(Zt@x), 56L) + expect_equal(Zt@x, rep.int(1, 56L)) + expect_that(Lambdat <- getME(gm1, "Lambdat"), is_a("dgCMatrix")) + expect_equivalent(as(Lambdat, "matrix"), diag(theta, 15L, 15L)) + expect_error(glFormula(cbind(incidence, size - incidence) ~ period + (1 | herd), + data = subset(cbpp, herd==levels(herd)[1]), family = binomial), + "must have > 1") + expect_warning(glmer(cbind(incidence, size - incidence) ~ period + (1 | herd), + data = subset(cbpp, herd %in% levels(herd)[1:4]), + family = binomial, + control=glmerControl(check.nlev.gtreq.5="warning")), + "< 5 sampled levels") + expect_warning(fm1. <- glmer(Reaction ~ Days + (Days|Subject), sleepstudy), + regexp="calling .* with family=gaussian .* as a shortcut") + options(warn=2) + cbppX <- transform(cbpp,prop=incidence/size) + expect_is(glmer(prop ~ period + (1 | herd), + data = cbppX, family = binomial, weights=size), "glmerMod") + expect_is(glmer(prop ~ period + (1 | herd), + data = cbppX, family = binomial, weights=size, start=NULL), + "glmerMod") + expect_is(glmer(prop ~ period + (1 | herd), + data = cbppX, family = binomial, weights=size, verbose=0L), + "glmerMod") + expect_is(glmer(prop ~ period + (1 | herd), + data = cbppX, family = binomial, weights=size, subset=TRUE), + "glmerMod") + expect_is(glmer(prop ~ period + (1 | herd), + data = cbppX, family = binomial, weights=size, na.action="na.exclude"), + "glmerMod") + expect_is(glmer(prop ~ period + (1 | herd), + data = cbppX, family = binomial, weights=size, offset=rep(0,nrow(cbppX))), + "glmerMod") + expect_is(glmer(prop ~ period + (1 | herd), + data = cbppX, family = binomial, weights=size, contrasts=NULL), + "glmerMod") + expect_is(glmer(prop ~ period + (1 | herd), + data = cbppX, family = binomial, weights=size, devFunOnly=FALSE), + "glmerMod") + expect_is(glmer(prop ~ period + (1 | herd), + data = cbppX, family = binomial, weights=size, + control=glmerControl(optimizer="Nelder_Mead")), + "glmerMod") + expect_is(glmer(prop ~ period + (1 | herd), + data = cbppX, family = binomial, weights=size, control=glmerControl()), + "glmerMod") + options(warn=0) + expect_warning(glmer(prop ~ period + (1 | herd), + data = cbppX, family = binomial, weights=size, junkArg=TRUE), + "extra argument.*disregarded") + expect_warning(glmer(cbind(incidence, size - incidence) ~ period + (1 | herd), + data = cbpp, family = binomial, + control=list()), + "instead of passing a list of class") + expect_warning(glmer(cbind(incidence, size - incidence) ~ period + (1 | herd), + data = cbpp, family = binomial, + control=lmerControl()), + "instead of passing a list of class") + + ## + load(system.file("testdata","radinger_dat.RData",package="lme4")) + mod <- glmer(presabs~predictor+(1|species),family=binomial, + radinger_dat) + expect_is(mod,"merMod") + ## TODO: is this reliable across platforms or do we have to loosen? + expect_equal(unname(fixef(mod)),c(0.5425528,6.4289962)) + set.seed(101) + d <- data.frame(y=rbinom(1000,size=1,p=0.5), + x=runif(1000), + f=factor(rep(1:20,each=50)), + x2=rep(0:1,c(999,1))) + mod2 <- glmer(y~x+x2+(1|f),data=d,family=binomial) + expect_equal(unname(fixef(mod2))[1:2], + c(-0.10036244,0.03548523),tol=1e-4) + expect_true(unname(fixef(mod2)[3]<(-10))) + mod3 <- update(mod2,family=binomial(link="probit")) + expect_equal(unname(fixef(mod3))[1:2], + c(-0.06288878,0.02224270),tol=1e-4) + expect_true(unname(fixef(mod3)[3]<(-4))) + mod4 <- update(mod2,family=binomial(link="cauchit")) + + ## on-the-fly creation of index variables + set.seed(101) + d <- data.frame(y1=rpois(100,1), x=rnorm(100), ID=1:100) + fit1 <- glmer(y1 ~ x+(1|ID),data=d,family=poisson) + ## fit2 <- update(fit1, .~ x+(1|rownames(d))) + fit2 <- glmer(y1 ~ x+(1|rownames(.GlobalEnv$d)),data=d,family=poisson) + expect_equal(unname(unlist(VarCorr(fit1))), + unname(unlist(VarCorr(fit2)))) + + ## + if (testLevel>1) { + load(system.file("testdata","mastitis.rda",package="lme4")) + t1 <- system.time(g1 <- + glmer(NCM ~ birth + calvingYear + (1|sire) + + (1|herd),mastitis,poisson)) + t2 <- system.time(g2 <- update(g1, + control=glmerControl(optimizer="bobyqa"))) + ## 20 seconds N-M vs 8 seconds bobyqa ... + ## problem is fairly ill-conditioned so parameters + ## are relatively far apart even though likelihoods are OK + expect_equal(logLik(g1),logLik(g2),tol=1e-7) + } + + }) Error: Test failed: 'glmer' invalid type (NULL) for variable 'rownames(.GlobalEnv$d)' 1: glmer(y1 ~ x + (1 | rownames(.GlobalEnv$d)), data = d, family = poisson) 2: eval(mc, parent.frame(1L)) 3: eval(expr, envir, enclos) 4: lme4::glFormula(formula = y1 ~ x + (1 | rownames(.GlobalEnv$d)), data = d, family = poisson) 5: eval(mf, parent.frame()) 6: eval(expr, envir, enclos) 7: model.frame(data = d, drop.unused.levels = TRUE, formula = y1 ~ x + (1 + rownames(.GlobalEnv$d))) 8: model.frame.default(data = d, drop.unused.levels = TRUE, formula = y1 ~ x + (1 + rownames(.GlobalEnv$d))) 9: .handleSimpleError(function (e) { e$calls <- head(sys.calls()[-seq_len(frame + 7)], -2) signalCondition(e) }, "invalid type (NULL) for variable 'rownames(.GlobalEnv$d)'", quote(model.frame.default(data = d, drop.unused.levels = TRUE, formula = y1 ~ x + (1 + rownames(.GlobalEnv$d))))) Execution halted