Skip to contents

Sample blockwise from clustered data

Usage

blocksample(data, size, idvar = NULL, replace = TRUE, ...)

Arguments

data

Data frame

size

Size of samples

idvar

Column defining the clusters

replace

Logical indicating wether to sample with replacement

...

additional arguments to lower level functions

Value

data.frame

Details

Original id is stored in the attribute 'id'

Author

Klaus K. Holst

Examples


d <- data.frame(x=rnorm(5), z=rnorm(5), id=c(4,10,10,5,5), v=rnorm(5))
(dd <- blocksample(d,size=20,~id))
#>              x          z id           v
#> 2   -0.9373884  0.4890758  1 -1.12402842
#> 3    1.0134409 -0.4148487  1 -0.22438290
#> 2.1 -0.9373884  0.4890758  2 -1.12402842
#> 3.1  1.0134409 -0.4148487  2 -0.22438290
#> 1   -0.2924015  1.4906009  3  1.06445737
#> 2.2 -0.9373884  0.4890758  4 -1.12402842
#> 3.2  1.0134409 -0.4148487  4 -0.22438290
#> 4    0.1894152 -0.1503866  5  0.07987716
#> 5   -0.6982603 -0.2859336  5  1.52257466
#> 4.1  0.1894152 -0.1503866  6  0.07987716
#> 5.1 -0.6982603 -0.2859336  6  1.52257466
#> 2.3 -0.9373884  0.4890758  7 -1.12402842
#> 3.3  1.0134409 -0.4148487  7 -0.22438290
#> 4.2  0.1894152 -0.1503866  8  0.07987716
#> 5.2 -0.6982603 -0.2859336  8  1.52257466
#> 4.3  0.1894152 -0.1503866  9  0.07987716
#> 5.3 -0.6982603 -0.2859336  9  1.52257466
#> 1.1 -0.2924015  1.4906009 10  1.06445737
#> 2.4 -0.9373884  0.4890758 11 -1.12402842
#> 3.4  1.0134409 -0.4148487 11 -0.22438290
#> 4.4  0.1894152 -0.1503866 12  0.07987716
#> 5.4 -0.6982603 -0.2859336 12  1.52257466
#> 4.5  0.1894152 -0.1503866 13  0.07987716
#> 5.5 -0.6982603 -0.2859336 13  1.52257466
#> 2.5 -0.9373884  0.4890758 14 -1.12402842
#> 3.5  1.0134409 -0.4148487 14 -0.22438290
#> 1.2 -0.2924015  1.4906009 15  1.06445737
#> 1.3 -0.2924015  1.4906009 16  1.06445737
#> 2.6 -0.9373884  0.4890758 17 -1.12402842
#> 3.6  1.0134409 -0.4148487 17 -0.22438290
#> 1.4 -0.2924015  1.4906009 18  1.06445737
#> 4.6  0.1894152 -0.1503866 19  0.07987716
#> 5.6 -0.6982603 -0.2859336 19  1.52257466
#> 4.7  0.1894152 -0.1503866 20  0.07987716
#> 5.7 -0.6982603 -0.2859336 20  1.52257466
attributes(dd)$id
#>  [1] 10 10 10 10  4 10 10  5  5  5  5 10 10  5  5  5  5  4 10 10  5  5  5  5 10
#> [26] 10  4  4 10 10  4  5  5  5  5

if (FALSE) { # \dontrun{
blocksample(data.table::data.table(d),1e6,~id)
} # }


d <- data.frame(x=c(1,rnorm(9)),
               z=rnorm(10),
               id=c(4,10,10,5,5,4,4,5,10,5),
               id2=c(1,1,2,1,2,1,1,1,1,2),
               v=rnorm(10))
dsample(d,~id, size=2)
#>             x           z id id2          v id.1
#> 4   1.2373588  0.31202103  5   1  1.0172015    1
#> 5   0.3743871  2.15876995  5   2 -0.5020760    1
#> 8   0.4826865  1.33638222  5   1 -0.3415735    1
#> 10 -0.6877808 -1.16227692  5   2 -1.1133414    1
#> 2  -2.6551641  0.07859043 10   1 -0.8982185    2
#> 3   0.5862268  0.72370982 10   2 -0.1147617    2
#> 9  -0.8040275  0.70668573 10   1  0.4021691    2
dsample(d,.~id+id2)
#>              x          z          v id
#> 3    0.5862268 0.72370982 -0.1147617  1
#> 4    1.2373588 0.31202103  1.0172015  2
#> 8    0.4826865 1.33638222 -0.3415735  2
#> 4.1  1.2373588 0.31202103  1.0172015  3
#> 8.1  0.4826865 1.33638222 -0.3415735  3
#> 4.2  1.2373588 0.31202103  1.0172015  4
#> 8.2  0.4826865 1.33638222 -0.3415735  4
#> 4.3  1.2373588 0.31202103  1.0172015  5
#> 8.3  0.4826865 1.33638222 -0.3415735  5
#> 4.4  1.2373588 0.31202103  1.0172015  6
#> 8.4  0.4826865 1.33638222 -0.3415735  6
#> 4.5  1.2373588 0.31202103  1.0172015  7
#> 8.5  0.4826865 1.33638222 -0.3415735  7
#> 2   -2.6551641 0.07859043 -0.8982185  8
#> 9   -0.8040275 0.70668573  0.4021691  8
#> 4.6  1.2373588 0.31202103  1.0172015  9
#> 8.6  0.4826865 1.33638222 -0.3415735  9
#> 2.1 -2.6551641 0.07859043 -0.8982185 10
#> 9.1 -0.8040275 0.70668573  0.4021691 10
dsample(d,x+z~id|x>0,size=5)
#>             x          z id
#> 1   1.0000000  0.5716432  1
#> 6   0.3351264 -0.1139183  1
#> 4   1.2373588  0.3120210  2
#> 5   0.3743871  2.1587699  2
#> 8   0.4826865  1.3363822  2
#> 1.1 1.0000000  0.5716432  3
#> 6.1 0.3351264 -0.1139183  3
#> 1.2 1.0000000  0.5716432  4
#> 6.2 0.3351264 -0.1139183  4
#> 3   0.5862268  0.7237098  5