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!