<jupyter_start><jupyter_text>Internet use and religion in Europe, part five-----------------------------------------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 <NAME>MIT License: http://opensource.org/licenses/MIT<jupyter_code>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<jupyter_output><empty_output><jupyter_text>Open the store containing resampled DataFrames.<jupyter_code>store = pd.HDFStore('ess.resamples.h5')<jupyter_output><empty_output><jupyter_text>Make the country objects<jupyter_code>country_map = ess.make_countries(store)<jupyter_output>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<jupyter_text>For each resampled frame, run both models and store the results in the Country objects<jupyter_code>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)
ess.process_all_frames(store, country_map, num,
                       smf.ols, FORMULA2, 2)
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<jupyter_output><empty_output><jupyter_text>Make a plot showing confidence interval of effect size for the given parameters<jupyter_code>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()
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()
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()
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()
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()
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()
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()
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()<jupyter_output>Writing ess5.08.png<jupyter_text>Plot the CDFs of effect size across countries<jupyter_code>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()<jupyter_output>-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<jupyter_text>Make similar figures for the second model.<jupyter_code>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()
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()
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()
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()
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()
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()
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()
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()
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()
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)
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)<jupyter_output>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
{'fraction_non_alphanumeric': 0.12344266463259598, 'fraction_numerical': 0.054792779049071955, 'mean_word_length': 3.713600958657879, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '378332', 'license': 'MIT', 'n_tokens_mistral': 3375, 'n_tokens_neox': 3115, 'n_words': 595, 'path': 'ess5.ipynb', 'repo_name': 'AllenDowney/ProbablyOverthinkingIt', 'size': '7872'}