简体   繁体   中英

Rolling correlation between zoo objects of unequal size

I have two zoo objects of unequal size (inflow and outflow). Outflow values lag inflow by some unknown amount of time. I would like to determine the correlation between the smaller outflow object (6 rows) and the larger inflow object (many rows), 6 rows at a time, starting at the inflow beginning time, incrementing by one, and find the period of highest correlation. I think this is called a "sliding window" comparison. I've tried many different ways, unsuccessfully, to use the "rollapply" function to do this but get an error because the size difference between the two objects. Hope someone will understand what I'm trying to ask and can offer a solution. Below is a portion of my data and an example of how I have tried to use the rollapply function.

> inflow
(03/14/13 07:00:00) 11.20451
(03/14/13 07:02:00) 11.03810
(03/14/13 07:04:00) 11.03012
(03/14/13 07:06:00) 11.09517
(03/14/13 07:08:00) 10.90878
(03/14/13 07:10:00) 11.23285
(03/14/13 07:12:00) 11.14890
(03/14/13 07:14:00) 11.17002
(03/14/13 07:16:00) 11.38342
(03/14/13 07:18:00) 11.70833
(03/14/13 07:20:00) 11.93776
(03/14/13 07:22:00) 12.17832
(03/14/13 07:24:00) 12.39648
(03/14/13 07:26:00) 12.24020
(03/14/13 07:28:00) 12.18667
(03/14/13 07:30:00) 12.45410
(03/14/13 07:32:00) 12.50012
(03/14/13 07:34:00) 12.54736
(03/14/13 07:36:00) 13.05010
(03/14/13 07:38:00) 13.06495
(03/14/13 07:40:00) 13.14084
(03/14/13 07:42:00) 12.92427
(03/14/13 07:44:00) 12.98699
(03/14/13 07:46:00) 12.84172
(03/14/13 07:48:00) 12.87263
(03/14/13 07:50:00) 12.51861
(03/14/13 07:52:00) 12.98763
(03/14/13 07:54:00) 12.31124
(03/14/13 07:56:00) 12.33696
(03/14/13 07:58:00) 12.49630
(03/14/13 08:00:00) 12.40648
(03/14/13 08:02:00) 11.87164
(03/14/13 08:04:00) 12.76058
(03/14/13 08:06:00) 12.50016
(03/14/13 08:08:00) 12.68696
(03/14/13 08:10:00) 12.88447
(03/14/13 08:12:00) 12.33336
(03/14/13 08:14:00) 13.06670
(03/14/13 08:16:00) 13.15070
(03/14/13 08:18:00) 12.82410
(03/14/13 08:20:00) 12.91953

outflow2

(03/14/13 07:54:00) (03/14/13 07:56:00) (03/14/13 07:58:00) (03/14/13 08:00:00) (03/14/13 08:02:00) (03/14/13 08:04:00) 
           11.51110            11.11878            11.05775            11.11303            10.95417            10.98035

Use:

> test <- rollapply(inflow, width = 6, by = 1, FUN = cor(inflow, outflow))
Error in cor(inflow, outflow) : incompatible dimensions

Try this:

rollapply(inflow, 6, cor, y = outflow)

This computes

value <- c( cor(inflow[1:6], outflow), cor(inflow[2:7], outflow), ...etc... )
ix <- seq(3, length = length(inflow) - 6 + 1)
zoo(value, time(inflow)[ix])

Depending on what you want to get out you may need the align= argument too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM