Created
May 20, 2025 21:45
-
-
Save acbass49/49637e9b55961074f0605166fe0450b9 to your computer and use it in GitHub Desktop.
9 Proselyting by Religion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import numpy as np | |
| import survey_tools as st | |
| from statsmodels.stats.proportion import proportions_ztest | |
| # load in the data | |
| data07 = pd.read_spss('../data/rls/rls2007.sav') | |
| data14 = pd.read_spss('../data/rls/rls2014.sav') | |
| data24 = pd.read_spss('../data/rls/rls2024.sav') | |
| # mormon variable | |
| data07['mormon'] = data07['reltrad'].isin([' Mormon']) | |
| data14['mormon'] = data14['RELTRAD'].isin(['Mormon']) | |
| data24['mormon'] = data24['RELTRAD'].isin(['Church of Jesus Christ of Latter-day Saints (Mormon)']) | |
| data24['age_rc'] = st.recode( | |
| data24, 'BIRTHDECADE', | |
| '"2000s" = 1; "1990s" = 1; "1980s" = 2; "1970s" = 3; "1960s" = 3; "1950s" = 4; "1940s or earlier" = 4; "No answer" = NaN' | |
| ) | |
| ## Mormons and Proselytizing | |
| # Q42d Share your faith with non-believers | |
| data07['q42d_rc'] = np.where(~data07['q42d'].isin(['At least once a week', 'Once or twice a month']), 0, 1) | |
| data07['q42d_rc2'] = np.where(~data07['q42d'].isin(['At least once a week']), 0, 1) | |
| st.tabs(data07, 'reltrad','q42d_rc', display = 'row',wts='weight') \ | |
| .apply(lambda x:x/100) \ | |
| .apply(lambda x:round(x,2)) \ | |
| .to_csv('~/Desktop/q42d_rc.csv') | |
| st.tabs(data07, 'reltrad','q42d_rc2', display = 'row',wts='weight') | |
| data14['qi2g_rc'] = np.where(~data14['qi2g'].isin(['At least once a week', 'Once or twice a month']), 0, 1) | |
| st.tabs(data14, 'RELTRAD','qi2g_rc', display = 'row',wts='WEIGHT') \ | |
| .apply(lambda x:x/100) \ | |
| .apply(lambda x:round(x,2)) \ | |
| .to_csv('~/Desktop/qi2g_rc.csv') | |
| data24['PRAC_D_rc'] = np.where(~data24['PRAC_D'].isin(['At least once a week', 'Once or twice a month']), 0, 1) | |
| data24['PRAC_D_rc2'] = np.where(~data24['PRAC_D'].isin(['At least once a week']), 0, 1) | |
| st.tabs(data24, 'RELTRAD','PRAC_D_rc', display = 'row',wts='WEIGHT') \ | |
| .apply(lambda x:x/100) \ | |
| .apply(lambda x:round(x,2)) \ | |
| .to_csv('~/Desktop/PRAC_D_rc.csv') | |
| st.tabs(data24, 'RELTRAD','PRAC_D_rc2', display = 'row',wts='WEIGHT') | |
| st.tabs(data24[data24.mormon == True], 'age_rc', 'PRAC_D_rc', display = 'row') | |
| st.tabs(data24[data24.mormon == True], 'GENDER', 'PRAC_D_rc', display = 'row') | |
| data14['jellobelt'] = np.where(data14['state'].isin(['Utah', 'Idaho']), 'jello', 'not jello') | |
| st.tabs(data14[data14.mormon == True], 'jellobelt', 'qi2g_rc', display = 'row') | |
| tbl = st.tabs(data14[data14.mormon == True], 'jellobelt', 'qi2g_rc', display = 'count') | |
| # proportions_ztest | |
| count = [ | |
| tbl.loc['jello', 1], # successes in jello states | |
| tbl.loc['not jello', 1] # successes in not jello states | |
| ] | |
| nobs = [ | |
| tbl.loc['jello'].sum(), # total in jello states | |
| tbl.loc['not jello'].sum() # total in not jello states | |
| ] | |
| stat, pval = proportions_ztest(count, nobs) | |
| print(f"Z-statistic: {stat:.3f}, p-value: {pval:.3f}") | |
| states_2024 = { | |
| "red": [ | |
| "Alabama", "Alaska", "Arkansas", "Florida", "Idaho", "Indiana", "Iowa", "Kansas", | |
| "Kentucky", "Louisiana", "Mississippi", "Missouri", "Montana", "Nebraska", "North Dakota", | |
| "Ohio", "Oklahoma", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", | |
| "West Virginia", "Wyoming" | |
| ], | |
| "blue": [ | |
| "California", "Colorado", "Connecticut", "Delaware", "Hawaii", "Illinois", "Maine", | |
| "Maryland", "Massachusetts", "Michigan", "Minnesota", "Nevada", "New Hampshire", | |
| "New Jersey", "New Mexico", "New York", "Oregon", "Pennsylvania", "Rhode Island", | |
| "Vermont", "Virginia", "Washington", "Wisconsin" | |
| ] | |
| } | |
| data14['red_states'] = np.where(data14['state'].isin(states_2024['red']), 'red', 'blue') | |
| st.tabs(data14[data14.mormon == True], 'red_states', 'qi2g_rc', display = 'row') | |
| tbl = st.tabs(data14[data14.mormon == True], 'red_states', 'qi2g_rc', display = 'count') | |
| # proportions_ztest | |
| count = [ | |
| tbl.loc['red', 1], # successes in red states | |
| tbl.loc['blue', 1] # successes in blue states | |
| ] | |
| nobs = [ | |
| tbl.loc['red'].sum(), # total in red states | |
| tbl.loc['blue'].sum() # total in blue states | |
| ] | |
| stat, pval = proportions_ztest(count, nobs) | |
| print(f"Z-statistic: {stat:.3f}, p-value: {pval:.3f}") | |
| # church attendance | |
| data24['attend'] = np.where(data24['ATTNDPERRLS'].isin(['Once a week', 'More than once a week']), 'weekly', 'less than weekly') | |
| st.tabs(data24[data24.mormon == True], 'attend', 'PRAC_D_rc', display = 'row') | |
| data24['White'] = data24.RACECMB.isin(['White']) | |
| st.tabs(data24[data24.mormon == True], 'White', 'PRAC_D_rc', display = 'row') | |
| st.tabs(data24[data24.mormon == True], 'EDUCREC', 'PRAC_D_rc', display = 'row') | |
| # Define the region dictionary | |
| us_regions = { | |
| "Northeast": [ | |
| "Connecticut", "Maine", "Massachusetts", "New Hampshire", "Rhode Island", "Vermont", | |
| "New Jersey", "New York", "Pennsylvania" | |
| ], | |
| "Midwest": [ | |
| "Illinois", "Indiana", "Michigan", "Ohio", "Wisconsin", | |
| "Iowa", "Kansas", "Minnesota", "Missouri", "Nebraska", "North Dakota", "South Dakota" | |
| ], | |
| "South": [ | |
| "Delaware", "Florida", "Georgia", "Maryland", "North Carolina", "South Carolina", "Virginia", "District of Columbia", "West Virginia", | |
| "Alabama", "Kentucky", "Mississippi", "Tennessee", | |
| "Arkansas", "Louisiana", "Oklahoma", "Texas" | |
| ], | |
| "West": [ | |
| "Arizona", "Colorado", "Idaho", "Montana", "Nevada", "New Mexico", "Utah", "Wyoming", | |
| "Alaska", "California", "Hawaii", "Oregon", "Washington" | |
| ] | |
| } | |
| # Create a mapping from state to region | |
| state_to_region = {} | |
| for region, states in us_regions.items(): | |
| for state in states: | |
| state_to_region[state] = region | |
| # Assign the region variable | |
| data14['region'] = data14['state'].map(state_to_region) | |
| st.tabs(data14[data14.mormon == True], 'region', 'qi2g_rc', display = 'row') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment