get_bootstrap_permutation: Generates a list of permutation vectors for bootsrapping (old...

Description Usage Arguments Examples

Description

Generates a list of permutation vectors for bootsrapping (old version not used)

Usage

1

Arguments

.data

dataframe containing two columns group and id.

n_boot

number of bootstrap permulations (resamplings). If n_rep = 0, no bootstraping is performed and we have the bootstrap sample, whcih consist of the same observations in the same order as source data.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
library(tibble)
library(dplyr)
library(purrr)
library(lassopmm)

# Generating sample data
n <- 100
sa_df <-
  tibble(
    ids = 1:n,
    .strata = statar::xtile(ids, 10),
    .cluster = c(
      rep(5, n * 0.1),
      rep(2, n * 0.3),
      rep(4, n * 0.4),
      rep(3, n * 0.05),
      rep(1, n * 0.15)
    )
  )

# Dataset, where cluster equall to strata
sa_df1 <- sa_df %>% mutate(.cluster = .strata)

# Dataset, where cluster equall to one number
sa_df2 <- sa_df %>% mutate(.cluster = 0)

# Dataset, where strata equall to one number
sa_df3 <- sa_df %>% mutate(.strata = 1)

# Function for generating cross-tabulation statistics
my_table <- function(x, y) {
  a <- table(x, y)
  b <- cbind(a, t(t(margin.table(a, 1))))
  cc <-
    margin.table(a, 2) %>%
    as.matrix() %>%
    t() %>%
    cbind(., sum(.))
  rbind(b, cc)
}

# Example of simple bootstrapping with clusters and stratas
# Original data strcructure
with(sa_df, my_table(.cluster, .strata))
# Structure of the three repetedly sampled datasets
get_bootstrap_permutation(sa_df, 3) %>%
  map(~ tibble(ids = .x) %>% left_join(sa_df, "ids")) %>%
  map(~ with(.x, my_table(.cluster, .strata)))

# Example of sampling with clusters and stratas,
# where data contains clusters equall to stratas.
# This is an equivalent of the only clustered sampling
with(sa_df1, my_table(.cluster, .strata))
# Structure of the three repetedly sampled datasets
get_bootstrap_permutation(sa_df1, 3) %>%
  map(~ tibble(ids = .x) %>% left_join(sa_df1, "ids")) %>%
  map(~ with(.x, my_table(.cluster, .strata)))

# Example of sampling with clusters and stratas,
# where data contains only one cluster equall to 0
# This is an equivalent of the only stratified sampling
with(sa_df2, my_table(.cluster, .strata))
# Structure of the three repetedly sampled datasets
get_bootstrap_permutation(sa_df2, 3) %>%
  map(~ tibble(ids = .x) %>% left_join(sa_df2, "ids")) %>%
  map(~ with(.x, my_table(.cluster, .strata)))

# Example of sampling with clusters and stratas,
# where data contains only one strata equall to 0
# This is an equivalent of the only clustered sampling
with(sa_df3, my_table(.cluster, .strata))
# Structure of the three repetedly sampled datasets
get_bootstrap_permutation(sa_df3, 3) %>%
  map(~ tibble(ids = .x) %>% left_join(sa_df3, "ids")) %>%
  map(~ with(.x, my_table(.cluster, .strata)))

# Comparing old bootstrapping logic with the new one
df <- tibble::tibble(id = 1:30, group = sort(rep(1:3, 10))) %>%
  mutate(ids = id, .strata = group, .cluster = 1)

set.seed(1)
old <- get_bootstrap_permutation_old(df, 3) %>%
  map(~ sort(.x))
set.seed(1)
new <- get_bootstrap_permutation(df, 3) %>%
  map(~ sort(.x))
all_equal(old, new)

EBukin/lassopmm documentation built on June 12, 2019, 9:51 a.m.