简体   繁体   中英

Cross-correlation (Amount and signifacence) in R

I have the time series data in the following format:

   hour1  hour2  hour3  hour4  hour5  hour6  hour7  hour8  hour9 hour10 hour11 hour12 hour13 hour14 hour15 hour16 hour17
1  1.300  0.888  0.792  0.324  0.650  0.670  0.836  1.018  1.220  1.258  1.744  2.302  2.582  3.182  3.962  4.630  4.378
2 -0.170 -0.534 -0.564 -1.022 -0.774 -0.660 -0.524 -0.344 -0.100 -0.064  0.024  0.614  1.360  2.072  2.722  3.330  3.398
3 -0.344 -0.684 -0.622 -1.128 -0.982 -0.844 -0.604 -0.432 -0.124  0.016  0.110  0.600  1.392  2.150  3.404  4.028  4.084
4  2.138  1.442  0.998  1.380  1.124  1.248  0.844  1.344  1.542  1.392  1.820  2.626  2.316  2.784  3.890  4.290  4.280
5  0.684 -0.178 -0.320 -0.792 -0.540 -0.550 -0.410 -0.278 -0.182  0.024  0.400  0.992  1.328  1.682  2.488  3.176  3.260
6  0.656 -0.060  1.858  1.042  3.200  2.868  1.154  1.388  1.472  2.092  2.382  3.848  3.214  3.952  4.576  5.340  4.386
  hour18 hour19 hour20 hour21 hour22 hour23 hour24
1  5.196  5.850  6.316  3.612  5.434  5.712  5.468
2  4.090  4.320  5.660  2.818  4.516  5.026  4.498
3  5.634  4.998  5.566  2.788  4.830  5.400  5.114
4  6.126  5.366  6.392  3.184  4.336  4.198  4.764
5  4.494  4.802  5.484  2.306  4.234  4.410  4.626
6  5.906  6.004  6.250  3.790  6.158  6.926  6.522

I would like to perform the cross-correlation analysis and I have used acf(od2) but I have faced with this error

Error in acf(od2) : 'lag.max' must be at least 0

Besides, how I can extract the correlation amount and significance automatically.

I guess you want this:

DF <- read.table(text="  hour1  hour2  hour3  hour4  hour5  hour6  hour7  hour8  hour9 hour10 hour11 hour12 hour13 hour14 hour15 hour16 hour17 hour18 hour19 hour20 hour21 hour22 hour23 hour24
1  1.300  0.888  0.792  0.324  0.650  0.670  0.836  1.018  1.220  1.258  1.744  2.302  2.582  3.182  3.962  4.630  4.378 5.196  5.850  6.316  3.612  5.434  5.712  5.468
2 -0.170 -0.534 -0.564 -1.022 -0.774 -0.660 -0.524 -0.344 -0.100 -0.064  0.024  0.614  1.360  2.072  2.722  3.330  3.398 4.090  4.320  5.660  2.818  4.516  5.026  4.498
3 -0.344 -0.684 -0.622 -1.128 -0.982 -0.844 -0.604 -0.432 -0.124  0.016  0.110  0.600  1.392  2.150  3.404  4.028  4.084 5.634  4.998  5.566  2.788  4.830  5.400  5.114
4  2.138  1.442  0.998  1.380  1.124  1.248  0.844  1.344  1.542  1.392  1.820  2.626  2.316  2.784  3.890  4.290  4.280 6.126  5.366  6.392  3.184  4.336  4.198  4.764
5  0.684 -0.178 -0.320 -0.792 -0.540 -0.550 -0.410 -0.278 -0.182  0.024  0.400  0.992  1.328  1.682  2.488  3.176  3.260 4.494  4.802  5.484  2.306  4.234  4.410  4.626
6  0.656 -0.060  1.858  1.042  3.200  2.868  1.154  1.388  1.472  2.092  2.382  3.848  3.214  3.952  4.576  5.340  4.386 5.906  6.004  6.250  3.790  6.158  6.926  6.522",header=TRUE)


m <- t(as.matrix(DF))
acf(m)

Or possibly this:

lapply(seq_len(ncol(m)),
         function(i) lapply(seq_len(ncol(m)), 
            function(j) ccf(drop(m[,i]),drop(m[,j]))))

(which is pretty much the same as print(acf(m)) , I believe)

Or just this:

sapply(seq_len(ncol(m)), 
         function(i) sapply(seq_len(ncol(m)), 
            function(j) cor(drop(m[,i]),drop(m[,j]))))

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