From cb67c48986779bbc3096b64034e624dd3d33b2cc Mon Sep 17 00:00:00 2001 From: Frank Date: Tue, 11 Mar 2025 17:01:00 -0600 Subject: [PATCH 1/2] Resolviendo conflicto en rct-157104.py --- assignments/rct/code/rct-157104.py | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 assignments/rct/code/rct-157104.py diff --git a/assignments/rct/code/rct-157104.py b/assignments/rct/code/rct-157104.py new file mode 100644 index 00000000..f50a13aa --- /dev/null +++ b/assignments/rct/code/rct-157104.py @@ -0,0 +1,48 @@ +# Imports +import os +import pandas as pd +import statsmodels.api as sm + +# Load data +PATH = os.path.join('..', 'data', 'raw.csv') +df = pd.read_csv(PATH) + + # Rename columns +df.columns = ['id', 'dark', 'views', 'time', 'purchase', 'mobile', 'location'] + +# Map columns to numeric types +df.replace( + to_replace={ + 'dark':{'A':'0', 'B':'1'}, + 'mobile':{'Mobile':'1', 'Desktop':'0'}, + 'purchase':{'No':'0', 'Yes':'1'}, + 'location':{'Northern Ireland':'Ireland'} + }, + inplace=True +) + +# Convert strings -> ints +df[['dark', 'mobile', 'purchase']] = df[['dark', 'mobile', 'purchase']].astype(int) +# A minusculas +df['location'] = df['location'].str.lower() + +# One-hot encoding +df = pd.get_dummies( + data = df, + prefix = '', + prefix_sep = '', + columns = ['location'], + dtype = int +) + +# Constant +df['const'] = 1 + +# Declare linear model +spec = sm.OLS( + endog = df['purchase'], + exog = df[['const', 'ireland','scotland','wales','dark']], + hasconst= True +) + +model = spec.fit() \ No newline at end of file From c4bbfefe703760c0de7b1941db2515441057a99f Mon Sep 17 00:00:00 2001 From: Frank Date: Mon, 19 May 2025 12:00:36 -0600 Subject: [PATCH 2/2] Intento --- assignments/did/code/did-157104.py | 94 ++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 assignments/did/code/did-157104.py diff --git a/assignments/did/code/did-157104.py b/assignments/did/code/did-157104.py new file mode 100644 index 00000000..c69c5e40 --- /dev/null +++ b/assignments/did/code/did-157104.py @@ -0,0 +1,94 @@ +import os +import pandas as pd +import numpy as np +from linearmodels.panel import PanelOLS + +# Cargar datos +data_path = os.path.join("..", "data", "callaway-santanna.csv") +df = pd.read_csv(data_path) + +# Renombrar columnas +df.rename(columns={ + "year": "t", + "countyreal": "i", + "first.treat": "treat_start" +}, inplace=True) + +# Variable de tiempo "k" +df.loc[df['treat_start'] == 0, 'treat_start'] = np.nan +df["k"] = df["t"] - df["treat_start"] + +# Event dummies w/Nans +k_dummies = pd.get_dummies(df["k"], prefix="k", dummy_na=True, dtype=int) +df = pd.concat([df, k_dummies], axis=1) + +# Indices +df = df.set_index(["i", "t"]) + +# ATT +mask_treated = df['treat_start'].notna() +mask_full_panel = ~df['lemp'].isna().groupby(level='i').any() +valid_units = mask_full_panel[mask_full_panel].index +mask = mask_treated & df.index.get_level_values('i').isin(valid_units) +df_att = df[mask].copy() +all_k_dummies = [col for col in df_att.columns if col.startswith('k_')] + +# Periodo referencia +k_dummies = [k for k in all_k_dummies if k not in ['k_-1.0', 'k_nan']] + +# Dummies +k_dummies_final = [k for k in k_dummies if df_att[k].nunique() > 1] +indep = df_att[k_dummies_final] + +spec0 = PanelOLS( + df_att["lemp"], + indep, + entity_effects=True, + time_effects=True, + drop_absorbed=True +) + +res0 = spec0.fit(cov_type="clustered") + +restriction0 = np.array([ + [1, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0], + [0, 0, 1, 0, 0, 0] +]) +values0 = np.array([0, 0, 0]) +f0=res0.wald_test(restriction0, values0) + +anticipation0 = False +att0 = '-' + +# ATT - All units +vars1 = [col for col in df.columns if col.startswith('k_') and col != 'k_-1.0'] +# Definir dep1 e indep +dep1 = df["lemp"] +indep1 = df[vars1] + +# +m1 = PanelOLS( + dependent=dep1, + exog=indep1, + entity_effects=True, + time_effects=True, + drop_absorbed=True +) + +# Estimar modelo +res1 = m1.fit(cov_type="clustered") + +# Probar efectos anticipados +restriction1 = np.array([ + [1, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 0], + [0, 0, 1, 0, 0, 0, 0] +]) +values1 = np.array([0, 0, 0]) +f1 = res1.wald_test(restriction1, values1) + +# Anticipation 1 effect +anticipation1 = False +# Signo git aAt1 +att1 = '-' \ No newline at end of file