Package: causalnlp
Welcome to CausalNLPWhat is CausalNLP?CausalNLP is a practical toolkit for causal inference with text as treatment, outcome, or "controlled-for" variable.FeaturesLow-codecausal inferencein as little as two commandsOut-of-the-box support for usingtextas a "controlled-for" variable(e.g., confounder)Built-inAutocoderthat transforms raw text into useful variables for causal analyses (e.g., topics, sentiment, emotion, etc.)Sensitivity analysis toassess robustness of causal estimatesQuick and simplekey driver analysisto yield clues on potential drivers of an outcome based on predictive power, correlations, etc.Can easily be applied to"traditional" tabular datasets without text(i.e., datasets with only numerical and categorical variables)Includes an experimentalPyTorch implementationofCausalBertby Veitch, Sridar, and Blei (based onreference implementationby R. Pryzant)Installpip install -U pippip install causalnlpNOTE: On Python 3.6.x, if you get aRuntimeError: Python version >= 3.7 required, try ensuring NumPy is installedbeforeCausalNLP (e.g.,pip install numpy==1.18.5).UsageTo try out theexamplesyourself:Example: What is the causal impact of a positive review on a product click?import pandas as pd
df = pd.read_csv('sample_data/music_seed50.tsv', sep='\t', error_bad_lines=False)The filemusic_seed50.tsvis a semi-simulated dataset fromhere. Columns of relevance include:Y_sim: outcome, where 1 means product was clicked and 0 means not.text: raw text of reviewrating: rating associated with review (1 through 5)T_true: 0 means rating less than 3, 1 means rating of 5, whereT_trueaffects the outcomeY_sim.T_ac: an approximation of true review sentiment (T_true) created withAutocoderfrom raw review textC_true:confounding categorical variable (1=audio CD, 0=other)We'll pretend the true sentiment (i.e., review rating andT_true) is hidden and only useT_acas the treatment variable.Using thetext_colparameter, we include the raw review text as another "controlled-for" variable.from causalnlp import CausalInferenceModel
from lightgbm import LGBMClassifier
cm = CausalInferenceModel(df, 
                         metalearner_type='t-learner', learner=LGBMClassifier(num_leaves=500),
                         treatment_col='T_ac', outcome_col='Y_sim', text_col='text',
                         include_cols=['C_true'])
cm.fit()outcome column (categorical): Y_sim
treatment column: T_ac
numerical/categorical covariates: ['C_true']
text covariate: text
preprocess time:  1.1179866790771484  sec
start fitting causal inference model
time to fit causal inference model:  10.361494302749634  secEstimating Treatment EffectsCausalNLP supports estimation of heterogeneous treatment effects (i.e., how causal impacts vary across observations, which could be documents, emails, posts, individuals, or organizations).We will first calculate the overall average treatment effect (or ATE), which shows that a positive review increases the probability of a click by13 percentage pointsin this dataset.Average Treatment Effect(orATE):print( cm.estimate_ate() ){'ate': 0.1309311542209525}Conditional Average Treatment Effect(orCATE): reviews that mention the word "toddler":print( cm.estimate_ate(df['text'].str.contains('toddler')) ){'ate': 0.15559234254638685}Individualized Treatment Effects(orITE):test_df = pd.DataFrame({'T_ac' : [1], 'C_true' : [1], 
                        'text' : ['I never bought this album, but I love his music and will soon!']})
effect = cm.predict(test_df)
print(effect)[[0.80538201]]Model Interpretability:print( cm.interpret(plot=False)[1][:10] )v_music    0.079042
v_cd       0.066838
v_album    0.055168
v_like     0.040784
v_love     0.040635
C_true     0.039949
v_just     0.035671
v_song     0.035362
v_great    0.029918
v_heard    0.028373
dtype: float64Features with thev_prefix are word features.C_trueis the categorical variable indicating whether or not the product is a CD.Text is Optional in CausalNLPDespite the "NLP" in CausalNLP, the library can be used for causal inference on datawithouttext (e.g., only numerical and categorical variables). Seethe examplesfor more info.DocumentationAPI documentation and additional usage examples are available at:https://amaiya.github.io/causalnlp/How to CitePlease citethe following paperwhen using CausalNLP in your work:@article{maiya2021causalnlp,
    title={CausalNLP: A Practical Toolkit for Causal Inference with Text},
    author={Arun S. Maiya},
    year={2021},
    eprint={2106.08043},
    archivePrefix={arXiv},
    primaryClass={cs.CL},
    journal={arXiv preprint arXiv:2106.08043},
}