0
votes

I have a panel data frame like this. where return is the dependent variable and mkreturn is the independent variable.

Firm date return mkreturn
A   Jan-07  7     12
A   Feb-07  8     14
A   Mar-07  2     16
A   Apr-07  4     18
A   May-07  2     20
A   Jun-07  5     15
A   Jul-07  3     17
A   Aug-07  4     13
A   Sep-07  2     19
A   Oct-07  5     20
A   Nov-07  4     21
A   Dec-07  6     13
A   Jan-08  7     21
A   Feb-08  7     33
A   Mar-08  5     12
A   Apr-08  5     12
A   May-08  5     15
A   Jun-08  4     17
A   Jul-08  4     13
A   Aug-08  4     12
A   Sep-08  2     12
A   Oct-08  3     17
A   Nov-08  4     15
A   Dec-08  7     10
B   Jan-07  12    12
B   Feb-07  4     14
B   Mar-07  10    16
B   Apr-07  9     18
B   May-07  9     20
B   Jun-07  13    15
B   Jul-07  7     17
B   Aug-07  14    13
B   Sep-07  21    19
B   Oct-07  13    20
B   Nov-07  12    21
B   Dec-07  18    13
B   Jan-08  13    21
B   Feb-08  16    33
B   Mar-08  14    12
B   Apr-08  5     12
B   May-08  21    15
B   Jun-08  12    17
B   Jul-08  16    13
B   Aug-08  11    12
B   Sep-08  11    12
B   Oct-08  10    17
B   Nov-08  15    15
B   Dec-08  9     10

Now I want to find out the yearly regression coefficient like this.(However, data may be daily and expected output may be monthly)

             mkreturn coeff
2007    A         ……
2008    A         ……
2007    B         ……
2008    B         ……

Could you please help me with this regard?

2
I don't know what "However, data may be daily and expected output may be monthly" means, but it looks like you need interactions lm(return~Firm*mkreturn). - user2974951
It means that period may change. like input may be in daily data form and expected output may monthly coefficients of each firm. - Max

2 Answers

0
votes

year column

test$Year <- format(test$date,format="%Y")

create groups each Year-Firm

test$id <- paste(test$Firm, test$Year, sep="_")

run GroupWise regression by using lme4 package. Here id is the group column

library(lme4)
fits <- lmList(return ~ mkreturn | id, data=df)
coefficients(fits)

output

       (Intercept)      mkreturn
A_2007   10.501558 -3.738318e-01
A_2008    2.967155  1.131965e-01
B_2007   11.833333 -6.869076e-16
B_2008    9.230499  2.234604e-01
0
votes

If I understand this right (your data frame's name is df)

#create new variable which is a combination of firm and date    
df$tuple=factor(paste0(df$Firm,substr(df$date,5,6))) 

#linear model with interatcions
lm(return~tuple*mkreturn,data=df)