<jupyter_start><jupyter_text>Step 1: model the effects of GDP per capita on conflict rate<jupyter_code># Create a logistic regression for conflict rate using GDP per capita
first_order_gdp_logit = sm.Logit(conflict_data["country_involved_in_conflict"],sm.add_constant(conflict_data[["log2_interpolated_gdp_per_capita"]])).fit()
first_order_gdp_logit.summary()
# Create a logistic regression for conflict rate using second order polynomial GDP per capita
second_order_gdp_logit = sm.Logit(conflict_data["country_involved_in_conflict"],sm.add_constant(conflict_data[["log2_interpolated_gdp_per_capita","log2_interpolated_gdp_per_capita_squared"]])).fit()
second_order_gdp_logit.summary()
# Create a logistic regression for conflict rate using third order polynomial GDP per capita
third_order_gdp_logit = sm.Logit(conflict_data["country_involved_in_conflict"],sm.add_constant(conflict_data[["log2_interpolated_gdp_per_capita","log2_interpolated_gdp_per_capita_squared","log2_interpolated_gdp_per_capita_cubed"]])).fit()
third_order_gdp_logit.summary()
# Use these regressions to predict the conflict rate for each country in each year
conflict_data["first_order_gdp_predicted_conflict_rate"] = first_order_gdp_logit.predict()
conflict_data["second_order_gdp_predicted_conflict_rate"] = second_order_gdp_logit.predict()
conflict_data["third_order_gdp_predicted_conflict_rate"] = third_order_gdp_logit.predict()<jupyter_output><empty_output><jupyter_text>Step 2: model the effects of democracy on conflict rate, and produce combined model<jupyter_code># When modelling the effects of democracy on conflict, exclude countries with a "-11" code, which are mostly colonies
no_colony_conflict_data = conflict_data[conflict_data["country_democracy_scores"] > -11]
# Convert Polity scores to a scale of 0 to 20, then square and cube them
no_colony_conflict_data["rescaled_country_democracy_scores"] = no_colony_conflict_data["country_democracy_scores"] + 10
no_colony_conflict_data["rescaled_country_democracy_scores_squared"] = no_colony_conflict_data["rescaled_country_democracy_scores"] ** 2
no_colony_conflict_data["rescaled_country_democracy_scores_cubed"] = no_colony_conflict_data["rescaled_country_democracy_scores"] ** 3
# Create a logistic regression for conflict rate using democracy
first_order_democracy_logit = sm.Logit(no_colony_conflict_data["country_involved_in_conflict"],sm.add_constant(no_colony_conflict_data[["rescaled_country_democracy_scores"]])).fit()
first_order_democracy_logit.summary()
# Create a logistic regression for conflict rate using second order polynomial democracy
second_order_democracy_logit = sm.Logit(no_colony_conflict_data["country_involved_in_conflict"],sm.add_constant(no_colony_conflict_data[["rescaled_country_democracy_scores","rescaled_country_democracy_scores_squared"]])).fit()
second_order_democracy_logit.summary()
# Create a logistic regression for conflict rate using third order polynomial democracy
third_order_democracy_logit = sm.Logit(no_colony_conflict_data["country_involved_in_conflict"],sm.add_constant(no_colony_conflict_data[["rescaled_country_democracy_scores","rescaled_country_democracy_scores_squared","rescaled_country_democracy_scores_cubed"]])).fit()
third_order_democracy_logit.summary()
# Create a logistic regression for conflict rate using various polynomials for GDP per capita and democracy
combined_polynomial_logit = sm.Logit(no_colony_conflict_data["country_involved_in_conflict"],sm.add_constant(no_colony_conflict_data[["rescaled_country_democracy_scores","rescaled_country_democracy_scores_squared","rescaled_country_democracy_scores_cubed","log2_interpolated_gdp_per_capita","log2_interpolated_gdp_per_capita_squared"]])).fit()
combined_polynomial_logit.summary()
# Use these regressions to predict the conflict rate for each country in each year
no_colony_conflict_data["first_order_democracy_predicted_conflict_rate"] = first_order_democracy_logit.predict()
no_colony_conflict_data["second_order_democracy_predicted_conflict_rate"] = second_order_democracy_logit.predict()
no_colony_conflict_data["third_order_democracy_predicted_conflict_rate"] = third_order_democracy_logit.predict()
no_colony_conflict_data["combined_polynomial_predicted_conflict_rate"] = combined_polynomial_logit.predict()<jupyter_output><empty_output><jupyter_text>Step 3: join the predictions together, and export the data<jupyter_code>join_columns = ["Unnamed: 0","rescaled_country_democracy_scores","rescaled_country_democracy_scores_squared","rescaled_country_democracy_scores_cubed","first_order_democracy_predicted_conflict_rate","second_order_democracy_predicted_conflict_rate","third_order_democracy_predicted_conflict_rate","combined_polynomial_predicted_conflict_rate"]
# Join the democracy predictions to the dataframe with GDP per capita predictions 
conflict_data_combined = conflict_data.join(no_colony_conflict_data[join_columns],on="Unnamed: 0",rsuffix="joined")
conflict_data_combined.drop("Unnamed: 0joined",axis=1,inplace=True)
conflict_data_combined
conflict_data_combined.to_csv("output_data/regression_df.csv")<jupyter_output><empty_output><jupyter_text>Step 4: visualise the curves created by the regression<jupyter_code>fig,ax = plt.subplots(figsize=(7,5))
plt.title("Share of years 1900-2017 in which a country suffered >100 deaths",size=10)
plt.suptitle("Third-order polynomial for democracy, >100 deaths",size=12,weight="bold")
ax.scatter(conflict_data_combined["country_democracy_scores"],conflict_data_combined["third_order_democracy_predicted_conflict_rate"])
ax.set_xlabel("Polity score")
ax.set_ylabel("Chance of fighting a war in a year")
plt.show()
fig,ax = plt.subplots(figsize=(7,5))
plt.title("Share of years 1900-2017 in which a country suffered >100 deaths",size=10)
plt.suptitle("Third-order polynomial for wealth, >100 deaths",size=12,weight="bold")
ax.scatter(conflict_data_combined["log2_interpolated_gdp_per_capita"],conflict_data_combined["third_order_gdp_predicted_conflict_rate"],c="darkblue")
ax.set_xlim(7,17)
ax.set_xticks([i for i in range(7,18)])
ax.set_xticklabels([2**i for i in range(7,18)])
ax.set_xlabel("Log2 GDP per capita")
ax.set_ylabel("Chance of fighting a war in a year")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0886177556590143, 'fraction_numerical': 0.012682613581634292, 'mean_word_length': 13.387990762124712, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '120558', 'license': 'MIT', 'n_tokens_mistral': 2231, 'n_tokens_neox': 2063, 'n_words': 333, 'path': 'data/2018-11-03-war-fighting/war_regression_script.ipynb', 'repo_name': 'sondreus/graphic-detail-data', 'size': '6229'}