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 = pd.HDFStore('ess.resamples.h5')
Make the country objects
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
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')
num = 201
ess.process_all_frames(store, country_map, num,
smf.ols, FORMULA1, 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 101 /YvdfEk 102 /ZEEBve 103 /ZXovwc 104 /ZgSZAY 105 /ZjGafB 106 /ZxNahg 107 /bJIOjl 108 /bNKOFy 109 /bOZZkd 110 /blIIdK 111 /btCIZx 112 /bvntaM 113 /cEzhky 114 /cYiUkH 115 /cgKsnt 116 /czQkEF 117 /dcjEvm 118 /dqGBQR 119 /dyuBXv 120 /dzpDVu 121 /eDVvJf 122 /ewfhTI 123 /fBFUGB 124 /fEkGRW 125 /ggYbXH 126 /gnJSCF 127 /hIQegI 128 /hOQHWV 129 /hTfXDB 130 /hlJZff 131 /hmmXxf 132 /iABzcU 133 /iGVZEK 134 /iWltCV 135 /iaTUMA 136 /iiSwHC 137 /ilWxnR 138 /jNqZpZ 139 /kVTeXb 140 /kfAnDn 141 /kuZzaN 142 /lHDxRr 143 /lfUmXq 144 /lsXAWo 145 /mbTfIj 146 /mtyzJg 147 /nOsmSf 148 /ncvQcP 149 /neEVfl 150 /nnERGx 151 /nnUXHn 152 /nvZcGU 153 /olGyuX 154 /pezcXZ 155 /qRQqmc 156 /qWZfql 157 /qchGUz 158 /qfZVHF 159 /qqYojL 160 /rCrtjG 161 /rSravW 162 /rUWSeP 163 /rYeOLP 164 /rnYDRv 165 /sPKzmv 166 /shBLMW 167 /siHRLd 168 /ssTuqu 169 /tIBOEC 170 /tJaytt 171 /taosRR 172 /tazMjo 173 /teuTPZ 174 /tiUoXr 175 /tjiIAT 176 /tjjdLV 177 /tuLFXm 178 /uHhvHu 179 /uOAidw 180 /uWhsWV 181 /uZGRbW 182 /vPnlcH 183 /vktdCG 184 /vqFufP 185 /vqxuWx 186 /wGQrTR 187 /wahSMf 188 /wsvHYt 189 /xfKUcU 190 /xgAfWd 191 /xjbtHM 192 /xtqhXa 193 /xwCakd 194 /xxGtEc 195 /yImumW 196 /yKuLlN 197 /ybmXrn 198 /yfOjqX 199 /ylSawW 200 /zBUKWF
ess.process_all_frames(store, country_map, num,
smf.ols, FORMULA2, 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 101 /YvdfEk 102 /ZEEBve 103 /ZXovwc 104 /ZgSZAY 105 /ZjGafB 106 /ZxNahg 107 /bJIOjl 108 /bNKOFy 109 /bOZZkd 110 /blIIdK 111 /btCIZx 112 /bvntaM 113 /cEzhky 114 /cYiUkH 115 /cgKsnt 116 /czQkEF 117 /dcjEvm 118 /dqGBQR 119 /dyuBXv 120 /dzpDVu 121 /eDVvJf 122 /ewfhTI 123 /fBFUGB 124 /fEkGRW 125 /ggYbXH 126 /gnJSCF 127 /hIQegI 128 /hOQHWV 129 /hTfXDB 130 /hlJZff 131 /hmmXxf 132 /iABzcU 133 /iGVZEK 134 /iWltCV 135 /iaTUMA 136 /iiSwHC 137 /ilWxnR 138 /jNqZpZ 139 /kVTeXb 140 /kfAnDn 141 /kuZzaN 142 /lHDxRr 143 /lfUmXq 144 /lsXAWo 145 /mbTfIj 146 /mtyzJg 147 /nOsmSf 148 /ncvQcP 149 /neEVfl 150 /nnERGx 151 /nnUXHn 152 /nvZcGU 153 /olGyuX 154 /pezcXZ 155 /qRQqmc 156 /qWZfql 157 /qchGUz 158 /qfZVHF 159 /qqYojL 160 /rCrtjG 161 /rSravW 162 /rUWSeP 163 /rYeOLP 164 /rnYDRv 165 /sPKzmv 166 /shBLMW 167 /siHRLd 168 /ssTuqu 169 /tIBOEC 170 /tJaytt 171 /taosRR 172 /tazMjo 173 /teuTPZ 174 /tiUoXr 175 /tjiIAT 176 /tjjdLV 177 /tuLFXm 178 /uHhvHu 179 /uOAidw 180 /uWhsWV 181 /uZGRbW 182 /vPnlcH 183 /vktdCG 184 /vqFufP 185 /vqxuWx 186 /wGQrTR 187 /wahSMf 188 /wsvHYt 189 /xfKUcU 190 /xgAfWd 191 /xjbtHM 192 /xtqhXa 193 /xwCakd 194 /xxGtEc 195 /yImumW 196 /yKuLlN 197 /ybmXrn 198 /yfOjqX 199 /ylSawW 200 /zBUKWF
store.close()
with open('ess5.pkl', 'wb') as fp:
pickle.dump(country_map, fp)
with open('ess5.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 = '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
xlabel1 = 'Difference in level of Internet use (0-7 scale)'
xlabel2 = 'Difference in level of Internet use (0-7 scale)'
xlim = [-1.0, 3.5]
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()
Writing ess5.01.png
<matplotlib.figure.Figure at 0x7f910bbec450>
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 ess5.02.png
<matplotlib.figure.Figure at 0x7f914ead4dd0>
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 ess5.03.png
<matplotlib.figure.Figure at 0x7f9176de5a50>
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 ess5.04.png
<matplotlib.figure.Figure at 0x7f911fb88f50>
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 ess5.05.png
<matplotlib.figure.Figure at 0x7f910c871d50>
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 ess5.06.png
<matplotlib.figure.Figure at 0x7f9147deebd0>
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 ess5.07.png
<matplotlib.figure.Figure at 0x7f9115ae6f10>
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()
Writing ess5.08.png
<matplotlib.figure.Figure at 0x7f9145e68190>
Plot the CDFs of effect size across countries
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()
-0.245125284714 -0.26345511532 -0.0925992328957 -0.0395775212147 0.0421007185354 0.0565209201039 0.117305836288 0.0693131635635 0.568379267708 0.612963693385 1.36588360845 1.3269043995 0.795743263863 0.910541867449 1.91490040499 1.83416921284 Writing ess5.09.png
<matplotlib.figure.Figure at 0x7f914506b990>
Make similar figures for the second model.
xlim = [-1.5, 3.5]
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()
Writing ess5.10.png
<matplotlib.figure.Figure at 0x7f9145f45ed0>
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()
Writing ess5.11.png
<matplotlib.figure.Figure at 0x7f9148560e50>
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 ess5.12.png
<matplotlib.figure.Figure at 0x7f914380f410>
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 ess5.13.png
<matplotlib.figure.Figure at 0x7f9138cc0690>
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 ess5.14.png
<matplotlib.figure.Figure at 0x7f9135735cd0>
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 ess5.15.png
<matplotlib.figure.Figure at 0x7f914db5ef50>
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 ess5.16.png
<matplotlib.figure.Figure at 0x7f91446fe850>
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()
Writing ess5.17.png
<matplotlib.figure.Figure at 0x7f911f8c41d0>
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()
-0.256137175641 -0.263852615538 -0.189269580411 -0.138621548651 0.0414823196446 0.060188890896 0.115147176451 0.0686866084364 0.55960831952 0.611396961511 1.35342846531 1.29659404412 0.791596015686 0.907234081272 1.90361837892 1.84010356899 Writing ess5.18.png
<matplotlib.figure.Figure at 0x7f9116b419d0>
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)
varname neg* neg pos pos* --------- ---- --- --- ---- tvtot_f 28 5 0 1 34 hasrelig_f 15 2 17 0 34 rdtot_f 8 5 7 14 34 inwyr07_f 1 2 3 28 34 yrbrn60_f 0 0 0 34 34 edurank_f 0 0 0 34 34 hincrank_f 0 0 1 33 34 nwsptot_f 0 2 9 23 34
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)
varname neg* neg pos pos* --------- ---- --- --- ---- tvtot_f 29 4 1 0 34 rlgdgr_f 24 8 2 0 34 rdtot_f 8 5 7 14 34 inwyr07_f 1 2 3 28 34 yrbrn60_f 0 0 0 34 34 edurank_f 0 0 0 34 34 hincrank_f 0 0 1 33 34 nwsptot_f 0 2 10 22 34