head(workplace)
library(survey)
# Survey design for stratified simple random sampling without replacement
dn <- if (packageVersion("survey") >= "4.2") {
# survey design with pre-calibrated weights
svydesign(ids = ~ID, strata = ~strat, fpc = ~fpc, weights = ~weight,
data = workplace, calibrate.formula = ~-1 + strat)
} else {
# legacy mode
svydesign(ids = ~ID, strata = ~strat, fpc = ~fpc, weights = ~weight,
data = workplace)
}
# Robust ratio M-estimator with Huber psi-function
rat <- svyratio_huber(~payroll, ~ employment, dn, k = 5)
# Summary of the ratio estimate
summary(rat)
# Diagnostic plots of the ration/regression M-estimate (e.g.,
# standardized residuals against fitted values)
plot(rat, which = 1L)
# Plot of the robustness weights of the ratio/regression M-estimate
# against its residuals
plot(residuals(rat), robweights(rat))
# Robust ratio predictor of the population mean
m <- svymean_ratio(rat, total = 1001233, N = 90840)
m
# Summary of the ratio estimate of the population mean
summary(m)
# Extract estimate
coef(m)
# Extract estimate of scale
scale(m)
# Extract estimated standard error
SE(m)
Run the code above in your browser using DataLab