I just start using R for statistical analysis and I am still learning. I have an issue with creating loops in R. I have the following case and I was wondering if any one can help me with it. For me, it seems impossible but for some of you, it is just a piece of cake. I have a dataset for different firms across different years. for each firm I have different observations for the same year and I need to run the following regression for each firm for each year (I have more than 1000 firms and it seems impossible to run the regression for each firm separately) :
Ri = α0 + β1Rm + β2Rz + Ɛ
the data I have looks like the following example:
Year Firm Ri Rm Rz
2009 A 30 55 85
2009 A 11 55 85
2009 A 1 55 85
2010 A 7 55 85
2010 A 15 55 85
2011 A 20 55 85
2011 A 3.5 55 85
2011 A 8 55 85
2009 B 24 55 85
2009 B 30 55 85
2009 B 25 55 85
2010 B 5.2 55 85
2010 B 11.8 55 85
2011 B 78 55 85
2011 B 90 55 85
2011 B 57 55 85
I need to obtain B1, B2 and the error term Ɛ for each firm for each year. just like this:
Year Firm B1 B2 Ɛ
2009 A 0.30 0.55 0.85
2010 A 0.11 0.55 0.85
2011 A 0.1 0.55 0.85
2009 B 0.7 0.55 0.85
2010 B 0.15 0.55 0.85
2011 B 0.20 0.55 0.85
Thank you in advance for your help