# Basic Workflow with svySE

# Introduction

`svySE` provides a workflow for estimating sampling errors for complex survey indicators.

It is designed for binary or categorical indicators and supports weighted estimates, percentages, standard errors, confidence intervals, coefficients of variation, design effects, unweighted counts, grouped estimates, optional division variables, and Excel exports.

# Example data

    library(svySE)

    set.seed(123)

    df <- data.frame(
      dept = rep(c("A", "B", "C"), each = 50),
      strata = rep(c("A", "B", "C"), each = 50),
      service = rep(c("S1", "S2"), length.out = 150),
      weight = runif(150, 10, 50),
      ind_1 = sample(c(0, 1), 150, replace = TRUE)
    )

    head(df)
    #>   dept strata service   weight ind_1
    #> 1    A      A      S1 21.50310     0
    #> 2    A      A      S2 41.53221     1
    #> 3    A      A      S1 26.35908     0
    #> 4    A      A      S2 45.32070     1
    #> 5    A      A      S1 47.61869     0
    #> 6    A      A      S2 11.82226     0

# Configuration

    cfg <- svySE_cfg(
      estimator = "prop",
      target = 1,
      valid_values = c(0, 1),
      lonely_psu = "adjust",
      truncate_lower_ci = TRUE
    )

    cfg
    #> svySE configuration
    #> --------------------------------------------------
    #> Estimator          : prop
    #> Variance           : taylor
    #> Lonely PSU         : adjust
    #> Confidence level   : 0.95
    #> Target value       : 1
    #> Valid values       : 0, 1
    #> Truncate lower CI  : TRUE
    #> Percentage mult.   : 100
    #> Include DEFF       : TRUE
    #> Include CV         : TRUE
    #> Remove NA          : TRUE

# Sampling error calculation

    res <- svySE_calc(
      data = df,
      indicators = "ind_1",
      group_vars = "dept",
      group_labels = "Department",
      strata = "strata",
      weight = "weight",
      cfg = cfg,
      verbose = FALSE
    )

    res
    #> svySE result
    #> --------------------------------------------------
    #> Indicators : ind_1
    #> Groups     : dept
    #> Strata     : strata
    #> Weight     : weight
    #> Division   : NULL
    #> Estimator  : prop
    #> Target     : 1
    #> Strict     : FALSE

# Inspect results

    res$results$ind_1$error$TOTAL
    #>       dept   est_abs  est_pct   se_abs   se_pct  ci_l_abs ci_l_pct  ci_u_abs
    #> 1 NACIONAL 2274.6821 50.26904 213.8255 4.322635 1855.5919 41.79684 2693.7724
    #> 2        A  760.7571 49.39397 129.0093 7.640085  507.9035 34.41968 1013.6106
    #> 3        B  933.1969 64.17897 123.9632 7.179269  690.2334 50.10786 1176.1604
    #> 4        C  580.7282 37.93676 117.0942 7.404192  351.2277 23.42481  810.2286
    #>   ci_u_pct        cv     deff n_unw
    #> 1 58.74125  8.598999 1.151851    72
    #> 2 64.36827 15.467646 1.224895    23
    #> 3 78.25008 11.186326 1.110240    30
    #> 4 52.44870 19.517198 1.213892    19

# Calculation with division variable

    res_div <- svySE_calc(
      data = df,
      indicators = "ind_1",
      group_vars = "dept",
      group_labels = "Department",
      strata = "strata",
      weight = "weight",
      division = "service",
      div_weight = "weight",
      cfg = cfg,
      verbose = FALSE
    )

    names(res_div$results$ind_1$error)
    #> [1] "TOTAL" "S1"    "S2"

# Export to Excel

The following example writes files to a temporary directory.

    file_err <- tempfile(fileext = ".xlsx")
    file_tab <- tempfile(fileext = ".xlsx")

    svySE_xlsx(
      x = res,
      file_err = file_err,
      file_tab = file_tab,
      cols_err = svySE_cols_err("full"),
      cols_tab = svySE_cols_tab("full")
    )

    file.exists(file_err)
    #> [1] TRUE
    file.exists(file_tab)
    #> [1] TRUE