
| Release channel | Version | Status |
|---|---|---|
| CRAN | 0.2.0 |
Stable |
| GitHub | 0.2.1 |
Development |
The CRAN release is recommended for regular use. The GitHub development version may include new features and improvements that are still under active testing.
svySE is an R package for estimating sampling errors,
producing descriptive indicator tables, and exporting structured results
from complex survey data.
The package provides two complementary workflows:
svySE_calc() calculates weighted estimates and sampling
errors using the survey design.svySE_simple() calculates unweighted frequencies and
percentages, expanded frequencies, or both, without sampling
errors.Results can be exported individually or consolidated across multiple
datasets, survey weights, indicators, or analysis runs using
svySE_xlsx().
svySE is built on top of the survey
package and provides a higher-level workflow for the routine production
of survey indicators while preserving the principles of design-based
estimation.
Survey indicator production often involves repeating the same technical steps:
svySE organizes these tasks into a reproducible and
configurable workflow.
The package is designed to be:
Although the package was developed from practical experience in survey sampling and official statistics, it can be used by any researcher, analyst, public institution, national statistical office, or survey practitioner working with indicator-based survey data.
.xlsx outputs| Step | Function | Purpose |
|---|---|---|
| 1 | svySE_cfg() |
Configure estimation settings, confidence level, target category, CV, DEFF, and other options. |
| 2A | svySE_calc() |
Calculate weighted estimates and sampling errors using the survey design. |
| 2B | svySE_simple() |
Calculate unweighted tables, expanded frequencies, or both, without sampling errors. |
| 3 | svySE_xlsx() |
Export one or multiple results to .xlsx files. |
The two calculation functions have separate responsibilities:
| Function | Uses weights | Uses survey design | Main output |
|---|---|---|---|
svySE_calc() |
Yes | Yes | Weighted estimates and sampling errors |
svySE_simple() |
Optional | No | Unweighted tables, expanded frequencies, or both |
Simple indicator tables also provide explicit control over missing
indicator values through the na_rm argument. Missing values
can be excluded from the calculation or treated as a validation
condition that stops the analysis.
This separation avoids redundant calculations and allows users to run only the workflow required for each analysis.
install.packages("svySE")install.packages("remotes")
remotes::install_github("lburgoss/svySE")The CRAN version is recommended for regular use. The GitHub version may contain features under development before they are submitted to CRAN.
Load the package with:
library(svySE)The following simulated dataset contains:
library(svySE)
set.seed(123)
df <- data.frame(
dept = rep(c("A", "B", "C"), each = 50),
strata = rep(c("S1", "S2", "S3"), each = 50),
cluster = rep(1:30, each = 5),
service = rep(c("S1", "S2"), length.out = 150),
weight = runif(150, 10, 50),
ind_1 = sample(c(0, 1), 150, replace = TRUE),
ind_2 = sample(c(0, 1), 150, replace = TRUE)
)
head(df)svySE_cfg() defines the common settings used during
sampling error estimation.
cfg <- svySE_cfg(
estimator = "prop",
variance = "taylor",
lonely_psu = "adjust",
conf_level = 0.95,
target = 1,
valid_values = c(0, 1),
truncate_lower_ci = TRUE,
pct_mult = 100,
deff = TRUE,
cv = TRUE,
na_rm = TRUE
)
cfgThe most relevant options are:
| Argument | Description |
|---|---|
estimator |
Estimator used in the analysis, such as "prop" or
"total". |
variance |
Variance estimation method. |
lonely_psu |
Treatment of strata containing a single PSU. |
conf_level |
Confidence level used for interval estimation. |
target |
Indicator category treated as the target value. |
valid_values |
Values considered valid for the indicator. |
truncate_lower_ci |
Whether lower confidence limits are truncated at zero. |
pct_mult |
Multiplier used to express percentages. |
deff |
Whether design effects are calculated. |
cv |
Whether coefficients of variation are calculated. |
na_rm |
Whether missing values are removed during estimation. |
svySE_calc() estimates weighted indicators and sampling
errors using the survey design.
res_error <- svySE_calc(
data = df,
indicators = c("ind_1", "ind_2"),
group_vars = "dept",
group_labels = "Department",
strata = "strata",
cluster = "cluster",
weight = "weight",
division = NULL,
div_weight = NULL,
cfg = cfg,
verbose = FALSE
)
res_errorThe result is an object of class:
class(res_error)A specific sampling error table can be inspected with:
res_error$results$ind_1$error$TOTALThe output may contain:
| Column | Description |
|---|---|
est_abs |
Weighted absolute estimate |
est_pct |
Weighted percentage estimate |
se_abs |
Standard error of the absolute estimate |
se_pct |
Standard error of the percentage |
ci_l_abs |
Lower confidence limit for the absolute estimate |
ci_l_pct |
Lower confidence limit for the percentage |
ci_u_abs |
Upper confidence limit for the absolute estimate |
ci_u_pct |
Upper confidence limit for the percentage |
cv |
Coefficient of variation |
deff |
Design effect |
n_unw |
Unweighted count of target cases |
svySE_calc() supports several survey design
structures.
| Design structure | strata |
cluster |
|---|---|---|
| Weight only | NULL |
NULL |
| Stratified design | Variable name | NULL |
| Clustered design | NULL |
Variable name |
| Stratified clustered design | Variable name | Variable name |
res_weight <- svySE_calc(
data = df,
indicators = "ind_1",
group_vars = "dept",
group_labels = "Department",
strata = NULL,
cluster = NULL,
weight = "weight",
cfg = cfg,
verbose = FALSE
)res_strata <- svySE_calc(
data = df,
indicators = "ind_1",
group_vars = "dept",
group_labels = "Department",
strata = "strata",
cluster = NULL,
weight = "weight",
cfg = cfg,
verbose = FALSE
)res_cluster <- svySE_calc(
data = df,
indicators = "ind_1",
group_vars = "dept",
group_labels = "Department",
strata = NULL,
cluster = "cluster",
weight = "weight",
cfg = cfg,
verbose = FALSE
)res_complex <- svySE_calc(
data = df,
indicators = "ind_1",
group_vars = "dept",
group_labels = "Department",
strata = "strata",
cluster = "cluster",
weight = "weight",
cfg = cfg,
verbose = FALSE
)A division variable can be used to calculate separate results for its categories while retaining the design-based estimation workflow.
res_domain <- svySE_calc(
data = df,
indicators = "ind_1",
group_vars = "dept",
group_labels = "Department",
strata = "strata",
cluster = "cluster",
weight = "weight",
division = "service",
div_weight = NULL,
cfg = cfg,
verbose = FALSE
)Available divisions can be inspected with:
names(res_domain$results$ind_1$error)When div_weight is supplied, that weight is used for the
corresponding division estimates.
svySE_simple() creates descriptive indicator tables
without calculating standard errors, confidence intervals, CV, or
DEFF.
It supports three output modes:
output |
Columns produced |
|---|---|
"unweighted" |
freq_0, pct_0, freq_1,
pct_1, freq_total, pct_total |
"weighted" |
exp_0, exp_pct_0, exp_1,
exp_pct_1, exp_total,
exp_pct_total |
"both" |
All unweighted and expanded columns |
This is the default and preserves the original behavior:
res_simple <- svySE_simple(
data = df,
indicators = c("ind_1", "ind_2"),
group_vars = "dept",
group_labels = "Department",
output = "unweighted",
target = 1,
valid_values = c(0, 1),
pct_mult = 100,
na_rm = TRUE,
verbose = FALSE
)
res_simple$results$ind_1$simple$TOTALExpanded frequencies are obtained by summing the specified weight within categories 0, 1, and the valid total:
res_expanded <- svySE_simple(
data = df,
indicators = "ind_1",
group_vars = "dept",
group_labels = "Department",
weight = "weight",
output = "weighted",
verbose = FALSE
)
res_expanded$results$ind_1$simple$TOTALThe result contains:
| Column | Description |
|---|---|
exp_0 |
Expanded frequency for category 0 |
exp_pct_0 |
Weighted percentage for category 0 |
exp_1 |
Expanded frequency for category 1 |
exp_pct_1 |
Weighted percentage for category 1 |
exp_total |
Expanded total of valid indicator records |
exp_pct_total |
Total weighted percentage |
res_both <- svySE_simple(
data = df,
indicators = "ind_1",
group_vars = "dept",
group_labels = "Department",
weight = "weight",
output = "both",
verbose = FALSE
)
res_both$results$ind_1$simple$TOTALBy default, na_rm = TRUE. Missing indicator values are
excluded from the calculation, and groups without valid observations for
a specific indicator are omitted from that indicator table.
Use na_rm = FALSE to stop the calculation when missing
indicator values are present:
svySE_simple(
data = df,
indicators = "ind_1",
group_vars = "dept",
na_rm = FALSE
)Records with missing weights do not contribute to expanded
frequencies. When output = "both", valid indicator records
still contribute to the unweighted columns.
Unweighted frequencies describe the observed sample. Expanded frequencies are weighted totals. Neither mode in
svySE_simple()calculates sampling errors.
res_simple_domain <- svySE_simple(
data = df,
indicators = "ind_1",
group_vars = "dept",
group_labels = "Department",
division = "service",
weight = "weight",
output = "both",
verbose = FALSE
)
names(res_simple_domain$results$ind_1$simple)svySE_xlsx() exports sampling error results, simple
indicator tables, or both.
file_err <- tempfile(fileext = ".xlsx")
svySE_xlsx(
x = res_error,
file_err = file_err,
file_tab = NULL,
cols_err = svySE_cols_err("full"),
overwrite = TRUE
)
file.exists(file_err)file_tab <- tempfile(fileext = ".xlsx")
svySE_xlsx(
x = res_simple,
file_err = NULL,
file_tab = file_tab,
cols_tab = svySE_cols_tab("full"),
overwrite = TRUE
)
file.exists(file_tab)Multiple results generated from different datasets, indicators, survey weights, or function calls can be exported together.
results <- list(
Main_errors = res_error,
Domain_errors = res_domain,
Main_simple = res_simple,
Domain_simple = res_simple_domain
)Export all available results:
file_err <- tempfile(fileext = ".xlsx")
file_tab <- tempfile(fileext = ".xlsx")
svySE_xlsx(
x = results,
file_err = file_err,
file_tab = file_tab,
cols_err = svySE_cols_err("full"),
cols_tab = svySE_cols_tab("full"),
overwrite = TRUE
)svySE_xlsx() automatically identifies:
svySE_calc();svySE_simple().Sampling error tables are written to file_err, while
simple indicator tables are written to file_tab.
The select argument can be used to export only chosen
elements from a named list.
svySE_xlsx(
x = results,
select = c("Main_errors", "Domain_errors"),
file_err = tempfile(fileext = ".xlsx"),
file_tab = NULL,
overwrite = TRUE
)Export only simple tables:
svySE_xlsx(
x = results,
select = c("Main_simple", "Domain_simple"),
file_err = NULL,
file_tab = tempfile(fileext = ".xlsx"),
overwrite = TRUE
)svySE_cols_err("full")A custom selection can be defined with:
error_columns <- svySE_cols_err(
type = "custom",
cols = c(
"est_pct",
"se_pct",
"ci_l_pct",
"ci_u_pct",
"cv",
"deff",
"n_unw"
)
)Use the selected columns during export:
svySE_xlsx(
x = res_error,
file_err = tempfile(fileext = ".xlsx"),
file_tab = NULL,
cols_err = error_columns
)Available profiles include:
svySE_cols_tab("full")
svySE_cols_tab("unweighted")
svySE_cols_tab("target")
svySE_cols_tab("freq")
svySE_cols_tab("pct")
svySE_cols_tab("expanded")
svySE_cols_tab("expanded_freq")
svySE_cols_tab("expanded_pct")
svySE_cols_tab("counts")
svySE_cols_tab("percentages")Export only sample and expanded counts:
svySE_xlsx(
x = res_both,
file_err = NULL,
file_tab = tempfile(fileext = ".xlsx"),
cols_tab = svySE_cols_tab("counts")
)A custom selection can be defined with:
simple_columns <- svySE_cols_tab(
type = "custom",
cols = c(
"freq_1",
"pct_1",
"exp_1",
"exp_pct_1",
"freq_total",
"exp_total",
"exp_pct_total"
)
)
svySE_xlsx(
x = res_both,
file_err = NULL,
file_tab = tempfile(fileext = ".xlsx"),
cols_tab = simple_columns
)When cols_tab = NULL, all columns available in each
simple result are exported automatically.
| Function | Description |
|---|---|
svySE_cfg() |
Configure sampling error estimation settings |
svySE_calc() |
Calculate weighted estimates and sampling errors |
svySE_simple() |
Calculate unweighted tables, expanded frequencies, or both |
svySE_xlsx() |
Export one or multiple results to .xlsx files |
svySE_cols_err() |
Select sampling error columns |
svySE_cols_tab() |
Select simple table columns |
| Output | Generated by | Weighted | Uses survey design | Typical use |
|---|---|---|---|---|
| Sampling error tables | svySE_calc() |
Yes | Yes | Official statistics, complex surveys, technical reports |
| Simple indicator tables | svySE_simple() |
Optional | No | Sample counts, expanded totals, and descriptive reporting |
svySE integrates functionality from established R
packages.
| Package | Role |
|---|---|
survey |
Design-based estimation, standard errors, confidence intervals, CV, and DEFF |
openxlsx |
Creation and formatting of .xlsx workbooks |
stats |
Statistical formulas, coefficients, and confidence intervals |
svySE |
Workflow for survey indicators, errors, tables, and export |
svySE does not replace survey. It provides
a structured interface for repeated indicator production and export
workflows built on top of its design-based estimation capabilities.
The package includes:
Open the package help:
help(package = "svySE")Browse available vignettes:
browseVignettes("svySE")Open documentation for the principal functions:
?svySE_cfg
?svySE_calc
?svySE_simple
?svySE_xlsxThe current development version introduces:
svySE_simple() workflow;select argument;.xlsx workbooks.svySE_simple();Future releases will focus on additional estimators, expanded quality indicators, more export options, and broader support for complex survey workflows.
Luis Burgos
Statistician • RENACYT Researcher (Peru)
Sampling Specialist
National Institute of Statistics and Informatics (INEI)
ENCAL — Public Expenditure Quality Monitoring Survey
The package was developed independently based on professional experience in complex survey sampling, official statistics, and statistical programming.
Email: lburgoss1996@gmail.com
Suggestions, bug reports, and feature requests are welcome through the GitHub issue tracker.
MIT License