简体   繁体   English

每30天如何计算和plot的相关系数

[英]How to calculate and plot correlation coefficient every 30 days

I would like to calculate the correlation coefficient every 30 days and then plot it as a line plot. My data.table is based on daily time data from 2018 to today.我想每 30 天计算一次相关系数,然后 plot 作为一条线 plot。我的 data.table 是基于从 2018 年到今天的每日时间数据。 The data.table looks like this: data.table 看起来像这样:

在此处输入图像描述

The correlation should be calculated between the prod1 and prod2 column.应该计算 prod1 和 prod2 列之间的相关性。

What could the code for that look like?它的代码是什么样的? The 30 days are just an example, it could also be that I want to calculate the correlation coefficient every 100 days and then plot it. 30天只是一个例子,也可能是我想每100天计算一次相关系数,然后是plot。 The plot should show the time data on the x-axis and the correlation coefficients on the y-axis. plot 应该在 x 轴上显示时间数据,在 y 轴上显示相关系数。

Try this solution.试试这个解决方案。 I will use some dummy data.我将使用一些虚拟数据。 The function rollapplyr() from zoo package can be useful for what you want and inside of it you can define the size of the window for correlations.来自zoo package 的 function rollapplyr()可用于您想要的内容,您可以在其中定义 window 的大小以进行关联。 Here the code:这里的代码:

library(dplyr)
library(zoo)
library(ggplot2)
#Seed for randomness in dummy data
set.seed(123)
#Data
df <- data.frame(Date=seq(as.Date('2020-01-01'),by='1 day',length.out = 180),
                 Germany=rnorm(180,2,1),
                 WeeklyMeanSpot=rnorm(180,5,2))
#Compute correlation
#Correlation function
corf <- function(z) cor(z[,1],z[,2])
#Compute
df2 <- df %>%
  mutate(Cor = rollapplyr(cbind(Germany, WeeklyMeanSpot),
                          30, corf, by.column = FALSE, fill = NA))
#Plot
ggplot(df2,aes(x=Date,y=Cor,group=1))+
  geom_line()

The output: output:

在此处输入图像描述

The output for df2 with computed correlation is next:接下来是具有计算相关性的df2的 output:

          Date     Germany WeeklyMeanSpot          Cor
1   2020-01-01  1.19566840      3.6946726           NA
2   2020-01-02  0.94347435      6.6512156           NA
3   2020-01-03  0.96460422      6.4999068           NA
4   2020-01-04  0.81443965      5.4603047           NA
5   2020-01-05  1.49956049      8.3968345           NA
6   2020-01-06  1.47501129      5.6317127           NA
7   2020-01-07  1.69756704      1.2517788           NA
8   2020-01-08  2.47196813      6.2125895           NA
9   2020-01-09  1.75161605      6.7580369           NA
10  2020-01-10  3.25931803      2.8137961           NA
11  2020-01-11  2.35092096      4.5250905           NA
12  2020-01-12  2.86331855      4.0167231           NA
13  2020-01-13  1.52108869      6.2325693           NA
14  2020-01-14  0.81152078      4.5071424           NA
15  2020-01-15  1.73143363      6.8472478           NA
16  2020-01-16  1.91977894      3.5537041           NA
17  2020-01-17  1.87676431      6.2689551           NA
18  2020-01-18  1.90056019      7.6625696           NA
19  2020-01-19  1.16524602      6.8619390           NA
20  2020-01-20  3.85777954      8.2171442           NA
21  2020-01-21  1.50160298      4.4852652           NA
22  2020-01-22  0.72994539      3.7480259           NA
23  2020-01-23  1.06305629      4.3896943           NA
24  2020-01-24  2.53343924      4.6650263           NA
25  2020-01-25  0.38323689      6.9921718           NA
26  2020-01-26  1.68294336      7.3718035           NA
27  2020-01-27  1.58023632      3.2206476           NA
28  2020-01-28  1.81914418      5.0806198           NA
29  2020-01-29  2.80181121      3.7171291           NA
30  2020-01-30  1.24608210      5.1656254 -0.069328611
31  2020-01-31  2.17777470      9.6932622 -0.041998773
32  2020-02-01  2.20834285      4.4412259 -0.031939643
33  2020-02-02  2.80313722     -0.5411659 -0.126551727
34  2020-02-03  0.40491851      5.3073543 -0.120457790
35  2020-02-04  3.59958366      4.4275978 -0.118749657
36  2020-02-05  2.49927063      5.6215082 -0.108412518
37  2020-02-06  2.36761410      4.1901185 -0.143451075
38  2020-02-07  1.58946984      4.6777601 -0.151660231
39  2020-02-08  2.92735042      6.3879691 -0.118660216
40  2020-02-09  2.86451608      7.5402940 -0.014142570
41  2020-02-10  2.72215229      6.5314283  0.010730700
42  2020-02-11  2.27126555      3.0690963  0.020957694
43  2020-02-12  2.31410815     -0.5702206 -0.009844143
44  2020-02-13  2.75552375      4.8392687 -0.027487761
45  2020-02-14  1.81084036      7.4972097 -0.027565466
46  2020-02-15  2.37724269      4.6652200 -0.034667100
47  2020-02-16  1.59176705      2.6149900 -0.009356898
48  2020-02-17  1.73390789      2.8832420  0.009686187
49  2020-02-18  2.08626516      5.0319071  0.043278282
50  2020-02-19  1.80148730      4.4002390 -0.081224484
51  2020-02-20  2.56108773      1.9128418 -0.111529257
52  2020-02-21  2.44717246      6.4163411 -0.129383738
53  2020-02-22  1.73245612      2.3592017 -0.118695448
54  2020-02-23  1.68611598      5.9183319 -0.129440483
55  2020-02-24  1.38920248      5.4855287 -0.061359523
56  2020-02-25  2.11631857      7.2027532 -0.032894093
57  2020-02-26  1.19588499      4.9261229 -0.057696575
58  2020-02-27  1.79937433      3.7816037 -0.047373869
59  2020-02-28  2.33899698      4.2787117 -0.034553964
60  2020-02-29  2.33822137      6.2447491 -0.016695285
61  2020-03-01  1.29077058      3.3391708  0.005120906
62  2020-03-02  2.52296888      5.8291996  0.019002552
63  2020-03-03  1.64635472      5.2335154  0.122045846
64  2020-03-04  1.22192124      1.3847397  0.255058702
65  2020-03-05  2.79730323      3.0493643  0.250388765
66  2020-03-06  1.09454848      4.8693789  0.214052761
67  2020-03-07  1.03896452      5.6941608  0.171494681
68  2020-03-08  1.74266416      2.8415849  0.185865239
69  2020-03-09  2.41083638      5.6567791  0.153216529
70  2020-03-10  1.60495980      3.9097297  0.073893995
71  2020-03-11  3.08204424      5.9867381  0.077400278
72  2020-03-12  2.05235965      7.9561615  0.099486113
73  2020-03-13  3.25492035      4.1286298  0.147580143
74  2020-03-14  2.02207206      4.5089341  0.147214275
75  2020-03-15  3.11233886      2.4308661  0.073639439
76  2020-03-16  1.64179508      2.7206389  0.090696545
77  2020-03-17  3.93310396      7.5960730  0.223265766
78  2020-03-18  1.88740774      3.5307611  0.215280006
79  2020-03-19  2.83203023      5.5233247  0.229482931
80  2020-03-20  2.98156351      4.4886564  0.218710223
81  2020-03-21  3.04108648      4.3510577  0.250144487
82  2020-03-22  1.94415267      7.3240372  0.215852160
83  2020-03-23  2.17571710      5.6210955  0.196962431
84  2020-03-24  1.08907376      7.2746353  0.129827357
85  2020-03-25  0.46736629      6.2506416  0.075280966
86  2020-03-26  3.04382316      3.8902802  0.051033629
87  2020-03-27  3.26713772      2.0326508 -0.022771486
88  2020-03-28  1.25425759      4.1685972 -0.018611666
89  2020-03-29  1.78537906      4.5814061 -0.015141877
90  2020-03-30  0.55916826      6.3867915 -0.079959328
91  2020-03-31  3.03646229      7.9588403 -0.041039748
92  2020-04-01  2.18654230      3.3342883 -0.050067885
93  2020-04-02  1.98364701      7.1449628 -0.052829309
94  2020-04-03  0.87784007      8.2796654 -0.210683405
95  2020-04-04  3.21831762      1.4485581 -0.249303841
96  2020-04-05  1.79057989      5.5392876 -0.262353845
97  2020-04-06  2.31089465      3.7248119 -0.256082115
98  2020-04-07  1.89472095      4.6288934 -0.282282546
99  2020-04-08  3.09624860      4.7436530 -0.286120837
100 2020-04-09  1.75106130      2.8165431 -0.274227411
101 2020-04-10  1.49548872      3.6162198 -0.271835083
102 2020-04-11  3.29498591      6.0214158 -0.240059217
103 2020-04-12  2.15258515      5.2863815 -0.229939274
104 2020-04-13  1.68842563      2.2941041 -0.195512846
105 2020-04-14  0.92979959      3.6129904 -0.117033776
106 2020-04-15  2.56985846      6.3892148 -0.128667936
107 2020-04-16  1.51054432      7.0069700 -0.277398502
108 2020-04-17  2.25076461      0.3918448 -0.275443670
109 2020-04-18  1.73878714      5.5785622 -0.294250216
110 2020-04-19  2.26153230      6.3358410 -0.282754674
111 2020-04-20  3.43107897      1.9195776 -0.339707067
112 2020-04-21  2.39608484      6.0239955 -0.331781212
113 2020-04-22  1.52481529      4.0114004 -0.324084396
114 2020-04-23  3.78863274      4.5602722 -0.272620718
115 2020-04-24  1.94889006      6.0793982 -0.240532336
116 2020-04-25  1.26054150      7.4817879 -0.268735652
117 2020-04-26  4.07936526      5.3875341 -0.173697707
118 2020-04-27  2.37667279      7.2425724 -0.176916649
119 2020-04-28 -0.28948775      6.6484258 -0.228180064
120 2020-04-29  3.18482679      6.6674888 -0.165084799
121 2020-04-30  2.30542690      4.3040041 -0.223958480
122 2020-05-01  2.64462958      5.5283809 -0.220184800
123 2020-05-02  3.16372687      6.7292477 -0.177536333
124 2020-05-03  2.93533418      9.4057928 -0.034177715
125 2020-05-04  2.09798281      2.0753314  0.042028799
126 2020-05-05  2.76823535      5.8257415  0.053708351
127 2020-05-06  3.10826971      3.5786374  0.029750660
128 2020-05-07  1.61457004      2.5527381  0.057862907
129 2020-05-08  2.40305722      4.3785420  0.061111764
130 2020-05-09  1.72254121      7.2454087  0.015646994
131 2020-05-10  2.55731351      7.1460101  0.001279747
132 2020-05-11  1.80334653      6.7058641 -0.026780358
133 2020-05-12  1.91806736      4.4419938 -0.021189986
134 2020-05-13  2.66234636      2.5769939 -0.075241124
135 2020-05-14  1.63385336      7.0378847 -0.147585480
136 2020-05-15  2.59522443      6.0830823 -0.149085725
137 2020-05-16  1.18439450      5.2343399 -0.119715185
138 2020-05-17  1.09319397      7.8585397 -0.192218899
139 2020-05-18  0.57639316      5.5739832 -0.182888162
140 2020-05-19  2.35030333      1.0771646 -0.177972434
141 2020-05-20  2.15803757      2.7999814 -0.102354986
142 2020-05-21 -0.07277026      3.6667601 -0.028759350
143 2020-05-22  2.82160575      4.0788493 -0.057996081
144 2020-05-23  1.40711360      3.7210454 -0.016449436
145 2020-05-24  1.02022598      2.8656543  0.028624385
146 2020-05-25  2.16204124     10.7421682  0.064280682
147 2020-05-26  2.60690903      3.8405760  0.051142268
148 2020-05-27  1.33978888      3.3738355  0.058259625
149 2020-05-28  1.49997767      3.5574732  0.150644740
150 2020-05-29  1.50356134      5.3614688  0.113900956
151 2020-05-30  0.10573719      2.3644583  0.192329330
152 2020-05-31  1.49020770      6.8182643  0.170441249
153 2020-06-01  1.22237027      5.4754067  0.127232194
154 2020-06-02  2.73645899      7.2688418  0.077586823
155 2020-06-03  3.35740356      3.5303725  0.049992953
156 2020-06-04  2.01387953      8.0645219  0.044162424
157 2020-06-05  0.49527608      9.3677753 -0.022251816
158 2020-06-06  2.89302696      5.9954997 -0.012804587
159 2020-06-07  1.56870826      6.5999076 -0.006541896
160 2020-06-08  2.42222369      4.0573725 -0.020046142
161 2020-06-09  1.66013636      6.8790633 -0.051388421
162 2020-06-10  1.50189792      1.8699504 -0.038053991
163 2020-06-11  0.96986890      8.0014292 -0.071007772
164 2020-06-12  2.75003594      6.9345386  0.003553137
165 2020-06-13  1.41120282      2.6551424  0.018304975
166 2020-06-14  3.35648599      4.5510642 -0.012458258
167 2020-06-15  2.64145069      5.1257506 -0.011624235
168 2020-06-16  3.11170119      7.6395649  0.071641902
169 2020-06-17  1.11459462      5.7198230  0.075340442
170 2020-06-18  0.96105868      1.1754863  0.164378201
171 2020-06-19  2.98615773      4.5273782  0.163922599
172 2020-06-20  1.30131703      6.4458046  0.112625203
173 2020-06-21  1.15387132      4.6452171  0.142764013
174 2020-06-22  1.58012656     10.2545247  0.102730292
175 2020-06-23  2.27452047      4.3093313  0.059121441
176 2020-06-24  3.78758803      2.9486305 -0.043807907
177 2020-06-25  3.08603766      5.2541586 -0.028202974
178 2020-06-26  0.94733431      4.5851817 -0.035074437
179 2020-06-27  2.28741992      3.1251400 -0.061364354
180 2020-06-28  3.10617185      6.3849361 -0.042380000

If I understand you, you only want to extract correlation for last day of months.如果我理解你,你只想提取几个月最后一天的相关性。 So you can use next code to extract those values:所以你可以使用下一个代码来提取这些值:

#Create dates for filter
i1 <- seq(as.Date("2020-01-01"), length=13, by="1 month") - 1
#Avoid day of 2019
i1 <- i1[-1]
#Filter
dfs <- df2[df2$Date %in% i1,]

Output: Output:

          Date  Germany WeeklyMeanSpot        Cor
31  2020-01-31 2.426464       5.238490 0.06685690
60  2020-02-29 2.215942       3.436927 0.06675448
91  2020-03-31 2.993504       6.690026 0.06052142
121 2020-04-30 2.117647       3.569516 0.08070001
152 2020-05-31 2.769042       2.623132 0.22305907

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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