Git Product home page Git Product logo

Comments (1)

samsunknight avatar samsunknight commented on June 24, 2024

Would it be correct to say that the current code takes a weighted difference between pre- and post-periods, but without estimating a full two-way fixed effects regression? From what I can tell, the estimation of the weights $\omega$, $\lambda$ and $\zeta$ look great, but I believe that the computation of $\tau$ as the difference between the weighted averages does not implement the two-way fixed effects (diff-in-diff) step as described in the paper.

I've adjusted my local version of the module with the following functions for estimating $\tau$:

  def regression_df(self):
    
    date_var = self.df.index.name
    melted_df = self.df.reset_index().melt(id_vars=date_var)
    id_var = melted_df.columns[1]
    melted_df['treatment'] = melted_df[id_var].isin(self.treatment).values * melted_df[date_var].isin(self.Y_post_t.index).values
    
    # sdid
    omega_weights, lambda_weights = self.estimated_params()
    melted_df = melted_df.merge(omega_weights.iloc[:-1].rename(columns={'features':id_var,
                                                                      'sdid_weight':'omega_weight'}),on=id_var,how='outer')
    melted_df = melted_df.merge(lambda_weights.rename(columns={'time':date_var,
                                                                      'sdid_weight':'lambda_weight'}),on=date_var,how='outer')
    
    melted_df['sdid_weight'] = melted_df['omega_weight'].fillna(1/len(self.treatment)) * melted_df['lambda_weight'].fillna(1/len(self.Y_post_t))
    
    # sc
    omega_weights_ADH = self.estimated_params('sc')
    melted_df = melted_df.merge(omega_weights_ADH.rename(columns={'features':id_var}),on=id_var,how='outer')
    melted_df['sc_weight'] = melted_df['sc_weight'].fillna(1/len(self.treatment))
    
    melted_df = melted_df.set_index([id_var,date_var])
    
    return melted_df

...

  def hat_tau(self, model="sdid"):
  
    """
    # adjusted from github to perform weighted TWFE regression 
    """
    
    regression_df = self.regression_df()
    

    if model == "sdid":
          
        regression_df_noZeroWeight = regression_df.loc[regression_df['sdid_weight']>0] # weights must be strictly positive for PanelOLS; as they're zero weight, OK to drop
        
        FE = PanelOLS(regression_df_noZeroWeight['value'], regression_df_noZeroWeight['treatment'],
        		  entity_effects = True, 
        		  time_effects = True,
                  weights = regression_df_noZeroWeight['sdid_weight'])
              
        result = FE.fit()
        		  
        tau_est = result.params[0]

    elif model == "sc":
      
        regression_df_noZeroWeight = regression_df.loc[regression_df['sc_weight']>0] # weights must be strictly positive for PanelOLS; as they're zero weight, OK to drop
        
        FE = PanelOLS(regression_df_noZeroWeight['value'], regression_df_noZeroWeight['treatment'],
        		  time_effects = True,
                  weights = regression_df_noZeroWeight['sc_weight'])
              
        result = FE.fit()
        
        tau_est = result.params[0]

I've also made a few smaller adjustments to plot this object differently, and to refactor the variance code to use these functions directly by making a deepcopy() of the given class instance and then reestimating the attribute objects, but the main changes were the above adjustments. Would be interested to hear if you think this is correct. Thanks again for building this code!

Here's the new version of the above graph with this new estimation method, which looks closer to expected:

Screen Shot 2023-03-07 at 9 44 24 PM

from pysynthdid.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.