# Cross correlation with R

I've done a cross correlation but I'm not sure if what I'm doing is correct ...

Here are the command lines I've used so far

(this one is for the first time series. It represents the numbers of subscriptions for men and women on a website during the hours of the day)

data <- read.table(text= "hour men women
00h00 475 295
01h00 321 157
02h00 206 127
03h00 141 61
04h00 73 29
05h00 49 22
06h00 71 30
07h00 100 32
08h00 163 55
09h00 219 126
10h00 300 199
11h00 342 255
12h00 407 247
13h00 480 334
14h00 459 358
15h00 481 281
16h00 490 347
17h00 458 309
18h00 599 475
19h00 618 419
20h00 579 453
21h00 565 530
22h00 659 605
23h00 600 435",header=TRUE)


(this one is for the second time series, which represents the time where ads have been shown on TV). I've add the time of the ads within a dummy time series. See this post for why I've done it in such manner : Time series and cross correlation )

hr <- read.table(text="hours
00h00
01h00
02h00
03h00
04h00
05h00
06h00
07h00
08h00
09h00
10h00
11h00
12h00
12h50
13h00
14h00
15h00
16h00
17h00
17h45
18h00
19h00
20h00
21h00
22h00
23h00
23h10",header=TRUE)


(Then I correlate the time series with the command line below)

ccf(data$men,hr,50)  And I'm having this graph (see below) but it doesn't make any sense for me. If you have any clue, I will be glad to hear them. • What are you trying to find out from this cross-correlation between the men's subscriptions and (an oddly modified) time? Are you sure it's what you wanted to do? – John Sep 4 '13 at 4:57 ## 1 Answer Your graph shows that as you shift hr and data.mem the peak positive correlation between them is when there is no lag of hr. Note that the x-axis is just units of shifting the values, your times are never used. In fact, none of them are even times, if you check str(data) you'll see that your time is just a factor. But hr is a data.frame with one factor in it called hours. I'm not sure why you want to do a cc with that. Perhaps you really wanted: ccf(data$men, data$women)  That's more reasonable. Note that the x-axis isn't time but can easily be translated because each value is evenly spaced. So, a shift of one is a shift of one hour. You'd have to worry about having a time series if you had uneven spacing. And what the graph shows is that men and women tend to be subscribing at around the same times. • Hi John, Thanks for answering. My aim is to see if there is an impact in term of the ad display and the subscriptions. By doing a cross correlation with ccf(data$men, data$women), I'm only doing a cross correlation between subscriptions of men and women and it is not what I want... – Andy K Sep 4 '13 at 5:42 • I've tried with ccf(data$men, data$women) and I have the same graphical shape as I've put in my question. – Andy K Sep 4 '13 at 5:49 • You need an ad display variable then. If that's what hr$hours was supposed to be then...it's not. Ads are either there or not there. Furthermore, you can't really do it with cc because what you want to do is predict subscriptions from ad display and perhaps sex. That's a completely different analysis. A couple of different kinds would work but you could do it with a chi-square contingency table with the counts entered in each cell for ad presence and sex. If you just want to test ad presence a chi-square goodness of fit would work. – John Sep 4 '13 at 16:08
• man ! Why my teacher told me to do a cross correlation then??? Thanks John. – Andy K Sep 5 '13 at 6:34
• Sorry for the rant. I spent 2 months on this ... and the frustration is very real. argh. #WorkingWithDataRant – Andy K Sep 5 '13 at 6:44