I am experiencing issues in trying to stratify permutations.
My data look like this :
gender party value
1 F Democrat 762
2 M Democrat 484
3 F Independent 327
4 M Independent 239
5 F Republican 468
6 M Republican 477
What I am simply trying to do is to stratified random permutation by party
library(dplyr)
md %>%
group_by(party) %>%
mutate(perm = sample(gender))
Which gives me a correct random permutation
gender party value perm
1 F Democrat 762 M
2 M Democrat 484 F
3 F Independent 327 M
4 M Independent 239 F
5 F Republican 468 F
6 M Republican 477 M
What I would like is to repeat this operation many times. Following the solution proposed here (non-stratification permutation)
library(broom)
md %>%
bootstrap(100) %>%
do(data.frame(., treat = sample(.$gender, 6, replace=TRUE)))
However, I am failing to introduce a group_by
argument.
md %>%
bootstrap(10) %>%
group_by(party) %>%
do(data.frame(., treat = sample(.$gender, 6, replace=TRUE)))
Any idea ?
Also, bootstrap
function is actually quite slow. Any idea why ? And any solution to make it faster ? Can we parallelise it somehow ?
library(reshape2)
M <- as.table(rbind(c(762, 327, 468), c(484, 239, 477)))
dimnames(M) <- list(gender = c("F", "M"),
party = c("Democrat","Independent", "Republican"))
md = melt(M)