Skip to content

Instantly share code, notes, and snippets.

@njtierney
Last active September 26, 2024 21:45
Show Gist options
  • Save njtierney/8f3a6eb7cd00f3c21ad09f8c81a28e33 to your computer and use it in GitHub Desktop.
Save njtierney/8f3a6eb7cd00f3c21ad09f8c81a28e33 to your computer and use it in GitHub Desktop.
# compare `{sampling}`, `{sondage}` on up-brewer Dev R,
# which is at: https://github.com/hturner/r-svn/tree/unequal-prob-sampling
# comparing UPbrewer method to `{sampling}` package
# And to implementation of  this in `{sondage}`:
# `sondage::sample_int(..., method = "marginal")`
# {sampling} is installed from CRAN
# {sondage} is installed from github:  https://github.com/dickoa/sondage
compare_sample_funs <- function(n, size, pik, seed){
  set.seed(seed)
  brewer_sample <- sampling::UPbrewer(pik = pik / sum(pik) * size)
  s_sampling <- which(brewer_sample==1)
  set.seed(seed)
  s_sondage <- sondage::sample_int(
    n = n, 
    size = size, 
    replace = FALSE,
    prob = pik,
    method = "marginal"
  )
  
  # in R-patched-up-brewer
  set.seed(seed)
  s_base <- sample.int(
    n = n, 
    size = size, 
    replace = FALSE,
    prob = pik,
    prob_method = "marginal"
  )
  
  obj <- list(
    sondage_v_sampling = identical(sort(s_sondage), s_sampling),
    sondage_v_base = identical(s_sondage, s_base),
    sampling_v_base = identical(s_sampling, sort(s_base))
  )

  obj
}

# Example 1. 
# Borrowed from `?sampling::UPbrewer`
compare_sample_funs(
  n = 6, 
  size = 3, 
  # using probability weights, which sum to size
  pik = c(0.2,0.7,0.8,0.5,0.4,0.4),
  seed = 2024-09-18-1439
)
## $sondage_v_sampling
## [1] TRUE
## 
## $sondage_v_base
## [1] TRUE
## 
## $sampling_v_base
## [1] TRUE
# Example 2
# prob sums to one but they do not sum to size
compare_sample_funs(
  n = 6, 
  size = 3, 
  # using probability weights that do *not* sum to size
  pik = c(0.2,0.7,0.8,0.5,0.4,0.4) / 3,
  seed = 2024-09-18-1439
)
## Warning in sample_pps(n, size, prob): rescaling prob, which changes inclusion
## probabilities
## Warning in sample.pps(n, size, prob): rescaling prob, which changes inclusion
## probabilities
## $sondage_v_sampling
## [1] TRUE
## 
## $sondage_v_base
## [1] TRUE
## 
## $sampling_v_base
## [1] TRUE
# Example 3
# Where prob sums to an integer that is not the size, and is not 1
compare_sample_funs(
  n = 6, 
  size = 3, 
  # using probability weights that do *not* sum to size
  pik = (c(0.2,0.7,0.8,0.5,0.4,0.4) / 3) * 2,
  seed = 2024-09-18-1439
)
## Warning in sample_pps(n, size, prob): sum(prob) is not equal to size or 1, rescaling
## Warning in sample.pps(n, size, prob): sum(prob) is not equal to size or 1, rescaling
## $sondage_v_sampling
## [1] TRUE
## 
## $sondage_v_base
## [1] TRUE
## 
## $sampling_v_base
## [1] TRUE
# Example 4. Large N example
compare_sample_funs(
  n = 1000, 
  size = 500, 
  # ensures they are unequal and sum to size, and it is length n
  pik = 1:1000 / sum(1:1000) * 500,
  seed = 2024-9-19-1008
)
## $sondage_v_sampling
## [1] TRUE
## 
## $sondage_v_base
## [1] TRUE
## 
## $sampling_v_base
## [1] TRUE
sessionInfo()
## R Under development (unstable) (2024-09-12 r87141)
## Platform: aarch64-apple-darwin23.5.0
## Running under: macOS Sonoma 14.5
## 
## Matrix products: default
## BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
## LAPACK: /Library/Frameworks/R.framework/Versions/R-devel-upbrewer/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## time zone: Europe/London
## tzcode source: internal
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] clipr_0.8.0 pak_0.8.0   fs_1.6.4   
## 
## loaded via a namespace (and not attached):
##  [1] sondage_0.0.0.9000 MASS_7.3-61        compiler_4.5.0     sampling_2.10     
##  [5] cli_3.6.3          tools_4.5.0        rstudioapi_0.16.0  highr_0.11        
##  [9] knitr_1.48         xfun_0.47          lpSolve_5.6.21     rlang_1.1.4       
## [13] evaluate_1.0.0
# session info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment