Open brianmsm opened 1 year ago
Bootstrap sampling with complex survey weights is much more involved than with simple random sampling. I do not believe that we currently have any bootstrapping implemented for any complex survey designs. https://arxiv.org/pdf/1902.08944v1.pdf
We cannot simply sample from the data, we would also re-create the survey design for each bootstrap-sample, right? I think, unless we find a good solution, we should for now give an informative message that bootstrapping is not possible for models with survey design.
Yes, the replications or resamples would have to come out of the previously created survey design object.
Procedurally I saw it this way, although I am not sure if it was correct.
# Load necessary libraries
library(survey)
#> Loading required package: grid
#> Loading required package: Matrix
#> Loading required package: survival
#>
#> Attaching package: 'survey'
#> The following object is masked from 'package:graphics':
#>
#> dotchart
library(boot)
#>
#> Attaching package: 'boot'
#> The following object is masked from 'package:survival':
#>
#> aml
# Use the mtcars dataset
data("mtcars")
# Create a fictitious survey design (random sampling weights)
# In real survey data, weights would be based on survey methodology
mtcars$weights <- runif(nrow(mtcars))
design <- svydesign(ids = ~1, data = mtcars, weights = ~weights)
# Fit a model using svyglm
# Predicting mpg (miles per gallon) based on wt (weight of the car)
model_svy <- svyglm(mpg ~ wt, design = design)
# Define the bootstrapping function
# This function fits the model to a resampled dataset and returns the coefficients
boot_function <- function(data, indices) {
# Create a resampled dataset
resampled_data <- data[indices, ]
# Create a new survey design for the resampled data
resampled_design <- svydesign(ids = ~1, data = resampled_data, weights = ~weights)
# Fit the model to the new survey design
resampled_model <- svyglm(mpg ~ wt, design = resampled_design)
# Return the coefficients
coef(resampled_model)
}
# Perform the bootstrapping process
# R is the number of bootstrap replications
boot_results <- boot(data = mtcars, statistic = boot_function, R = 1000)
# View the results
print(boot_results)
#>
#> ORDINARY NONPARAMETRIC BOOTSTRAP
#>
#>
#> Call:
#> boot(data = mtcars, statistic = boot_function, R = 1000)
#>
#>
#> Bootstrap Statistics :
#> original bias std. error
#> t1* 37.848293 0.20758589 2.1499704
#> t2* -5.535675 -0.08357097 0.6595995
Created on 2023-11-20 with reprex v2.0.2
Simply resampling cases like that won't meet regularity conditions that we need for bootstrap inference unfortunately.
This is the basic procedure for resampling the weights to use in the bootstrap from the paper I link above
We could implement this or similar procedures, but I wonder if someone has already implemented these in another package?
Maybe what is implemented in this package can help
When I use this function
parameters(model, bootstrap = TRUE)
with a binary logistic model estimated using svyglm from the survey package, the bootstrap = TRUE argument does not function as expected. However, when I apply the same function with the bootstrap = TRUE argument to a model estimated using glm, it works correctly.Created on 2023-11-17 with reprex v2.0.2
Session info
``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.2 (2023-10-31) #> os Garuda Linux #> system x86_64, linux-gnu #> ui X11 #> language #> collate es_PE.UTF-8 #> ctype es_PE.UTF-8 #> tz America/Lima #> date 2023-11-17 #> pandoc 3.1.5 @ /usr/lib/rstudio/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> bayestestR 0.13.1 2023-04-07 [2] CRAN (R 4.3.0) #> boot 1.3-28.1 2022-11-22 [2] CRAN (R 4.3.2) #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.2) #> coda 0.19-4 2020-09-30 [2] CRAN (R 4.3.1) #> codetools 0.2-19 2023-02-01 [2] CRAN (R 4.3.2) #> datawizard 0.9.0 2023-09-15 [1] CRAN (R 4.3.1) #> DBI 1.1.3 2022-06-18 [2] CRAN (R 4.3.1) #> digest 0.6.33 2023-07-07 [2] CRAN (R 4.3.1) #> emmeans 1.8.9 2023-10-17 [2] CRAN (R 4.3.1) #> estimability 1.4.1 2022-08-05 [2] CRAN (R 4.3.0) #> evaluate 0.23 2023-11-01 [2] CRAN (R 4.3.2) #> fastmap 1.1.1 2023-02-24 [2] CRAN (R 4.3.1) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.2) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.2) #> htmltools 0.5.7 2023-11-03 [2] CRAN (R 4.3.2) #> insight 0.19.6 2023-10-12 [1] CRAN (R 4.3.2) #> knitr 1.45 2023-10-30 [2] CRAN (R 4.3.1) #> lattice 0.21-9 2023-10-01 [2] CRAN (R 4.3.2) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.3.2) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.2) #> MASS 7.3-60 2023-05-04 [2] CRAN (R 4.3.2) #> Matrix * 1.6-1.1 2023-09-18 [1] CRAN (R 4.3.1) #> mitools 2.4 2019-04-26 [1] CRAN (R 4.3.1) #> multcomp 1.4-25 2023-06-20 [1] CRAN (R 4.3.1) #> mvtnorm 1.2-3 2023-08-25 [2] CRAN (R 4.3.1) #> parameters * 0.21.3 2023-11-02 [2] CRAN (R 4.3.2) #> purrr 1.0.2 2023-08-10 [2] CRAN (R 4.3.1) #> R.cache 0.16.0 2022-07-21 [2] CRAN (R 4.3.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.3.1) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.3.1) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.3.1) #> reprex 2.0.2 2022-08-17 [2] CRAN (R 4.3.1) #> rlang 1.1.2 2023-11-04 [1] CRAN (R 4.3.2) #> rmarkdown 2.25 2023-09-18 [2] CRAN (R 4.3.1) #> rstudioapi 0.15.0 2023-07-07 [2] CRAN (R 4.3.1) #> sandwich 3.0-2 2022-06-15 [1] CRAN (R 4.3.1) #> sessioninfo 1.2.2 2021-12-06 [2] CRAN (R 4.3.0) #> styler 1.10.2 2023-08-29 [2] CRAN (R 4.3.1) #> survey * 4.2-1 2023-05-03 [1] CRAN (R 4.3.1) #> survival * 3.5-7 2023-08-14 [2] CRAN (R 4.3.2) #> TH.data 1.1-2 2023-04-17 [1] CRAN (R 4.3.1) #> vctrs 0.6.4 2023-10-12 [1] CRAN (R 4.3.2) #> withr 2.5.2 2023-10-30 [1] CRAN (R 4.3.2) #> xfun 0.41 2023-11-01 [2] CRAN (R 4.3.2) #> xtable 1.8-4 2019-04-21 [2] CRAN (R 4.3.1) #> yaml 2.3.7 2023-01-23 [2] CRAN (R 4.3.0) #> zoo 1.8-12 2023-04-13 [1] CRAN (R 4.3.1) #> #> [1] /home/brian/R/x86_64-pc-linux-gnu-library/4.3 #> [2] /usr/lib/R/library #> #> ────────────────────────────────────────────────────────────────────────────── ```