diff --git a/DESCRIPTION b/DESCRIPTION index cd735fb..43156c5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: jointCalib Type: Package Title: A Joint Calibration of Totals and Quantiles Version: 0.1.2 -Authors@R: +Authors@R: c(person(given = "Maciej", family = "Beręsewicz", role = c("aut", "cre"), @@ -13,9 +13,10 @@ License: GPL-3 Encoding: UTF-8 RdMacros: mathjaxr LazyData: yes -Depends: +Depends: R (>= 3.5.0) -URL: https://github.com/ncn-foreigners/jointCalib, https://ncn-foreigners.github.io/jointCalib/ +URL: https://github.com/ncn-foreigners/jointCalib, + https://ncn-foreigners.github.io/jointCalib/ BugReports: https://github.com/ncn-foreigners/jointCalib/issues Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 @@ -29,5 +30,5 @@ Imports: CBPS Suggests: knitr, - rmarkdown + rmarkdown, tinytest VignetteBuilder: knitr diff --git a/README.Rmd b/README.Rmd index 44c553e..b645d24 100644 --- a/README.Rmd +++ b/README.Rmd @@ -28,8 +28,7 @@ knitr::opts_chunk$set( ## Details -A small package for joint calibration of totals and quantiles (see [Beręsewicz and Szymkowiak (2023)](https://arxiv.org/abs/2308.13281) working paper for details). The -package combines the following approaches: +A small package for joint calibration of totals and quantiles for probability and non-probability surveys as well as causal inference based on observational data. The package combines the following approaches: - Deville, J. C., and Särndal, C. E. (1992). [Calibration estimators in survey @@ -47,6 +46,7 @@ package combines the following approaches: Inference](https://onlinelibrary.wiley.com/doi/10.1111/insr.12518), International Statistical Review 91, 165--192. + which allows to calibrate weights to known (or estimated) totals and quantiles jointly. As an backend for calibration [sampling](https://CRAN.R-project.org/package=sampling) @@ -70,10 +70,10 @@ Currently supports: - calibration of quantiles, - calibration of quantiles and totals, -- calibration using standard calibration, empirical likelihood and +- calibration using standard calibration (i.e. Deville and Särndal, 1992), empirical likelihood and entropy balancing method, -- covariate distribution entropy balancing for ATT and QTT (distributional entropy balancing; DEB), -- covariate distribution balancing propensity score for ATE and QTE (distributional propensity score; DPS). +- covariate distribution entropy balancing for ATT and QTT (distributional entropy balancing; DEB) via the [ebal](https://CRAN.R-project.org/package=ebal) package, +- covariate distribution balancing propensity score for ATE and QTE (distributional propensity score; DPS) via the [CBPS](https://CRAN.R-project.org/package=CBPS) package. Further plans: @@ -81,6 +81,12 @@ Further plans: - calibration for Gini and other metrics, - ... +For details see: + +- Beręsewicz M, and Szymkowiak, M. (2023). [A note on joint calibration estimators for + totals and quantiles](https://arxiv.org/abs/2308.13281), working paper (arxiv 2308.13281). +- Beręsewicz M (2023). [Survey calibration for causal inference: a simple method to balance covariate distributions](https://arxiv.org/abs/2310.11969), working paper (arxiv 2310.11969). + ## Funding Work on this package is supported by the the National Science Centre, diff --git a/README.md b/README.md index 16a222f..44a3cd4 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,9 @@ Statistics](https://awesome.re/mentioned-badge.svg)](http://www.awesomeofficials ## Details -A small package for joint calibration of totals and quantiles (see -[Beręsewicz and Szymkowiak (2023)](https://arxiv.org/abs/2308.13281) -working paper for details). The package combines the following +A small package for joint calibration of totals and quantiles for +probability and non-probability surveys as well as causal inference +based on observational data. The package combines the following approaches: - Deville, J. C., and Särndal, C. E. (1992). [Calibration estimators in @@ -60,12 +60,14 @@ Currently supports: - calibration of quantiles, - calibration of quantiles and totals, -- calibration using standard calibration, empirical likelihood and - entropy balancing method, +- calibration using standard calibration (i.e. Deville and Särndal, + 1992), empirical likelihood and entropy balancing method, - covariate distribution entropy balancing for ATT and QTT - (distributional entropy balancing; DEB), + (distributional entropy balancing; DEB) via the + [ebal](https://CRAN.R-project.org/package=ebal) package, - covariate distribution balancing propensity score for ATE and QTE - (distributional propensity score; DPS). + (distributional propensity score; DPS) via the + [CBPS](https://CRAN.R-project.org/package=CBPS) package. Further plans: @@ -73,6 +75,17 @@ Further plans: - calibration for Gini and other metrics, - … +For details see: + +- Beręsewicz M, and Szymkowiak, M. (2023). [A note on joint calibration + estimators for totals and + quantiles](https://arxiv.org/abs/2308.13281), working paper (arxiv + 2308.13281). +- Beręsewicz M (2023). [Survey calibration for causal inference: a + simple method to balance covariate + distributions](https://arxiv.org/abs/2310.11969), working paper (arxiv + 2310.11969). + ## Funding Work on this package is supported by the the National Science Centre, diff --git a/inst/tinytest/test_jointCalib.R b/inst/tinytest/test_jointCalib.R new file mode 100644 index 0000000..aa3f72e --- /dev/null +++ b/inst/tinytest/test_jointCalib.R @@ -0,0 +1,4 @@ + +# Placeholder with simple test +expect_equal(1 + 1, 2) + diff --git a/inst/tinytest/test_joint_calib_cbps.R b/inst/tinytest/test_joint_calib_cbps.R index ec7ae77..4833c76 100644 --- a/inst/tinytest/test_joint_calib_cbps.R +++ b/inst/tinytest/test_joint_calib_cbps.R @@ -44,6 +44,15 @@ expect_error( data = dat) ) +## check one x for quantiles +expect_silent( + result <- joint_calib_cbps(formula_means = ~ X1, + formula_quantiles = ~ X1, + treatment = ~ D, + data = dat) +) + + ## test variable selection expect_error( @@ -65,3 +74,5 @@ expect_silent( ) + + diff --git a/tests/tinytest.R b/tests/tinytest.R new file mode 100644 index 0000000..f78fe46 --- /dev/null +++ b/tests/tinytest.R @@ -0,0 +1,5 @@ + +if ( requireNamespace("tinytest", quietly=TRUE) ){ + tinytest::test_package("jointCalib") +} + diff --git a/vignettes/b_survey.Rmd b/vignettes/b_survey.Rmd index 238886d..744eb92 100644 --- a/vignettes/b_survey.Rmd +++ b/vignettes/b_survey.Rmd @@ -25,7 +25,7 @@ knitr::opts_chunk$set( # Setup -```{r setup} +```{r setup, message=FALSE} library(jointCalib) library(survey) library(laeken) @@ -45,14 +45,16 @@ set.seed(123) N <- 100 x1 <- rbinom(n = 100, prob = 0.7, size = 1) x2 <- rlnorm(n = 100)*1000 -pop<-data.frame(x1, x2) +pop <- data.frame(x1, x2) sample_df <- pop[sample(1:N, 20), ] sample_df$d <- as.integer(N/nrow(sample_df)) -colSums(sample_df[, c("x1", "x2")]*sample_df$d) -with(sample_df, weightedMedian(x2, d)) +colSums(sample_df[, c("x1", "x2")]*sample_df$d) ## sample totals +with(sample_df, weightedMedian(x2, d)) # sample median +median(x2) # population median ``` Calibrate using: + + totals: `x1` and `x2`, + median for: `x2`. @@ -67,15 +69,15 @@ res <- joint_calib(formula_totals = ~ x1 + x2, method = "linear", control = control_calib(interpolation = "linear")) -for_example <- res$Xs[, c(3,4,2)] |> as.data.frame() -for_example$w <- sample_df$d * res$g -for_example +df_example <- res$Xs[, c(3,4,2)] |> as.data.frame() +df_example$w <- sample_df$d * res$g +df_example ``` Median of `x2` after calibration. ```{r} -weightedQuantile(sample_df$x2, res$g, 0.5) +weightedQuantile(df_example$x2, df_example$w, 0.5) ```