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 Horvitz-Thompson M-estimator of the population total
svytotal_huber(~employment, dn, k = 9, type = "rht")
# Robust weighted M-estimator of the population mean
m <- svymean_huber(~employment, dn, k = 12, type = "rwm")
# Summary statistic
summary(m)
# Plot of the robustness weights of the M-estimate against its residuals
plot(residuals(m), robweights(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