### Main R Script for codes in folder "Codes/02_GenAI_exposure_by_internet_access"

# Cleaning workspace 
rm(list= ls())

# Paths -------------------------------------------------------------------

# Set package path ---

# Replace "your/path/to/Reproducibility_package" with the actual path to your package.

root_dir <- "C:/Users/wb519501/OneDrive - WBG/Desktop/RR_WLD_2025_528/files upload/v3/Reproducibility_package"

data_dir <- file.path(root_dir, "Data", "Raw")

cleaned_data_dir <- file.path(root_dir, "Data", "Cleaned")

graphs_dir <- file.path(root_dir, "Outputs")

# Libraries ---------------------------------------------------------------

install.packages("pacman")

require(pacman)

p_load(dplyr,            # For data manipulation
       tidyr,            # For reshaping data
       purrr,            # For iterating results into a df  
       haven,            # For reading .dta files  
       stats,            # For dropping rows with NAs
       fastDummies,      # For creating dummy variables
       WDI,              # For downloading World Development Indicators
       wbstats,          # # For downloading WB data/indicators
       readxl,           # For reading .xlsx files
       forcats,          # For reordering factor levels  
       rsample,          # For creating train/test splits
       hardhat,          # For specifying observation-level importance weights in modeling workflows
       tidymodels,       # For modeling and statistical analysis
       yardstick,        # For extracting metric coordinates
       doParallel,       # For registering a parallel backend to run tasks concurrently 
       pROC,             # For extracting threshold-related coordinates from ROC curves
       stringr,          # For string manipulation
       FNN,              # For fast k-nearest-neighbors
       xtable,           # For converting R objects into LaTeX or HTML tables
       ggplot2,          # For plots (base package)
       ggthemes,         # Includes the theme used in the graphs
       ggpubr,           # For arranging multiple ggplots into a single figure
       scales,           # For formatting numbers as percentages
       countrycode,      # For treating country names and codes 
       openxlsx,         # For creating Excel workbooks programmatically     
       Hmisc             # For weighted statistical estimates
       )

# R scripts ---------------------------------------------------------------

source(file.path(root_dir, "Codes", "02_GenAI_exposure_by_internet_access", "01-processing-data-with-internet.R"))

source(file.path(root_dir, "Codes", "02_GenAI_exposure_by_internet_access", "02-processing-data-without-internet.R"))

source(file.path(root_dir, "Codes", "02_GenAI_exposure_by_internet_access", "03-models-estimation.R"))

source(file.path(root_dir, "Codes", "02_GenAI_exposure_by_internet_access", "04-estimating-avg-exposure-gradients.R"))

source(file.path(root_dir, "Codes", "02_GenAI_exposure_by_internet_access", "05-knn-closest_match(internet-exercise).R"))

source(file.path(root_dir, "Codes", "02_GenAI_exposure_by_internet_access", "06-graphs.R"))
