<jupyter_start><jupyter_text>Estimating AKM, Bias-Corrected AKM, and CRE TWFE models using PyTwoWay<jupyter_code># Navigate to parent folder for import
# Weird code needed for output to show
import os, sys
stdout = sys.stdout
sys.path.insert(1, os.path.join(sys.path[0], '..'))
sys.stdout = stdout
# Import sim_twfe_network to simulate data
from sim_twfe_network import sim_twfe_network as stwn
from twfe_network import twfe_network as twn<jupyter_output><empty_output><jupyter_text>First, get your data<jupyter_code># For the example, we simulate data
sim_data = stwn().sim_network()
display(sim_data)<jupyter_output><empty_output><jupyter_text>Second, create a twfe_network object using your data<jupyter_code># We need to specify a column dictionary to make sure columns are named correctly. You can also manually update column names yourself
col_dict = {'fid': 'fid', 'wid': 'wid', 'year': 'year', 'comp': 'comp'}
tw_net = twn(data=sim_data, col_dict=col_dict) # tw_net for two-way network<jupyter_output><empty_output><jupyter_text>Third, clean your data<jupyter_code>tw_net.clean_data()<jupyter_output><empty_output><jupyter_text>Fourth, refactor your data into event study format<jupyter_code>tw_net.refactor_es()<jupyter_output><empty_output><jupyter_text>Now we can run AKM<jupyter_code># Optional parameters
akm_params = {'ncore': 1, 'batch': 1, 'ndraw_pii': 50, 'ndraw_tr': 5, 'check': False, 'hetero': False, 'out': 'res_fe.json', 'con': False, 'logfile': '', 'levfile': '', 'statsonly': False}
akm_res = tw_net.run_akm_corrected(user_akm=akm_params)<jupyter_output>100%|██████████| 5/5 [00:00<00:00, 268.25it/s]<jupyter_text>Fifth, cluster your data for CRE estimates<jupyter_code># Optional parameters
KMeans_params = {'n_clusters': 10, 'init': 'k-means++', 'n_init': 500, 'max_iter': 300, 'tol': 0.0001, 'precompute_distances': 'deprecated', 'verbose': 0, 'random_state': None, 'copy_x': True, 'n_jobs': 'deprecated', 'algorithm': 'auto'} # These parameters are specifically for the KMeans algorithm
cluster_params = {'cdf_resolution': 10, 'grouping': 'quantile_all', 'year': None, 'user_KMeans': KMeans_params}
tw_net.cluster()<jupyter_output><empty_output><jupyter_text>Now we can run CRE<jupyter_code># Optional parameters
cre_params = {'ncore': 1, 'ndraw_tr': 5, 'ndp': 50, 'out': 'res_cre.json', 'posterior': False, 'wobtw': False}
cre_res = tw_net.run_cre()<jupyter_output><empty_output><jupyter_text>Finally, we can investigate the results<jupyter_code>display(akm_res)
display(cre_res)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14184397163120568, 'fraction_numerical': 0.01773049645390071, 'mean_word_length': 8.50936329588015, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '544092', 'license': 'MIT', 'n_tokens_mistral': 990, 'n_tokens_neox': 924, 'n_words': 224, 'path': 'docs/source/notebooks/.ipynb_checkpoints/PyTwoWay Example-checkpoint.ipynb', 'repo_name': 'economoser/pytwoway', 'size': '2538'}