temp-bikes/03 Caliper weights/caliper-diagnosis.R

############################################################
### 4. Diagnose the caliper weights                      ###
############################################################


caliper_weights <- function(cw_val) {

    weight_df <- caliper_relevance_new(
        bikes_atom,
        sotw_cont,
        670,
        cw = cw_val,
        matching_vars = matchi # matchi
    )

    RAL_data <- RAL_calculator(weight_df, bikes_atom)
    cal_wt <- RAL_data[, .(method, weight = exp(RAL)/sum(exp(RAL))), by = .(t)]
    cal_wt <- data.frame(cal_wt, cw_val)
    return(data.table(cal_wt))
}


############################################################
### Actual weights for different cal widths + gewisano   ###
############################################################

gg <- data.table(gen_gewisano(bikes_atom, 201, pratig = TRUE)[[2]])
colnames(gg) <- c("t", "BART", "BREGLOG", "SVBVAR")
gewisano <- data.table::melt(gg, measure.vars = c("BART", "BREGLOG", "SVBVAR"), variable.name = "method", value.name = "weight")
gewisano$cw_val <- "gewisano"

aaa <- lapply(c(0.1, 1), caliper_weights)
dfa <- do.call(rbind, aaa)
dfa <- rbind(dfa, gewisano[t>670])

ggplot(dfa, aes(x = t, y = weight, col = method)) +
    geom_line() +
    facet_wrap(~cw_val, ncol = 1)
ggsave("temp/cal_weights.pdf")


############################################################
### Adding the lpdenses                                  ###
############################################################

gen_agg_preds(
    bikes_atom,
    start_agg = 670, 
    sotw_cont,
    mahala = FALSE,
    cw = 1
)



df_agg <- data.table(df_agg)

df_agg[t >= 670, .(meanpred = mean(lpdens)), by = method]

predabil <- RAL_data[, .(method, lpdens), by = .(t)]
ggplot(predabil, aes(x = t, y = lpdens, col = method)) + geom_line()
ggsave("temp/plot_predabil.pdf")

df_agg <- data.table(df_agg)
View(df_agg[t >= 670])
ggplot(df_agg[t >= 670], aes(x = t, y = lpdens, color = method)) + geom_line()
ooelrich/oscbvar documentation built on Sept. 8, 2021, 3:31 p.m.