[R] lm( y ~ A/x ) ... how do I extract the coefficients by factor?

Bill.Venables at csiro.au Bill.Venables at csiro.au
Mon Jul 5 00:48:15 CEST 2010


It's a bit like gravy.  You have to make it, it doesn't "just come" when you cook the joint.

Here is a mock-up of your situation:

> dat <- data.frame(ByMonth = gl(6, 10, labels = paste("2010-0", 1:6, sep="")), 
+   r1 = rnorm(60), v = rnorm(60), r = rnorm(60))
> head(dat)
  ByMonth         r1          v           r
1 2010-01 -1.2328644  1.0877184 -1.36304660
2 2010-01 -0.6852108 -0.5373950 -0.05885205
3 2010-01 -0.5269571 -0.2035928  1.20186345
4 2010-01  1.1226140 -0.4533569 -0.35883730
5 2010-01  0.2229836  0.5185975  0.06541686
6 2010-01  0.7999289  0.5695903  0.67197424
> ## looks OK. Now fit the regressions omiting a grand mean term
> ## note especially the 'subset' argument, which omits the first record
> ## as you seem to want to do
> fm <- lm(r1 ~ ByMonth/(r+v) -1, dat, subset = -1)
> summary(fm)$coef
                    Estimate Std. Error     t value  Pr(>|t|)
ByMonth2010-01    0.50509121  0.3319875  1.52141636 0.1358311
ByMonth2010-02   -0.00967993  0.2971797 -0.03257265 0.9741734
ByMonth2010-03   -0.12691181  0.3002052 -0.42275027 0.6746865
ByMonth2010-04    0.31516801  0.3034831  1.03850264 0.3051221
ByMonth2010-05    0.02638460  0.5182775  0.05090824 0.9596459
ByMonth2010-06   -0.16056032  0.3292923 -0.48759210 0.6284360
ByMonth2010-01:r -0.32589472  0.2967119 -1.09835423 0.2784582
ByMonth2010-02:r -0.03650704  0.2036778 -0.17923914 0.8586328
ByMonth2010-03:r  0.20627253  0.3605508  0.57210401 0.5703753
ByMonth2010-04:r  0.04537062  0.3257498  0.13928058 0.8899104
ByMonth2010-05:r -0.15926998  0.4748852 -0.33538628 0.7390438
ByMonth2010-06:r  0.11647989  0.3378386  0.34477966 0.7320225
ByMonth2010-01:v  1.11974211  0.6781761  1.65110814 0.1063569
ByMonth2010-02:v  0.35369142  0.2245097  1.57539503 0.1228511
ByMonth2010-03:v -0.01547968  0.3042272 -0.05088197 0.9596667
ByMonth2010-04:v  0.01473308  0.2993017  0.04922484 0.9609791
ByMonth2010-05:v -0.39752574  0.3303060 -1.20350755 0.2356831
ByMonth2010-06:v -0.29591044  0.3837625 -0.77107698 0.4450825
> ## that't the meat, now to make the gravy:
> B <- matrix(coef(fm), nrow = 6)
> dimnames(B) <- list(levels(dat$ByMonth), c("Intercepts", "r", "v"))
> B
         Intercepts           r           v
2010-01  0.50509121 -0.32589472  1.11974211
2010-02 -0.00967993 -0.03650704  0.35369142
2010-03 -0.12691181  0.20627253 -0.01547968
2010-04  0.31516801  0.04537062  0.01473308
2010-05  0.02638460 -0.15926998 -0.39752574
2010-06 -0.16056032  0.11647989 -0.29591044
>  ## pretty much as you wanted it.

BTW, it would be courteous to give us your real name.  

Bill Venables.

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of WanderingWizard
Sent: Monday, 5 July 2010 7:46 AM
To: r-help at r-project.org
Subject: [R] lm( y ~ A/x ) ... how do I extract the coefficients by factor?


When regressing by month, how do I get the coefficients out into a new data
set?

I'm looking for 
        [ month, a, b, c ] 
from the Pastor-Stambaugh model I'm using which is: 
        r[i+1] = a + b * r[i] + c * v[i] + e
the model I'm using wants to create a new dataseries based on the
coefficient in each month.  I'm doing a simple linear regression on DataSet,
and 

> DataSet$ByMonth <- format(DataSet$d, "%Y-%m");
> fittedmodel <- lm(r1 ~ ByMonth[2:n-1] / (r + v), x[2:n-1,1:10] )

I've think I've done it, and I think I've got the results:

> summary(fittedmodel$coefficients)
      Min.    1st Qu.     Median       Mean    3rd Qu.       Max. 
-2.896e-01 -6.624e-04 -4.051e-11  4.720e-02  2.748e-03  1.086e+00 

but what am I to do with these results?  fittedmodel$coefficients doesn't
know the month from the regression result.  It looks like this

> fittedmodel

Call:
lm(formula = r1 ~ ByMonth[2:n - 1]/(r + v), data = x[2:n - 1, 1:10])

Coefficients:
                                 (Intercept)  
                                  -8.833e-04  
                     ByMonth[2:n - 1]2010-02  
                                   1.432e-03  
                     ByMonth[2:n - 1]2010-03  
                                  -1.346e-03  
                     ByMonth[2:n - 1]2010-04  
                                   1.933e-03  
                     ByMonth[2:n - 1]2010-05  
                                   5.113e-03  
                     ByMonth[2:n - 1]2010-06  
                                   3.020e-03  
        ByMonth[2:n - 1]2010-01:r  
                                  -2.896e-01  
        ByMonth[2:n - 1]2010-02:r  
                                   1.260e-01  
        ByMonth[2:n - 1]2010-03:r  
                                  -6.284e-03  
        ByMonth[2:n - 1]2010-04:r  
                                   1.086e+00  
        ByMonth[2:n - 1]2010-05:r  
                                  -1.566e-01  
        ByMonth[2:n - 1]2010-06:r  
                                   8.081e-02  
ByMonth[2:n - 1]2010-01:v  
                                  -1.747e-10  
ByMonth[2:n - 1]2010-02:v  
                                  -3.324e-10  
ByMonth[2:n - 1]2010-03:v  
                                   9.430e-11  
ByMonth[2:n - 1]2010-04:v  
                                  -1.571e-09  
ByMonth[2:n - 1]2010-05:v  
                                   9.364e-11  
ByMonth[2:n - 1]2010-06:v  
                                  -4.069e-10  
----

Did I do what I think I did?  It's great that R-project will split up all
those regressions for me, but I've had a hard time searching for examples of
any linear regressions grouped by factor.  I'd like a data series with each
month and each regression result.  Something more like this:

Month     	(Intercept)  	r            	v
2010-01  	-8.83E-004	-2.90E-001	-1.75E-010
2010-02  	1.43E-003	1.26E-001	-3.32E-010
2010-03  	-1.35E-003	-6.28E-003	9.43E-011
2010-04  	1.93E-003	1.09E+000	-1.57E-009
2010-05  	5.11E-003	-1.57E-001	9.36E-011
2010-06  	3.02E-003	8.08E-002	-4.07E-010


How do I translate that unwieldy factor-wise list of coefficients (from my
sample output in the middle) into a matrix or dataframe (like my table at
the end) that I can then work with and treat as a new data series?


-- 
View this message in context: http://r.789695.n4.nabble.com/lm-y-A-x-how-do-I-extract-the-coefficients-by-factor-tp2277925p2277925.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org 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.



More information about the R-help mailing list