I posted this question yesterday but received some valuable feedback that my post left a little to be desired :). Here is an updated attempt that hopefully is much clearer:
I have a xts zoo object and would like to determine the t-statistic of the slope coefficient over the last 20 periods using R and then test whether that t value is > 2.
class(prices)
[1] "xts" "zoo"
tail(prices)
IWM SPY TLT
2012-10-24 81.20 141.02 121.48
2012-10-25 81.53 141.43 120.86
2012-10-26 81.14 141.35 122.64
2012-10-31 81.63 141.35 123.36
2012-11-01 82.49 142.83 122.35
2012-11-02 81.19 141.56 122.26
I could not figure out how to perform a regression for each column versus time so I created a new column (daynumber) with the index and performed the regression versus the index:
lastprices = last(prices,20)
prices.data.frame = as.data.frame(lastprices)
daynumber = index(prices.data.frame)
pdfd = data.frame(prices.data.frame, daynumber)
pdfd.lm = lm(pdfd$daynumber ~ ., data=pdfd)
tstat = coef(summary(pdfd.lm))
tstat[,"t value"]
(Intercept) IWM SPY TLT
4.5426630 -0.1788975 -1.3521969 -2.2362345
tstattest = ifelse(tstat[,"t value"]>2,1,0)
tstattest
(Intercept) IWM SPY TLT
1 0 0 0
I cant help but think this is not the most efficient way to accomplish this task. Does anyone have ideas on how to just perform the regression for each column versus time without creating the daynumber column?
Thanks – take it easy on me I just started learning!
I think your original problem had made useful progress. What I thought was an output format that came from a unnamed package was really the output of lm where there were multiple columns on hte LHS of the formula. What I have done with your second version is this:
Notice that there are no t-values in that output. the summary.lm function will create those:
To extract the t-values from that list of three matrices you can use lapply:
To get just the intercepts you can use this:
PLEASE note that the Intercept value has almost no meaning in this context. If you use the numbers 1:6 you get one intercept and if you use the Dates-classes value you get a completely different value. The t-test is on whether the intercept is zero, and since the scale is rather arbitrary it has no interpretation unless you are very sure of the coding and what a zero-value would imply.