This notebook presents explorations of the association between Internet use and religion in Europe, using data from the European Social Survey (http://www.europeansocialsurvey.org).
Copyright 2015 Allen Downey
MIT License: http://opensource.org/licenses/MIT
In [36]:
from __future__ import print_function, division
import string
import random
import cPickle as pickle
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
import thinkstats2
import thinkplot
import matplotlib.pyplot as plt
import ess
# colors by colorbrewer2.org
BLUE1 = '#a6cee3'
BLUE2 = '#1f78b4'
GREEN1 = '#b2df8a'
GREEN2 = '#33a02c'
PINK = '#fb9a99'
RED = '#e31a1c'
ORANGE1 = '#fdbf6f'
ORANGE2 = '#ff7f00'
PURPLE1 = '#cab2d6'
PURPLE2 = '#6a3d9a'
YELLOW = '#ffff99'
BROWN = '#b15928'
%matplotlib inline
Open the store containing resampled DataFrames.
In [2]:
store = pd.HDFStore('ess.resamples.h5')
Make the country objects
In [3]:
country_map = ess.make_countries(store)
For each resampled frame, run both models and store the results in the Country objects
In [4]:
FORMULA1 = ('netuse_f ~ inwyr07_f + yrbrn60_f + yrbrn60_f2 + '
'edurank_f + hincrank_f +'
'tvtot_f + rdtot_f + nwsptot_f + hasrelig_f')
FORMULA2 = ('netuse_f ~ inwyr07_f + yrbrn60_f + yrbrn60_f2 + '
'edurank_f + hincrank_f +'
'tvtot_f + rdtot_f + nwsptot_f + rlgdgr_f')
In [5]:
num = 201
ess.process_all_frames(store, country_map, num,
smf.ols, FORMULA1, 1)
In [6]:
ess.process_all_frames(store, country_map, num,
smf.ols, FORMULA2, 2)
In [37]:
store.close()
In [7]:
with open('ess5.pkl', 'wb') as fp:
pickle.dump(country_map, fp)
In [34]:
with open('ess5.pkl', 'rb') as fp:
country_map = pickle.load(fp)
In [75]:
plot_counter = 1
def save_plot(flag=True):
"""Saves plots in png format.
flag: boolean, whether to save or not
"""
global plot_counter
if flag:
root = 'ess5.%2.2d' % plot_counter
thinkplot.Save(root=root, formats=['png'])
plot_counter += 1
Make a plot showing confidence interval of effect size for the given parameters
In [76]:
xlabel1 = 'Difference in level of Internet use (0-7 scale)'
xlabel2 = 'Difference in level of Internet use (0-7 scale)'
In [77]:
xlim = [-1.0, 3.5]
In [78]:
reload(ess)
t = ess.extract_ranges(country_map, 'yrbrn60_f', 'hasrelig_f')
ess.plot_cis(t, GREEN2)
thinkplot.Config(title='Year born',
xlabel=xlabel1, xlim=xlim)
save_plot()
In [79]:
t = ess.extract_ranges(country_map, 'inwyr07_f', 'hasrelig_f')
ess.plot_cis(t, GREEN1)
thinkplot.Config(title='Interview year',
xlabel=xlabel1, xlim=xlim)
save_plot()
In [80]:
t = ess.extract_ranges(country_map, 'edurank_f', 'hasrelig_f')
ess.plot_cis(t, ORANGE2)
thinkplot.Config(title='Education (relative rank)',
xlabel=xlabel1, xlim=xlim)
save_plot()
In [81]:
t = ess.extract_ranges(country_map, 'hincrank_f', 'hasrelig_f')
ess.plot_cis(t, ORANGE1)
thinkplot.Config(title='Income (relative rank)',
xlabel=xlabel1, xlim=xlim)
save_plot()
In [82]:
t = ess.extract_ranges(country_map, 'tvtot_f', 'hasrelig_f')
ess.plot_cis(t, RED)
thinkplot.Config(title='Television watching',
xlabel=xlabel1, xlim=xlim)
save_plot()
In [83]:
t = ess.extract_ranges(country_map, 'rdtot_f', 'hasrelig_f')
ess.plot_cis(t, BLUE1)
thinkplot.Config(title='Radio listening',
xlabel=xlabel1, xlim=xlim)
save_plot()
In [84]:
t = ess.extract_ranges(country_map, 'nwsptot_f', 'hasrelig_f')
ess.plot_cis(t, BLUE2)
thinkplot.Config(title='Newspaper reading',
xlabel=xlabel1, xlim=xlim)
save_plot()
In [85]:
reload(ess)
t = ess.extract_ranges(country_map, 'hasrelig_f', 'hasrelig_f')
ess.plot_cis(t, BROWN)
thinkplot.Config(title='Religious affiliation',
xlabel=xlabel1, xlim=xlim)
save_plot()
Plot the CDFs of effect size across countries
In [86]:
reload(ess)
cdfnames = ['tvtot_f', 'hasrelig_f', 'rdtot_f',
'nwsptot_f', 'hincrank_f', 'edurank_f',
'inwyr07_f', 'yrbrn60_f']
ess.plot_cdfs(country_map, ess.extract_ranges, cdfnames=cdfnames)
thinkplot.Config(xlabel=xlabel1,
xlim=xlim,
legend=True,
loc='lower right')
save_plot()
Make similar figures for the second model.
In [87]:
xlim = [-1.5, 3.5]
In [88]:
t = ess.extract_ranges2(country_map, 'yrbrn60_f', 'hasrelig_f')
ess.plot_cis(t, GREEN2)
thinkplot.Config(title='Year born',
xlabel=xlabel2, xlim=xlim)
save_plot()
In [89]:
t = ess.extract_ranges2(country_map, 'inwyr07_f', 'hasrelig_f')
ess.plot_cis(t, GREEN1)
thinkplot.Config(title='Interview year',
xlabel=xlabel2, xlim=xlim)
save_plot()
In [90]:
t = ess.extract_ranges2(country_map, 'edurank_f', 'rlgdgr_f')
ess.plot_cis(t, ORANGE2)
thinkplot.Config(title='Education rank',
xlabel=xlabel2,
xlim=xlim)
save_plot()
In [91]:
t = ess.extract_ranges2(country_map, 'hincrank_f', 'hasrelig_f')
ess.plot_cis(t, ORANGE1)
thinkplot.Config(title='Income rank',
xlabel=xlabel2,
xlim=xlim)
save_plot()
In [92]:
t = ess.extract_ranges2(country_map, 'tvtot_f', 'hasrelig_f')
ess.plot_cis(t, RED)
thinkplot.Config(title='Television watching',
xlabel=xlabel2,
xlim=xlim)
save_plot()
In [93]:
t = ess.extract_ranges2(country_map, 'rdtot_f', 'hasrelig_f')
ess.plot_cis(t, BLUE1)
thinkplot.Config(title='Radio listening',
xlabel=xlabel2,
xlim=xlim)
save_plot()
In [94]:
t = ess.extract_ranges2(country_map, 'nwsptot_f', 'hasrelig_f')
ess.plot_cis(t, BLUE2)
thinkplot.Config(title='Newspaper reading',
xlabel=xlabel2,
xlim=xlim)
save_plot()
In [95]:
t = ess.extract_ranges2(country_map, 'rlgdgr_f', 'hasrelig_f')
ess.plot_cis(t, BROWN)
thinkplot.Config(title='Degree of religiosity',
xlabel=xlabel2,
xlim=xlim)
save_plot()
In [96]:
reload(ess)
cdfnames = ['tvtot_f', 'rlgdgr_f', 'rdtot_f',
'nwsptot_f', 'hincrank_f', 'edurank_f',
'inwyr07_f', 'yrbrn60_f']
ess.plot_cdfs(country_map, ess.extract_ranges2, cdfnames=cdfnames)
thinkplot.Config(xlabel=xlabel2,
xlim=xlim,
ylabel='CDF',
loc='lower right')
save_plot()
In [97]:
reload(ess)
varnames = ['inwyr07_f', 'yrbrn60_f', 'hasrelig_f', 'edurank_f',
'tvtot_f', 'hincrank_f', 'rdtot_f', 'nwsptot_f']
ts = ess.make_table(country_map, varnames, ess.extract_ranges)
ess.print_table(ts)
In [98]:
varnames = ['inwyr07_f', 'yrbrn60_f', 'rlgdgr_f', 'edurank_f',
'tvtot_f', 'hincrank_f', 'rdtot_f', 'nwsptot_f']
ts = ess.make_table(country_map, varnames, ess.extract_ranges2)
ess.print_table(ts)
In [ ]:
In [ ]: