Just modify your call to boot()
like this:
data.boot <- boot(data, boot.huber, 1999, strata=data$Subject)
?boot
provides this description of the strata=
argument, which does exactly what you are asking for:
strata: An integer vector or factor specifying the strata for multi-sample problems. This may be specified for any simulation, but is ignored when ‘sim = "parametric"’. When ‘strata’ is supplied for a nonparametric bootstrap, the simulations are done within the specified strata.
Additional note:
To confirm that it's working as you'd like, you can call debugonce(boot)
, run the call above, and step through the debugger until the object i
(whose rows contain the indices used to resample rows of data
to create each bootstrap resample) has been assigned, and then have a look at it.
debugonce(boot)
data.boot <- boot(data, boot.huber, 1999, strata=data$Subject)
# Browse[2]>
## [Press return 34 times]
# Browse[2]> head(i)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
# [1,] 9 10 11 16 9 14 15 16 9 2 15 16 1 10
# [2,] 9 14 7 12 5 6 15 4 13 6 11 16 13 6
# [3,] 5 10 15 16 9 6 3 4 1 2 15 12 5 6
# [4,] 5 10 11 4 9 6 15 16 9 14 11 16 5 2
# [5,] 5 10 3 4 1 10 15 16 9 6 3 8 13 14
# [6,] 13 10 3 12 5 10 3 4 5 14 7 16 5 14
# [,15] [,16]
# [1,] 7 8
# [2,] 11 16
# [3,] 3 16
# [4,] 3 8
# [5,] 7 8
# [6,] 7 12
(You can enter Q
to leave the debugger at any time.)