{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Internet use and religion in Europe, part five\n", "-----------------------------------------\n", "\n", "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).\n", "\n", "Copyright 2015 Allen Downey\n", "\n", "MIT License: http://opensource.org/licenses/MIT" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from __future__ import print_function, division\n", "\n", "import string\n", "import random\n", "import cPickle as pickle\n", "\n", "import numpy as np\n", "import pandas as pd\n", "import statsmodels.formula.api as smf\n", "\n", "import thinkstats2\n", "import thinkplot\n", "import matplotlib.pyplot as plt\n", "\n", "import ess\n", "\n", "# colors by colorbrewer2.org\n", "BLUE1 = '#a6cee3'\n", "BLUE2 = '#1f78b4'\n", "GREEN1 = '#b2df8a'\n", "GREEN2 = '#33a02c'\n", "PINK = '#fb9a99'\n", "RED = '#e31a1c'\n", "ORANGE1 = '#fdbf6f'\n", "ORANGE2 = '#ff7f00'\n", "PURPLE1 = '#cab2d6'\n", "PURPLE2 = '#6a3d9a'\n", "YELLOW = '#ffff99'\n", "BROWN = '#b15928'\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open the store containing resampled DataFrames." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "store = pd.HDFStore('ess.resamples.h5')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make the country objects" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Austria\n", "Belgium\n", "Bulgaria\n", "Switzerland\n", "Cyprus\n", "Czech Rep\n", "Germany\n", "Denmark\n", "Estonia\n", "Spain\n", "Finland\n", "France\n", "UK\n", "Greece\n", "Croatia\n", "Hungary\n", "Ireland\n", "Israel\n", "Iceland\n", "Italy\n", "Lithuania\n", "Luxembourg\n", "Latvia\n", "Netherlands\n", "Norway\n", "Poland\n", "Portugal\n", "Romania\n", "Russia\n", "Sweden\n", "Slovenia\n", "Slovakia\n", "Turkey\n", "Ukraine\n" ] } ], "source": [ "country_map = ess.make_countries(store)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For each resampled frame, run both models and store the results in the Country objects" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "FORMULA1 = ('netuse_f ~ inwyr07_f + yrbrn60_f + yrbrn60_f2 + '\n", " 'edurank_f + hincrank_f +'\n", " 'tvtot_f + rdtot_f + nwsptot_f + hasrelig_f')\n", "\n", "FORMULA2 = ('netuse_f ~ inwyr07_f + yrbrn60_f + yrbrn60_f2 + '\n", " 'edurank_f + hincrank_f +'\n", " 'tvtot_f + rdtot_f + nwsptot_f + rlgdgr_f')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 /AAVZWa\n", "1 /ADbUvD\n", "2 /AJEDdF\n", "3 /AOacJP\n", "4 /AsSyrK\n", "5 /BIXejR\n", "6 /Blwttj\n", "7 /BytXnJ\n", "8 /CuiQgF\n", "9 /CxkVBv\n", "10 /DOKcxz\n", "11 /DSSzPM\n", "12 /DdpHTg\n", "13 /EBHNWn\n", "14 /EHuhuk\n", "15 /EIaigX\n", "16 /EOOBpB\n", "17 /EdeAYH\n", "18 /EiftYh\n", "19 /EoHBcy\n", "20 /Evkitq\n", "21 /FJboqX\n", "22 /FWawby\n", "23 /GIKXkG\n", "24 /GPBBMj\n", "25 /GYhuaT\n", "26 /GdTLTY\n", "27 /GeUlsB\n", "28 /GeolrR\n", "29 /GkMwBV\n", "30 /GownbC\n", "31 /GrCTmE\n", "32 /HGSBFA\n", "33 /HemGKU\n", "34 /HujYDN\n", "35 /IKLjEu\n", "36 /IORbkE\n", "37 /IXYMov\n", "38 /InEXbB\n", "39 /JKBolS\n", "40 /JVSJPq\n", "41 /JofMZK\n", "42 /JomohW\n", "43 /JznRlw\n", "44 /KEthFz\n", "45 /KFwczR\n", "46 /KUVnJc\n", "47 /KnKXTR\n", "48 /KuGUhG\n", "49 /KudtCP\n", "50 /LaUmLC\n", "51 /LissvE\n", "52 /LmraEV\n", "53 /MCmopN\n", "54 /MIdmWa\n", "55 /MgSdJx\n", "56 /NJjQrX\n", "57 /NfzPAX\n", "58 /OJZEtt\n", "59 /Oaksmf\n", "60 /OdhAjf\n", "61 /PJETsk\n", "62 /PXxSpS\n", "63 /PiWfGA\n", "64 /PptHII\n", "65 /PvfGpy\n", "66 /QTTYTa\n", "67 /QbhbQt\n", "68 /QoHLXF\n", "69 /QskeUe\n", "70 /QtkeEX\n", "71 /RHVBHl\n", "72 /RRpxwc\n", "73 /RYtpJo\n", "74 /RuCVox\n", "75 /RwJMYt\n", "76 /SHnJcB\n", "77 /ScbnLb\n", "78 /TOcaLi\n", "79 /TRVSRU\n", "80 /TaHTXL\n", "81 /UKzbGY\n", "82 /UVvNeb\n", "83 /UfXGIO\n", "84 /VHIVpS\n", "85 /VcRwRL\n", "86 /VgqgVe\n", "87 /VlUfcv\n", "88 /VzZAXk\n", "89 /WczOWP\n", "90 /WkLtrX\n", "91 /WkfCQW\n", "92 /WlHtRg\n", "93 /WwTDDj\n", "94 /WxWlWp\n", "95 /XGmIIH\n", "96 /XOxJQN\n", "97 /XhgvtL\n", "98 /YMsFSK\n", "99 /YeASVz\n", "100 /YoxGxL\n", "101 /YvdfEk\n", "102 /ZEEBve\n", "103 /ZXovwc\n", "104 /ZgSZAY\n", "105 /ZjGafB\n", "106 /ZxNahg\n", "107 /bJIOjl\n", "108 /bNKOFy\n", "109 /bOZZkd\n", "110 /blIIdK\n", "111 /btCIZx\n", "112 /bvntaM\n", "113 /cEzhky\n", "114 /cYiUkH\n", "115 /cgKsnt\n", "116 /czQkEF\n", "117 /dcjEvm\n", "118 /dqGBQR\n", "119 /dyuBXv\n", "120 /dzpDVu\n", "121 /eDVvJf\n", "122 /ewfhTI\n", "123 /fBFUGB\n", "124 /fEkGRW\n", "125 /ggYbXH\n", "126 /gnJSCF\n", "127 /hIQegI\n", "128 /hOQHWV\n", "129 /hTfXDB\n", "130 /hlJZff\n", "131 /hmmXxf\n", "132 /iABzcU\n", "133 /iGVZEK\n", "134 /iWltCV\n", "135 /iaTUMA\n", "136 /iiSwHC\n", "137 /ilWxnR\n", "138 /jNqZpZ\n", "139 /kVTeXb\n", "140 /kfAnDn\n", "141 /kuZzaN\n", "142 /lHDxRr\n", "143 /lfUmXq\n", "144 /lsXAWo\n", "145 /mbTfIj\n", "146 /mtyzJg\n", "147 /nOsmSf\n", "148 /ncvQcP\n", "149 /neEVfl\n", "150 /nnERGx\n", "151 /nnUXHn\n", "152 /nvZcGU\n", "153 /olGyuX\n", "154 /pezcXZ\n", "155 /qRQqmc\n", "156 /qWZfql\n", "157 /qchGUz\n", "158 /qfZVHF\n", "159 /qqYojL\n", "160 /rCrtjG\n", "161 /rSravW\n", "162 /rUWSeP\n", "163 /rYeOLP\n", "164 /rnYDRv\n", "165 /sPKzmv\n", "166 /shBLMW\n", "167 /siHRLd\n", "168 /ssTuqu\n", "169 /tIBOEC\n", "170 /tJaytt\n", "171 /taosRR\n", "172 /tazMjo\n", "173 /teuTPZ\n", "174 /tiUoXr\n", "175 /tjiIAT\n", "176 /tjjdLV\n", "177 /tuLFXm\n", "178 /uHhvHu\n", "179 /uOAidw\n", "180 /uWhsWV\n", "181 /uZGRbW\n", "182 /vPnlcH\n", "183 /vktdCG\n", "184 /vqFufP\n", "185 /vqxuWx\n", "186 /wGQrTR\n", "187 /wahSMf\n", "188 /wsvHYt\n", "189 /xfKUcU\n", "190 /xgAfWd\n", "191 /xjbtHM\n", "192 /xtqhXa\n", "193 /xwCakd\n", "194 /xxGtEc\n", "195 /yImumW\n", "196 /yKuLlN\n", "197 /ybmXrn\n", "198 /yfOjqX\n", "199 /ylSawW\n", "200 /zBUKWF\n" ] } ], "source": [ "num = 201\n", "ess.process_all_frames(store, country_map, num, \n", " smf.ols, FORMULA1, 1)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 /AAVZWa\n", "1 /ADbUvD\n", "2 /AJEDdF\n", "3 /AOacJP\n", "4 /AsSyrK\n", "5 /BIXejR\n", "6 /Blwttj\n", "7 /BytXnJ\n", "8 /CuiQgF\n", "9 /CxkVBv\n", "10 /DOKcxz\n", "11 /DSSzPM\n", "12 /DdpHTg\n", "13 /EBHNWn\n", "14 /EHuhuk\n", "15 /EIaigX\n", "16 /EOOBpB\n", "17 /EdeAYH\n", "18 /EiftYh\n", "19 /EoHBcy\n", "20 /Evkitq\n", "21 /FJboqX\n", "22 /FWawby\n", "23 /GIKXkG\n", "24 /GPBBMj\n", "25 /GYhuaT\n", "26 /GdTLTY\n", "27 /GeUlsB\n", "28 /GeolrR\n", "29 /GkMwBV\n", "30 /GownbC\n", "31 /GrCTmE\n", "32 /HGSBFA\n", "33 /HemGKU\n", "34 /HujYDN\n", "35 /IKLjEu\n", "36 /IORbkE\n", "37 /IXYMov\n", "38 /InEXbB\n", "39 /JKBolS\n", "40 /JVSJPq\n", "41 /JofMZK\n", "42 /JomohW\n", "43 /JznRlw\n", "44 /KEthFz\n", "45 /KFwczR\n", "46 /KUVnJc\n", "47 /KnKXTR\n", "48 /KuGUhG\n", "49 /KudtCP\n", "50 /LaUmLC\n", "51 /LissvE\n", "52 /LmraEV\n", "53 /MCmopN\n", "54 /MIdmWa\n", "55 /MgSdJx\n", "56 /NJjQrX\n", "57 /NfzPAX\n", "58 /OJZEtt\n", "59 /Oaksmf\n", "60 /OdhAjf\n", "61 /PJETsk\n", "62 /PXxSpS\n", "63 /PiWfGA\n", "64 /PptHII\n", "65 /PvfGpy\n", "66 /QTTYTa\n", "67 /QbhbQt\n", "68 /QoHLXF\n", "69 /QskeUe\n", "70 /QtkeEX\n", "71 /RHVBHl\n", "72 /RRpxwc\n", "73 /RYtpJo\n", "74 /RuCVox\n", "75 /RwJMYt\n", "76 /SHnJcB\n", "77 /ScbnLb\n", "78 /TOcaLi\n", "79 /TRVSRU\n", "80 /TaHTXL\n", "81 /UKzbGY\n", "82 /UVvNeb\n", "83 /UfXGIO\n", "84 /VHIVpS\n", "85 /VcRwRL\n", "86 /VgqgVe\n", "87 /VlUfcv\n", "88 /VzZAXk\n", "89 /WczOWP\n", "90 /WkLtrX\n", "91 /WkfCQW\n", "92 /WlHtRg\n", "93 /WwTDDj\n", "94 /WxWlWp\n", "95 /XGmIIH\n", "96 /XOxJQN\n", "97 /XhgvtL\n", "98 /YMsFSK\n", "99 /YeASVz\n", "100 /YoxGxL\n", "101 /YvdfEk\n", "102 /ZEEBve\n", "103 /ZXovwc\n", "104 /ZgSZAY\n", "105 /ZjGafB\n", "106 /ZxNahg\n", "107 /bJIOjl\n", "108 /bNKOFy\n", "109 /bOZZkd\n", "110 /blIIdK\n", "111 /btCIZx\n", "112 /bvntaM\n", "113 /cEzhky\n", "114 /cYiUkH\n", "115 /cgKsnt\n", "116 /czQkEF\n", "117 /dcjEvm\n", "118 /dqGBQR\n", "119 /dyuBXv\n", "120 /dzpDVu\n", "121 /eDVvJf\n", "122 /ewfhTI\n", "123 /fBFUGB\n", "124 /fEkGRW\n", "125 /ggYbXH\n", "126 /gnJSCF\n", "127 /hIQegI\n", "128 /hOQHWV\n", "129 /hTfXDB\n", "130 /hlJZff\n", "131 /hmmXxf\n", "132 /iABzcU\n", "133 /iGVZEK\n", "134 /iWltCV\n", "135 /iaTUMA\n", "136 /iiSwHC\n", "137 /ilWxnR\n", "138 /jNqZpZ\n", "139 /kVTeXb\n", "140 /kfAnDn\n", "141 /kuZzaN\n", "142 /lHDxRr\n", "143 /lfUmXq\n", "144 /lsXAWo\n", "145 /mbTfIj\n", "146 /mtyzJg\n", "147 /nOsmSf\n", "148 /ncvQcP\n", "149 /neEVfl\n", "150 /nnERGx\n", "151 /nnUXHn\n", "152 /nvZcGU\n", "153 /olGyuX\n", "154 /pezcXZ\n", "155 /qRQqmc\n", "156 /qWZfql\n", "157 /qchGUz\n", "158 /qfZVHF\n", "159 /qqYojL\n", "160 /rCrtjG\n", "161 /rSravW\n", "162 /rUWSeP\n", "163 /rYeOLP\n", "164 /rnYDRv\n", "165 /sPKzmv\n", "166 /shBLMW\n", "167 /siHRLd\n", "168 /ssTuqu\n", "169 /tIBOEC\n", "170 /tJaytt\n", "171 /taosRR\n", "172 /tazMjo\n", "173 /teuTPZ\n", "174 /tiUoXr\n", "175 /tjiIAT\n", "176 /tjjdLV\n", "177 /tuLFXm\n", "178 /uHhvHu\n", "179 /uOAidw\n", "180 /uWhsWV\n", "181 /uZGRbW\n", "182 /vPnlcH\n", "183 /vktdCG\n", "184 /vqFufP\n", "185 /vqxuWx\n", "186 /wGQrTR\n", "187 /wahSMf\n", "188 /wsvHYt\n", "189 /xfKUcU\n", "190 /xgAfWd\n", "191 /xjbtHM\n", "192 /xtqhXa\n", "193 /xwCakd\n", "194 /xxGtEc\n", "195 /yImumW\n", "196 /yKuLlN\n", "197 /ybmXrn\n", "198 /yfOjqX\n", "199 /ylSawW\n", "200 /zBUKWF\n" ] } ], "source": [ "ess.process_all_frames(store, country_map, num,\n", " smf.ols, FORMULA2, 2)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": true }, "outputs": [], "source": [ "store.close()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "with open('ess5.pkl', 'wb') as fp:\n", " pickle.dump(country_map, fp)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open('ess5.pkl', 'rb') as fp:\n", " country_map = pickle.load(fp)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": true }, "outputs": [], "source": [ "plot_counter = 1\n", "\n", "def save_plot(flag=True):\n", " \"\"\"Saves plots in png format.\n", " \n", " flag: boolean, whether to save or not\n", " \"\"\"\n", " global plot_counter\n", " if flag:\n", " root = 'ess5.%2.2d' % plot_counter\n", " thinkplot.Save(root=root, formats=['png'])\n", " plot_counter += 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make a plot showing confidence interval of effect size for the given parameters" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": true }, "outputs": [], "source": [ "xlabel1 = 'Difference in level of Internet use (0-7 scale)'\n", "xlabel2 = 'Difference in level of Internet use (0-7 scale)'" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": true }, "outputs": [], "source": [ "xlim = [-1.0, 3.5]" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.01.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "reload(ess)\n", "t = ess.extract_ranges(country_map, 'yrbrn60_f', 'hasrelig_f')\n", "ess.plot_cis(t, GREEN2)\n", "thinkplot.Config(title='Year born',\n", " xlabel=xlabel1, xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.02.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges(country_map, 'inwyr07_f', 'hasrelig_f')\n", "ess.plot_cis(t, GREEN1)\n", "thinkplot.Config(title='Interview year',\n", " xlabel=xlabel1, xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.03.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges(country_map, 'edurank_f', 'hasrelig_f')\n", "ess.plot_cis(t, ORANGE2)\n", "thinkplot.Config(title='Education (relative rank)',\n", " xlabel=xlabel1, xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.04.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges(country_map, 'hincrank_f', 'hasrelig_f')\n", "ess.plot_cis(t, ORANGE1)\n", "thinkplot.Config(title='Income (relative rank)',\n", " xlabel=xlabel1, xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.05.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges(country_map, 'tvtot_f', 'hasrelig_f')\n", "ess.plot_cis(t, RED)\n", "thinkplot.Config(title='Television watching',\n", " xlabel=xlabel1, xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 83, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.06.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges(country_map, 'rdtot_f', 'hasrelig_f')\n", "ess.plot_cis(t, BLUE1)\n", "thinkplot.Config(title='Radio listening',\n", " xlabel=xlabel1, xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 84, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.07.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges(country_map, 'nwsptot_f', 'hasrelig_f')\n", "ess.plot_cis(t, BLUE2)\n", "thinkplot.Config(title='Newspaper reading',\n", " xlabel=xlabel1, xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 85, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.08.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "reload(ess)\n", "t = ess.extract_ranges(country_map, 'hasrelig_f', 'hasrelig_f')\n", "ess.plot_cis(t, BROWN)\n", "thinkplot.Config(title='Religious affiliation',\n", " xlabel=xlabel1, xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot the CDFs of effect size across countries" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.245125284714 -0.26345511532\n", "-0.0925992328957 -0.0395775212147\n", "0.0421007185354 0.0565209201039\n", "0.117305836288 0.0693131635635\n", "0.568379267708 0.612963693385\n", "1.36588360845 1.3269043995\n", "0.795743263863 0.910541867449\n", "1.91490040499 1.83416921284\n", "Writing ess5.09.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "reload(ess)\n", "cdfnames = ['tvtot_f', 'hasrelig_f', 'rdtot_f', \n", " 'nwsptot_f', 'hincrank_f', 'edurank_f',\n", " 'inwyr07_f', 'yrbrn60_f']\n", "ess.plot_cdfs(country_map, ess.extract_ranges, cdfnames=cdfnames)\n", "thinkplot.Config(xlabel=xlabel1,\n", " xlim=xlim,\n", " legend=True,\n", " loc='lower right')\n", "save_plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make similar figures for the second model." ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "collapsed": true }, "outputs": [], "source": [ "xlim = [-1.5, 3.5]" ] }, { "cell_type": "code", "execution_count": 88, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.10.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges2(country_map, 'yrbrn60_f', 'hasrelig_f')\n", "ess.plot_cis(t, GREEN2)\n", "thinkplot.Config(title='Year born',\n", " xlabel=xlabel2, xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.11.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges2(country_map, 'inwyr07_f', 'hasrelig_f')\n", "ess.plot_cis(t, GREEN1)\n", "thinkplot.Config(title='Interview year',\n", " xlabel=xlabel2, xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 90, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.12.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges2(country_map, 'edurank_f', 'rlgdgr_f')\n", "ess.plot_cis(t, ORANGE2)\n", "thinkplot.Config(title='Education rank',\n", " xlabel=xlabel2,\n", " xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 91, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.13.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges2(country_map, 'hincrank_f', 'hasrelig_f')\n", "ess.plot_cis(t, ORANGE1)\n", "thinkplot.Config(title='Income rank',\n", " xlabel=xlabel2,\n", " xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.14.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges2(country_map, 'tvtot_f', 'hasrelig_f')\n", "ess.plot_cis(t, RED)\n", "thinkplot.Config(title='Television watching',\n", " xlabel=xlabel2,\n", " xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.15.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges2(country_map, 'rdtot_f', 'hasrelig_f')\n", "ess.plot_cis(t, BLUE1)\n", "thinkplot.Config(title='Radio listening',\n", " xlabel=xlabel2,\n", " xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.16.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges2(country_map, 'nwsptot_f', 'hasrelig_f')\n", "ess.plot_cis(t, BLUE2)\n", "thinkplot.Config(title='Newspaper reading',\n", " xlabel=xlabel2,\n", " xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing ess5.17.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "t = ess.extract_ranges2(country_map, 'rlgdgr_f', 'hasrelig_f')\n", "ess.plot_cis(t, BROWN)\n", "thinkplot.Config(title='Degree of religiosity',\n", " xlabel=xlabel2,\n", " xlim=xlim)\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.256137175641 -0.263852615538\n", "-0.189269580411 -0.138621548651\n", "0.0414823196446 0.060188890896\n", "0.115147176451 0.0686866084364\n", "0.55960831952 0.611396961511\n", "1.35342846531 1.29659404412\n", "0.791596015686 0.907234081272\n", "1.90361837892 1.84010356899\n", "Writing ess5.18.png\n" ] }, { "data": { "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "reload(ess)\n", "cdfnames = ['tvtot_f', 'rlgdgr_f', 'rdtot_f', \n", " 'nwsptot_f', 'hincrank_f', 'edurank_f',\n", " 'inwyr07_f', 'yrbrn60_f']\n", "ess.plot_cdfs(country_map, ess.extract_ranges2, cdfnames=cdfnames)\n", "thinkplot.Config(xlabel=xlabel2,\n", " xlim=xlim,\n", " ylabel='CDF',\n", " loc='lower right')\n", "save_plot()" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "varname \tneg* \tneg \tpos \tpos*\n", "--------- \t---- \t--- \t--- \t----\n", "tvtot_f \t28 \t5 \t0 \t1 \t34\n", "hasrelig_f \t15 \t2 \t17 \t0 \t34\n", "rdtot_f \t8 \t5 \t7 \t14 \t34\n", "inwyr07_f \t1 \t2 \t3 \t28 \t34\n", "yrbrn60_f \t0 \t0 \t0 \t34 \t34\n", "edurank_f \t0 \t0 \t0 \t34 \t34\n", "hincrank_f \t0 \t0 \t1 \t33 \t34\n", "nwsptot_f \t0 \t2 \t9 \t23 \t34\n" ] } ], "source": [ "reload(ess)\n", "varnames = ['inwyr07_f', 'yrbrn60_f', 'hasrelig_f', 'edurank_f', \n", " 'tvtot_f', 'hincrank_f', 'rdtot_f', 'nwsptot_f']\n", "\n", "ts = ess.make_table(country_map, varnames, ess.extract_ranges)\n", "ess.print_table(ts)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "varname \tneg* \tneg \tpos \tpos*\n", "--------- \t---- \t--- \t--- \t----\n", "tvtot_f \t29 \t4 \t1 \t0 \t34\n", "rlgdgr_f \t24 \t8 \t2 \t0 \t34\n", "rdtot_f \t8 \t5 \t7 \t14 \t34\n", "inwyr07_f \t1 \t2 \t3 \t28 \t34\n", "yrbrn60_f \t0 \t0 \t0 \t34 \t34\n", "edurank_f \t0 \t0 \t0 \t34 \t34\n", "hincrank_f \t0 \t0 \t1 \t33 \t34\n", "nwsptot_f \t0 \t2 \t10 \t22 \t34\n" ] } ], "source": [ "varnames = ['inwyr07_f', 'yrbrn60_f', 'rlgdgr_f', 'edurank_f', \n", " 'tvtot_f', 'hincrank_f', 'rdtot_f', 'nwsptot_f']\n", "\n", "ts = ess.make_table(country_map, varnames, ess.extract_ranges2)\n", "ess.print_table(ts)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }