echo "=================================================================================================="
echo Initializing path to code for all script files, please run this script in the /Simulations/ folder
echo "=================================================================================================="

echo Initializing data generation scripts in /Scripts/Data_generation/
echo "..."
sed -i "s|/home/corentin/Documents/These/Recherche/Simulations|$PWD|g" /Scripts/Data_generation/DIF/*
sed -i "s|/home/corentin/Documents/These/Recherche/Simulations|$PWD|g" /Scripts/Data_generation/NoDIF/*
echo DONE

echo Initializing analysis scripts in /Scripts/Analysis/
echo "..."
sed -i "s|/home/corentin/Documents/These/Recherche/Simulations|$PWD|g" /Scripts/Analysis/DIF/*
sed -i "s|/home/corentin/Documents/These/Recherche/Simulations|$PWD|g" /Scripts/Analysis/DIF-RESIDUALS/*
sed -i "s|/home/corentin/Documents/These/Recherche/Simulations|$PWD|g" /Scripts/Analysis/DIF-ROSALI/*
echo DONE

echo Initializing analysis scripts in /RProject/Scripts/
echo "..."
sed -i "s|/home/corentin/Documents/These/Recherche/Simulations|$PWD|g" /RProject/Scripts/*
echo DONE

echo Done initializing code path, running code


echo "=================================================================================================="
echo Installing R package dependencies
echo "=================================================================================================="

Rscript -e 'install.packages("TAM", repos="https://cloud.r-project.org")'
Rscript -e 'install.packages("doMC", repos="https://cloud.r-project.org")'
Rscript -e 'install.packages("parallel", repos="https://cloud.r-project.org")'
Rscript -e 'install.packages("pbmcapply", repos="https://cloud.r-project.org")'
Rscript -e 'install.packages("funprog", repos="https://cloud.r-project.org")'
Rscript -e 'install.packages("parallel", repos="https://cloud.r-project.org")'
Rscript -e 'install.packages("pbmcapply", repos="https://cloud.r-project.org")'
Rscript -e 'install.packages("funprog", repos="https://cloud.r-project.org")'
Rscript -e 'install.packages("plyr", repos="https://cloud.r-project.org")'
Rscript -e 'install.packages("dplyr", repos="https://cloud.r-project.org")'
Rscript -e 'install.packages("readxl", repos="https://cloud.r-project.org")'

echo "=================================================================================================="
echo Running data generation
echo "=================================================================================================="

echo "--- Generating no DIF data ---"
echo "N=50"
echo "..."
stata -b run /Scripts/Data_generation/NoDIF/scenarios_noDIF_baseline_50.do
echo DONE

echo "N=100"
echo "..."
stata -b run /Scripts/Data_generation/NoDIF/scenarios_noDIF_baseline_100.do
echo DONE

echo "N=200"
echo "..."
stata -b run /Scripts/Data_generation/NoDIF/scenarios_noDIF_baseline_200.do
echo DONE

echo "N=300"
echo "..."
stata -b run /Scripts/Data_generation/NoDIF/scenarios_noDIF_baseline_300.do
echo DONE

echo "--- Generating DIF data ---"
echo "N=50 / J=4"
echo "..."
stata -b run /Scripts/Data_generation/DIF/scenarios_DIF_J4_baseline_50.do
echo DONE

echo "N=100 / J=4"
echo "..."
stata -b run /Scripts/Data_generation/DIF/scenarios_DIF_J4_baseline_100.do
echo DONE

echo "N=200 / J=4"
echo "..."
stata -b run /Scripts/Data_generation/DIF/scenarios_DIF_J4_baseline_200.do
echo DONE

echo "N=300 / J=4"
echo "..."
stata -b run /Scripts/Data_generation/DIF/scenarios_DIF_J4_baseline_300.do
echo DONE

echo "N=50 / J=7"
echo "..."
stata -b run /Scripts/Data_generation/DIF/scenarios_DIF_J7_baseline_50.do
echo DONE

echo "N=100 / J=7"
echo "..."
stata -b run /Scripts/Data_generation/DIF/scenarios_DIF_J7_baseline_100.do
echo DONE

echo "N=200 / J=7"
echo "..."
stata -b run /Scripts/Data_generation/DIF/scenarios_DIF_J7_baseline_200.do
echo DONE

echo "N=300 / J=7"
echo "..."
stata -b run /Scripts/Data_generation/DIF/scenarios_DIF_J7_baseline_300.do
echo DONE



echo "=================================================================================================="
echo Running no DIF PCM analysis
echo "=================================================================================================="

cd RProject/Scripts/Analysis

echo "..."
Rscript pcm_nodif.R
echo DONE

cd -


echo "=================================================================================================="
echo Running DIF PCM analysis
echo "=================================================================================================="

echo "N=50 / J=4"
echo "..."
stata -b run /Scripts/Analysis/DIF/pcm_dif_50_J4.do
echo DONE

echo "N=50 / J=7"
echo "..."
stata -b run /Scripts/Analysis/DIF/pcm_dif_50_J7.do
echo DONE

echo "All others"
echo "WARNING: This will take a few hours."
echo "..."
stata -b run /Scripts/Analysis/DIF/pcm_dif.do
echo DONE


echo "=================================================================================================="
echo Running ROSALI DIF PCM analysis
echo "=================================================================================================="

echo "WARNING: This will take a long time (multiple weeks). Please modify the code to run multiple simultaneous sessions of stata to compute faster if necessary."
echo "..."
stata -b run /Scripts/Analysis/DIF-ROSALI/pcm_dif_rosali.do
echo DONE


echo "=================================================================================================="
echo Running RESIDUALS DIF PCM analysis
echo "=================================================================================================="

cd RProject/Scripts/Analysis

echo "Performing DIF detection"
echo "WARNING: This will take a few hours."
echo "..."
Rscript resali_analysis.R
echo DONE

cd -

echo "Fitting PCM models"
echo "WARNING: This will take a few hours."
echo "..."
stata -b run /Scripts/Analysis/DIF-RESIDUALS/pcm_dif_residus.do
echo DONE

echo "=================================================================================================="
echo END OF SCRIPT
echo "=================================================================================================="

echo "Run /RProject/Scripts/Analysis/aggregation.R to aggregate and access results"
