This repository provides all the materials necessary to replicate our study "Reciprocity as a social strategy evolves more readily under conflict rather than cooperation" by A. Romano, A.S. Saral and C.K.W. De Dreu.
The repository contains the following items:
code/simulation
: This directory contains the simulation code implemented in Python 3.code/analysis
: This directory contains the code for the analysis and plots written in R.output/
: This directory will contain the output for the simulation (data).plots/
: This directory will contain the plots generated by R.
1. Simulate the study on your machine
You can reproduce our results by running the simulation code on your machine. Please note that running the simulation code with every combination in the paper may takes considerable time. (Several days on a standard machine with eight cores). However, you can still run it with fewer iterations and without the robustness check parameters. By default, the parameters in this repository are a reduced version. You can find those parameters and change them in code/simulation/simulate.py
and comment on the full parameter set.
2. Use Our Precomputed Data We also provide the precomputed data that we used in our study, which you can download from the following link (805 MB in total): https://www.dropbox.com/sh/zu6czzbl1jiscmq/AACE0-A3NiDN2Q98zk9R6QQ9a?dl=0
3. Just demo the analysis/plots We already have a small simulation data in the repository for demonstration. You can test the analysis code by producing the plots using this small data.
To reproduce the analysis and plots presented in our study, you should run the R scripts provided in the code/
directory. These scripts use the data generated by the simulation code or the precomputed data, depending on your chosen option. Note that you must install the necessary R packages listed in the code/requirements.R
file before running the analysis scripts. The resulting plots and analysis will be saved in the plots/
directory.
1- Clone the repository
git clone https://github.com/aseyq/reciprocity_under_conflict.git
2- Download .parquet
files from the Dropbox link (805 MB) and extract the .parquet
files (df_aggregate_payoffs.parquet
, df_coop.parquet
, df_long.parquet
) in the folder output/processeddata
https://www.dropbox.com/sh/zu6czzbl1jiscmq/AACE0-A3NiDN2Q98zk9R6QQ9a?dl=0 Overwrite the files that are already there.
Alternatively, you can use wget to download the files using the command line to output/processeddata
:
cd reciprocity_under_conflict
cd output/processeddata
Remove the demo files that are already there.
rm *.parquet
Download the files
wget "https://www.dropbox.com/scl/fi/62bn7bziqjq5k8spocf96/df_aggregate_payoffs.parquet?rlkey=bg5k5yddm3ln5ceza1c3y8qxk&dl=1" -O df_aggregate_payoffs.parquet
wget "https://www.dropbox.com/scl/fi/ob4bba2s41ao015l9cf4d/df_coop.parquet?rlkey=f5pbpps68i1u4lmzvm2qzpcib&dl=1" -O df_coop.parquet
wget "https://www.dropbox.com/scl/fi/z0ninjglk72kh4jiz0ds9/df_long.parquet?rlkey=y0zbhe0m37o0dr0nosmoevd1i&dl=1" -O df_long.parquet
3- Install the required R packages (if necessary)
# (go to the main folder `reciprocity_under_conflict` if you are not)
cd code/analysis
RScript requirements.R
4- Run the following scripts
- Plots in the paper: `paper_plots
- SI:
si_plots.R
,si_basefit.R
,si_moran.R
Rscript paper_plots.R
Rscript si_plots.R
Rscript si_basefit.R
Rscript si_moran.R
1- Clone the repository
git clone https://github.com/aseyq/reciprocity_under_conflict.git
2- Go to the downloaded folder
cd reciprocity_under_conflict
3- (Optional but recommended) Create a Python virtual environment. This will let you install packages only in the virtual environment in an isolated manner.
python3 -m venv venv
and activate it
(MacOS and Ubuntu)
source venv/bin/activate
(Windows)
venv\Scripts\activate
4- Install required packages
cd code/simulation
pip install -r requirements.txt
5- Run simulations
The paper uses 1000 generations and 1000 iterations, while the reproduction code has 300 generations and 12 iterations. This is sufficient to test to code but not to replicate the results. You set these numbers closer to the original number of generations and iterations by modifying lines 23 and 24 in simulate.py
.
Please note that it might take some time, even with the default reduced number of parameters. With the computer we tested, it takes about 17 minutes. We run the study in a HPC cluster with multiple cores over 75 hours, but one should expect to wait 80-240 hours for the complete simulations to run.
python3 simulate.py
6 - Install the required R packages (if necessary)
# (go to the main folder `reciprocity_under_conflict` if you are not)
cd ../analysis
Rscript requirements.R
7- Go to code/analysis
folder and combine CSV files created by the simulation (might take a couple of minutes)
cd ../analysis
Rscript 1_combine_csvs.R
This should combine the CSV files generated by simulations, and you should have combined_data.csv
in output/combineddata
folder.
7- Convert CSV files to a parquet file
Rscript 2_convert_parquet.R
This should convert CSV file to parquet data and create combined.parquet
in output/combineddata
folder.
8- Reshape data into a long format and generate additional tables (might take a couple of minutes)
Rscript 3_reshape_data.R
This should create df_aggregate_payoffs.parquet
, df_coop.parquet
and df_long.parquetin the processed data folder
output/processeddata. Then you can run the scripts to regenerate the analyses in the paper. At this point, you can delete the previous data generated and keep only the data in
processeddata` folder.
9- Go to code/analysis
folder and execute the following scripts to create plots in plots/
folder. (Takes a few minutes)
- Plots in the paper:
paper_plots.R
Rscript paper_plots.R
- Plots in Supplementary Information:
si_plots.R
,si_basefit.R
,si_moran.R
(Note that you should have also simulated additional parameter set of robustness checks.
Rscript si_plots.R
Rscript si_basefit.R
Rscript si_moran.R
1- Clone the repository
git clone https://github.com/aseyq/reciprocity_under_conflict.git
2 - Install the required R packages (if necessary)
# (go to the main folder `reciprocity_under_conflict` if you are not)
cd ../analysis
Rscript requirements.R
3- Execute the following scripts to create demo plots in plots/
folder.
- Plots in the paper:
paper_plots.R
Rscript paper_plots.R
Any modern personal computer or server that supports Windows, MacOS, or Linux operating system with enough RAM (at least 4 GB recommended.) The original simulations take a long time and generate data around 2GBs. Some operations, like combining the data files after the simulations, might require larger RAM depending on the number of simulation parameters. But if you once converted it to parquet (or downloaded the parquet
files we provided), you wouldn't need to use memory as big as the dataset as it uses Apache arrow (arrow) to solve this issue.
We tested this repository with the following machine: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz Processor 32,0 GB RAM
You need to have Python 3 (used 3.8) for simulations and R (used 4.2.3) for the analysis.
Additionally, we used the following packages (and their dependencies), which can be installed automatically using requirements files in the repository:
Python packages:
- Numpy 1.22
- Pandas 2.0.3
- joblib 1.3.1
- Pytest 6.2.5
R packages
- dplyr (1.1.0)
- purr (1.0.0)
- forcats (1.0.0)
- string (1.5.0)
We tested this repository on Windows 11 and Ubuntu 18 Linux operating systems.
If you use our materials for your own research, please cite our original paper using the following reference:
[TBA]