library(metafor)
library(dplyr)
library(ggplot2)
# source("./Utilities-Main.R")

### Examine publication bias
### regs: Results for Egger's test
### tf: Results for Trim and Fill analyses
### Note that personality should be coded in OCEAN order.
TF_analysis <- function(input, valueX, valueY, IV_name) {
  if (missing(valueX)) {
    valueX <- mean(input$rxx, na.rm = TRUE)
  }
  if (missing(valueY)) {
    valueY <- mean(input$ryy, na.rm = TRUE)
  }
  
  regs <- list(O = NULL, C = NULL, E = NULL, A = NULL, N = NULL)
  tfs <- list(O = NULL, C = NULL, E = NULL, A = NULL, N = NULL)
  
  ### In OCEAN sequence. O = 1, C = 2, E = 3, A = 4, N = 5.
  for (i in 1:5) {
    regs[[i]] = regtest(calculation(input %>% filter(get(IV_name) == i))$result)
    tfs[[i]] <- trimfill(calculation(input %>% filter(get(IV_name) == i))$result)
  }
  return(list(regs = regs, tfs = tfs))
}

### Examine publication bias
### regs: Results for Egger's test
### tf: Results for Trim and Fill analyses
### Note that personality should be coded in OCXAEH order. (OCEAN + Honesty-Humility)
TF_analysis_HEX <- function(input, valueX, valueY, IV_name) {
  if (missing(valueX)) {
    valueX <- mean(input$rxx, na.rm = TRUE)
  }
  if (missing(valueY)) {
    valueY <- mean(input$ryy, na.rm = TRUE)
  }
  
  regs <- list(O = NULL, C = NULL, X = NULL, A = NULL, E = NULL, H = NULL)
  tfs <- list(O = NULL, C = NULL, X = NULL, A = NULL, E = NULL, H = NULL)
  
  
  for (i in 1:6) {
    df <- input %>% filter(get(IV_name) == i)
    regs[[i]] = regtest(calculation(df)$result)
    tfs[[i]] <- trimfill(calculation(df)$result)
  }
  return(list(regs = regs, tfs = tfs))
}
