Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 计算时间序列上的滚动相关性_R_Finance_Rolling Computation - Fatal编程技术网

R 计算时间序列上的滚动相关性

R 计算时间序列上的滚动相关性,r,finance,rolling-computation,R,Finance,Rolling Computation,我有一个tbl df对象,我需要计算第二列和第三列中的值之间的22天滚动相关性。 这是我的对象的dput: structure(list(date = structure(c(1568937600, 1569196800, 1569283200, 1569369600, 1569456000, 1569542400, 1569801600, 1569888000, 1569974400, 1570060800, 1570147200, 1570406400, 1570492800, 1570

我有一个tbl df对象,我需要计算第二列和第三列中的值之间的22天滚动相关性。 这是我的对象的dput:

structure(list(date = structure(c(1568937600, 1569196800, 1569283200, 
1569369600, 1569456000, 1569542400, 1569801600, 1569888000, 1569974400, 
1570060800, 1570147200, 1570406400, 1570492800, 1570579200, 1570665600, 
1570752000, 1571011200, 1571097600, 1571184000, 1571270400, 1571356800, 
1571616000, 1571702400, 1571788800, 1571875200, 1571961600, 1572220800, 
1572307200, 1572393600, 1572480000, 1572566400, 1572825600, 1572912000, 
1572998400, 1573084800, 1573171200, 1573430400, 1573516800, 1573603200, 
1573689600, 1573776000, 1574035200, 1574121600, 1574208000, 1574294400, 
1574380800, 1574640000, 1574726400, 1574812800, 1574985600, 1575244800, 
1575331200, 1575417600, 1575504000, 1575590400, 1575849600, 1575936000, 
1576022400, 1576108800, 1576195200, 1576454400, 1576540800, 1576627200, 
1576713600, 1576800000, 1577059200, 1577145600, 1577404800, 1577664000, 
1577750400, 1577923200, 1578009600, 1578268800, 1578355200, 1578441600, 
1578528000, 1578614400, 1578873600, 1578960000, 1579046400, 1579132800, 
1579219200, 1579564800, 1579651200, 1579737600, 1579824000, 1580083200, 
1580169600, 1580256000, 1580342400, 1580428800, 1580688000, 1580774400, 
1580860800, 1580947200, 1581033600, 1581292800, 1581379200, 1581465600, 
1581552000, 1581638400, 1581984000, 1582070400, 1582156800, 1582243200, 
1582502400, 1582588800, 1582675200, 1582761600, 1582848000, 1583107200, 
1583193600, 1583280000, 1583366400, 1583452800, 1583712000, 1583798400, 
1583884800, 1583971200, 1584057600, 1584316800, 1584403200, 1584489600, 
1584576000, 1584662400, 1584921600, 1585008000, 1585094400, 1585180800, 
1585267200, 1585526400, 1585612800, 1585699200, 1585785600, 1585872000, 
1586131200, 1586217600, 1586304000, 1586390400, 1586822400, 1586908800, 
1586995200, 1587081600, 1587340800, 1587427200, 1587513600, 1587600000, 
1587686400, 1587945600, 1588032000, 1588118400, 1588204800, 1588291200, 
1588550400, 1588636800, 1588723200, 1588809600, 1589155200, 1589241600, 
1589328000, 1589414400, 1589500800, 1589760000, 1589846400, 1589932800, 
1590019200, 1590105600, 1590451200, 1590537600, 1590624000, 1590710400, 
1590969600, 1591056000, 1591142400, 1591228800, 1591315200, 1591574400, 
1591660800, 1591747200, 1591833600, 1591920000, 1592179200, 1592265600, 
1592352000, 1592438400, 1592524800, 1592784000, 1592870400, 1592956800, 
1593043200, 1593129600, 1593388800, 1593475200, 1593561600, 1593648000, 
1593993600, 1594080000, 1594166400, 1594252800, 1594339200, 1594598400, 
1594684800, 1594771200, 1594857600, 1594944000, 1595203200, 1595289600, 
1595376000, 1595462400, 1595548800, 1595808000, 1595894400, 1595980800, 
1596067200, 1596153600, 1596412800, 1596499200, 1596585600, 1596672000, 
1596758400, 1597017600, 1597104000, 1597190400, 1597276800, 1597363200, 
1597622400, 1597708800, 1597795200, 1597881600, 1597968000, 1598227200, 
1598313600, 1598400000, 1598486400, 1598572800, 1598918400, 1599004800, 
1599091200, 1599177600, 1599523200), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), pct_change = c(-0.000596715420165126, 0.00202340947964922, 
-0.00730158486244312, -0.014938661539249, 0.00285144700422259, 
-0.00339834001067637, -0.00209711565226938, -0.000307086514332147, 
-0.0109459128837458, 0.00341740978090339, 0.00489854217701997, 
-0.00519224264463548, -0.00403930471186504, -0.0085856890828658, 
0.0104573683004581, 0.0120692364486615, -0.00286134296490432, 
-0.00043618402445876, -0.00498545079168986, 0.0151762074108415, 
-0.00592494865057045, -0.00362952450017206, 0.010200856199525, 
-0.0037077984315362, -0.00166336276495436, 0.00131778004706018, 
0.00492124028424499, -0.0023455905368452, -0.0136498716075143, 
-0.0011651379015539, 0.0107149023795997, 0.0216715780546814, 
0.00222880579064545, 0.00371194597266459, 0.00633863105957239, 
-0.0105625201205021, -0.000841499008675126, 0.00174161638380853, 
-0.00986931902062625, 0.00512938065349265, -0.00252189187096898, 
0.00114078921010915, -0.00109533561230724, 0.00421552918263898, 
-0.000654948020443269, 0.00362659319875373, 0.00221767602410725, 
-0.00927130458006276, 0.000666559545869329, -0.00606700429583427, 
-0.00534448355522695, 0.00434779094431235, 0.00669770946773387, 
-0.00555134620308045, 0.00925630847408512, -0.0183181362553997, 
-0.0091874574991897, 0.00328287942984755, 0.00591107956009583, 
0.00235263419157157, 0.00746987951920315, 0.00335319521365887, 
0.0193792886740876, 0.000411231100331551, -0.00205918125467949, 
0.00919430122438798, 0.000607973385848824, 0.00133724591512061, 
-0.000769827901095321, -0.000246376818682614, 0.0138881792647982, 
-0.00695614493298667, -0.00780812215451075, 0.00261678054808587, 
0.0110740493427473, -0.00101617734370166, 0.00390664152874322, 
0.00237542859896723, -0.00217896637468118, -0.00574348323866828, 
0.00745157194884545, 0.00459098375978059, -0.0145744678456647, 
0.00543837159702655, -0.00403259403355083, 0.000137145394772488, 
-0.0222825660619035, -0.00184909329449571, 0.00174866505502086, 
-0.00353161899460482, -0.0235913880390603, -0.00141392475501259, 
0.0142153276221924, 0.0191567926590408, 0.00241971842001304, 
-0.0155230251410077, 0.00452147737759137, 0.00226989122647369, 
0.0166499127645609, -0.0011407963911766, 0.00859315285361464, 
-0.00391677057788944, 0.00153823029674903, -0.0209605313364784, 
0.000589233863350489, -0.0258046969552563, -0.0219572892328593, 
-0.000540540540540536, -0.0372254746823231, -0.028653123858235, 
0.00278964491787297, -0.0159705058641368, 0.0110873554281495, 
-0.0205836977136822, -0.0290513238707949, -0.142087521539029, 
0.0587448962043518, -0.0321117026577603, -0.0757525386819656, 
-0.019265906567242, -0.114213021931422, 0.0948310428686241, -0.114870962588344, 
0.0253943215269972, -0.0429614559510525, -0.00377037058874649, 
0.0662232820600183, 0.0335114310527437, -0.0224514448358218, 
-0.043514879281115, -0.0159664898331904, 0.0360637473231365, 
-0.0350455603699173, 0.028492937440162, -0.0133801971165971, 
0.0532453969164105, 0.0168123466804395, 0.0123063128055145, 0.0376588863956676, 
0.0257211430890991, -0.019397099007884, -0.0321161672070509, 
0.0233331192412959, -0.00768407096377526, -0.0226784225928103, 
0.0314020505218372, 0.0149009566485295, 0.0283920905009396, -0.00643843750027715, 
0.0111944149837418, 0.0349597574971198, -0.0332607391648296, 
-0.0353490950131612, -0.00341265699771237, 0.0268890561557766, 
0.0256940530962842, 0.0111042792718394, -0.00432994146966078, 
-0.0431832177021884, 0.00819812727410664, 0.00248675537374819, 
0.0184108436630447, 0.016595375071531, -0.000426628559632864, 
0.0109773813045141, 0.0016704486951116, -0.00472882521373529, 
-0.00426204910706144, 0.00944299808753867, -0.00795306846721028, 
-0.00743052189544043, 0.0186201734619213, 0.0116604889841671, 
0.02209125789131, -0.0157075207305394, 0.0179553710087674, 0.0155791636538823, 
-0.00789652449477334, -0.00146174856670025, -0.0573798562303973, 
0.0351553626604071, -0.00140871390280699, 0.0077630822074133, 
-4.10762222653638e-05, -0.00255531169457129, 0.00889427745664869, 
-0.0140052206880535, 0.0120523037819092, -0.0236343075141621, 
0.0199592602820173, -0.00830487034909847, 0.0126320701923816, 
0.00367168785797061, 0.00238891809114559, 0.00713663960561863, 
0.00161583281524188, -0.000658823813549225, 0.00511114435726825, 
0.00126601561927261, 0.00614639173514924, -0.00670096962182707, 
0.00901284770487524, 0.00585836470667396, -0.000561396677366042, 
0.00473355040587285, 0.00541956455714137, -0.00137340930230303, 
-0.0149459838893368, -0.0104773480585221, -0.00516762296407205, 
0.0137674158859203, 0.00328687838817947, 0.000390438217303781, 
-0.0203625863225501, -0.0110334664609242, -0.00574970859260905, 
0.0035174314881728, 0.00932613613390663, -0.000497474821847449, 
-0.000131531556191598, -0.00400890901276382, -0.000174033898890413, 
0.0115418349860765, -0.000630859523089557, 0.00209068902203075, 
-0.00176960918882073, -0.00263743166935199, -0.00100613855461638, 
0.000325210859045598, 0.0066055774314958, 0.0155425710297051, 
-0.000263772356322114, 0.000889232236059887, -0.00485134515050521, 
0.00103805757000386, 0.00337171600827231, 0.00092670694368378, 
-0.005054728949937, 0.010480854764867, -0.0215764218141165), 
    pct_change_second = c(-0.00932094208921797, 0.000818917212731368, 
    -0.0027521518829881, 0.00519655387979917, -0.0116533058303007, 
    -0.0140137463826531, 0.00855392091246724, -0.00613817538393172, 
    -0.00545680343179959, 0.00304795869933593, 0.00360341342572055, 
    -0.00513149858369002, -0.00541441675047788, 0.0157920224491813, 
    0.0150073048366694, 0.0106632863775459, 0.00604407808585764, 
    -0.00443203712949445, -0.007262266547329, -0.000539774051100301, 
    -0.0182597134535798, 0.00731547899432845, 0.000263028779812938, 
    0.00251633712311902, -0.00475031035429309, 0.0150377339562151, 
    0.00612428547948301, -0.0132957765501841, -0.001984519247229, 
    -0.00649254127908213, 0.0208650232574573, 0.00951444197766564, 
    0.0129432128498977, -0.00636795207105839, 0.0165556167836716, 
    -0.00376760713157869, -0.0210407130778879, -0.00651160827428543, 
    -0.00309124719148727, 0.00922925034923163, -0.00989803203035052, 
    -0.00192576471127871, 0.00923900528463679, -0.0114574018128231, 
    -0.00088603393021102, -0.0119731428361454, 0.010451937078928, 
    0.00321313816727882, 0.00547060197327286, -0.0226414364522527, 
    -0.00936433542838102, -0.00136240151105323, 0.0121656591915528, 
    0.00375037265112616, 0.0142275155205388, -0.0122993387697959, 
    0.007125683836204, 0.00139716598426925, 0.0139118590906868, 
    -0.00108309921475284, 0.0120606478270519, 0.0111576477020639, 
    0.00142500960733081, -0.00244806034901611, 0.00495421105599495, 
    -0.0182328190743337, 0.00598686703279244, 0.000222130099400147, 
    0.00773876772110026, 0.0117347992026333, 0.0229027638333928, 
    -0.00898830429998026, -0.000675850927534066, 0.00963743848850286, 
    0.00233278510106247, 0.00672145428843467, -0.0010002223060904, 
    0.0180077339579916, -0.0068240563771369, -0.0103727017830614, 
    0.00209852934262822, 0.00963612290419883, -0.0340018209930309, 
    0.00333995928250125, -0.0197033180163816, -0.0113782314023862, 
    -0.0541474991338043, 0.00268095994188749, 0.00571123490281537, 
    -0.017692109719371, -0.0231427427271063, -0.0117967338431969, 
    0.056334628005213, 0.00427415847743974, -0.0019416043033327, 
    -0.00354159157285356, 0.025584857983308, 0.0146129271693578, 
    0.0147067260632083, -0.0176216434533555, 0.00927207638711626, 
    0.0139814324489246, 0.00901900427223147, 0.00351178731345936, 
    0.00854826711531704, -0.0224673563962681, -0.00226276484005542, 
    0.0103341370995522, -0.00983076804077678, -0.0172625955021704, 
    0.0207980448806442, -0.00628618660759551, 0.0223397206878186, 
    -0.002142385249468, -0.0331379512209853, -0.0368957506267908, 
    0.0521335317135169, -0.0331533445599508, -0.0473444062443006, 
    0.0516097394214619, -0.130432729372713, 0.0641560662448785, 
    -0.0430691919083382, 0.0228060790923828, -0.0389007307595852, 
    0.00130001816409215, 0.0425790478414648, 0.00229197790806254, 
    0.00540958084387589, -0.0458379299916283, 0.0199051016536868, 
    -0.00538168956409432, -0.0133533588121288, 0.0411306088379135, 
    -0.00181717714949802, 0.0251259882444328, -0.0228711791501901, 
    0.0126677237606605, -0.00888642532083094, 0.0097576989467521, 
    -0.0145302467112683, 0.00915484467082813, 0.0103847168602931, 
    -0.00157448740739305, -0.0141358505993763, 0.022631100596766, 
    -0.00296846011131738, -0.0034344532605641, 0.00687251658668276, 
    0.00216815593558854, 0.0101600999172466, -0.0288073358628783, 
    -0.0407994439152162, 0.013879120838396, 0.0211678106691482, 
    0.0329112366695573, 0.014356339702332, -0.00251772576870498, 
    -0.00214487249354127, -0.000963748128144201, 0.00643801417493254, 
    -0.0193700152807951, 0.0226972433467466, -0.0197238751463581, 
    0.00349293043654202, -0.0162107665855251, -0.011460828867736, 
    -0.00218806222751035, -0.00795188948697273, -0.0106884209038211, 
    0.0123565294444015, 0.0242176468386907, 0.00828527799995787, 
    4.00823793559004e-05, -0.0228791071807997, 0.0216560902695499, 
    0.00368147971471757, -0.00910615403516489, 0.0069492660652015, 
    -0.0303363144363387, 0.0251649223935893, -0.00486399255361116, 
    0.00207216784032331, 0.0138328407908514, 0.00888498117879822, 
    0.00609991893374192, 0.00922734359708256, -0.00145942439763838, 
    -0.00330028355760857, 0.00504351657916211, 0.000325919012860395, 
    0.000161135215625376, 0.0116569812539948, 0.0189853144118567, 
    0.0278944246133963, 0.0938150150986084, -0.0194320868235043, 
    0.0246487065037745, 0.0050467942102798, -0.0166997614452528, 
    0.00920169290095369, -0.00101521615301257, -0.0118397981377035, 
    -0.0496539902994323, 0.00310066285930365, 0.0278818833683611, 
    -0.00750092207357974, -0.013814726332025, 0.000654295695858955, 
    -0.0353932688535409, -0.0108666443789427, 0.000218343265444939, 
    0.0284691264726313, -0.0126112615183572, 0.00613523761366211, 
    0.0325087351269042, -0.00295374559093848, -0.00381536075114063, 
    0.00712113101078772, -0.0198949298244639, 0.0111592975165209, 
    -0.0119326693552177, -0.00195085379211902, -0.0112709841369685, 
    0.0114780496706889, 0.0275499244058404, -0.00882505098989571, 
    -0.0159331234954312, 0.00548391761656122, 0.00402809344325894, 
    0.00610837832340035, 0.00404328562725829, -0.0113988567458328, 
    0.00724692697647314, 0.0215818195181752, -0.0005478992874822, 
    0.0087238804996817, -0.0169716528074893, 0.00518213964825958, 
    -0.0291933261773754)), row.names = c(NA, -240L), class = c("tbl_df", 
"tbl", "data.frame"), na.action = structure(1:22, .Names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22"), class = "omit"))
我试过这个:

price_data_corr <- rollapplyr(price_data_join, 22, function(price_data_join) cor(price_data_join[, 2], price_data_join[, 3]), by.column = FALSE)

我建议采取下一个办法。看起来您可以在
mutate()
语句中将
rollapply()
样式函数与
cbind()
组合,以缓解问题并计算所需的相关性(有关更多信息,请查看@G.Grothendieck的注释):

图书馆(动物园)
图书馆(dplyr)
#代码
mycor尝试使用滚动函数包。下面的代码计算22天的相关性(能够处理不规则的时间序列)。
您可以滚动整个
data.frame
,这意味着函数中的
x
将在
k=“22天”
窗口内成为
数据的子集。窗口是在引用数据中的列的
idx=“date”
上计算的(如果您愿意,也可以直接指定
idx=data$date

有关更多信息,请访问

库(运行程序)
跑步者(
x=数据,
f=函数(x){
cor(x$pct变化,x$pct变化秒)
},
k=“22天”,
idx=“日期”
)
总目(数据)
#更改日期更改更改第二次更改
#2019-09-20-0.0005967154-0.0093209421北美
# 2 2019-09-23  0.0020234095      0.0008189172  1.0000000
# 3 2019-09-24 -0.0073015849     -0.0027521519  0.1066286
# 4 2019-09-25 -0.0149386615      0.0051965539 -0.5860520
# 5 2019-09-26  0.0028514470     -0.0116533058 -0.7029877
# 6 2019-09-27 -0.0033983400     -0.0140137464 -0.5868171

享受猜测,可能是因为您使用的是TIBLE,当data.frame为TIBLE时,一列上的子集将返回一个包含1列的数据帧,即list。尝试将您的数据更改为dataframe:
price\u data\u join@G.Grothendieck噢,这是一个打字错误!我现在就搬。非常感谢您的澄清。@G.Grothendieck我将删除该信息,并将该信息归功于您!
 Error in cor(price_data_join[, 2], price_data_join[, 3]) : 
  'x' must be numeric 
library(zoo)
library(dplyr)
#Code
mycor <- function(x) cor(x[, 1], x[, 2])
#Apply
DF2 <- price_data_join %>%
  mutate(Cor = rollapplyr(cbind(pct_change, pct_change_second), 22, mycor, by.column = FALSE, fill = NA))
  
# A tibble: 240 x 4
   date                pct_change pct_change_second   Cor
   <dttm>                   <dbl>             <dbl> <dbl>
 1 2019-09-20 00:00:00  -0.000597         -0.00932     NA
 2 2019-09-23 00:00:00   0.00202           0.000819    NA
 3 2019-09-24 00:00:00  -0.00730          -0.00275     NA
 4 2019-09-25 00:00:00  -0.0149            0.00520     NA
 5 2019-09-26 00:00:00   0.00285          -0.0117      NA
 6 2019-09-27 00:00:00  -0.00340          -0.0140      NA
 7 2019-09-30 00:00:00  -0.00210           0.00855     NA
 8 2019-10-01 00:00:00  -0.000307         -0.00614     NA
 9 2019-10-02 00:00:00  -0.0109           -0.00546     NA
10 2019-10-03 00:00:00   0.00342           0.00305     NA
# ... with 230 more rows