Skip to content
Merged

Dev #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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
Expand All @@ -29,5 +30,5 @@ Imports:
CBPS
Suggests:
knitr,
rmarkdown
rmarkdown, tinytest
VignetteBuilder: knitr
16 changes: 11 additions & 5 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -70,17 +70,23 @@ 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:

- generalized calibration via `sampling::gencalib`,
- 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,
Expand Down
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,19 +60,32 @@ 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:

- generalized calibration via `sampling::gencalib`,
- 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,
Expand Down
4 changes: 4 additions & 0 deletions inst/tinytest/test_jointCalib.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# Placeholder with simple test
expect_equal(1 + 1, 2)

11 changes: 11 additions & 0 deletions inst/tinytest/test_joint_calib_cbps.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -65,3 +74,5 @@ expect_silent(
)




5 changes: 5 additions & 0 deletions tests/tinytest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

if ( requireNamespace("tinytest", quietly=TRUE) ){
tinytest::test_package("jointCalib")
}

18 changes: 10 additions & 8 deletions vignettes/b_survey.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ knitr::opts_chunk$set(

# Setup

```{r setup}
```{r setup, message=FALSE}
library(jointCalib)
library(survey)
library(laeken)
Expand All @@ -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`.

Expand All @@ -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)
```


Expand Down