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 [43]:
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 [11]:
store.close()
store = pd.HDFStore('ess.resamples.h5')
Make the country objects
In [12]:
reload(ess)
country_map = ess.make_countries(store)
For each resampled frame, run both models and store the results in the Country objects
In [15]:
keys = store.keys()
len(keys)
Out[15]:
In [16]:
reload(ess)
FORMULA1 = ('hasrelig_f ~ inwyr07_f + yrbrn60_f + yrbrn60_f2 + '
'edurank_f + hincrank_f +'
'tvtot_f + rdtot_f + nwsptot_f + netuse_f')
num = 101
ess.process_all_frames(store, country_map, num,
smf.logit, FORMULA1, model_num=1)
In [17]:
reload(ess)
FORMULA2 = ('rlgdgr_f ~ inwyr07_f + yrbrn60_f + yrbrn60_f2 + '
'edurank_f + hincrank_f +'
'tvtot_f + rdtot_f + nwsptot_f + netuse_f')
ess.process_all_frames(store, country_map, num,
smf.ols, FORMULA2, model_num=2)
In [18]:
store.close()
In [19]:
with open('ess4.pkl', 'wb') as fp:
pickle.dump(country_map, fp)
In [20]:
with open('ess4.pkl', 'rb') as fp:
country_map = pickle.load(fp)
In [72]:
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 = 'ess4.%2.2d' % plot_counter
thinkplot.Save(root=root, formats=['png'])
plot_counter += 1
Make a plot showing confidence interval for the given parameters
In [73]:
xlabel1 = 'Difference in percentage points of hasrelig'
xlabel2 = 'Difference in religiosity (0-10 scale)'
In [74]:
xlim = [-25, 15]
First let's check on the estimated parameters for the age variables.
In [75]:
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 almost every country, year born is associated with less religiosity.
In [76]:
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 [77]:
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 [78]:
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 [79]:
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 [80]:
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 [81]:
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 [82]:
t = ess.extract_ranges(country_map, 'netuse_f', 'hasrelig_f')
ess.plot_cis(t, PURPLE2)
thinkplot.Config(title='Internet use',
xlabel=xlabel1, xlim=xlim)
save_plot()
In [83]:
reload(ess)
cdfnames = ['yrbrn60_f', 'netuse_f', 'edurank_f', 'tvtot_f', 'hincrank_f',
'rdtot_f', 'nwsptot_f',
'inwyr07_f' ]
ess.plot_cdfs(country_map, ess.extract_ranges, cdfnames=cdfnames)
thinkplot.Config(xlabel='Difference in percentage points',
xlim=[-20, 10],
ylabel='CDF',
legend=True,
loc='upper left')
save_plot()
In [84]:
t = ess.extract_ranges(country_map, 'netuse_f', 'hasrelig_f')
ess.plot_scatter(t, BLUE)
thinkplot.Config(title='',
xlabel=xlabel1,
ylabel='Fraction affiliated',
xlim=[-10, 5], ylim=[0, 1])
save_plot()
In [85]:
t = ess.extract_ranges(country_map, 'netuse_f', 'rlgdgr_f')
ess.plot_scatter(t, BLUE)
thinkplot.Config(title='',
xlabel=xlabel1,
ylabel='Mean religiosity',
xlim=[-10, 5], ylim=[0, 7.5])
save_plot()
In [86]:
t = ess.extract_ranges(country_map, 'netuse_f', 'netuse_f')
ess.plot_scatter(t, BLUE)
thinkplot.Config(title='',
xlabel=xlabel1,
ylabel='Mean Internet use',
xlim=[-10, 5], ylim=[0, 7.5])
save_plot()
Make similar figures for the second model, with degree of religiosity as the dependent variable.
In [87]:
xlim = [-2.5, 1.0]
In [88]:
t = ess.extract_ranges2(country_map, 'yrbrn60_f', 'rlgdgr_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', 'rlgdgr_f')
ess.plot_cis(t, GREEN1)
thinkplot.Config(title='Education rank',
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, 'netuse_f', 'hasrelig_f')
ess.plot_cis(t, PURPLE2)
thinkplot.Config(title='Internet use',
xlabel=xlabel2,
xlim=xlim)
save_plot()
In [96]:
cdfnames = ['netuse_f', 'edurank_f', 'tvtot_f', 'hincrank_f',
'rdtot_f', 'nwsptot_f',
'inwyr07_f', 'yrbrn60_f']
ess.plot_cdfs(country_map, ess.extract_ranges2, cdfnames=cdfnames)
thinkplot.Config(xlabel=xlabel2,
xlim=[-2, 0.7],
ylabel='CDF',
loc='upper left')
save_plot()
Here's the scatter plot of effect size on rlgdgr versus mean value of rlgdgr
rlgdgr is on a 0 to 10 scale, so it is mildly astonishing that national means vary as much as they do, from 2.5 to 7.
In [97]:
t = ess.extract_ranges2(country_map, 'netuse_f', 'hasrelig_f')
ess.plot_scatter(t, BLUE)
thinkplot.Config(title='',
xlabel=xlabel2,
ylabel='Fraction affiliated',
xlim=[-2.5, 0.5], ylim=[0, 1]
)
save_plot()
In [98]:
t = ess.extract_ranges2(country_map, 'netuse_f', 'rlgdgr_f')
ess.plot_scatter(t, BLUE)
thinkplot.Config(title='',
xlabel=xlabel2,
ylabel='Mean religiosity',
xlim=[-2.5, 0.5], ylim=[0, 7.5]
)
save_plot()
In [99]:
t = ess.extract_ranges2(country_map, 'netuse_f', 'netuse_f')
ess.plot_scatter(t, PURPLE2)
thinkplot.Config(title='',
xlabel=xlabel2,
ylabel='Mean Internet use',
xlim=[-2.5, 0.5], ylim=[0, 7.5])
save_plot()
In [101]:
reload(ess)
varnames = ['inwyr07_f', 'yrbrn60_f', 'netuse_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 [102]:
ts = ess.make_table(country_map, varnames, ess.extract_ranges2)
ess.print_table(ts)
In [ ]:
In [ ]: