Skip to content
Prev 305445 / 398506 Next

calculate within-day correlations

On Thu, Sep 13, 2012 at 7:35 PM, emorway <emorway at usgs.gov> wrote:
<snip data>
Thanks for the reproducible example.  This is pretty simple with xts:
library(xts)
xQ <- xts(Q_use["Q"], Q_use$date)
xSC <- xts(SC_use["SC"], SC_use$date)
x <- merge(xQ,xSC)

Now all the dates for both data sets are aligned in 'x', so you can
use apply.daily() to run a function over each day:
apply.daily(x, function(y) cor(y[,1],y[,2],use="pairwise.complete.obs"))
                          [,1]
2002-03-28 23:45:00 -0.4916499
2002-03-29 23:45:00 -0.6085098
2002-03-30 23:45:00 -0.1489898
2002-03-31 00:00:00         NA

Note that I had to create a small anonymous wrapper function so I
could pass two objects to the cor() function.

Hope that helps.
Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com