Here are three ways. The first uses a common
error term and the others separate. They all
give the same coefficient estimates.
Lines <- "Month ExcessReturn Return STO
8 0.047595875 0.05274292 0.854352503
8 0.016134874 0.049226941 4.399372005
8 -0.000443869 0.004357305 -1.04980297
9 0.002206554 -0.089068828 0.544809429
9 0.021296551 0.003795071 0.226875834
9 0.006741578 0.014104606 0.721986383
"
# DF <- read.table("myfile.dat", header = TRUE)
DF <- read.table(textConnection(Lines), header = TRUE)
DF$Month <- factor(DF$Month)
# 1 - common error term
lm(ExcessReturn ~ Month/(Return + STO) - 1, DF)
# 2
lapply(levels(DF$Month),
function(x) lm(ExcessReturn ~ Return + STO, DF, subset = Month == x)
)
# 3
library(nlme)
lmList(ExcessReturn ~ Return + STO | Month, DF, pool = FALSE)
On 5/28/07, Benoit Chemineau <benoitchemineau at gmail.com>
wrote:> Hi R-programmers !
>
> I would like to perform a linear model regression month by month using the
> 'lm' function and i don't know how to do it.
>
> The data is organised as below:
> Month ExcessReturn Return STO
> 8 0.047595875 0.05274292 0.854352503
> 8 0.016134874 0.049226941 4.399372005
> 8 -0.000443869 0.004357305 -1.04980297
> 9 0.002206554 -0.089068828 0.544809429
> 9 0.021296551 0.003795071 0.226875834
> 9 0.006741578 0.014104606 0.721986383
>
> the model is:
> ExcessReturn= a + b1*Return + b2*STO + u, u is the error term, a is the
> intercept.
>
> I would like to get two vectors of estimates (each of them containing the
> estimation of b1 and b2) using the least squares estimation.
>
> I tried it using 'aggregate? but it didn't seem to work.
>
> Thank you for your help !
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>