# Use ksmooth which implements Nadaraya-Watson
h_vals <- c(0.05, 0.2, 0.8) # Bandwidth h (span for ksmooth is different)
# ksmooth bandwidth 'h' corresponds to the full width 2h of the uniform kernel
# ksmooth 'bandwidth' argument seems different, maybe relates to h directly? Let's test.
x_grid <- seq(min(sim_data$x), max(sim_data$x), length.out = 100)
nw_preds_h <- list()
for (h_val in h_vals) {
# Use kernel="box" for uniform
fit <- ksmooth(x = sim_data$x, y = sim_data$y, kernel = "box", bandwidth = h_val, x.points = x_grid)
nw_preds_h[[as.character(h_val)]] <- data.frame(x = fit$x, pred = fit$y, h = h_val)
}
nw_pred_h_df <- bind_rows(nw_preds_h)