<jupyter_start><jupyter_text>Analysis for the manuscript: data analysis This analysis is conducted based on the Dauost experiment replication data, fielded in 2020. 1) checking for randomization of treatment conditions 2) experimental data analysis 3) three-way interactions<jupyter_code>import numpy as np
import pandas as pd
import utility as util
df_rep = pd.read_csv('../output/df_rep.csv')
df_rep.shape
df_rep.columns<jupyter_output><empty_output><jupyter_text>1) checking for randomization of treatment conditions<jupyter_code>democols = ['sex', 'marital', 'age_group', 'education']

for col in democols:
    print(df_rep[col].value_counts(dropna=False).sort_index())
    print("................")
# recode demographic variables
df_rep['sex_r'] = df_rep['sex']

df_rep['marital_r'] = np.nan
df_rep.loc[df_rep['marital']==1, ['marital_r']] = 1 # married
df_rep.loc[np.isin(df_rep['marital'], [2, 3, 4, 5]), ['marital_r']] = 2 # not married

df_rep['age_group_r'] = np.nan
df_rep.loc[np.isin(df_rep['age_group'], [2, 3, 4, 5]), ['age_group_r']] = 1 # 18-54
df_rep.loc[df_rep['age_group']==6, ['age_group_r']] = 2 # 55-64
df_rep.loc[df_rep['age_group']==7, ['age_group_r']] = 3 # 65+

df_rep['education_r'] = np.nan
df_rep.loc[np.isin(df_rep['education'], [1, 2, 3]), ['education_r']] = 1 # less than college
df_rep.loc[np.isin(df_rep['education'], [4, 5]), ['education_r']] = 2 # college or more
# checking recoded variables
democols_r = ['sex_r', 'marital_r', 'age_group_r', 'education_r']

for col in democols_r:
    print(df_rep[col].value_counts(dropna=False).sort_index())
    print("................")
for col in democols_r:
    util.crosstab_chisq(col, 'condition', df_rep, chisqtest=True)<jupyter_output><empty_output><jupyter_text>2) experimental data analysis<jupyter_code># numeric -> character display
SD_cols = ['visit', 'over', 'outdoors']

for col in SD_cols:
    df_rep[col] = df_rep[[col]].replace([1, 2, 8, 11], ["1a. Yes", "2. No", "8. Unsure", "1b. Only when necessary/occasionally"])
# raw distributions for cross-tabs
for col in SD_cols:
    util.crosstab_chisq(col, 'condition', df_rep, chisqtest=False)
# combining 1a, 1b, and 8 together as 1/yes
combine_three = {"1a. Yes": "1. Yes", 
                 "2. No": "2. No",
                 "1b. Only when necessary/occasionally": "1. Yes", 
                 "8. Unsure": "1. Yes"}

for col in SD_cols:
    df_rep[f'{col}_r'] = df_rep[col].map(combine_three)
    
recoded_cols = ['visit_r', 'over_r', 'outdoors_r'] # use recoded_cols for further analyses
# cross-tabs based on recodes
for col in recoded_cols:
    util.crosstab_chisq(col, 'condition', df_rep, chisqtest=True)<jupyter_output><empty_output><jupyter_text>3) testing interactions Testing whether the experimental effects vary by demographic variables ('sex', 'marital', 'age_group', 'education')<jupyter_code>df_rep['condition_r'] = np.nan
df_rep.loc[df_rep['condition']=='A', ['condition_r']] = 1 # condition A
df_rep.loc[df_rep['condition']=='B', ['condition_r']] = 2 # condition B
# recode y as numerics
for col in recoded_cols:
    df_rep[col] = df_rep[[col]].replace(['1. Yes'], [1]) # yes have done the mentioned behavior
    df_rep[col] = df_rep[[col]].replace(['2. No'], [0]) # no haven not done the mentioned behavior
# to use StatsModels package in python, run the following
#X1 = df_rep[['condition_r', 'sex_r']].values
#X2 = df_rep[['condition_r', 'marital_r']].values
#X3 = df_rep[['condition_r', 'age_group_r']].values
#X4 = df_rep[['condition_r', 'education_r']].values

#y1 = df_rep['visit_r'].values
#y2 = df_rep['over_r'].values
#y3 = df_rep['outdoors_r'].values
df_rep['condition_r'].value_counts()<jupyter_output><empty_output><jupyter_text>- Running logistic regressions in R DV: 'visit_r', 'over_r', 'outdoors_r' -> 1=have done; 2=haven't done IV:  1) "condition_r" -> 1=condition A, 2=condition B with excuse statement and guilty free response options 2) demographic vars: 'sex_r', 'marital_r', 'age_group_r', 'education_r' -> see freq of recoded values above<jupyter_code># use R for logistic regression tables
%load_ext rpy2.ipython
%%R -i df_rep
dim(df_rep)
%%R -i df_rep
print('VISIT - SEX')
summary(glm(visit_r ~ factor(condition_r) * factor(sex_r), family=binomial, data=df_rep))
%%R -i df_rep
print('VISIT - MARITAL')
summary(glm(visit_r ~ factor(condition_r) * factor(marital_r), family=binomial, data=df_rep))
%%R -i df_rep
print('VISIT - AGE')
summary(glm(visit_r ~ factor(condition_r) * factor(age_group_r), family=binomial, data=df_rep))
%%R -i df_rep
print('VISIT - EDUCATION')
summary(glm(visit_r ~ factor(condition_r) * factor(education_r), family=binomial, data=df_rep))
%%R -i df_rep
print('OVER - SEX')
summary(glm(over_r ~ factor(condition_r) * factor(sex_r), family=binomial, data=df_rep))
%%R -i df_rep
print('OVER - MARITAL')
summary(glm(over_r ~ factor(condition_r) * factor(marital_r), family=binomial, data=df_rep))
%%R -i df_rep
print('OVER - AGE')
summary(glm(over_r ~ factor(condition_r) * factor(age_group_r), family=binomial, data=df_rep))
%%R -i df_rep
print('OVER - EDUCATION')
summary(glm(over_r ~ factor(condition_r) * factor(education_r), family=binomial, data=df_rep))
%%R -i df_rep
print('OUTDOORS - SEX')
summary(glm(outdoors_r ~ factor(condition_r) * factor(sex_r), family=binomial, data=df_rep))
%%R -i df_rep
print('OUTDOORS - MARITAL')
summary(glm(outdoors_r ~ factor(condition_r) * factor(marital_r), family=binomial, data=df_rep))
%%R -i df_rep
print('OUTDOORS - AGE')
summary(glm(outdoors_r ~ factor(condition_r) * factor(age_group_r), family=binomial, data=df_rep))
%%R -i df_rep
print('OUTDOORS - EDUCATION')
summary(glm(outdoors_r ~ factor(condition_r) * factor(education_r), family=binomial, data=df_rep))<jupyter_output>[1] "OUTDOORS - EDUCATION"

Call:
glm(formula = outdoors_r ~ factor(condition_r) * factor(education_r), 
    family = binomial, data = df_rep)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-1.7822  -1.4803   0.7455   0.8553   0.9022  

Coefficients:
                                          Estimate Std. Error z value Pr(>|z|)
(Intercept)                                0.81718    0.06080  13.441  < 2e-16
factor(condition_r)2                       0.54250    0.09266   5.854 4.79e-09
factor(education_r)2                      -0.12855    0.09060  -1.419    0.156
factor(condition_r)2:factor(education_r)2 -0.09283    0.13581  -0.684    0.494
                                             
(Intercept)                               ***
factor(condition_r)2                      ***
factor(education_r)2                         
factor(condition_r)2:factor(education_r)2    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial [...]
{'fraction_non_alphanumeric': 0.15914489311163896, 'fraction_numerical': 0.03191805225653207, 'mean_word_length': 4.338351822503962, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '552906', 'license': 'MIT', 'n_tokens_mistral': 2667, 'n_tokens_neox': 2462, 'n_words': 561, 'path': 'report/9.2_STL_rep_analysis.ipynb', 'repo_name': 'rosalynnyang/fb_uas_study', 'size': '6736'}