[R] lm( y ~ A/x ) ... how do I extract the coefficients by factor?
WanderingWizard
bulkwood at hotmail.com
Sun Jul 4 23:46:15 CEST 2010
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.
More information about the R-help
mailing list