0

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?

Max
  • 117
  • 7
  • 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 Oct 22 '18 at 10:11
  • It means that period may change. like input may be in daily data form and expected output may monthly coefficients of each firm. – Max Oct 22 '18 at 10:16

2 Answers2

0

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) 
user2974951
  • 9,535
  • 1
  • 17
  • 24
  • thank you so much for your effort. But sorry it is not exactly what I want. If you check with my expected output, your result is not like that. rather it is producing too many outputs. – Max Oct 22 '18 at 10:35
  • If the table that you provided under your data is all that you need then see the edit, just change the * for +. This way you will get all the coefficients of mkreturn, plus all the constants. – user2974951 Oct 22 '18 at 10:41
  • see you are using tuple as an independent variable like mkreturn. However, tuple should be the groups and we need mkreturn coefficient of each group. In our case, we have four groups. So four mkreturn coefficients for each four group. – Max Oct 22 '18 at 10:49
  • Tuple needs to be included in the model to "group". I don't know where you see 16 groups, I only see 4. List all the groups that you think can be made from these. – user2974951 Oct 22 '18 at 10:59
  • ok that means you are saying tupleA-1 tupleB-0 tupleB-1 mkreturn ( these four coefficients produce from your code) are the mkreturn coefficients of each group? – Max Oct 22 '18 at 11:06
  • In that case, you should have three more intercept coefficients – Max Oct 22 '18 at 11:10
  • I got the solution. Please check my answer – Max Oct 22 '18 at 11:34
  • Thank you so much by the way. – Max Oct 22 '18 at 11:35
0

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
Max
  • 117
  • 7
  • You do realize that tuple here represents a random effect and not a fixed effect? – user2974951 Oct 22 '18 at 11:37
  • Sorry, I did not realize. I think I need to change the code for the tuple. It should be year firm group. Could you help me? then I can edit it. or you can edit your post – Max Oct 22 '18 at 11:54
  • Can't really help because I still don't understand what you are trying to do, I still believe that my answer is what you are looking for. – user2974951 Oct 22 '18 at 12:05
  • I have edited my answer. Now it is ok. You can check if you like – Max Oct 22 '18 at 12:55
  • This is a wrong approach, you built 4 different models on 4 separat groups, that's not how it's done (you lost information this way). Show us the output of these models in the answer. – user2974951 Oct 23 '18 at 05:13
  • I inserted the output. – Max Oct 23 '18 at 08:03
  • That is exactly what you should get if you use my code, 8 terms, 4 intercepts and 4 slopes. Do you get something different? – user2974951 Oct 23 '18 at 08:05
  • I have checked it by excel one by one and giving same result. so result is correct. – Max Oct 23 '18 at 08:09