Setup
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
#chart_studio.tools.set_credentials_file(username='econdata777', api_key='ADDhere')
import statsmodels.api as sm
import statsmodels.formula.api as smf
import warnings
#warnings.filterwarnings('ignore')
Import data
df1 = pd.read_csv("https://raw.githubusercontent.com/quarcs-lab/mendez2020-convergence-clubs-code-data/master/assets/dat.csv")
#df1 = pd.read_csv("https://raw.githubusercontent.com/quarcs-lab/mendez2020-convergence-clubs-code-data/master/assets/dat.csv", parse_dates =['year'])
df1
Dataset definitions
# Import definitions of dataset
df1_def = pd.read_csv("https://raw.githubusercontent.com/quarcs-lab/mendez2020-convergence-clubs-code-data/master//assets/dat-definitions.csv")
df1_def
Select African countries
afri =df1.query("region=='Africa'")
afri
Descriptive statistics
afri.describe().round(2)
Prepare data
query
afri_2008 = afri[['country', 'region', 'hi1990', 'year', 'GDPpc', 'h', 'ky', 'TFP']].query("year == 2008")
afri_2008
afri_ghana = afri[['country', 'year', 'GDPpc', 'h', 'ky', 'TFP']].query("country == 'Ghana'")
afri_ghana
afri2 = afri[['country', 'year', 'h']].query("country==['Ghana', 'Nigeria','South Africa'] and year>=2005 & year<=2012")
afri2
Visualize data
Strip plot
px.strip(afri_2008, x = 'GDPpc', hover_name= 'country')
fig1 = px.strip(afri_2008, x = 'GDPpc', hover_name= 'country')
#save2cs.plot(fig1, filename = 'figureName', auto_open=True)
Strip All Countries
px.strip(afri.query("region=='Africa'"), x = 'year', y = 'GDPpc', hover_name= 'country', color = 'country')
px.strip(df1,
x = 'GDPpc',
y = 'region',
hover_name= 'country',
hover_data= ['h', 'ky'],
color= 'region',
animation_frame= 'year')
Line plots
px.line(afri, x='year', y='log_lp', color='country')
Years of schooling
px.line(afri, x='year', y='s', color='country')
px.line(df1, x='year', y='log_lp', color='country', facet_col= 'region', facet_col_wrap= 2, height= 800)
px.line(afri.query("country=='Ghana'"), x="year", y="log_lp")
px.line(
df1.query("country==['Ghana', 'Nigeria','South Africa', 'Algeria', 'Egypt', 'Tunisia']"),
x="year",
y="log_lp",
color="country"
)
px.line(
df1.query("region=='Africa'"),
x="year",
y="log_lp",
color="country"
)
Treemap plot
px.treemap(df1.query("year == 2008"), color = "log_lp", values = "pop", path = ["region", "country"], hover_name = "country")
Sunburst plot
px.sunburst(df1.query("year == 2008"), color = "log_lp", values = "pop", path = ["region", "country"], hover_name = "country")
Scatter plots
Simple
px.scatter(
df1,
x="log_h",
y="log_lp",
color="region",
hover_name="country",
animation_frame = 'year'
)
Regression
px.scatter(
df1.query("year == 2008"),
x="log_h",
y="log_lp",
color="hi1990",
hover_name="country",
hover_data= ['region'],
trendline="ols",
trendline_scope="overall"
)
Multivariate
px.scatter(
afri.query("year == 2008"),
x="log_h",
y="log_lp",
color="country",
size="pop", size_max=60,
hover_name="country",
labels=dict(log_h="Human capital index in 2008 (in logs)",
log_lp="Labor productivity in 2008 (in logs)",
region="Africa",
pop= "Population")
)
Customized
fig = px.scatter(df1.query("year == 2008"),
y="log_lp",
x="log_h",
log_x = False,
color = "country",
size ="pop", size_max=60,
hover_name = "country",
height =500, width=800,
template = "simple_white",
color_discrete_sequence=px.colors.qualitative.G10,
#title = "Year 1990",
labels=dict(region = "Africa",
pop = "Population",
log_lp = "Labor productivity (in logs)",
log_h = "Human capital index (in logs)")
)
fig.update_layout(font_family = "Rockwell",
legend=dict(orientation = "h", title="", y=1.1, x=1, xanchor="right", yanchor="bottom"))
fig.add_hline(df1.query("year == 2008")['log_lp'].mean(), line_width=1, line_dash="dot")
fig.add_vline(df1.query("year == 2008")['log_h'].mean(), line_width=1, line_dash="dot")
fig.show()
px.scatter(df1,
animation_frame="year",
x="log_h",
y="log_lp",
range_x= [0.2, 1.62],
range_y= [7, 12.2],
color="country",
size="pop", size_max=60,
hover_name="country",
hover_data = ['log_ky', 'log_tfp'],
labels=dict(log_h="Human capital index (in logs)",
log_lp="Labor productivity (in logs)",
region="Africa",
pop= "Population")
)
px.scatter(
df1,
x="log_h",
y="log_lp",
color="region",
size="pop", size_max=60,
trendline="ols",
hover_name="country",
facet_col="hi1990",
animation_frame="year"
)
px.density_contour(
afri,
x="log_h",
y="log_lp",
hover_name="country",
marginal_x="histogram",
marginal_y="histogram",
animation_frame="year"
)
px.density_contour(
afri,
x="log_h",
y="log_lp",
marginal_x="box",
marginal_y="box",
animation_frame="year"
)
px.density_contour(
afri,
x="log_h",
y="log_lp",
hover_name="country",
marginal_x="rug",
marginal_y="rug",
animation_frame="year"
)
den = px.density_contour(
afri.query("year == 2014"),
x="log_h",
y="log_lp",
)
den.update_traces(contours_coloring="fill", contours_showlabels = True)
den.show()
den = px.density_contour(
afri.query("year == 2014"),
x="log_h",
y="log_lp",
)
den.update_traces(contours_coloring="fill", contours_showlabels = True, colorscale = 'Viridis')
den.show()
3D
px.scatter_3d(
df1.query("year == 2008"),
x="log_h",
y="log_lp",
z= "pop",
color="region",
hover_name="country"
)
px.choropleth(
afri.query("year == 2008"),
locations="isocode",
color="log_lp",
hover_name="country",
color_continuous_scale=px.colors.sequential.Plasma,
projection="natural earth")
px.choropleth(
afri,
locations="isocode",
color="log_lp",
hover_name="country",
animation_frame="year",
color_continuous_scale=px.colors.sequential.Plasma,
projection="natural earth")