strata.data: Stratify a univariate population using the data

View source: R/strata.data.R

strata.dataR Documentation

Stratify a univariate population using the data

Description

This function takes in the univariate population data (argument data) and a fixed sample size (n) to compute the optimum stratum boundaries (OSB) for a given number of strata (L), optimum sample sizes (nh), etc. directly from the data. The main idea used is from Khan et al (2008) whereby the problem of stratification is formulated into a Mathematical Programming Problem (MPP) using the best-fit frequency distribution and its parameters estimated from the data. This MPP is then solved for the OSB using a Dynamic Programming (DP) solution procedure.

Usage

strata.data(
  data,
  h,
  n,
  cost = FALSE,
  ch = NULL,
  method = c("dp", "cobyla", "global", "exact"),
  n_starts = 20L,
  max_iter = 2000L,
  tol = 1e-09,
  verbose = FALSE,
  fpc = FALSE,
  takeall = FALSE
)

Arguments

data

A numeric vector with the values of the stratification variable for every unit of the population. It must have no missing or infinite values and at least 2 * h values.

h

The number of strata, a whole number of at least 2.

n

The total sample size, a whole number between h and the population size.

cost

A logical: has default cost=FALSE. If it is a stratum-cost problem, cost=TRUE, with which, one must provide the Ch parameter.

ch

A numeric: denotes a vector of stratum costs. When cost=FALSE, it has a default of NULL.

method

Character. Optimisation method. "dp" (default) is the original Dynamic Programming solver, globally optimal on a 1,000-point grid for the fitted distribution. "exact" runs Dynamic Programming on the data and returns the partition with the smallest empirical Neyman cost over all partitions with at least two units per stratum; with more than 20,000 distinct cut points it searches 20,000 cut points spread evenly in rank (set options(stratifyR.exact_max_cand = ) to change this). "cobyla" is a fast multi-start search (COBYLA and BOBYQA) on the empirical objective, and "global" is a DIRECT-L global search followed by local refinement.

n_starts

Integer. Number of random restarts for the COBYLA solver. Default 20L. Used only by "cobyla".

max_iter

Integer. Maximum function evaluations per COBYLA start. Default 2000L.

tol

Numeric. Convergence tolerance for COBYLA. Default 1e-9.

verbose

Logical. Print per-start progress. Default FALSE.

fpc

Logical. With method = "exact" only. If TRUE, the solver minimises the full variance of the stratified mean under Neyman allocation, including the finite population correction, V = (\sum W_h S_h)^2/n - \sum W_h S_h^2/N, instead of \sum W_h S_h. The minimum is certified when the reported optimality_gap is 0. Default FALSE.

takeall

Logical. With method = "exact" only. If TRUE, the top stratum may be made a take-all stratum, sampled completely, when this gives a lower variance; the rest of the sample is allocated by Neyman allocation over the other strata. Implies fpc = TRUE. Default FALSE.

Value

strata.data returns an object of class "stratifyR" with the Optimum Strata Boundaries (OSB; the upper boundary of each stratum, the last being the largest data value), stratum weights (Wh), stratum variances (Vh), Optimum Sample Sizes (nh), stratum population sizes (Nh) and sampling fraction (fh). It also contains variance, the variance of the stratified mean with the finite population correction for the final allocation, takeall, the number of units in a take-all top stratum (0 if there is none), and fpc, which records whether the full variance was minimised. Use boundaries, allocation, objective and variance to get the results, and summary() for the full table.

Author(s)

Karuna Reddy <karuna.reddy@auckland.ac.nz>
MGM Khan <mgm.khan@usp.ac.fj>

See Also

strata.distr, boundaries, summary.stratifyR, compare_designs, plot.stratifyR

Examples



data(sugarcane)
dp    <- strata.data(sugarcane$Production, h = 4, n = 500)
exact <- strata.data(sugarcane$Production, h = 4, n = 500, method = "exact")
c(dp = dp$WhShTot, exact = exact$WhShTot)

# minimise the full variance, allowing a take-all top stratum
set.seed(1)
x  <- (runif(2000)^(-1/2) - 1)          # heavy-tailed (Pareto II) population
ta <- strata.data(x, h = 4, n = 200, method = "exact", takeall = TRUE)
c(variance = ta$variance, takeall = ta$takeall)

## Not run: 
data <- rweibull(1000, shape=2, scale = 1.5)
hist(data)
obj <- strata.data(data, h = 2, n=300)
summary(obj)
#-------------------------------------------------------------
data(anaemia)
Iron <- anaemia$Iron
res <- strata.data(Iron, h = 2, n=350)
summary(res)
#-------------------------------------------------------------
data(SHS) #Household Spending data from stratification package
weight <- SHS$WEIGHT
hist(weight); length(weight)
res <- strata.data(weight, h = 2, n=500)
summary(res)
#-------------------------------------------------------------
data(sugarcane)
Production <- sugarcane$Production
hist(Production)
res <- strata.data(Production, h = 2, n=1000)
summary(res)
#-------------------------------------------------------------
#The function be dynamically used to visualize the the strata boundaries,
#for 2 strata, over the density (or observations) of the "mag" variable
#from the quakes data (with purrr and ggplot2 packages loaded).
output <- quakes %>%
          pluck("mag") %>%
          strata.data(h = 2, n = 300)
quakes %>%
      ggplot(aes(x = mag)) +
      geom_density(fill = "blue", colour = "black", alpha = 0.3) +
      geom_vline(xintercept = output$OSB, linetype = "dotted", color = "red")
#-------------------------------------------------------------

## End(Not run)



stratifyR documentation built on Nov. 13, 2026, 9:06 a.m.