## generate data based on Haziza and Lesage (2016)
set.seed(123)
N <- 1000
x <- runif(N, 0, 80)
y <- exp(-0.1 + 0.1*x) + rnorm(N, 0, 300)
p <- rbinom(N, 1, prob = exp(-0.2 - 0.014*x))
probs <- seq(0.1, 0.9, 0.1)
quants_known <- list(x=quantile(x, probs))
totals_known <- c(x=sum(x))
df <- data.frame(x, y, p)
df_resp <- df[df$p == 1, ]
df_resp$d <- N/nrow(df_resp)
y_quant_true <- quantile(y, probs)
## standard calibration for comparison
result0 <- sampling::calib(Xs = cbind(1, df_resp$x),
d = df_resp$d,
total = c(N, totals_known),
method = "linear")
y_quant_hat0 <- laeken::weightedQuantile(x = df_resp$y,
probs = probs,
weights = result0*df_resp$d)
x_quant_hat0 <- laeken::weightedQuantile(x = df_resp$x,
probs = probs,
weights = result0*df_resp$d)
## example 1: calibrate only quantiles (deciles)
result1 <- joint_calib(formula_quantiles = ~x,
data = df_resp,
dweights = df_resp$d,
N = N,
pop_quantiles = quants_known,
method = "linear",
backend = "sampling")
## estimate quantiles
y_quant_hat1 <- laeken::weightedQuantile(x = df_resp$y,
probs = probs,
weights = result1$g*df_resp$d)
x_quant_hat1 <- laeken::weightedQuantile(x = df_resp$x,
probs = probs,
weights = result1$g*df_resp$d)
## compare with known
data.frame(standard = y_quant_hat0, est=y_quant_hat1, true=y_quant_true)
## example 2: calibrate with quantiles (deciles) and totals
result2 <- joint_calib(formula_totals = ~x,
formula_quantiles = ~x,
data = df_resp,
dweights = df_resp$d,
N = N,
pop_quantiles = quants_known,
pop_totals = totals_known,
method = "linear",
backend = "sampling")
## estimate quantiles
y_quant_hat2 <- laeken::weightedQuantile(x = df_resp$y,
probs = probs,
weights = result2$g*df_resp$d)
x_quant_hat2 <- laeken::weightedQuantile(x = df_resp$x,
probs = probs,
weights = result2$g*df_resp$d)
## compare with known
data.frame(standard = y_quant_hat0, est1=y_quant_hat1,
est2=y_quant_hat2, true=y_quant_true)
## example 3: calibrate wigh quantiles (deciles) and totals with
## hyperbolic sinus (sinh) and survey package
result3 <- joint_calib(formula_totals = ~x,
formula_quantiles = ~x,
data = df_resp,
dweights = df_resp$d,
N = N,
pop_quantiles = quants_known,
pop_totals = totals_known,
method = "sinh",
backend = "survey")
## estimate quantiles
y_quant_hat3 <- laeken::weightedQuantile(x = df_resp$y,
probs = probs,
weights = result3$g*df_resp$d)
x_quant_hat3 <- laeken::weightedQuantile(x = df_resp$x,
probs = probs,
weights = result3$g*df_resp$d)
## example 4: calibrate wigh quantiles (deciles) and totals with ebal package
result4 <- joint_calib(formula_totals = ~x,
formula_quantiles = ~x,
data = df_resp,
dweights = df_resp$d,
N = N,
pop_quantiles = quants_known,
pop_totals = totals_known,
method = "eb",
backend = "ebal")
## estimate quantiles
y_quant_hat4 <- laeken::weightedQuantile(x = df_resp$y,
probs = probs,
weights = result4$g*df_resp$d)
x_quant_hat4 <- laeken::weightedQuantile(x = df_resp$x,
probs = probs,
weights = result4$g*df_resp$d)
## compare with known
data.frame(standard = y_quant_hat0,
est1=y_quant_hat1,
est2=y_quant_hat2,
est3=y_quant_hat3,
est4=y_quant_hat4,
true=y_quant_true)
## compare with known X
data.frame(standard = x_quant_hat0,
est1=x_quant_hat1,
est2=x_quant_hat2,
est3=x_quant_hat3,
est4=x_quant_hat4,
true = quants_known$x)
Run the code above in your browser using DataLab