pythonlinearmodels

How to include year fixed effect (in a daily panel data)


I am working on a panel dataset that includes daily stock returns of 450 firms for 5 years and daily ESG score(momentum based) for 5 years. I want to regress stock return on daily ESG scores, keeping Firm and year fixed effect. I have used linearmodels.panel function in python and set the index('Stock ticker", "Date") before running the regressions with entity and time effects. In the regression result, the number of entities shows 450, which is perfect but the time period shows 1800. I am wondering how python is capturing the time effects? Is it based on year or some other way? What I want is a year fixed effects, where for a particular year all firm will have same indicator variable. Can someone please help me to do it in the right way? the image shows the format of the data, where panel is based on daily returns


Solution

  • Sounds like your model is capturing daily fixed effects instead of yearly fixed effects. This is happening because you set Date as an index, so you're telling Python that you want one fixed effect per date.

    You have to create a new column that only contains the year. That is, convert the date column to datetime format (see pandas.to_datetime) and then:

    # Extract year from Date
    df['Year'] = pd.DatetimeIndex(df['Date']).year
    
    # Set indices
    df = df.set_index(['Ticker','Year'])
    

    Then run your model.

    I recommend using linearmodels.PanelOLS because that module is specifically made for fitting fixed effects models.

    For future reference, post your code and a replicable example so we can help you out more easily.