[R-sig-ME] logistic regression with glmer, three ways

Malcolm Fairbrother M.Fairbrother at bristol.ac.uk
Thu Nov 26 15:13:27 CET 2015


Dear all,
I am trying to fit a logistic mixed model using lme4, and finding some
results I can't understand.
If I fit the model either using "cbind" (A) or representing counts using
weights (B), I get identical or virtually identical results. If I fit the
model with one observation per row (C), I also get the same random effects
variances and fixed effects estimates... but the standard errors are very
different.
What am I missing, that explains this?
Full code for replication is below. Any help/clarification would be greatly
appreciated.
- Malcolm



library(lme4)

obj <- structure(list(wi = c(10004, 20004, 30002, 30003, 30004, 40004,
50003, 50004, 60002, 60003, 60004, 70004, 80002, 80003, 80004,
90002, 100003, 100004, 110004, 120002, 120003, 120004, 130002,
130003, 130004, 140003, 140004, 150002, 150003, 150004, 160002,
160003, 160004, 170004, 180002, 180003, 180004, 190002, 190003,
190004, 200003, 200004, 210002, 210003, 210004, 220002, 220003,
220004, 230002, 230003, 230004, 240002, 240003, 240004, 250004,
260003, 260004, 270004, 280003, 280004, 290004, 300002, 300003,
300004, 310004, 320004, 330002, 330003, 330004, 350002, 350004,
360002, 360003, 360004, 370002, 370003, 370004, 380003, 380004,
390003, 390004, 400004, 410003, 410004, 420003, 420004, 430002,
430003, 430004, 440002, 440003, 440004, 450004, 460003, 460004,
470003, 470004, 480002), X0 = c(713L, 422L, 755L, 868L, 693L,
661L, 254L, 446L, 1399L, 684L, 312L, 484L, 223L, 274L, 429L,
615L, 467L, 536L, 473L, 846L, 830L, 731L, 648L, 563L, 554L, 490L,
747L, 394L, 598L, 560L, 379L, 479L, 325L, 337L, 2040L, 943L,
1012L, 453L, 506L, 366L, 923L, 1097L, 375L, 304L, 562L, 396L,
483L, 369L, 175L, 217L, 243L, 1061L, 927L, 425L, 337L, 434L,
715L, 730L, 524L, 630L, 492L, 20L, 76L, 79L, 535L, 494L, 481L,
328L, 319L, 833L, 700L, 230L, 436L, 783L, 297L, 218L, 184L, 157L,
410L, 932L, 540L, 409L, 376L, 420L, 534L, 637L, 872L, 474L, 325L,
820L, 738L, 704L, 421L, 411L, 809L, 340L, 468L, 413L), X1 = c(604L,
979L, 679L, 580L, 743L, 638L, 667L, 857L, 1363L, 1148L, 1194L,
901L, 792L, 618L, 765L, 1103L, 467L, 853L, 425L, 1232L, 908L,
844L, 347L, 348L, 894L, 379L, 623L, 140L, 384L, 352L, 551L, 1078L,
1163L, 901L, 1381L, 1008L, 926L, 1335L, 1414L, 1610L, 190L, 333L,
588L, 628L, 904L, 290L, 426L, 399L, 824L, 770L, 630L, 948L, 977L,
960L, 981L, 416L, 488L, 472L, 586L, 892L, 816L, 373L, 925L, 1225L,
735L, 807L, 530L, 669L, 1186L, 394L, 370L, 631L, 535L, 625L,
874L, 759L, 1341L, 854L, 832L, 1239L, 736L, 888L, 812L, 792L,
407L, 620L, 1741L, 625L, 1080L, 226L, 208L, 187L, 751L, 765L,
1251L, 617L, 693L, 1401L), be = c(1, 2, 3, 3, 3, 4, 5, 5, 6,
6, 6, 7, 8, 8, 8, 9, 10, 10, 11, 12, 12, 12, 13, 13, 13, 14,
14, 15, 15, 15, 16, 16, 16, 17, 18, 18, 18, 19, 19, 19, 20, 20,
21, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 26, 26, 27,
28, 28, 29, 30, 30, 30, 31, 32, 33, 33, 33, 35, 35, 36, 36, 36,
37, 37, 37, 38, 38, 39, 39, 40, 41, 41, 42, 42, 43, 43, 43, 44,
44, 44, 45, 46, 46, 47, 47, 48), xD = c(0.524307559318364,
0.763542402955787,
-0.175670143766333, 0.00982801368753794, 0.171734367713831,
0.853465151804533,
-0.120558155492305, 0.564928392055597, -0.155064052602568,
0.00679037740374344,
0.111975199497572, 0.512877661043456, -0.157973257281779,
-0.171234692228043,
0.419036653726705, -0.132665914601771, -0.150714124150286,
0.24994498095932,
0.206700520630643, -0.208268170700368, -0.0750100628941794,
0.296858558168208,
-0.174592030130771, 0.0180614401400381, 0.128207276958865,
-0.214857479897565,
0.374642212780139, -0.134729936508285, 0.0533554402688754,
0.161401987751531,
-0.127379608982776, 0.008495320648783, 0.115214858592721, 0.42430939422253,
-0.134278135313655, -0.00190924064453402, 0.13491769790403,
-0.180932304380458,
-0.00605077384790365, 0.175496607807141, -0.0454797547642141,
0.242021561287634, -0.188556011418235, -0.061841275029169,
0.258249752442066,
-0.140950742135457, -0.0147706249172281, 0.191706749943735,
-0.457953860744226,
0.0707520487160007, 0.295169775614251, -0.118201446303778,
0.00559417031071785,
0.0187882678385503, 0.155675695667328, -0.254338988033491,
0.465851265111546,
0.115872339471522, 0.00880128187019125, 0.230608970064243,
0.225412467657899,
-0.267152578878703, 0.0646910643525653, 0.200582879060123,
0.21133836700213,
0.25249689149948, -0.189772066302413, 0.0244390231314893,
0.186314037458162,
-0.253461034585483, 0.156182014608826, -0.3060350195241,
0.00908366935986171,
0.37818509859163, -0.186216139704441, 0.0376706945549361,
0.127207172092761,
-0.182961496155655, 0.431715611799, -0.255051113988716, 0.380085409356923,
0.315317829875132, -0.0805760582482353, 0.414358344214618,
-0.097803077410747,
0.254981382125769, -0.191238180985892, 0.00488053924246579,
0.1715300555248,
-0.13917965385563, -0.00869608920645071, 0.139200601300228,
0.129019867839904,
-0.067090005487124, 0.178986504919836, -0.361092444872377,
0.306102862840488,
-0.167277811360057), xM = c(1.63256478157338, 1.23639947382672,
3.6113561255061, 3.6113561255061, 3.6113561255061, 1.81132913921155,
2.10837829325362, 2.10837829325362, 3.58252523980364, 3.58252523980364,
3.58252523980364, 1.72445279981385, 2.31322790712443, 2.31322790712443,
2.31322790712443, 3.57189978569991, 2.83531097614585, 2.83531097614585,
3.37202028911156, 3.07483904377024, 3.07483904377024, 3.07483904377024,
3.67882319468092, 3.67882319468092, 3.67882319468092, 2.83390807391282,
2.83390807391282, 3.48808505940913, 3.48808505940913, 3.48808505940913,
3.50918800324632, 3.50918800324632, 3.50918800324632, 1.35056965726225,
3.58350726435065, 3.58350726435065, 3.58350726435065, 3.45520042020839,
3.45520042020839, 3.45520042020839, 3.22983948291011, 3.22983948291011,
2.89167097993014, 2.89167097993014, 2.89167097993014, 3.49665408229335,
3.49665408229335, 3.49665408229335, 3.57003324070401, 3.57003324070401,
3.57003324070401, 3.54396756799594, 3.54396756799594, 3.54396756799594,
1.86800831970497, 2.57755160015926, 2.57755160015926, 3.02925424277375,
4.32327961159352, 4.32327961159352, 2.18172420173543, 3.12702563543265,
3.12702563543265, 3.12702563543265, 1.14700264441952, 2.39953372528027,
3.67205886070582, 3.67205886070582, 3.67205886070582, 4.0215200389434,
4.0215200389434, 2.61735400902176, 2.61735400902176, 2.61735400902176,
3.19592659160605, 3.19592659160605, 3.19592659160605, 2.46045386124236,
2.46045386124236, 2.73368404950427, 2.73368404950427, 2.21213125086224,
2.79123784432272, 2.79123784432272, 3.173043343244, 3.173043343244,
3.37451273144198, 3.37451273144198, 3.37451273144198, 3.569975263304,
3.569975263304, 3.569975263304, 3.8786709708336, 2.55946987580579,
2.55946987580579, 1.86429229550736, 1.86429229550736, 3.77987342556174
)), .Names = c("wi", "X0", "X1", "be", "xD", "xM"), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
18L, 19L, 20L, 21L, 22L, 23L, 24L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 35L, 36L, 37L, 38L, 40L, 41L, 42L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 53L, 54L, 55L, 57L, 58L, 59L, 61L, 62L, 63L,
64L, 65L, 66L, 69L, 70L, 71L, 72L, 74L, 75L, 76L, 77L, 78L, 80L,
81L, 82L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L,
96L, 97L, 99L, 100L, 102L, 103L, 105L, 106L, 107L, 109L, 110L,
111L, 112L, 113L, 114L, 115L, 116L, 118L), class = "data.frame", na.action
= structure(c(9L,
17L, 25L, 34L, 39L, 43L, 52L, 56L, 60L, 67L, 68L, 73L, 79L, 83L,
84L, 98L, 101L, 104L, 108L, 117L), .Names = c("9", "17", "25",
"34", "39", "43", "52", "56", "60", "67", "68", "73", "79", "83",
"84", "98", "101", "104", "108", "117"), class = "omit"))

objB <- reshape(obj, direction="long", varying=2:3, sep="", idvar="wi")
names(objB)[5:6] <- c("y", "count")
objC <- apply(obj, 1, function(Z) data.frame(rep(0:1, Z[2:3]),
matrix(rep(Z[c(1,4:6)], sum(Z[2:3])), ncol=4, byrow=T)))
objC <- do.call(rbind, objC)
names(objC) <- c("y", "wi", "be", "xD", "xM")
summary(glmer(cbind(X1, X0) ~ xD + xM + (1 | wi) + (1 | be), obj,
family=binomial))
summary(glmer(y ~ xD + xM + (1 | wi) + (1 | be), objB, family=binomial,
weights=count)) # throws a warning
summary(glmer(y ~ xD + xM + (1 | wi) + (1 | be), objC, family=binomial)) #
takes a minute or so on my machine

	[[alternative HTML version deleted]]



More information about the R-sig-mixed-models mailing list