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
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.
store.close()
store = pd.HDFStore('ess.resamples.h5')
Make the country objects
reload(ess)
country_map = ess.make_countries(store)
Austria Belgium Bulgaria Switzerland Cyprus Czech Rep Germany Denmark Estonia Spain Finland France UK Greece Croatia Hungary Ireland Israel Iceland Italy Lithuania Luxembourg Latvia Netherlands Norway Poland Portugal Romania Russia Sweden Slovenia Slovakia Turkey Ukraine
For each resampled frame, run both models and store the results in the Country objects
keys = store.keys()
len(keys)
204
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)
0 /AAVZWa 1 /ADbUvD 2 /AJEDdF 3 /AOacJP 4 /AsSyrK 5 /BIXejR 6 /Blwttj 7 /BytXnJ 8 /CuiQgF 9 /CxkVBv 10 /DOKcxz 11 /DSSzPM 12 /DdpHTg 13 /EBHNWn 14 /EHuhuk 15 /EIaigX 16 /EOOBpB 17 /EdeAYH 18 /EiftYh 19 /EoHBcy 20 /Evkitq 21 /FJboqX 22 /FWawby 23 /GIKXkG 24 /GPBBMj 25 /GYhuaT 26 /GdTLTY 27 /GeUlsB 28 /GeolrR 29 /GkMwBV 30 /GownbC 31 /GrCTmE 32 /HGSBFA 33 /HemGKU 34 /HujYDN 35 /IKLjEu 36 /IORbkE 37 /IXYMov 38 /InEXbB 39 /JKBolS 40 /JVSJPq 41 /JofMZK 42 /JomohW 43 /JznRlw 44 /KEthFz 45 /KFwczR 46 /KUVnJc 47 /KnKXTR 48 /KuGUhG 49 /KudtCP 50 /LaUmLC 51 /LissvE 52 /LmraEV 53 /MCmopN 54 /MIdmWa 55 /MgSdJx 56 /NJjQrX 57 /NfzPAX 58 /OJZEtt 59 /Oaksmf 60 /OdhAjf 61 /PJETsk 62 /PXxSpS 63 /PiWfGA 64 /PptHII 65 /PvfGpy 66 /QTTYTa 67 /QbhbQt 68 /QoHLXF 69 /QskeUe 70 /QtkeEX 71 /RHVBHl 72 /RRpxwc 73 /RYtpJo 74 /RuCVox 75 /RwJMYt 76 /SHnJcB 77 /ScbnLb 78 /TOcaLi 79 /TRVSRU 80 /TaHTXL 81 /UKzbGY 82 /UVvNeb 83 /UfXGIO 84 /VHIVpS 85 /VcRwRL 86 /VgqgVe 87 /VlUfcv 88 /VzZAXk 89 /WczOWP 90 /WkLtrX 91 /WkfCQW 92 /WlHtRg 93 /WwTDDj 94 /WxWlWp 95 /XGmIIH 96 /XOxJQN 97 /XhgvtL 98 /YMsFSK 99 /YeASVz 100 /YoxGxL
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)
0 /AAVZWa 1 /ADbUvD 2 /AJEDdF 3 /AOacJP 4 /AsSyrK 5 /BIXejR 6 /Blwttj 7 /BytXnJ 8 /CuiQgF 9 /CxkVBv 10 /DOKcxz 11 /DSSzPM 12 /DdpHTg 13 /EBHNWn 14 /EHuhuk 15 /EIaigX 16 /EOOBpB 17 /EdeAYH 18 /EiftYh 19 /EoHBcy 20 /Evkitq 21 /FJboqX 22 /FWawby 23 /GIKXkG 24 /GPBBMj 25 /GYhuaT 26 /GdTLTY 27 /GeUlsB 28 /GeolrR 29 /GkMwBV 30 /GownbC 31 /GrCTmE 32 /HGSBFA 33 /HemGKU 34 /HujYDN 35 /IKLjEu 36 /IORbkE 37 /IXYMov 38 /InEXbB 39 /JKBolS 40 /JVSJPq 41 /JofMZK 42 /JomohW 43 /JznRlw 44 /KEthFz 45 /KFwczR 46 /KUVnJc 47 /KnKXTR 48 /KuGUhG 49 /KudtCP 50 /LaUmLC 51 /LissvE 52 /LmraEV 53 /MCmopN 54 /MIdmWa 55 /MgSdJx 56 /NJjQrX 57 /NfzPAX 58 /OJZEtt 59 /Oaksmf 60 /OdhAjf 61 /PJETsk 62 /PXxSpS 63 /PiWfGA 64 /PptHII 65 /PvfGpy 66 /QTTYTa 67 /QbhbQt 68 /QoHLXF 69 /QskeUe 70 /QtkeEX 71 /RHVBHl 72 /RRpxwc 73 /RYtpJo 74 /RuCVox 75 /RwJMYt 76 /SHnJcB 77 /ScbnLb 78 /TOcaLi 79 /TRVSRU 80 /TaHTXL 81 /UKzbGY 82 /UVvNeb 83 /UfXGIO 84 /VHIVpS 85 /VcRwRL 86 /VgqgVe 87 /VlUfcv 88 /VzZAXk 89 /WczOWP 90 /WkLtrX 91 /WkfCQW 92 /WlHtRg 93 /WwTDDj 94 /WxWlWp 95 /XGmIIH 96 /XOxJQN 97 /XhgvtL 98 /YMsFSK 99 /YeASVz 100 /YoxGxL
store.close()
with open('ess4.pkl', 'wb') as fp:
pickle.dump(country_map, fp)
with open('ess4.pkl', 'rb') as fp:
country_map = pickle.load(fp)
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
xlabel1 = 'Difference in percentage points of hasrelig'
xlabel2 = 'Difference in religiosity (0-10 scale)'
xlim = [-25, 15]
First let's check on the estimated parameters for the age variables.
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()
Writing ess4.01.png
<matplotlib.figure.Figure at 0x7f9efe9d6710>
In almost every country, year born is associated with less religiosity.
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()
Writing ess4.02.png
<matplotlib.figure.Figure at 0x7f9edf072610>
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()
Writing ess4.03.png
<matplotlib.figure.Figure at 0x7f9ede9d0d50>
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()
Writing ess4.04.png
<matplotlib.figure.Figure at 0x7f9edc38e710>
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()
Writing ess4.05.png
<matplotlib.figure.Figure at 0x7f9ee4d5dc50>
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()
Writing ess4.06.png
<matplotlib.figure.Figure at 0x7f9f04f371d0>
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()
Writing ess4.07.png
<matplotlib.figure.Figure at 0x7f9efc2d6090>
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()
Writing ess4.08.png
<matplotlib.figure.Figure at 0x7f9efe2d6690>
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()
-7.6100903746 -7.99861220321 -3.47246804305 -3.39621681489 -1.56600401422 -1.95101119651 -0.948475855492 -1.04940487676 0.283111223687 -0.100804047038 -0.128763052394 0.188254315036 0.248812913673 0.625585601944 1.30795708007 0.426758433373 Writing ess4.09.png
<matplotlib.figure.Figure at 0x7f9f005c7250>
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()
0.30172553685 Writing ess4.10.png
<matplotlib.figure.Figure at 0x7f9edf8c6550>
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()
0.299832107839 Writing ess4.11.png
<matplotlib.figure.Figure at 0x7f9ed421d490>
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()
-0.377644186914 Writing ess4.12.png
<matplotlib.figure.Figure at 0x7f9ede54ca50>
Make similar figures for the second model, with degree of religiosity as the dependent variable.
xlim = [-2.5, 1.0]
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()
Writing ess4.13.png
<matplotlib.figure.Figure at 0x7f9f00a4a550>
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()
Writing ess4.14.png
<matplotlib.figure.Figure at 0x7f9f00a4a050>
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()
Writing ess4.15.png
<matplotlib.figure.Figure at 0x7f9edfb70e50>
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()
Writing ess4.16.png
<matplotlib.figure.Figure at 0x7f9edf92d710>
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()
Writing ess4.17.png
<matplotlib.figure.Figure at 0x7f9efeafff90>
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()
Writing ess4.18.png
<matplotlib.figure.Figure at 0x7f9ee55f3f90>
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()
Writing ess4.19.png
<matplotlib.figure.Figure at 0x7f9efe256d50>
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()
Writing ess4.20.png
<matplotlib.figure.Figure at 0x7f9f005c7450>
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()
-0.361527956195 -0.334836169355 -0.185573584648 -0.201108610193 -0.217090335159 -0.223876985972 -0.113349234086 -0.125387887266 -0.0348431722332 -0.0408627145833 -0.0198243358803 -0.0057449815362 0.0265003098879 0.0239471272503 -0.529880792947 -0.598870219159 Writing ess4.21.png
<matplotlib.figure.Figure at 0x7f9ee5ce4490>
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.
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()
-0.224489530769 Writing ess4.22.png
<matplotlib.figure.Figure at 0x7f9eec1b6750>
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()
-0.229662125758 Writing ess4.23.png
<matplotlib.figure.Figure at 0x7f9efc2d6590>
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()
-0.065890035769 Writing ess4.24.png
<matplotlib.figure.Figure at 0x7f9e9182f610>
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)
varname neg* neg pos pos* --------- ---- --- --- ---- yrbrn60_f 27 3 2 2 34 netuse_f 24 6 4 0 34 edurank_f 19 7 3 5 34 tvtot_f 14 10 4 6 34 nwsptot_f 10 4 11 9 34 rdtot_f 8 7 13 6 34 hincrank_f 4 15 11 4 34 inwyr07_f 2 9 11 12 34
ts = ess.make_table(country_map, varnames, ess.extract_ranges2)
ess.print_table(ts)
varname neg* neg pos pos* --------- ---- --- --- ---- yrbrn60_f 29 3 1 1 34 netuse_f 24 7 3 0 34 tvtot_f 22 6 3 3 34 edurank_f 21 4 5 4 34 hincrank_f 16 14 3 1 34 rdtot_f 13 7 7 7 34 nwsptot_f 12 5 13 4 34 inwyr07_f 4 10 12 8 34