## ------------------------------------------------------------
## Case A: Conditional density evaluated at a single point (x, y)
## ------------------------------------------------------------
## This illustrates the most basic usage: estimating pi(y | x)
## at one covariate value x and one response value y.
dat <- generate_clustered_mar(
n = 200, m = 4, d = 2,
target_missing = 0.3, seed = 1
)
fit <- fit_cond_density_quantile(
dat,
y_col = "Y", delta_col = "delta",
x_cols = c("X1", "X2"),
taus = seq(0.05, 0.95, by = 0.02),
method = "rq",
seed = 1
)
## a single covariate value x
x1 <- matrix(c(0.2, -1.0), nrow = 1)
colnames(x1) <- c("X1", "X2")
## estimate pi(y | x) at y = 0.5
fit$predict_density(x1, y_new = 0.5)
## ------------------------------------------------------------
## Case B: Conditional density as a function of y (density curve)
## ------------------------------------------------------------
## Here we fix x and evaluate pi(y | x) over a grid of y values,
## which produces an estimated conditional density curve.
y_grid <- seq(-3, 3, length.out = 201)
## reuse the same x by repeating it to match the y-grid
x_rep <- x1[rep(1, length(y_grid)), , drop = FALSE]
f_grid <- fit$predict_density(x_rep, y_grid)
## ------------------------------------------------------------
## True conditional density under the data generator
## ------------------------------------------------------------
## Data are generated as:
## Y = X^T beta + b + eps,
## b ~ N(0, sigma_b^2), eps ~ N(0, sigma_eps^2)
## Hence the marginal conditional density is:
## Y | X = x ~ N(x^T beta, sigma_b^2 + sigma_eps^2)
beta_true <- c(0.5, 0.6)
sigma_b_true <- 0.7
sigma_eps_true <- 1.0
mu_true <- drop(x1 %*% beta_true)
sd_true <- sqrt(sigma_b_true^2 + sigma_eps_true^2)
f_true <- stats::dnorm(y_grid, mean = mu_true, sd = sd_true)
## ------------------------------------------------------------
## Visualization: estimated vs true conditional density
## (use smooth.spline on log-density for a smoother display)
## ------------------------------------------------------------
## smooth the estimated curve for visualization
ok <- is.finite(f_grid) & (f_grid > 0)
sp <- stats::smooth.spline(y_grid[ok], log(f_grid[ok]), spar = 0.85)
f_smooth <- exp(stats::predict(sp, y_grid)$y)
ymax <- max(c(f_smooth, f_true), na.rm = TRUE)
plot(
y_grid, f_smooth,
type = "l", lwd = 2,
xlab = "y",
ylab = expression(hat(pi)(y ~ "|" ~ x)),
ylim = c(0, 1.2 * ymax),
main = "Conditional density at a fixed x: estimated vs true"
)
grid(col = "gray85", lty = 1)
lines(y_grid, f_true, lwd = 2, lty = 2)
legend(
"topright",
legend = c("Estimated (smoothed)", "True (generator)"),
lty = c(1, 2), lwd = c(2, 2), bty = "n"
)
Run the code above in your browser using DataLab