Skip to content

Instantly share code, notes, and snippets.

@rubenarslan
Created October 11, 2011 00:59
Show Gist options
  • Save rubenarslan/1277014 to your computer and use it in GitHub Desktop.
Save rubenarslan/1277014 to your computer and use it in GitHub Desktop.
reproducible example for stats post
set.seed(272773)
individuals = data.frame(
children = c(6, 6, 1, 4, 1, 1, 1, 2, 11, 3, 7, 2, 6, 4, 4, 3, 1, 4, 2, 10,
2, 1, 2, 3, 1, 6, 14, 4, 4, 1, 7, 8, 2, 3, 2, 1, 1, 8, 8, 5,
7, 4, 1, 3, 4, 1, 2, 8, 1, 1, 1, 1, 2, 7, 4, 3, 4, 1, 1, 3, 1,
1, 6, 3, 1, 3, 4, 3, 3, 5, 5, 5, 3, 6, 2, 6, 4, 7, 4, 2, 7, 7,
5, 1, 4, 1, 9, 1, 6, 5, 3, 1, 4, 1, 9, 3, 5, 3, 3, 12, 1, 4,
4, 8, 14, 2, 1, 4, 5, 4, 4, 10, 3, 5, 1, 1, 2, 5, 4, 1, 4, 1,
5, 1, 3, 5, 2, 1, 8, 3, 7, 2, 6, 5, 3, 2, 4, 1, 3, 7, 3, 1, 1,
14, 3, 1, 3, 3, 2, 1, 5, 3, 4, 13, 7, 9, 3, 3, 7, 2, 2, 4, 7,
2, 3, 2, 2, 2, 3, 4, 7, 5, 1, 1, 4, 5, 5, 10, 3, 5, 7, 2, 3,
9, 4, 2, 4, 4, 2, 2, 3, 4, 2, 5, 1, 5, 2, 2, 4, 2, 1, 14, 2,
7, 2, 2, 5, 2, 12, 15, 2, 2, 2, 3, 1, 1, 2, 1, 3, 4, 4, 3, 1,
1, 3, 4, 6, 1, 2, 9, 3, 1, 8, 2, 3, 1, 3, 10, 4, 2, 1, 3, 5,
1, 9, 5, 3, 3, 6, 8, 4, 4, 7, 1, 3, 3, 1, 1, 1, 4, 2, 1, 4, 2,
4, 5, 5, 4, 5, 4, 2, 2, 5, 7, 2, 3, 11, 5, 5, 5, 6, 2, 5, 4,
2, 7, 5, 2, 8, 4, 5, 6, 4, 2, 6, 3, 1, 1, 5, 8, 2, 2, 2, 7, 3,
4, 6, 3, 6, 4, 2, 3, 8, 7, 5, 3, 4, 8, 2, 3, 4, 14, 1, 7, 5,
7, 5, 6, 6, 1, 4, 4, 7, 2, 3, 5, 2, 7, 1, 2, 1, 2, 2, 11, 1,
4, 1, 2, 3, 13, 8, 1, 3, 4, 5, 15, 6, 3, 2, 4, 3, 3, 1, 6, 1,
10, 3, 3, 1, 3, 12, 5, 4, 1, 2, 5, 3, 9, 7, 1, 8, 1, 3, 5, 2,
1, 4, 7, 3, 4, 2, 1, 2, 3, 4, 2, 1, 7, 2, 6, 2, 6, 6, 11, 8,
8, 3, 4, 6, 3, 2, 2, 1, 1, 4, 2, 4, 8, 5, 8, 6, 2, 1, 4, 6, 6,
6, 7, 5, 1, 3, 3, 3, 2, 2, 8, 1, 1, 7, 7, 6, 3, 8, 3, 3, 3, 1,
4, 23, 3, 8, 2, 3, 1, 2, 1, 4, 5, 1, 1, 6, 9, 1, 15, 6, 6, 8,
2, 2, 3, 2, 6, 4, 5, 4, 1, 9, 4, 3, 8, 5, 9, 2, 4, 1, 1, 10,
4, 3, 1, 3, 4, 4, 1, 3, 4, 7, 3, 4, 4), #rpois(8543,0.8),
birth.order = rpois(500,0.8),
predictor = c(29, 44, 39, 25, 41, 35, 28, 35, 48, 42, 40, 26, 21, 36, 43,
31, 37, 40, 29, 31, 36, 25, 29, 32, 39, 24, 31, 36, 50, 23, 46,
39, 57, 31, 45, 23, 35, 60, 38, 29, 31, 38, 34, 27, 24, 28, 29,
36, 26, 45, 28, 27, 31, 45, 33, 19, 37, 38, 24, 24, 36, 29, 30,
31, 37, 36, 41, 32, 45, 32, 20, 31, 52, 22, 46, 46, 38, 51, 36,
30, 37, 28, 32, 52, 23, 30, 47, 45, 39, 35, 31, 33, 33, 30, 34,
33, 30, 48, 48, 29, 30, 28, 30, 44, 28, 22, 30, 42, 39, 24, 25,
20, 47, 55, 29, 35, 25, 25, 37, 32, 39, 38, 34, 30, 44, 40, 34,
30, 24, 40, 32, 44, 34, 46, 36, 28, 23, 27, 37, 39, 27, 50, 27,
44, 34, 42, 36, 33, 31, 50, 54, 26, 22, 49, 43, 34, 42, 41, 26,
46, 46, 24, 29, 45, 32, 41, 23, 41, 38, 38, 41, 34, 29, 36, 53,
41, 53, 31, 32, 24, 51, 38, 40, 37, 23, 33, 37, 46, 41, 44, 27,
22, 48, 40, 38, 40, 40, 27, 32, 34, 31, 26, 27, 21, 34, 31, 33,
24, 47, 35, 34, 34, 49, 47, 31, 32, 38, 36, 40, 41, 26, 27, 40,
23, 27, 32, 54, 45, 34, 40, 28, 30, 33, 32, 38, 44, 36, 32, 48,
37, 41, 24, 39, 43, 32, 41, 44, 27, 31, 41, 27, 28, 35, 27, 37,
26, 36, 36, 42, 33, 40, 34, 45, 25, 31, 28, 30, 34, 47, 32, 47,
32, 32, 28, 27, 40, 43, 40, 29, 60, 52, 23, 23, 26, 36, 38, 36,
25, 53, 35, 43, 25, 46, 33, 30, 39, 27, 40, 30, 30, 30, 31, 35,
32, 27, 34, 22, 57, 26, 37, 33, 38, 24, 40, 41, 40, 39, 38, 28,
24, 40, 37, 52, 49, 35, 39, 36, 34, 39, 41, 36, 30, 38, 27, 32,
31, 27, 28, 34, 26, 33, 34, 30, 58, 27, 40, 32, 29, 31, 38, 19,
35, 31, 38, 33, 28, 39, 36, 25, 36, 50, 24, 30, 34, 26, 26, 32,
29, 35, 40, 33, 36, 36, 30, 39, 38, 26, 42, 36, 38, 37, 35, 38,
33, 27, 30, 30, 34, 25, 34, 37, 33, 43, 29, 37, 30, 26, 28, 22,
46, 31, 25, 34, 45, 26, 28, 39, 42, 45, 22, 30, 49, 28, 38, 35,
38, 26, 20, 24, 33, 28, 33, 25, 28, 26, 39, 53, 37, 32, 26, 36,
39, 27, 26, 35, 32, 22, 42, 33, 49, 41, 41, 43, 38, 35, 28, 49,
54, 45, 35, 47, 45, 33, 30, 25, 43, 45, 34, 26, 30, 41, 47, 23,
31, 45, 42, 26, 46, 28, 43, 49, 26, 39, 29, 39, 31, 26, 39, 41,
28, 41, 44, 38, 32, 33, 37, 46, 27, 33, 35, 24, 35, 45, 46, 36,
34, 29, 27, 24, 52),
Family = as.factor(sample(1:300,500,replace=T))
)
library(lme4)
(model.fixedslope = lmer(children ~ birth.order + predictor + (0+ predictor|Family)+ (1|Family),individuals))
(model.randomslope = lmer(children ~ birth.order + predictor + (1+ predictor|Family)+ (1|Family),individuals))
anova(model.fixedslope,model.randomslope)
library(arm)
bayes.kids = bayesglm(children ~ birth.order + predictor + factor(Family), family="quasipoisson",individuals,na.action=na.omit)
summary(bayes.kids) # doesn't summarize within-group variance, tells me the significance for each group
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment