ABC-rejection models

Author

Thomas Gorman

Published

March 21, 2024

Code
pacman::p_load(dplyr,purrr,tidyr,ggplot2, data.table, here, patchwork, conflicted, 
               stringr,future,furrr, knitr, reactable, flextable,ggstance, htmltools)
conflict_prefer_all("dplyr", quiet = TRUE)
options(scipen = 999)
walk(c("Display_Functions","fun_alm","fun_indv_fit","fun_model"), ~ source(here::here(paste0("Functions/", .x, ".R"))))
ds <- readRDS(here::here("data/e1_md_11-06-23.rds"))  |> as.data.table()

fd <- readRDS(here("data/e1_08-21-23.rds"))
test <- fd |> filter(expMode2 == "Test") 
testAvg <- test %>% group_by(id, condit, vb, bandInt,bandType,tOrder) %>%
  summarise(nHits=sum(dist==0),vx=mean(vx),dist=mean(dist),sdist=mean(sdist),n=n(),Percent_Hit=nHits/n)
nbins=3
trainAvg <- fd |> filter(expMode2 == "Train") |> group_by(id) |> 
  mutate(tr=trial,x=vb,Block=case_when(expMode2=="Train" ~ cut(tr,breaks=seq(1,max(tr), length.out=nbins+1),include.lowest=TRUE,labels=FALSE),
                                         expMode2=="Test" ~ 4)) |> 
  group_by(id,condit,vb,x,Block) |> 
  summarise(dist=mean(dist),y=mean(vx))

# df with trial count columns for each level of expMode, for each id
id_n <- ds |> group_by(id,condit) |> summarise(nTest=n_distinct(tr[expMode2=="Test"]),
                                        nTrain=n_distinct(tr[expMode2=="Train"])) |>
  mutate(nTotal=nTest+nTrain)



testAvg <- test %>% group_by(id, condit, vb, bandInt,bandType,tOrder) %>%
  summarise(nHits=sum(dist==0),vx=mean(vx),dist=mean(dist),sdist=mean(sdist),n=n(),Percent_Hit=nHits/n)

input_layer <<- output_layer <<-  c(100,350,600,800,1000,1200)

ids <- c(1,2,4,5,6,7,8, 10,11,12,13)
ids2 <- c(1,66,36)
ids3 <- c(20,71,101,4,76,192)
idsBad <- c(76,192, 101)

#file_name <- "n_iter_300_ntry_3000_0800"
#file_name <- "n_iter_100_ntry_200_4509"
file_name <- "n_iter_100_ntry_400_3247"
file_name <- "n_iter_200_ntry_300_5354"

# list.files(here('data/abc_reject'))
# (grep("Train",list.files(here(paste0('data/abc_reject/',file_name)),
#                                            pattern="EXAM_Test",full.names = TRUE),
#                                 invert=TRUE, value=TRUE))


ind_fits <- map(list.files(here(paste0('data/abc_reject/'),file_name),full.names=TRUE), readRDS)
# ind_fits <- map(list.files(here('data/abc_reject/n_iter_2000_ntry_10_2918'),full.names=TRUE), readRDS)
ind_fits_df <- ind_fits |> map(~list(dat=.x[[1]], Model = .x[["Model"]], Fit_Method=.x[["Fit_Method"]]))
ind_fits_df <- ind_fits_df |> map(~rbindlist(.x$dat) |> mutate(Model = .x$Model, Fit_Method = .x$Fit_Method)) |> rbindlist() 


avg_samples <- ind_fits_df |> group_by(id,condit,Model,Fit_Method) |> 
  summarise(inc=max(inc_count),iter_sum=sum(iter_count))
mean(avg_samples$iter_sum)
[1] 205708.7
Code
mean(avg_samples$iter_sum)
[1] 205708.7
Code
min(avg_samples$iter_sum)
[1] 9965
Code
max(avg_samples$iter_sum)
[1] 1644491
Code
sum(avg_samples$iter_sum)
[1] 192543336
Code
sum(avg_samples$iter_sum)
[1] 192543336
Code
avg_samples |> group_by(Model,Fit_Method) |> summarise(mean(inc),mean(iter_sum))
# A tibble: 6 × 4
# Groups:   Model [2]
  Model Fit_Method `mean(inc)` `mean(iter_sum)`
  <chr> <chr>            <dbl>            <dbl>
1 ALM   Test              763.          295556.
2 ALM   Test_Train        501.          197177.
3 ALM   Train             358.          163335.
4 EXAM  Test              581.          220765.
5 EXAM  Test_Train        445.          185647.
6 EXAM  Train             351.          171773.
Code
#run_params <- tibble::lst(file_name,cMean=ind_fits[[1]]$cMean, cSig=ind_fits[[1]]$cSig, 
                          
run_params <- keep(ind_fits[[1]], ~any(class(.x) %in% c("character", "numeric")))
run_params <- tibble(!!!run_params) |> select(-Model,-Fit_Method)

extract_info <- function(raw_names, run_params) {
  fname <- tools::file_path_sans_ext(basename(raw_names))
  n_samp <- str_extract(raw_names, "(?<=_)\\d+(?=_)")
  ntry_ <- str_extract(raw_names, "(?<=ntry_)\\d+")
  type <- 'ss'
  data.frame(run_params, n_samp, ntry_, type, fname)
}

run_info <- extract_info(file_name,run_params)
Code
generate_data <- function(Model, post_samples, data, num_samples = 1, return_dat = "train_data, test_data") {
  # Filter data for the specific id without invalidating selfref
  sbj_data <- copy(data[id == post_samples$id[1]])
  simulation_function <- ifelse(Model == "EXAM", full_sim_exam, full_sim_alm)

  target_data <- switch(return_dat,
                        "test_data" = copy(sbj_data[expMode2 == "Test"]),
                        "train_data" = copy(sbj_data[expMode2 == "Train"]),
                        "train_data, test_data" = copy(sbj_data[expMode2 %in% c("Test", "Train")]))
  
  post_samples <- post_samples[order(mean_error)][1:num_samples, .(c, lr, mean_error, rank = .I)]

  simulated_data_list <- lapply(1:nrow(post_samples), function(i) {
    params <- post_samples[i]
    sim_data <- simulation_function(sbj_data, params$c, params$lr, input_layer = input_layer, 
                                    output_layer = output_layer, return_dat = return_dat)
    sim_data_dt <- data.table(id = sbj_data$id[1], condit = sbj_data$condit[1], 
                              expMode2 = target_data$expMode2, Model = Model,tr=target_data$tr,
                              y = target_data$y, x = target_data$x, c = params$c, 
                              lr = params$lr, mean_error = params$mean_error, rank = i,
                              pred = sim_data)
    return(sim_data_dt)
  })
  
  result_dt <- rbindlist(simulated_data_list)
  setcolorder(result_dt, c("id", "condit", "expMode2","tr", "c", "lr", "x", "y", "pred"))
  return(result_dt)
}

future::plan(multisession)

nestSbjModelFit <- ind_fits_df %>% nest(.by=c(id,Model,Fit_Method))

# organize test data predictions
post_dat <- nestSbjModelFit |> mutate(pp=furrr::future_pmap(list(id,Model,Fit_Method,data), ~{
   generate_data(..2, ..4 |> mutate(id=..1), ds, num_samples = 20, return_dat="test_data")
   })) |> 
  select(Fit_Method,pp,-data) |>  
  unnest(pp) |>  filter(expMode2=="Test") |> as.data.table()

post_dat_avg <- post_dat |> group_by(id, condit, Model, Fit_Method, x, c, lr, rank) |> 
  summarise(y = mean(y), pred = mean(pred), error = y - pred) |> as.data.table() 

post_dat_avg2 <- post_dat_avg |>
  group_by(id, condit, Model, Fit_Method, c, lr, rank) |> 
  summarise(y = mean(y), pred = mean(pred), error = y - pred) |> as.data.table() |> arrange(rank)

map_id <- post_dat |> group_by(id, condit, Model, Fit_Method) |> 
  filter(rank==1) |> slice_head(n=1) |> select(id,condit,Fit_Method,Model,c,lr,mean_error,rank) |>
  left_join(id_n,by=(c("id","condit")))

# compute BIC for each row of map_id; p=2 for c and lr
map_id <- map_id |> mutate(n = fifelse(Fit_Method == "Test", nTest, fifelse(Fit_Method == "Train", nTrain, nTotal)),BIC = -2 * log(mean_error) + log(n)*2)

bic_id <- map_id %>%
  group_by(id, Fit_Method) %>%
  arrange(Model) %>%
  summarize(BIC_diff = diff(BIC),
            .groups = 'drop') %>%
  left_join(map_id, by = c("id", "Fit_Method")) %>%
  select(id, Fit_Method, Model, BIC, BIC_diff)



setorder(post_dat_avg, id, x, rank)
post_dat_l <- melt(post_dat_avg, id.vars = c("id", "condit", "Model", "Fit_Method", "x", "c", "lr", "rank","error"),
                   measure.vars = c("pred", "y"), variable.name = "Resp", value.name = "val")
post_dat_l[, Resp := fifelse(Resp == "y", "Observed",
                             fifelse(Model == "ALM", "ALM", "EXAM"))]
setorder(post_dat_l, id, Resp)
#rm(post_dat_avg)


# organize training data predictions
pd_train <- nestSbjModelFit |> mutate(pp=furrr::future_pmap(list(id,Model,Fit_Method,data), ~{
   generate_data(..2, ..4 |> mutate(id=..1), ds, num_samples = 20, return_dat="train_data")
   })) |>
  select(Fit_Method,pp,-data) |>
  unnest(pp) |> as.data.table() |> filter(expMode2=="Train")

nbins <- 3
pd_train <- pd_train |> group_by(id,condit,Model,Fit_Method) |>
  mutate(Block=cut(tr,breaks=seq(1,max(tr), length.out=nbins+1),include.lowest=TRUE,labels=FALSE))
setorder(pd_train, id, x,Block, rank)

pd_train_l <- melt(pd_train, id.vars = c("id", "condit", "Model","Block", "Fit_Method", "x", "c", "lr", "rank"),
                   measure.vars = c("pred", "y"), variable.name = "Resp", value.name = "val") |> as.data.table()
pd_train_l[, Resp := fifelse(Resp == "y", "Observed",
                             fifelse(Model == "ALM", "ALM", "EXAM"))] 
setorder(pd_train_l, id,Block, Resp) 

pd_train_l <- pd_train_l |>
  mutate(dist = case_when(
    val >= x & val <= x + 200 ~ 0,                 
    val < x ~ abs(x - val),                       
    val > x + 200 ~ abs(val - (x + 200)),           
    TRUE ~ NA_real_                                 
  ))
plan(sequential)

plan(sequential)


# theta_map <- post_dat_avg |> group_by(condit,Model,Fit_Method) |> summarize(c=median(c),lr=median(lr))
# 
# map <- simulation_function(sbj_data <- ds |> theta_map$c[1], , params$lr, input_layer = input_layer, 
#                                     output_layer = output_layer, return_dat = return_dat)

# AIC and BIC
# mc2 <- post_dat |> filter(rank==1) |>
#   group_by(id,condit,Model,Fit_Method) |>
#   mutate(e2=abs(y-pred),n=n(),k=2) |>
#   group_by(id,condit,Model,Fit_Method,x) |>
#   summarise(y=mean(y), pred=mean(pred), e2=mean(e2), mean_error=first(mean_error),n=first(n),k=first(k)) |>
#   group_by(id,condit,Model,Fit_Method) |>
#   summarise(e2=mean(e2), mean_error=first(mean_error),n=first(n),k=first(k)) |>
#   mutate(AIC=2*k+n*mean_error, BIC=log(n)*k+n*mean_error)

# Single run extraction:
# exam_test <- ind_fits_df |> filter(Model == "EXAM", Fit_Method == "Test")
# post_dat_trial <- exam_test %>% split(f =c(.$id), drop=TRUE) |> 
#   map(~generate_data(.x$Model, .x, ds, num_samples = 15, return_dat="train_data, test_data")) |> 
#   rbindlist() |> 
#   filter(expMode2=="Test")
# 
# post_dat_avg <- post_dat_trial |> group_by(id,condit,x,c,lr,rank) |> 
#   summarise(y = mean(y),pred = mean(pred)) |> arrange(id,x,rank)

#post_dat_l |> filter(Model == "EXAM", Fit_Method == "Test",id==1) |> arrange(rank)
# head(post_dat_l)


bestTestEXAM <- post_dat_l |> 
  filter(Fit_Method=="Test", rank==1, Resp=="EXAM") |> 
  group_by(id,condit,Model,Fit_Method) |>
  summarise(mae=mean(abs(error))) |> arrange(mae) |> ungroup() |>
  mutate(group_rank = row_number(),.by=c(condit))

bestTestALM<- post_dat_l |> 
  filter(Fit_Method=="Test", rank==1, Resp=="ALM") |> 
  group_by(id,condit,Model,Fit_Method) |>
  summarise(mae=mean(abs(error))) |> arrange(mae) |> ungroup() |>
  mutate(group_rank = row_number(),.by=c(condit))

bestTrainALM<- post_dat_l |> 
  filter(Fit_Method=="Train", rank==1, Resp=="ALM") |> 
  group_by(id,condit,Model,Fit_Method) |>
  summarise(mae=mean(abs(error))) |> arrange(mae) |> ungroup() |>
  mutate(group_rank = row_number(),.by=c(condit))

bestTrainEXAM<- post_dat_l |> 
  filter(Fit_Method=="Train", rank==1, Resp=="EXAM") |> 
  group_by(id,condit,Model,Fit_Method) |>
  summarise(mae=mean(abs(error))) |> arrange(mae) |> ungroup() |>
  mutate(group_rank = row_number(),.by=c(condit))

bestTtEXAM <- post_dat_l |> 
  filter(Fit_Method=="Test_Train", rank==1, Resp=="EXAM") |> 
  group_by(id,condit,Model,Fit_Method) |>
  summarise(mae=mean(abs(error))) |> arrange(mae) |> ungroup() |>
  mutate(group_rank = row_number(),.by=c(condit))

Posterior Average Table

Code
# post_tabs <- abc_tables(post_dat_l)
# post_tabs$et_sum |> gt::gt()

post_tabs <- abc_tables(post_dat,post_dat_l)

# tables with fit error
# post_tabs$agg_full |> flextable::tabulator(rows=c("Fit_Method","Model"), columns=c("condit"), 
#                        `ME` = as_paragraph(mean_error)) |> as_flextable() |>
#   set_caption("Mean from full_posterior") 

# post_tabs$agg_best |> flextable::tabulator(rows=c("Fit_Method","Model"), columns=c("condit"), 
#                        `ME` = as_paragraph(mean_error)) |> as_flextable() |>
#   set_caption("Mean from best parameters") 

post_tabs$agg_pred_full |> flextable::tabulator(rows=c("Fit_Method","Model"), columns=c("condit"), 
                       `ME` = as_paragraph(mean_error)) |> as_flextable() |>
  set_caption("Mean from full_posterior") 

Full Posterior

{#tbl-anonymous-2089966-1}

Fit_Method

Model

Constant

Varied

Test

ALM

275.4

226.9

EXAM

214.6

212.4

Test_Train

ALM

286.4

265.5

EXAM

227.5

246.2

Train

ALM

530.6

365.5

EXAM

339.1

370.7

Code
post_tabs$agg_pred_best |> flextable::tabulator(rows=c("Fit_Method","Model"), columns=c("condit"), 
                       `ME` = as_paragraph(mean_error)) |> as_flextable() |>
  set_caption("Mean from full_posterior") 

Best Posterior

{#tbl-anonymous-2089966-2}

Fit_Method

Model

Constant

Varied

Test

ALM

273.8

219.3

EXAM

213.3

208.4

Test_Train

ALM

285.1

260.8

EXAM

226.9

240.8

Train

ALM

517.0

363.7

EXAM

338.5

356.7

Posterior Distribution

Interactive Test Predictions Table

Code
tags$select(
  tags$option(value = "", "All"),
  purrr::map(unique(post_tabs$agg_x_full$condit), tags$option),
    purrr::map(unique(post_tabs$agg_x_full$Model), tags$option),
    purrr::map(unique(post_tabs$agg_x_full$Fit_Method), tags$option),
   purrr::map(unique(post_tabs$agg_x_full$x), tags$option)
)
Code
selectFilter <- function(tableId, style = "width: 100%; height: 100%;") {
  function(values, name) {
    tags$select(
      # Set to undefined to clear the filter
      onchange = sprintf("
        const value = event.target.value
        Reactable.setFilter('%s', '%s', value === '__ALL__' ? undefined : value)
      ", tableId, name),
      # "All" has a special value to clear the filter, and is the default option
      tags$option(value = "__ALL__", "All"),
      lapply(unique(values), tags$option),
      "aria-label" = sprintf("Filter %s", name),
      style = style
    )
  }
}


post_tabs$agg_x_full |> 
  left_join(post_tabs$agg_x_best |> rename("best_error"=mean_error,"best_pred"=pred), 
            by=c("condit","Model","Fit_Method","x","y")) |> 
  arrange(desc(condit),x,Fit_Method) |>
  mutate(x=as.character(x)) |>
  reactable::reactable(
    columns = list(condit=colDef(name="Condit",filterInput=selectFilter("my-tbl")),
                   Model=colDef(name="Model",filterInput=selectFilter("my-tbl")),
                   Fit_Method=colDef(name="Fit_Method",filterInput=selectFilter("my-tbl"),
                                     filterMethod = JS("(rows, columnId, filterValue) => {
        return rows.filter(row => row.values[columnId] === filterValue)
      }")),
      x=colDef(name="x",filterInput=selectFilter("my-tbl"),
               filterMethod = JS("(rows, columnId, filterValue) => {
        return rows.filter(row => row.values[columnId] === filterValue)
      }"))),
    elementId = "my-tbl",
    defaultPageSize=15,
    filterable=TRUE) 

Train Predictions

Code
pt_train <- abc_train_tables(pd_train,pd_train_l)
pt_train$agg_pred_full |> flextable::tabulator(rows=c("Fit_Method","Model"), columns=c("condit"), 
                       `ME` = as_paragraph(mean_error)) |> as_flextable() |>
  set_caption("Mean from full_posterior")

Fit_Method

Model

Constant

Varied

Test

ALM

851.2

3,049.9

EXAM

711.9

1,047.5

Test_Train

ALM

229.7

324.7

EXAM

233.4

331.3

Train

ALM

216.1

301.8

EXAM

216.0

302.3

Code
pt_train$block_pred_full |> 
  flextable::tabulator(rows=c("Fit_Method","Model","Block"), columns=c("condit"), 
                       `Observed` = as_paragraph(y), 
                       `Predicted` = as_paragraph(pred),
                       `ME` = as_paragraph(mean_error)) |> as_flextable() |>
  set_caption("Mean from full_posterior")

Fit_Method

Model

Block

Constant

Varied

Observed

Predicted

ME

Observed

Predicted

ME

Test

ALM

1

927.4

730.3

876.2

1,069.0

746.6

1,838.5

2

922.8

484.7

1,001.5

1,091.9

-4,869.1

7,073.9

3

916.5

1,202.4

714.1

1,100.8

951.3

1,022.6

EXAM

1

927.4

796.9

980.6

1,069.0

889.1

1,248.6

2

922.8

895.1

720.6

1,091.9

1,024.3

1,139.0

3

916.5

918.0

442.5

1,100.8

1,005.0

772.5

Test_Train

ALM

1

927.4

820.4

294.8

1,069.0

872.4

393.2

2

922.8

885.6

202.9

1,091.9

980.9

307.6

3

916.5

886.9

191.9

1,100.8

1,008.5

277.9

EXAM

1

927.4

832.4

296.8

1,069.0

864.5

411.7

2

922.8

902.8

205.8

1,091.9

989.2

309.7

3

916.5

902.2

198.3

1,100.8

1,020.4

277.8

Train

ALM

1

927.4

830.6

272.3

1,069.0

921.7

355.6

2

922.8

889.9

193.6

1,091.9

1,005.0

289.1

3

916.5

890.3

183.0

1,100.8

1,022.5

265.3

EXAM

1

927.4

831.4

272.1

1,069.0

916.3

355.6

2

922.8

890.3

193.4

1,091.9

1,001.8

289.5

3

916.5

890.0

183.1

1,100.8

1,020.8

266.1

Code
pt_train$block_pred_full |> filter(condit=="Varied") |>
  flextable::tabulator(rows=c("Fit_Method","Block"), columns=c("Model"), 
                      # `Observed` = as_paragraph(y), 
                       `Predicted` = as_paragraph(pred),
                       `ME` = as_paragraph(mean_error)) |> as_flextable() |>
  set_caption("Mean from full_posterior")

Fit_Method

Block

ALM

EXAM

Predicted

ME

Predicted

ME

Test

1

746.6

1,838.5

889.1

1,248.6

2

-4,869.1

7,073.9

1,024.3

1,139.0

3

951.3

1,022.6

1,005.0

772.5

Test_Train

1

872.4

393.2

864.5

411.7

2

980.9

307.6

989.2

309.7

3

1,008.5

277.9

1,020.4

277.8

Train

1

921.7

355.6

916.3

355.6

2

1,005.0

289.1

1,001.8

289.5

3

1,022.5

265.3

1,020.8

266.1

Code
pt_train$block_pred_full |>
  flextable::tabulator(rows=c("Fit_Method","Block"), columns=c("condit","Model"), 
                      # `Observed` = as_paragraph(y), 
                       `Predicted` = as_paragraph(pred),
                       `ME` = as_paragraph(mean_error)) |> as_flextable() |>
  set_caption("Mean from full_posterior")

Fit_Method

Block

Constant

Varied

ALM

EXAM

ALM

EXAM

Predicted

ME

Predicted

ME

Predicted

ME

Predicted

ME

Test

1

730.3

876.2

796.9

980.6

746.6

1,838.5

889.1

1,248.6

2

484.7

1,001.5

895.1

720.6

-4,869.1

7,073.9

1,024.3

1,139.0

3

1,202.4

714.1

918.0

442.5

951.3

1,022.6

1,005.0

772.5

Test_Train

1

820.4

294.8

832.4

296.8

872.4

393.2

864.5

411.7

2

885.6

202.9

902.8

205.8

980.9

307.6

989.2

309.7

3

886.9

191.9

902.2

198.3

1,008.5

277.9

1,020.4

277.8

Train

1

830.6

272.3

831.4

272.1

921.7

355.6

916.3

355.6

2

889.9

193.6

890.3

193.4

1,005.0

289.1

1,001.8

289.5

3

890.3

183.0

890.0

183.1

1,022.5

265.3

1,020.8

266.1

Code
pt_train$block_pred_full |>
  flextable::tabulator(rows=c("Fit_Method","Block"), columns=c("condit","Model"), 
                      # `Observed` = as_paragraph(y), 
                       `ME` = as_paragraph(mean_error)) |> as_flextable() |>
  set_caption("Mean from full_posterior")

Fit_Method

Block

Constant

Varied

ALM

EXAM

ALM

EXAM

Test

1

876.2

980.6

1,838.5

1,248.6

2

1,001.5

720.6

7,073.9

1,139.0

3

714.1

442.5

1,022.6

772.5

Test_Train

1

294.8

296.8

393.2

411.7

2

202.9

205.8

307.6

309.7

3

191.9

198.3

277.9

277.8

Train

1

272.3

272.1

355.6

355.6

2

193.6

193.4

289.1

289.5

3

183.0

183.1

265.3

266.1

Code
best_indices <- pt_train$block_pred_full_x %>%
   filter(!(Fit_Method=="Test")) |>
  nest(data = -condit) %>%
  mutate(indices = map(data, ~{
    .x %>%
      select(-y, -pred, -mean_error) %>%
      pivot_wider(names_from = Model, values_from = best) %>%
      ungroup() |>
      select(ALM, EXAM) %>%
      pmap(.f = function(ALM, EXAM) {
        which(c(ALM, EXAM) == 1)
      })
  })) %>%
  select(condit, indices)
   


pt_train$block_pred_full_x |> filter(condit=="Varied",!(Fit_Method=="Test")) |>
  flextable::tabulator(rows=c("Fit_Method","Block","x"), columns=c("Model"), 
                      # `Observed` = as_paragraph(y), 
                       `Predicted` = as_paragraph(pred),
                       `ME` = as_paragraph(mean_error)) |> 
  as_flextable() %>% 
  apply_best_formatting(.,pull(filter(best_indices,condit=="Varied"),indices) %>% .[[1]]) |> 
  set_caption("Vared Train Predictions")

Fit_Method

Block

x

ALM

EXAM

Predicted

ME

Predicted

ME

Test_Train

1

800

845.3

356.8

849.0

375.5

1,000

875.0

387.1

868.9

404.0

1,200

898.1

423.9

879.2

440.9

2

800

940.2

291.3

960.6

292.8

1,000

984.0

291.7

992.2

293.7

1,200

1,019.2

339.7

1,015.3

341.9

3

800

960.2

261.2

973.8

267.9

1,000

1,009.0

259.7

1,026.3

260.9

1,200

1,054.8

308.0

1,060.3

300.4

Train

1

800

892.3

321.8

887.0

319.7

1,000

923.4

355.1

918.1

355.7

1,200

947.3

378.0

941.6

379.8

2

800

979.9

269.3

975.1

269.5

1,000

1,001.0

281.0

997.7

282.1

1,200

1,035.6

316.4

1,033.5

316.5

3

800

992.9

253.4

989.2

255.0

1,000

1,017.2

247.3

1,017.3

247.6

1,200

1,055.9

291.5

1,053.9

291.9

Code
pt_train$block_pred_full_x |> filter(condit=="Constant",!(Fit_Method=="Test")) |>
  flextable::tabulator(rows=c("Fit_Method","Block","x"), columns=c("Model"), 
                      # `Observed` = as_paragraph(y), 
                       `Predicted` = as_paragraph(pred),
                       `ME` = as_paragraph(mean_error)) |> 
  as_flextable() %>% 
  apply_best_formatting(.,pull(filter(best_indices,condit=="Constant"),indices) %>% .[[1]]) |> 
  set_caption("Constant Train Predictions")

Fit_Method

Block

x

ALM

EXAM

Predicted

ME

Predicted

ME

Test_Train

1

800

820.8

294.3

832.5

296.1

2

886.0

202.5

902.6

205.4

3

887.7

191.1

902.8

197.5

Train

1

830.7

271.6

831.6

271.4

2

889.9

193.4

890.3

193.2

3

890.6

182.3

890.4

182.3

Code
# pull(filter(best_indices,condit=="Varied"),indices) %>% .[[1]] 

Posterior Predictive:

Code
group_predictive_plots(post_dat_l) 

Code
testAvg |> ggplot(aes(x=vx)) + geom_density() + facet_wrap(~vb) +



post_dat_avg |> filter(rank==1) |> ggplot(aes(x=pred)) + geom_density() + facet_wrap(~x)

Code
post_dat_avg |> filter(rank==1) |> ggplot(aes(x=pred)) + 
  geom_density() + geom_density(data=post_dat_avg, aes(x=y), color="red") +
  facet_wrap(condit~x,ncol=6)

Code
post_dat_avg |> filter(Model=="EXAM",Fit_Method=="Test") |> ggplot(aes(x=pred)) + 
  geom_density() + geom_density(data=post_dat_avg, aes(x=y), color="red") +
  facet_wrap(condit~x,ncol=6)

Code
post_dat_avg |> filter(Model=="ALM",Fit_Method=="Test") |> ggplot(aes(x=pred)) + 
  geom_density() + geom_density(data=post_dat_avg, aes(x=y), color="red") +
  facet_wrap(condit~x,ncol=6)

Code
post_dat_avg |> filter(Fit_Method=="Test") |> ggplot(aes(x=pred)) + 
  geom_density(aes(fill=Model),alpha=.5) + geom_density(data=post_dat_avg, aes(x=y), color="black",alpha=2) +
  facet_wrap(condit~x,ncol=6)

Code
post_dat_avg |> filter(Fit_Method=="Test_Train") |> ggplot(aes(x=pred)) + 
  geom_density(aes(fill=Model),alpha=.5) + geom_density(data=post_dat_avg, aes(x=y), color="black",alpha=2) +
  facet_wrap(condit~x,ncol=6)

Code
post_dat_avg |> filter(Fit_Method=="Train",pred>0,pred<2000) |> ggplot(aes(x=pred)) + 
  geom_density(aes(fill=Model),alpha=.5) + geom_density(data=post_dat_avg, aes(x=y), color="black",alpha=2) +
  facet_wrap(condit~x,ncol=6)

Alt grouping of Posterior Predictive

Code
 group_predictive_plots2(post_dat_l) 

Code
post_dat_avg |> group_by(id,condit,Fit_Method,x) |>
  ggplot(aes(x = x, y = error, fill=Model)) + 
  stat_bar + 
  facet_wrap(condit~Fit_Method, scales="free") + 
  labs(title = "Model Resituals - full posterior")

Code
post_dat_avg |> group_by(id,condit,Fit_Method,x) |>
  filter(rank==1) |> 
  ggplot(aes(x = x, y = error, fill=Model)) + 
  stat_bar + 
  facet_wrap(condit~Fit_Method, scales="free") + 
  labs(title = "Model Residuals - best parameters")

Code
plot_indv_posterior(ind_fits_df |> mutate(Group=condit))

Code
plot_indv_posterior(post_dat |> filter(rank==1) |> mutate(Group=condit))

Code
# plot join density of c and lr
# post_dat |> filter(rank==1, c<.001) |> 
#   ggplot(aes(x=c, y=lr, color=condit)) + geom_point() + facet_wrap(~Model+Fit_Method)

post_dat |>  filter(c<.001) |> ggplot(aes(x=c,y=condit,fill=condit)) + geom_boxploth(width=.5) + facet_wrap(~Model+Fit_Method,scales="free")

Code
post_dist <- post_dat_avg %>%
  group_by(id, condit, Model, Fit_Method, rank) %>%
  slice_head(n = 1) %>%
  ungroup() %>% # Make sure to remove the grouping by rank before the next group_by
  group_by(id, condit, Model, Fit_Method) %>%
  summarize(across(c:lr, list(mean = mean, sd = sd, se = ~sd(.)/sqrt(n()))), .groups = 'keep')

post_dist |> 
  group_by(condit, Model, Fit_Method) %>%
  mutate(across(c_mean, list(gmean = mean, gsd = sd, gse = ~sd(.)/sqrt(n()))), .groups = 'keep') |>
  filter(c_mean<(c_mean_gmean+(.5*c_mean_gse))) |>
  ggplot(aes(y=id, x = c_mean)) + 
  stat_pointinterval() + 
  ggh4x::facet_nested_wrap(Fit_Method~condit~Model,scales="free")

Code
post_dist |> 
  group_by(condit, Model, Fit_Method) %>%
  mutate(across(c_mean, list(gmean = median, gsd = sd, gse = ~sd(.)/sqrt(n()))), .groups = 'keep') |>
  filter(c_mean<(c_mean_gmean+(.5*c_mean_gse))) |>
  ggplot(aes( x = c_mean,fill=condit)) + geom_density() +
  ggh4x::facet_nested_wrap(Fit_Method~Model,scales="free")

Code
post_dist |> 
  group_by(condit, Model, Fit_Method) %>%
  mutate(across(c_mean, list(gmean = median, gsd = sd, gse = ~sd(.)/sqrt(n()))), .groups = 'keep') |>
  filter(c_mean<(c_mean_gmean+(.5*c_mean_gse))) |>
  ggplot(aes( x = log(c_mean),fill=condit)) + geom_density() +
  ggh4x::facet_nested_wrap(Fit_Method~Model,scales="free")

Code
 post_dat_avg %>%
    group_by(id, condit, Model, Fit_Method, rank) %>%
    slice_head(n = 1) |>
    ggplot(aes( x = log(c),fill=condit)) + geom_density() +
    ggh4x::facet_nested_wrap(Fit_Method~Model,scales="free")

Code
 post_dat_avg %>%
    group_by(id, condit, Model, Fit_Method, rank) %>%
    slice_head(n = 1) |>
    ggplot(aes(y=Fit_Method, x = log(c),col=condit)) + stat_pointinterval(position=position_dodge(.2)) +
    ggh4x::facet_nested_wrap(~Model,scales="free")

Code
 post_dat_avg %>%
    group_by(id, condit, Model, Fit_Method, rank) %>%
    slice_head(n = 1) |>
    ggplot(aes(y=Fit_Method, x = lr,col=condit)) + stat_pointinterval(position=position_dodge(.2)) +
    ggh4x::facet_nested_wrap(~Model,scales="free")

Code
# post_dat_avg %>%
#     group_by(id, condit, Model, Fit_Method, rank) %>%
#     slice_head(n = 1) |>
#     ggplot(aes(y=Fit_Method, x = (lr),fill=condit)) + 
#     stat_halfeye(position=position_dodge(.2)) +
#     ggh4x::facet_nested_wrap(~Model,scales="free")   
#   

Model Comparison:

Code
indv_best_plots(post_dat_l)

Code
post_dat_l |> group_by(condit,Model,Fit_Method,rank) |> 
   summarise(mean_error=mean(abs(error)), n=n()) |> 
   ggplot(aes(x=rank,y=mean_error,fill=Model))+geom_col()+facet_wrap(~Fit_Method,scales="free")

Code
post_dat_l |> group_by(condit,Model,Fit_Method,rank,x) |> 
   summarise(mean_error=mean(abs(error)), n=n()) |> 
   ggplot(aes(x=rank,y=mean_error,fill=Model))+geom_col()+facet_wrap(Fit_Method~x,ncol=6,scales="free")

Code
group_best_plots(post_dat_l)

Individual Predictive Plots

Code
indv_predictive_plots(post_dat_l, ids2)

Code
indv_predictive_plots(post_dat_l, idsBad)

Subject 1

Code
indv_predictive_dist((post_dat_l |> filter(rank<=200)),ind_fits_df, sbj=list(1))

Code
abc_tables(post_dat |> filter(id==1))$agg_full |> flextable() |> set_caption("Mean from full posterior")

condit

Model

Fit_Method

mean_error

Varied

ALM

Test

239.1

Varied

ALM

Test_Train

291.9

Varied

ALM

Train

297.5

Varied

EXAM

Test

219.8

Varied

EXAM

Test_Train

276.8

Varied

EXAM

Train

297.7

Code
abc_tables(post_dat |> filter(id==1))$agg_best |> flextable() |> set_caption("Mean from best parameters")

condit

Model

Fit_Method

mean_error

Varied

ALM

Test

237.5

Varied

ALM

Test_Train

276.0

Varied

ALM

Train

296.0

Varied

EXAM

Test

219.5

Varied

EXAM

Test_Train

269.1

Varied

EXAM

Train

296.0

Code
post_dat_l |> filter(id==1) |> group_by(condit,Model,Fit_Method,rank) |> 
   summarise(mean_error=mean(abs(error)), n=n()) |> 
   ggplot(aes(x=rank,y=mean_error,fill=Model))+geom_col()+facet_wrap(~Fit_Method)

Code
# plot_indv_posterior(ind_fits_df |> filter(id==1))
# ind_fits_df |> filter(id==1, Fit_Method=="Test Only", Model=="EXAM") |> pull(c) |> unique()

Subject 36

Code
indv_predictive_dist(post_dat_l,ind_fits_df, sbj=list(36))

Code
#abc_tables(post_dat_l |> filter(id==36))$et_sum |> gt::gt()

abc_tables(post_dat |> filter(id==36))$agg_full |> flextable() |> set_caption("Mean from full posterior")

condit

Model

Fit_Method

mean_error

Varied

ALM

Test

221.2

Varied

ALM

Test_Train

316.3

Varied

ALM

Train

268.5

Varied

EXAM

Test

214.2

Varied

EXAM

Test_Train

305.6

Varied

EXAM

Train

269.3

Code
abc_tables(post_dat |> filter(id==36))$agg_best |> flextable() |> set_caption("Mean from best parameters")

condit

Model

Fit_Method

mean_error

Varied

ALM

Test

216.3

Varied

ALM

Test_Train

315.9

Varied

ALM

Train

261.4

Varied

EXAM

Test

210.7

Varied

EXAM

Test_Train

296.2

Varied

EXAM

Train

268.4

Code
# plot_indv_posterior(ind_fits_df |> filter(id==1))
# ind_fits_df |> filter(id==1, Fit_Method=="Test Only", Model=="EXAM") |> pull(c) |> unique()

More Comparisons

Code
best_id_x <- post_dat |> group_by(id,condit,Model,Fit_Method,x) |>
  mutate(e2=(y-pred)) |>
  summarise(y=mean(y), pred=mean(pred), mean_error=mean(e2),abs_me=abs(mean_error)) |>
  group_by(id,condit,Fit_Method,x) |> mutate(best=ifelse(mean_error==min(mean_error),1,0)) |>
  group_by(id,condit,Fit_Method,Model) |> mutate(n_best=sum(best)) 

best_id <- best_id_x |> group_by(id,condit,Fit_Method,Model) |>
  summarise(mean_error=mean(mean_error), n_best=first(n_best),abs_me=mean(abs(mean_error)))
  
lowest_error_model <- best_id %>%
  group_by(id, condit,Fit_Method) %>%
  summarise(Best_Model = Model[which.min(mean_error)],
            n_best = n_best[which.min(mean_error)],
            Lowest_error = min(mean_error),
            differential = min(mean_error) - max(mean_error)) %>%
  ungroup()

error_difference <- best_id %>%
  select(id, condit, Model,Fit_Method, mean_error,abs_me) %>%
  pivot_wider(names_from = Model, values_from = c(abs_me,mean_error)) %>%
  mutate(Error_difference = (mean_error_ALM - mean_error_EXAM), abs_error_dif = (abs_me_ALM - abs_me_EXAM))

full_comparison <- lowest_error_model |> left_join(error_difference, by=c("id","condit","Fit_Method"))  |> 
  group_by(condit,Fit_Method,Best_Model) |> mutate(nGrp=n(), model_rank = nGrp - rank(Error_difference) ) |> 
  arrange(Fit_Method,-Error_difference)



best_id |> filter(Fit_Method=="Test_Train") |> 
  arrange(mean_error) |>
  ungroup() |>
  mutate(id = reorder(id, mean_error)) %>%
  ggplot(aes(y=id,x=mean_error,fill=Model))+
  geom_col()+
  ggh4x::facet_grid2(~condit,axes="all",scales="free_y", independent = "y")

Code
best_id |> filter(Fit_Method=="Test_Train") |> 
  ungroup() |>
  mutate(id = reorder(id, abs_me)) %>%
  ggplot(aes(y=id,x=abs_me,fill=Model))+
  geom_col()+
  ggh4x::facet_grid2(~condit,axes="all",scales="free_y", independent = "y")

Code
full_comparison |> filter(Fit_Method=="Test_Train") |> 
  ungroup() |>
  mutate(id = reorder(id, abs_error_dif)) %>%
  ggplot(aes(y=id,x=abs_error_dif))+
  geom_col()+
  ggh4x::facet_grid2(~condit,axes="all",scales="free_y", independent = "y")

Code
full_comparison |> filter(Fit_Method=="Test_Train") |> 
  ungroup() |>
  mutate(id = reorder(id, Error_difference)) %>%
  ggplot(aes(y=id,x=Error_difference))+
  geom_col()+
  ggh4x::facet_grid2(~condit,axes="all",scales="free_y", independent = "y")

Code
d <- testAvg |> left_join(full_comparison, by=c("id","condit")) |> filter(Fit_Method=="Test_Train")

d |> ggplot(aes(x=vb,y=dist,fill=condit)) + stat_bar + facet_wrap(Fit_Method~Best_Model,ncol=2)

Code
d |> 
  group_by(condit,Fit_Method,Best_Model) |> 
  mutate(nGrp2=n()) |>
  filter(abs(Error_difference)>15) |> 
  ggplot(aes(x=vb,y=dist,fill=condit)) + 
  stat_bar + facet_wrap(Fit_Method~Best_Model,ncol=2)

Code
d |> group_by(condit,Fit_Method,Best_Model) %>% tally() |> mutate(n=n/6)
# A tibble: 4 × 4
# Groups:   condit, Fit_Method [2]
  condit   Fit_Method Best_Model     n
  <fct>    <chr>      <chr>      <dbl>
1 Constant Test_Train ALM           31
2 Constant Test_Train EXAM          49
3 Varied   Test_Train ALM           46
4 Varied   Test_Train EXAM          30
Code
d |> group_by(condit,Fit_Method,Best_Model) |> filter(abs(Error_difference)>15) |> tally() |> mutate(n=n/6)
# A tibble: 4 × 4
# Groups:   condit, Fit_Method [2]
  condit   Fit_Method Best_Model     n
  <fct>    <chr>      <chr>      <dbl>
1 Constant Test_Train ALM           11
2 Constant Test_Train EXAM          35
3 Varied   Test_Train ALM           31
4 Varied   Test_Train EXAM          16
Code
d |> filter(bandInt==100) |> group_by(condit,Fit_Method) |> summarise(m=mean(Error_difference), 
                                              sd=sd(Error_difference), 
                                              n=n(),se=sd/sqrt(n))
# A tibble: 2 × 6
# Groups:   condit [2]
  condit   Fit_Method     m    sd     n    se
  <fct>    <chr>      <dbl> <dbl> <int> <dbl>
1 Constant Test_Train 27.6   59.3    80  6.63
2 Varied   Test_Train -8.98  54.6    76  6.27
Code
# head(map_id,2)
# # A tibble: 2 × 13
# # Groups:   id, condit, Model, Fit_Method [2]
#   id    condit Fit_Method Model          c    lr mean_error  rank nTest nTrain nTotal     n   BIC
#   <fct> <fct>  <chr>      <chr>      <dbl> <dbl>      <dbl> <int> <int>  <int>  <int> <int> <dbl>
# 1 1     Varied Test       ALM   0.00000165  5.32       238.     1    63     86    149    63 -6.80
# 2 1     Varied Test_Train ALM   0.0000634   5.60       276.     1    63     86    149   149 -6.24
Code
#full_comparison |> round_tibble(1) |> reactable(filterable=T,defaultPageSize=15)

ggplot(full_comparison, aes(x = Best_Model, fill = condit)) +
  geom_bar(position = "dodge") +
  facet_wrap(~Fit_Method)+
  labs(title = "Distribution of Subjects Best Fit by Model",
       x = "Model with Lowest Error",
       y = "Count of Subjects") +
  theme_minimal() +
  scale_fill_brewer(palette = "Set1")

Code
# Scatter plot showing the differential in mean error
ggplot(full_comparison, aes(x = Error_difference, y = condit, color = Best_Model)) +
  geom_point(alpha=.5) +
  stat_pointinterval()+
  facet_wrap(~Fit_Method)+
  labs(title = "Differential in Mean Error Between Models",
       x = "Error Difference (ALM - EXAM)",
       y = "Condition") +
  theme_minimal() +
  scale_color_brewer(palette = "Set1")

Code
# ggplot(full_comparison, aes(x = Error_difference)) +
#   geom_histogram(aes(fill=Best_Model),bins = 40, alpha = 0.7) +
#   geom_vline(aes(xintercept = mean(Error_difference)), color = "red", linetype = "dashed", size = 1) +
#   facet_wrap(~Fit_Method)+
#   labs(
#     title = "Distribution of Error Differential (ALM - EXAM)",
#     x = "Error Differential",
#     y = "Frequency"
#   ) +
#   theme_minimal()


best_exam <- full_comparison |> filter(Best_Model=="EXAM") 
best_alm <- full_comparison |> filter(Best_Model=="ALM") 


d <- testAvg |> left_join(full_comparison, by=c("id","condit"))

d |> ggplot(aes(x=vb,y=dist,fill=condit)) + stat_bar + facet_wrap(Fit_Method~Best_Model,ncol=2)

Code
d |> filter(abs(Error_difference)>20) |> 
  ggplot(aes(x=vb,y=dist,fill=condit)) + 
  stat_bar + facet_wrap(Fit_Method~Best_Model,ncol=2)

Code
d |> ggplot(aes(x=vb,y=vx,fill=condit)) + stat_bar + facet_wrap(Fit_Method~Best_Model,ncol=2)

Code
d |> filter(Fit_Method=="Test") |>
  ggplot(aes(x=Error_difference,y=dist,col=condit)) + geom_point() + facet_wrap(~vb,scales="free",ncol=2)

Code
d |> group_by(id,condit,Fit_Method) |> summarise(Error_difference=mean(Error_difference),dist=mean(dist)) |> 
  ggplot(aes(x=Error_difference,y=dist,col=condit)) + geom_point() + facet_wrap(~Fit_Method,scales="free")

Code
# ds |> filter(expMode2=="Test", id %in% best_exam$id) |> 
#   group_by(id,condit,x) |> summarise(y=mean(y), dist=mean(abs(x-y))) |>
#   mutate(x=as.factor(x)) |>
#   ggplot(aes(x=x,y=dist,fill=condit)) + stat_bar

Best Test - EXAM Fits

Code
strong_fits <- full_comparison |> filter(model_rank<=1)
strong_fits |> filter(Fit_Method=="Test",Best_Model=="EXAM") |> pull(id) %>% indv_predictive_plots(post_dat_l, .)

Best Test - ALM Fits

Code
strong_fits <- full_comparison |> filter(model_rank<=1)
strong_fits |> filter(Fit_Method=="Test",Best_Model=="ALM") |> pull(id) %>% indv_predictive_plots(post_dat_l, .)

Best Test_Train - EXAM Fits

Code
strong_fits <- full_comparison |> filter(model_rank<1)
strong_fits |> filter(Fit_Method=="Test_Train",Best_Model=="EXAM") |> pull(id) %>% indv_predictive_plots(post_dat_l, .)

Best Test_Train - ALM Fits

Code
strong_fits <- full_comparison |> filter(model_rank<=1)
strong_fits |> filter(Fit_Method=="Test_Train",Best_Model=="ALM") |> pull(id) %>% indv_predictive_plots(post_dat_l, .)

Best Train - EXAM Fits

Code
strong_fits <- full_comparison |> filter(model_rank<=1)
strong_fits |> filter(Fit_Method=="Train",Best_Model=="EXAM") |> pull(id) %>% indv_predictive_plots(post_dat_l, .)

Best Train - ALM Fits

Code
strong_fits <- full_comparison |> filter(model_rank<=1)
strong_fits |> filter(Fit_Method=="Train",Best_Model=="ALM") |> pull(id) %>% indv_predictive_plots(post_dat_l, .)

Learning Curves

Code
pd_train_l |> ggplot(aes(x=Block,y=dist,col=Resp))+
  geom_point( stat = "summary", fun = mean) + 
  stat_summary( geom = "line", fun = mean) +
  stat_summary(geom="errorbar",fun.data=mean_se,width=.1,alpha=.4) +
  #facet_wrap(condit~x,scales="free")
  ggh4x::facet_nested_wrap(Fit_Method~condit~x,scales="free") +
  #coord_cartesian(ylim = c(600, 1300)) +
  labs(title="Full Posterior")

Learning Curves
Code
pd_train_l |>filter(rank==1) |> ggplot(aes(x=Block,y=val,col=Resp))+
  geom_point( stat = "summary", fun = mean) + 
  stat_summary( geom = "line", fun = mean) +
  stat_summary(geom="errorbar",fun.data=mean_se,width=.1,alpha=.4) +
  #facet_wrap(condit~x,scales="free")
  ggh4x::facet_nested_wrap(Fit_Method~condit~x,scales="free") +
  coord_cartesian(ylim = c(600, 1300)) +labs(title="Best Parameters")

Learning Curves
Code
pd_train_l |> filter(Fit_Method=="Train") |> 
  mutate(x=as.factor(x), Resp=as.factor(Resp), Block=as.factor(Block)) |>
  ggplot(aes(x=x,y=val,fill=Block))+
  stat_bar +
  #facet_wrap(condit~x,scales="free")
  facet_wrap(condit~Resp,scales="free",ncol=3) +
  coord_cartesian(ylim = c(600, 1300))

Learning Curves
Code
# pd_train_l |> filter(id %in% c(1,33,66, 36,76), Fit_Method=="Train") |> 
#   ggplot(aes(x=Block,y=val,col=Resp))+
#   geom_point( stat = "summary", fun = mean) + 
#   stat_summary( geom = "line", fun = mean) +
#   stat_summary(geom="errorbar",fun.data=mean_se,width=.1,alpha=.4) +
#   #facet_wrap(condit~x,scales="free")
#   facet_wrap(id~x,scales="free",ncol=3) +
#   coord_cartesian(ylim = c(600, 1300))


pd_train_l |> 
  #filter(id %in% c(1,33,66, 36,76), Fit_Method=="Train") |> 
  mutate(x=as.factor(x), Resp=as.factor(Resp), Block=as.factor(Block)) |>
  ggplot(aes(x=x,y=dist,fill=Block))+
  stat_bar +
  #facet_wrap(condit~x,scales="free")
   #coord_cartesian(ylim = c(600, 1300)) +
  facet_wrap(id~Resp,scales="free",ncol=3) 

Learning Curves
Code
post_dat |> ggplot(aes(x=Model,y=mean_error,fill=Model))+geom_col()+facet_wrap(~Fit_Method)
post_dat |> ggplot(aes(x=Model,y=mean_error,fill=Model))+geom_col()+facet_wrap(condit~Fit_Method)
Code
# post_tabs <- map(post_tabs, ~cbind(.x,run_info) |> mutate(fn=file_name))
# post_tabs$agg_pred_full_train <- pt_train$agg_pred_full
# post_tabs$block_pred_full <- pt_train$block_pred_full
# post_tabs$block_pred_full_x <- pt_train$block_pred_full_x

# # path1 = "../../../data/abc_tabs"
# saveRDS(post_tabs, paste0(path1, "/", tools::file_path_sans_ext(basename(file_name)),"_post_tab", ".rds"))
# saveRDS(post_tabs, paste0(tools::file_path_sans_ext(basename(file_name)),"_post_tab", ".rds"))