Computing monthly rolling skewness with daily data

Hi everyone,

I am looking to calculate the rolling skewness of daily stock returns over a monthly time window. Specifically, I define the skewness of a stock in month t to be the skewness of the stock’s daily returns calculated using 3 months of data up to and including the given month t (months t − 2 through t, inclusive). E.g. the data point March includes daily returns from Jan-March.

I have managed to calculate the monthly skewness for each stock individually using the following code:

rolling_skew <- retDaily %>% gather(stock, return, -date) %>% as_tsibble(key = stock, index = date) %>% group_by_key() %>% index_by(year_month = ~ yearmonth(.)) %>% summarise(skewness = skewness(return)) %>% spread(stock, skewness)

I end up with the monthly skewness, but the returns of the past 2 months are not included. Thus I need to include a lag somehow...Any ideas?

Blockquote

Here is my sample data for daily returns (called retDaily):

structure(list(date = structure(c(17533, 17534, 17535, 17536,
17539, 17540, 17541, 17542, 17543, 17547, 17548, 17549, 17550,
17553, 17554, 17555, 17556, 17557, 17560, 17561, 17562, 17563,
17564, 17567, 17568, 17569, 17570, 17571, 17574, 17575, 17576,
17577, 17578, 17582, 17583, 17584, 17585, 17588, 17589, 17590,
17591, 17592, 17595, 17596, 17597, 17598, 17599, 17602, 17603,
17604, 17605, 17606, 17609, 17610, 17611, 17612, 17613, 17616,
17617, 17618, 17619, 17623, 17624, 17625, 17626, 17627, 17630,
17631, 17632, 17633, 17634, 17637, 17638, 17639, 17640, 17641,
17644, 17645, 17646, 17647, 17648, 17651, 17652, 17653, 17654,
17655, 17658, 17659, 17660, 17661, 17662, 17665, 17666, 17667,
17668, 17669, 17672, 17673, 17674, 17675, 17676, 17680, 17681,
17682, 17683, 17686, 17687, 17688, 17689, 17690, 17693, 17694,
17695, 17696, 17697, 17700, 17701, 17702, 17703, 17704, 17707,
17708, 17709, 17710, 17711), tzone = "UTC", tclass = "Date", class = "Date"),
10026 = c(NA, -0.00998786767581905, 0.0138127158236847,
-0.00955052427703185, 0.000741739716790146, -0.00720975675493563,
-0.0127595357676122, 0.00776838940132052, -0.000613957295859224,
0.00109215017064845, 0.0367516705304787, -0.01025971719829,
0.0219947490202173, -0.026332899869961, 0.00313856427378956,
-0.0123818399680468, 0.0172553248854137, -0.0145109992048768,
0.00551334633228007, -0.0706117686392511, -0.00395719088012159,
0.00794560820572077, -0.0183459236309247, -0.0273031829464154,
0.0200389507626124, -0.00860856449120728, -0.0190738445098826,
0.0130135431641067, 0.00261416087833322, 0.00275618275058243,
0.0278582571874304, 0.00303555941023403, 0.00230587980977082,
-0.00359453604640292, -0.0191198398903435, -0.00970945200441331,
0.00586793433855748, 0.0125535371437011, -0.0070011668611436,
-0.0134400705052877, 0.00126554008784319, 0.0202230483271375,
0.0107127240926979, 0.0162232316677482, 0.00276713495104319,
-0.002334960730206, 0.00567375886524824, -0.00486600846262342,
-0.00070873786407788, -0.0147507279448781, -0.025552366339334,
0.00967646624316743, -0.00424317799400087, -0.00330614943795471,
-0.013563246351172, -0.000896801606874886, -0.0126403141361257,
0.0240133341423632, -0.000739680480816629, -0.0034053893988748,
0.0144109344822463, -0.0167691857059168, 0.0236091457510985,
0.0130238649592549, 0.00933706816059776, -0.0145876325339785,
0.0022385181975737, 0.00943886515158621, 0, 0.000142683787103248,
-0.00413938053097351, 0.0205676523267631, 0.0109542869180534,
0.000138987289018733, -0.0133342583975098, -0.00985436687165719,
-7.10883628349812e-05, -0.00277264325323467, 0.00128323946674258,
0.000142399430402396, -0.00989535132056663, -0.0120074777106702,
-0.0388617276762973, -0.0182479731772566, -0.00647840505938613,
0.0301194666884437, 0.0177091183119822, 0.0033320992225101,
0.00177129151291533, 0.000441947808903276, 0.00942562592047125,
-0.00809746133644595, 0.00544245054056058, 0.0141174007667761,
0.0119734564339298, 0.00285103349964344, 0.00106616915422886,
-0.0255591746141871, 0.00582877959927153, 0.0057225642883012,
0.00165665514261004, 0.00740634159730047, 0.0251248376070781,
-0.0139256370978972, 0.010097443863861, 0.0195036700454385,
0.0110394953373558, -0.00874872838250251, 6.84181718664245e-05,
0.00759389751659034, 0.0033269961977187, 0.00629356432293404,
0.00416953597848013, 0.0113848773516692, 0.00463514766256123,
-0.000329554442393909, 0.00962616206237232, 0.00176314242800246,
0.00352027402348587, 0.00610627517214501, -0.000645661157024802,
0.0144074815867683, -0.0171962943249661, -0.00602682911023267,
-0.00593297692006778), 10028 = c(NA, 0.0102061855670104,
-0.0205122971731809, -0.0103146488851844, 0.0631645436361723,
-0.0690167343301317, 0.0316953839608594, -0.00824742268041234,
0.0343035343035343, -0.0733668341708542, 0.0734273318872016,
-0.000303122158229807, 0.0309278350515465, -0.0287254901960784,
0.00938730190774195, 0.0501, -0.0191410341872202, 0, -0.0679611650485438,
0, 0.046875, -0.0348258706467661, -0.020618556701031, -0.0313684210526315,
0.000869376222560447, 0.0206297502714439, -0.0106382978723403,
-0.0322580645161291, 0.0222222222222221, 0, 0.0158695652173912,
-0.00481489407233038, 0.02128803354478, -0.0209495736393304,
0.0103225806451612, 0.00234142188165176, 0.00934380972605653,
-0.000525983589312107, -0.000105252078728579, -0.0210526315789473,
-0.0053763440860215, 0.0810810810810809, -0.053, -0.0390707497360083,
0.0219780219780219, -0.0108602150537634, 0.000108707468203129,
0, 0.0080434782608696, -0.0187621306879447, 0.000109890109890154,
-0.000109878035380762, 0, -0.0307692307692308, -0.0258503401360545,
0.0416666666666667, -0.0100558659217878, -0.0244920993227992,
-0.0396852944579429, 0.013975903614458, -0.0137832699619772,
-0.0240963855421685, 0.0246913580246912, 0.0110843373493976,
-0.0238322211630123, -0.00769042968750011, 0.017099274203469,
0.0293904208998548, 0.0222065562213607, -0.0219540229885058,
0, 0.00434833705488313, 0.0063187456119822, 0.00813953488372099,
-0.00807381776239913, 0, -0.000116279069767411, -0.0232585184323759,
0.0121443028932016, -0.0295259381249265, 0.0060606060606061,
0.0120481927710843, -0.0119047619047619, -0.00614457831325299,
-0.0180627954903624, -0.0246913580246914, -0.0126582278481012,
0.0384615384615385, -0.0234567901234568, -0.00126422250316061,
-0.0341772151898735, -0.0252948885976408, -0.0181524808390481,
-0.0153382634894549, 0.0739916550764952, -0.0282310282310283,
0.0126599147121536, -0.0130280300039479, -0.0253333333333333,
0.0292749658002736, -0.00318979266347685, -0.0270666666666667,
-0.0132931341647253, 0.125, -0.0903703703703704, 0.0177795874049946,
-0.0266702226963595, -0.0135635018495684, 0.0048611111111112,
-0.00884588804422959, 0.0181285734207224, -0.0001369675386933,
-0.0273972602739726, -0.0140845070422535, 0.157142857142857,
-0.109876543209877, 0.014008321775312, 0.0258514567090686,
-0.00346666666666673, -0.0445544554455446, 0.00504131074079273,
0.0590776090288421, -0.0723589001447179, 0.00694936888384623,
0.0140845070422535), 10032 = c(NA, 0.00130975769482644,
0.0122629169391759, 0.00492650621870472, 0.0466929197138954,
-0.00906019656019663, -0.0119324345265769, 0.0123902132998746,
0.0204492641363285, -0.0318809776833157, 0.00925199937274579,
-0.0658794282162835, 0.0073186959414504, -0.00974240422721273,
0.00533600133400025, -0.0106153590976945, 0.0149203688181057,
0.0122233234225306, -0.0119125326370758, -0.00990916597853009,
-0.00333611342785656, 0.0123849372384937, -0.0110762109439577,
-0.027415580073554, 0.0134066689584049, 0.00729308005427409,
-0.0313184037716787, -0.00104293412132805, 0.010266225856969,
-0.0072338959696866, 0.0152671755725191, 0.0105946684894054,
0.000338180588434156, 0.0270453008789724, -0.00971033574720204,
0.00515206913744382, 0.00793650793650791, 0.0150918635170605,
-0.0105042016806723, -0.0148619957537155, -0.00513925729442977,
0.0236627228795201, 0.00455803353410378, 0.0134500081024145,
0.00959385992964501, -0.0023756731073804, 0.0204794411811398,
0.0132233976353453, -0.0102871180715493, -0.00853242320819103,
0.00688468158347666, 0.0226884226884227, -0.0132198753988756,
-0.00615953187557727, -0.00185931205453993, -0.0276311704439616,
-0.0370370370370371, -0.00116047745358094, -0.0293775933609959,
0.000341997264021954, 0.0210256410256409, -0.0165745856353591,
0.0156622403813413, 0.00134093194770379, 0.00150652828925346,
-0.0207253886010362, -0.00273084144051894, 0.0255005990073593,
0.00901201602136181, 0.00628514720476359, 0.0175871137409598,
0.00242287191083834, 0.00032226877215602, 0.00982603092783507,
-0.0012761205933961, -0.00511100463184799, 0.000481618237277326,
-0.00706033376123227, -0.00646412411118302, -0.110279765777489,
0.0157221206581353, -0.0129589632829373, 0.0255288110867979,
0.00782361308677104, -0.000705716302046522, 0.027542372881356,
-0.0020618556701032, 0.00447658402203865, -0.00308536167295181,
0.0110041265474552, 0.00544217687074822, -0.00541271989174563,
-0.00935374149659862, 0.0116738197424893, 0.00916341422026123,
0.00504456028249556, -0.00150577212648495, -0.0102211796246648,
-0.0135432537667174, -0.00858074480864934, 0.003635104725636,
0.00569161779924121, 0.0157777396672953, -0.0182340030390005,
0.012725709372313, 0.00950925454236717, 0.0218671152228762,
0.00954732510288059, -0.000978314038806327, -0.00440672433491107,
0.00508196721311482, -0.00505627140760079, -0.00786885245901636,
0.00908790482485133, 0.00425740953004738, -0.00538072721343552,
-0.000983606557377059, 0.0129635707253035, -0.00437388627895663,
0.00325414904002597, -0.0311385014596172, 0.0132239705390023,
-0.0079299520898729, -0.00932556203164026, 0.000840477391158112
), 10044 = c(NA, 0.0256410256410258, 0.00249999999999995,
-0.00249376558603487, -0.0083333333333333, 0.000840336134453779,
0.00923593618807717, 0.0391014975041597, -0.0120096076861489,
-0.00729335494327388, 0.00489795918367353, -0.00649878147847283,
0.017170891251022, -0.00401929260450151, 0.00322841000807106,
-0.00876910699919553, 0.0136352568785003, -0.00712627111858444,
0, 0.0024193548387097, -0.00321802091713586, -0.00968523002421318,
0.00407497962510184, -0.0089285714285714, -0.000819000819000992,
0.00491803278688518, -0.00407830342577475, 0.0141195741195741,
-0.00180901925313359, 0.00970873786407767, 0.00881410256410242,
-0.0015885623510723, 0.000795544948289484, -0.0174880763116058,
0.00404530744336573, -0.0112812248186946, -0.00488997555012216,
0.00654381654381653, -0.00681047038625204, -0.000507938588586221,
0.00327868852459035, -0.000816993464052285, -0.00899427636958305,
-0.00165016501650161, -0.00165289256198342, 0.0124172185430464,
-0.00368765331152898, -0.0102503918784725, -0.0074626865671642,
-0.0158730158730159, -0.00169779286926997, 0.056122448979592,
-0.0177133655394526, -0.012295081967213, 0, -0.0032365145228217,
0.00907501456997761, -0.0053877887788778, 0.00956473408711944,
-0.0115036976170912, 0, 0.00332502078137997, -0.00248550124275071,
0.00249169435215957, 0.0149130074565038, -0.0204081632653061,
0.00916666666666655, 0, -0.000825763831544202, 0.000371900826446225,
0.00293279358916121, -0.00329489291598029, 0.00543801652892562,
-0.00458662808857624, -0.00328654004954576, 0.000480522278007101,
-0.00960582974494861, -0.0142140468227426, 0.000848176420695568,
-0.0144067796610169, 0, -0.00343938091143603, 0.00690250215703192,
-0.00783204798628967, -0.00592472319624138, 0.00955690703735868,
-0.00413080895008611, -0.00535776011061173, -0.00086880973066894,
0.0057304347826086, -0.00310395213515591, -0.000867302688638327,
7.81249999999289e-05, 0.00426181982310414, 0.00172860847018153,
-0.00431406384814503, -0.00519930675909863, -0.00958188153310113,
0.00143359718557612, 0.00108902804247202, -0.00514971005465537,
-0.00352733686067008, 0.000884955752212369, -0.0141467727674625,
0.011659192825112, -0.0328014184397163, 0.00412465627864345,
-0.00502053856686446, -0.000917431192660523, -0.00183654729109284,
-0.00183992640294384, -0.00184331797235016, -0.000923361034164349,
0.0138632162661738, 0.0209662716499543, 0.00714285714285712,
0.00443262411347534, 0.00220653133274507, 0.00660501981505934,
-0.00349956255468054, -0.00482879719051799, -0.00926334362593739,
-0.0178094390026715, -0.00815956482320945, 0.0457038391224862
)), class = c("data.table", "data.frame"), row.names = c(NA,
-125L))

Hi @Pia. I can think of a way to do skewness of three month period but the code is not much elegant. Hope it can help.

library(tsibble)
library(e1071)
library(tidyverse)

retDaily <- structure(list(date = structure(c(17533, 17534, 17535, 17536,
                                  17539, 17540, 17541, 17542, 17543, 17547, 17548, 17549, 17550,
                                  17553, 17554, 17555, 17556, 17557, 17560, 17561, 17562, 17563,
                                  17564, 17567, 17568, 17569, 17570, 17571, 17574, 17575, 17576,
                                  17577, 17578, 17582, 17583, 17584, 17585, 17588, 17589, 17590,
                                  17591, 17592, 17595, 17596, 17597, 17598, 17599, 17602, 17603,
                                  17604, 17605, 17606, 17609, 17610, 17611, 17612, 17613, 17616,
                                  17617, 17618, 17619, 17623, 17624, 17625, 17626, 17627, 17630,
                                  17631, 17632, 17633, 17634, 17637, 17638, 17639, 17640, 17641,
                                  17644, 17645, 17646, 17647, 17648, 17651, 17652, 17653, 17654,
                                  17655, 17658, 17659, 17660, 17661, 17662, 17665, 17666, 17667,
                                  17668, 17669, 17672, 17673, 17674, 17675, 17676, 17680, 17681,
                                  17682, 17683, 17686, 17687, 17688, 17689, 17690, 17693, 17694,
                                  17695, 17696, 17697, 17700, 17701, 17702, 17703, 17704, 17707,
                                  17708, 17709, 17710, 17711), tzone = "UTC", tclass = "Date", class = "Date"),
               `10026` = c(NA, -0.00998786767581905, 0.0138127158236847,
                         -0.00955052427703185, 0.000741739716790146, -0.00720975675493563,
                         -0.0127595357676122, 0.00776838940132052, -0.000613957295859224,
                         0.00109215017064845, 0.0367516705304787, -0.01025971719829,
                         0.0219947490202173, -0.026332899869961, 0.00313856427378956,
                         -0.0123818399680468, 0.0172553248854137, -0.0145109992048768,
                         0.00551334633228007, -0.0706117686392511, -0.00395719088012159,
                         0.00794560820572077, -0.0183459236309247, -0.0273031829464154,
                         0.0200389507626124, -0.00860856449120728, -0.0190738445098826,
                         0.0130135431641067, 0.00261416087833322, 0.00275618275058243,
                         0.0278582571874304, 0.00303555941023403, 0.00230587980977082,
                         -0.00359453604640292, -0.0191198398903435, -0.00970945200441331,
                         0.00586793433855748, 0.0125535371437011, -0.0070011668611436,
                         -0.0134400705052877, 0.00126554008784319, 0.0202230483271375,
                         0.0107127240926979, 0.0162232316677482, 0.00276713495104319,
                         -0.002334960730206, 0.00567375886524824, -0.00486600846262342,
                         -0.00070873786407788, -0.0147507279448781, -0.025552366339334,
                         0.00967646624316743, -0.00424317799400087, -0.00330614943795471,
                         -0.013563246351172, -0.000896801606874886, -0.0126403141361257,
                         0.0240133341423632, -0.000739680480816629, -0.0034053893988748,
                         0.0144109344822463, -0.0167691857059168, 0.0236091457510985,
                         0.0130238649592549, 0.00933706816059776, -0.0145876325339785,
                         0.0022385181975737, 0.00943886515158621, 0, 0.000142683787103248,
                         -0.00413938053097351, 0.0205676523267631, 0.0109542869180534,
                         0.000138987289018733, -0.0133342583975098, -0.00985436687165719,
                         -7.10883628349812e-05, -0.00277264325323467, 0.00128323946674258,
                         0.000142399430402396, -0.00989535132056663, -0.0120074777106702,
                         -0.0388617276762973, -0.0182479731772566, -0.00647840505938613,
                         0.0301194666884437, 0.0177091183119822, 0.0033320992225101,
                         0.00177129151291533, 0.000441947808903276, 0.00942562592047125,
                         -0.00809746133644595, 0.00544245054056058, 0.0141174007667761,
                         0.0119734564339298, 0.00285103349964344, 0.00106616915422886,
                         -0.0255591746141871, 0.00582877959927153, 0.0057225642883012,
                         0.00165665514261004, 0.00740634159730047, 0.0251248376070781,
                         -0.0139256370978972, 0.010097443863861, 0.0195036700454385,
                         0.0110394953373558, -0.00874872838250251, 6.84181718664245e-05,
                         0.00759389751659034, 0.0033269961977187, 0.00629356432293404,
                         0.00416953597848013, 0.0113848773516692, 0.00463514766256123,
                         -0.000329554442393909, 0.00962616206237232, 0.00176314242800246,
                         0.00352027402348587, 0.00610627517214501, -0.000645661157024802,
                         0.0144074815867683, -0.0171962943249661, -0.00602682911023267,
                         -0.00593297692006778), `10028` = c(NA, 0.0102061855670104,
                                                          -0.0205122971731809, -0.0103146488851844, 0.0631645436361723,
                                                          -0.0690167343301317, 0.0316953839608594, -0.00824742268041234,
                                                          0.0343035343035343, -0.0733668341708542, 0.0734273318872016,
                                                          -0.000303122158229807, 0.0309278350515465, -0.0287254901960784,
                                                          0.00938730190774195, 0.0501, -0.0191410341872202, 0, -0.0679611650485438,
                                                          0, 0.046875, -0.0348258706467661, -0.020618556701031, -0.0313684210526315,
                                                          0.000869376222560447, 0.0206297502714439, -0.0106382978723403,
                                                          -0.0322580645161291, 0.0222222222222221, 0, 0.0158695652173912,
                                                          -0.00481489407233038, 0.02128803354478, -0.0209495736393304,
                                                          0.0103225806451612, 0.00234142188165176, 0.00934380972605653,
                                                          -0.000525983589312107, -0.000105252078728579, -0.0210526315789473,
                                                          -0.0053763440860215, 0.0810810810810809, -0.053, -0.0390707497360083,
                                                          0.0219780219780219, -0.0108602150537634, 0.000108707468203129,
                                                          0, 0.0080434782608696, -0.0187621306879447, 0.000109890109890154,
                                                          -0.000109878035380762, 0, -0.0307692307692308, -0.0258503401360545,
                                                          0.0416666666666667, -0.0100558659217878, -0.0244920993227992,
                                                          -0.0396852944579429, 0.013975903614458, -0.0137832699619772,
                                                          -0.0240963855421685, 0.0246913580246912, 0.0110843373493976,
                                                          -0.0238322211630123, -0.00769042968750011, 0.017099274203469,
                                                          0.0293904208998548, 0.0222065562213607, -0.0219540229885058,
                                                          0, 0.00434833705488313, 0.0063187456119822, 0.00813953488372099,
                                                          -0.00807381776239913, 0, -0.000116279069767411, -0.0232585184323759,
                                                          0.0121443028932016, -0.0295259381249265, 0.0060606060606061,
                                                          0.0120481927710843, -0.0119047619047619, -0.00614457831325299,
                                                          -0.0180627954903624, -0.0246913580246914, -0.0126582278481012,
                                                          0.0384615384615385, -0.0234567901234568, -0.00126422250316061,
                                                          -0.0341772151898735, -0.0252948885976408, -0.0181524808390481,
                                                          -0.0153382634894549, 0.0739916550764952, -0.0282310282310283,
                                                          0.0126599147121536, -0.0130280300039479, -0.0253333333333333,
                                                          0.0292749658002736, -0.00318979266347685, -0.0270666666666667,
                                                          -0.0132931341647253, 0.125, -0.0903703703703704, 0.0177795874049946,
                                                          -0.0266702226963595, -0.0135635018495684, 0.0048611111111112,
                                                          -0.00884588804422959, 0.0181285734207224, -0.0001369675386933,
                                                          -0.0273972602739726, -0.0140845070422535, 0.157142857142857,
                                                          -0.109876543209877, 0.014008321775312, 0.0258514567090686,
                                                          -0.00346666666666673, -0.0445544554455446, 0.00504131074079273,
                                                          0.0590776090288421, -0.0723589001447179, 0.00694936888384623,
                                                          0.0140845070422535), `10032` = c(NA, 0.00130975769482644,
                                                                                         0.0122629169391759, 0.00492650621870472, 0.0466929197138954,
                                                                                         -0.00906019656019663, -0.0119324345265769, 0.0123902132998746,
                                                                                         0.0204492641363285, -0.0318809776833157, 0.00925199937274579,
                                                                                         -0.0658794282162835, 0.0073186959414504, -0.00974240422721273,
                                                                                         0.00533600133400025, -0.0106153590976945, 0.0149203688181057,
                                                                                         0.0122233234225306, -0.0119125326370758, -0.00990916597853009,
                                                                                         -0.00333611342785656, 0.0123849372384937, -0.0110762109439577,
                                                                                         -0.027415580073554, 0.0134066689584049, 0.00729308005427409,
                                                                                         -0.0313184037716787, -0.00104293412132805, 0.010266225856969,
                                                                                         -0.0072338959696866, 0.0152671755725191, 0.0105946684894054,
                                                                                         0.000338180588434156, 0.0270453008789724, -0.00971033574720204,
                                                                                         0.00515206913744382, 0.00793650793650791, 0.0150918635170605,
                                                                                         -0.0105042016806723, -0.0148619957537155, -0.00513925729442977,
                                                                                         0.0236627228795201, 0.00455803353410378, 0.0134500081024145,
                                                                                         0.00959385992964501, -0.0023756731073804, 0.0204794411811398,
                                                                                         0.0132233976353453, -0.0102871180715493, -0.00853242320819103,
                                                                                         0.00688468158347666, 0.0226884226884227, -0.0132198753988756,
                                                                                         -0.00615953187557727, -0.00185931205453993, -0.0276311704439616,
                                                                                         -0.0370370370370371, -0.00116047745358094, -0.0293775933609959,
                                                                                         0.000341997264021954, 0.0210256410256409, -0.0165745856353591,
                                                                                         0.0156622403813413, 0.00134093194770379, 0.00150652828925346,
                                                                                         -0.0207253886010362, -0.00273084144051894, 0.0255005990073593,
                                                                                         0.00901201602136181, 0.00628514720476359, 0.0175871137409598,
                                                                                         0.00242287191083834, 0.00032226877215602, 0.00982603092783507,
                                                                                         -0.0012761205933961, -0.00511100463184799, 0.000481618237277326,
                                                                                         -0.00706033376123227, -0.00646412411118302, -0.110279765777489,
                                                                                         0.0157221206581353, -0.0129589632829373, 0.0255288110867979,
                                                                                         0.00782361308677104, -0.000705716302046522, 0.027542372881356,
                                                                                         -0.0020618556701032, 0.00447658402203865, -0.00308536167295181,
                                                                                         0.0110041265474552, 0.00544217687074822, -0.00541271989174563,
                                                                                         -0.00935374149659862, 0.0116738197424893, 0.00916341422026123,
                                                                                         0.00504456028249556, -0.00150577212648495, -0.0102211796246648,
                                                                                         -0.0135432537667174, -0.00858074480864934, 0.003635104725636,
                                                                                         0.00569161779924121, 0.0157777396672953, -0.0182340030390005,
                                                                                         0.012725709372313, 0.00950925454236717, 0.0218671152228762,
                                                                                         0.00954732510288059, -0.000978314038806327, -0.00440672433491107,
                                                                                         0.00508196721311482, -0.00505627140760079, -0.00786885245901636,
                                                                                         0.00908790482485133, 0.00425740953004738, -0.00538072721343552,
                                                                                         -0.000983606557377059, 0.0129635707253035, -0.00437388627895663,
                                                                                         0.00325414904002597, -0.0311385014596172, 0.0132239705390023,
                                                                                         -0.0079299520898729, -0.00932556203164026, 0.000840477391158112
                                                          ), `10044` = c(NA, 0.0256410256410258, 0.00249999999999995,
                                                                       -0.00249376558603487, -0.0083333333333333, 0.000840336134453779,
                                                                       0.00923593618807717, 0.0391014975041597, -0.0120096076861489,
                                                                       -0.00729335494327388, 0.00489795918367353, -0.00649878147847283,
                                                                       0.017170891251022, -0.00401929260450151, 0.00322841000807106,
                                                                       -0.00876910699919553, 0.0136352568785003, -0.00712627111858444,
                                                                       0, 0.0024193548387097, -0.00321802091713586, -0.00968523002421318,
                                                                       0.00407497962510184, -0.0089285714285714, -0.000819000819000992,
                                                                       0.00491803278688518, -0.00407830342577475, 0.0141195741195741,
                                                                       -0.00180901925313359, 0.00970873786407767, 0.00881410256410242,
                                                                       -0.0015885623510723, 0.000795544948289484, -0.0174880763116058,
                                                                       0.00404530744336573, -0.0112812248186946, -0.00488997555012216,
                                                                       0.00654381654381653, -0.00681047038625204, -0.000507938588586221,
                                                                       0.00327868852459035, -0.000816993464052285, -0.00899427636958305,
                                                                       -0.00165016501650161, -0.00165289256198342, 0.0124172185430464,
                                                                       -0.00368765331152898, -0.0102503918784725, -0.0074626865671642,
                                                                       -0.0158730158730159, -0.00169779286926997, 0.056122448979592,
                                                                       -0.0177133655394526, -0.012295081967213, 0, -0.0032365145228217,
                                                                       0.00907501456997761, -0.0053877887788778, 0.00956473408711944,
                                                                       -0.0115036976170912, 0, 0.00332502078137997, -0.00248550124275071,
                                                                       0.00249169435215957, 0.0149130074565038, -0.0204081632653061,
                                                                       0.00916666666666655, 0, -0.000825763831544202, 0.000371900826446225,
                                                                       0.00293279358916121, -0.00329489291598029, 0.00543801652892562,
                                                                       -0.00458662808857624, -0.00328654004954576, 0.000480522278007101,
                                                                       -0.00960582974494861, -0.0142140468227426, 0.000848176420695568,
                                                                       -0.0144067796610169, 0, -0.00343938091143603, 0.00690250215703192,
                                                                       -0.00783204798628967, -0.00592472319624138, 0.00955690703735868,
                                                                       -0.00413080895008611, -0.00535776011061173, -0.00086880973066894,
                                                                       0.0057304347826086, -0.00310395213515591, -0.000867302688638327,
                                                                       7.81249999999289e-05, 0.00426181982310414, 0.00172860847018153,
                                                                       -0.00431406384814503, -0.00519930675909863, -0.00958188153310113,
                                                                       0.00143359718557612, 0.00108902804247202, -0.00514971005465537,
                                                                       -0.00352733686067008, 0.000884955752212369, -0.0141467727674625,
                                                                       0.011659192825112, -0.0328014184397163, 0.00412465627864345,
                                                                       -0.00502053856686446, -0.000917431192660523, -0.00183654729109284,
                                                                       -0.00183992640294384, -0.00184331797235016, -0.000923361034164349,
                                                                       0.0138632162661738, 0.0209662716499543, 0.00714285714285712,
                                                                       0.00443262411347534, 0.00220653133274507, 0.00660501981505934,
                                                                       -0.00349956255468054, -0.00482879719051799, -0.00926334362593739,
                                                                       -0.0178094390026715, -0.00815956482320945, 0.0457038391224862
                                                          )), class = c("data.table", "data.frame"), row.names = c(NA,
                                                                                                                   -125L))


retDaily %>% 
  gather(stock, return, -date) %>% 
  as_tsibble(key = stock, index = date) %>% 
  group_by_key() %>% 
  index_by(year_month = ~ yearmonth(.)) %>% 
  summarise(data = list(return)) %>%
  mutate(skewness = map_dbl(1:length(data), ~{
    skewness(unlist(data[max(1, .x - 2) : .x]), na.rm = TRUE)
  })) %>%
  select(-data) %>%
  spread(stock, skewness)
#> Warning in as.POSIXlt.POSIXct(new, tz = "UTC"): unknown timezone 'zone/tz/
#> 2019b.1.0/zoneinfo/Asia/Hong_Kong'
#> # A tsibble: 6 x 5 [1M]
#>   year_month `10026` `10028` `10032` `10044`
#>        <mth>   <dbl>   <dbl>   <dbl>   <dbl>
#> 1   2018 Jan -1.11    -0.406   1.05    0.434
#> 2   2018 Feb -0.961   -0.262   0.636   0.527
#> 3   2018 Mar -0.964    0.240  -0.742   1.99 
#> 4   2018 Apr  0.0949   0.591  -2.55    2.19 
#> 5   2018 May -0.275    1.75   -2.82    2.59 
#> 6   2018 Jun -0.500    1.12   -3.51    0.968

Created on 2019-09-25 by the reprex package (v0.3.0)

Hi @raytong - thanks for taking the time and providing your solution! :relaxed:

Any other ideas on this?

Hi @Pia. If you want to use lag in the code. Try this.

library(tsibble)
library(e1071)
library(tidyverse)

retDaily <- structure(list(date = structure(c(17533, 17534, 17535, 17536,
                                              17539, 17540, 17541, 17542, 17543, 17547, 17548, 17549, 17550,
                                              17553, 17554, 17555, 17556, 17557, 17560, 17561, 17562, 17563,
                                              17564, 17567, 17568, 17569, 17570, 17571, 17574, 17575, 17576,
                                              17577, 17578, 17582, 17583, 17584, 17585, 17588, 17589, 17590,
                                              17591, 17592, 17595, 17596, 17597, 17598, 17599, 17602, 17603,
                                              17604, 17605, 17606, 17609, 17610, 17611, 17612, 17613, 17616,
                                              17617, 17618, 17619, 17623, 17624, 17625, 17626, 17627, 17630,
                                              17631, 17632, 17633, 17634, 17637, 17638, 17639, 17640, 17641,
                                              17644, 17645, 17646, 17647, 17648, 17651, 17652, 17653, 17654,
                                              17655, 17658, 17659, 17660, 17661, 17662, 17665, 17666, 17667,
                                              17668, 17669, 17672, 17673, 17674, 17675, 17676, 17680, 17681,
                                              17682, 17683, 17686, 17687, 17688, 17689, 17690, 17693, 17694,
                                              17695, 17696, 17697, 17700, 17701, 17702, 17703, 17704, 17707,
                                              17708, 17709, 17710, 17711), tzone = "UTC", tclass = "Date", class = "Date"),
                           `10026` = c(NA, -0.00998786767581905, 0.0138127158236847,
                                       -0.00955052427703185, 0.000741739716790146, -0.00720975675493563,
                                       -0.0127595357676122, 0.00776838940132052, -0.000613957295859224,
                                       0.00109215017064845, 0.0367516705304787, -0.01025971719829,
                                       0.0219947490202173, -0.026332899869961, 0.00313856427378956,
                                       -0.0123818399680468, 0.0172553248854137, -0.0145109992048768,
                                       0.00551334633228007, -0.0706117686392511, -0.00395719088012159,
                                       0.00794560820572077, -0.0183459236309247, -0.0273031829464154,
                                       0.0200389507626124, -0.00860856449120728, -0.0190738445098826,
                                       0.0130135431641067, 0.00261416087833322, 0.00275618275058243,
                                       0.0278582571874304, 0.00303555941023403, 0.00230587980977082,
                                       -0.00359453604640292, -0.0191198398903435, -0.00970945200441331,
                                       0.00586793433855748, 0.0125535371437011, -0.0070011668611436,
                                       -0.0134400705052877, 0.00126554008784319, 0.0202230483271375,
                                       0.0107127240926979, 0.0162232316677482, 0.00276713495104319,
                                       -0.002334960730206, 0.00567375886524824, -0.00486600846262342,
                                       -0.00070873786407788, -0.0147507279448781, -0.025552366339334,
                                       0.00967646624316743, -0.00424317799400087, -0.00330614943795471,
                                       -0.013563246351172, -0.000896801606874886, -0.0126403141361257,
                                       0.0240133341423632, -0.000739680480816629, -0.0034053893988748,
                                       0.0144109344822463, -0.0167691857059168, 0.0236091457510985,
                                       0.0130238649592549, 0.00933706816059776, -0.0145876325339785,
                                       0.0022385181975737, 0.00943886515158621, 0, 0.000142683787103248,
                                       -0.00413938053097351, 0.0205676523267631, 0.0109542869180534,
                                       0.000138987289018733, -0.0133342583975098, -0.00985436687165719,
                                       -7.10883628349812e-05, -0.00277264325323467, 0.00128323946674258,
                                       0.000142399430402396, -0.00989535132056663, -0.0120074777106702,
                                       -0.0388617276762973, -0.0182479731772566, -0.00647840505938613,
                                       0.0301194666884437, 0.0177091183119822, 0.0033320992225101,
                                       0.00177129151291533, 0.000441947808903276, 0.00942562592047125,
                                       -0.00809746133644595, 0.00544245054056058, 0.0141174007667761,
                                       0.0119734564339298, 0.00285103349964344, 0.00106616915422886,
                                       -0.0255591746141871, 0.00582877959927153, 0.0057225642883012,
                                       0.00165665514261004, 0.00740634159730047, 0.0251248376070781,
                                       -0.0139256370978972, 0.010097443863861, 0.0195036700454385,
                                       0.0110394953373558, -0.00874872838250251, 6.84181718664245e-05,
                                       0.00759389751659034, 0.0033269961977187, 0.00629356432293404,
                                       0.00416953597848013, 0.0113848773516692, 0.00463514766256123,
                                       -0.000329554442393909, 0.00962616206237232, 0.00176314242800246,
                                       0.00352027402348587, 0.00610627517214501, -0.000645661157024802,
                                       0.0144074815867683, -0.0171962943249661, -0.00602682911023267,
                                       -0.00593297692006778), `10028` = c(NA, 0.0102061855670104,
                                                                          -0.0205122971731809, -0.0103146488851844, 0.0631645436361723,
                                                                          -0.0690167343301317, 0.0316953839608594, -0.00824742268041234,
                                                                          0.0343035343035343, -0.0733668341708542, 0.0734273318872016,
                                                                          -0.000303122158229807, 0.0309278350515465, -0.0287254901960784,
                                                                          0.00938730190774195, 0.0501, -0.0191410341872202, 0, -0.0679611650485438,
                                                                          0, 0.046875, -0.0348258706467661, -0.020618556701031, -0.0313684210526315,
                                                                          0.000869376222560447, 0.0206297502714439, -0.0106382978723403,
                                                                          -0.0322580645161291, 0.0222222222222221, 0, 0.0158695652173912,
                                                                          -0.00481489407233038, 0.02128803354478, -0.0209495736393304,
                                                                          0.0103225806451612, 0.00234142188165176, 0.00934380972605653,
                                                                          -0.000525983589312107, -0.000105252078728579, -0.0210526315789473,
                                                                          -0.0053763440860215, 0.0810810810810809, -0.053, -0.0390707497360083,
                                                                          0.0219780219780219, -0.0108602150537634, 0.000108707468203129,
                                                                          0, 0.0080434782608696, -0.0187621306879447, 0.000109890109890154,
                                                                          -0.000109878035380762, 0, -0.0307692307692308, -0.0258503401360545,
                                                                          0.0416666666666667, -0.0100558659217878, -0.0244920993227992,
                                                                          -0.0396852944579429, 0.013975903614458, -0.0137832699619772,
                                                                          -0.0240963855421685, 0.0246913580246912, 0.0110843373493976,
                                                                          -0.0238322211630123, -0.00769042968750011, 0.017099274203469,
                                                                          0.0293904208998548, 0.0222065562213607, -0.0219540229885058,
                                                                          0, 0.00434833705488313, 0.0063187456119822, 0.00813953488372099,
                                                                          -0.00807381776239913, 0, -0.000116279069767411, -0.0232585184323759,
                                                                          0.0121443028932016, -0.0295259381249265, 0.0060606060606061,
                                                                          0.0120481927710843, -0.0119047619047619, -0.00614457831325299,
                                                                          -0.0180627954903624, -0.0246913580246914, -0.0126582278481012,
                                                                          0.0384615384615385, -0.0234567901234568, -0.00126422250316061,
                                                                          -0.0341772151898735, -0.0252948885976408, -0.0181524808390481,
                                                                          -0.0153382634894549, 0.0739916550764952, -0.0282310282310283,
                                                                          0.0126599147121536, -0.0130280300039479, -0.0253333333333333,
                                                                          0.0292749658002736, -0.00318979266347685, -0.0270666666666667,
                                                                          -0.0132931341647253, 0.125, -0.0903703703703704, 0.0177795874049946,
                                                                          -0.0266702226963595, -0.0135635018495684, 0.0048611111111112,
                                                                          -0.00884588804422959, 0.0181285734207224, -0.0001369675386933,
                                                                          -0.0273972602739726, -0.0140845070422535, 0.157142857142857,
                                                                          -0.109876543209877, 0.014008321775312, 0.0258514567090686,
                                                                          -0.00346666666666673, -0.0445544554455446, 0.00504131074079273,
                                                                          0.0590776090288421, -0.0723589001447179, 0.00694936888384623,
                                                                          0.0140845070422535), `10032` = c(NA, 0.00130975769482644,
                                                                                                           0.0122629169391759, 0.00492650621870472, 0.0466929197138954,
                                                                                                           -0.00906019656019663, -0.0119324345265769, 0.0123902132998746,
                                                                                                           0.0204492641363285, -0.0318809776833157, 0.00925199937274579,
                                                                                                           -0.0658794282162835, 0.0073186959414504, -0.00974240422721273,
                                                                                                           0.00533600133400025, -0.0106153590976945, 0.0149203688181057,
                                                                                                           0.0122233234225306, -0.0119125326370758, -0.00990916597853009,
                                                                                                           -0.00333611342785656, 0.0123849372384937, -0.0110762109439577,
                                                                                                           -0.027415580073554, 0.0134066689584049, 0.00729308005427409,
                                                                                                           -0.0313184037716787, -0.00104293412132805, 0.010266225856969,
                                                                                                           -0.0072338959696866, 0.0152671755725191, 0.0105946684894054,
                                                                                                           0.000338180588434156, 0.0270453008789724, -0.00971033574720204,
                                                                                                           0.00515206913744382, 0.00793650793650791, 0.0150918635170605,
                                                                                                           -0.0105042016806723, -0.0148619957537155, -0.00513925729442977,
                                                                                                           0.0236627228795201, 0.00455803353410378, 0.0134500081024145,
                                                                                                           0.00959385992964501, -0.0023756731073804, 0.0204794411811398,
                                                                                                           0.0132233976353453, -0.0102871180715493, -0.00853242320819103,
                                                                                                           0.00688468158347666, 0.0226884226884227, -0.0132198753988756,
                                                                                                           -0.00615953187557727, -0.00185931205453993, -0.0276311704439616,
                                                                                                           -0.0370370370370371, -0.00116047745358094, -0.0293775933609959,
                                                                                                           0.000341997264021954, 0.0210256410256409, -0.0165745856353591,
                                                                                                           0.0156622403813413, 0.00134093194770379, 0.00150652828925346,
                                                                                                           -0.0207253886010362, -0.00273084144051894, 0.0255005990073593,
                                                                                                           0.00901201602136181, 0.00628514720476359, 0.0175871137409598,
                                                                                                           0.00242287191083834, 0.00032226877215602, 0.00982603092783507,
                                                                                                           -0.0012761205933961, -0.00511100463184799, 0.000481618237277326,
                                                                                                           -0.00706033376123227, -0.00646412411118302, -0.110279765777489,
                                                                                                           0.0157221206581353, -0.0129589632829373, 0.0255288110867979,
                                                                                                           0.00782361308677104, -0.000705716302046522, 0.027542372881356,
                                                                                                           -0.0020618556701032, 0.00447658402203865, -0.00308536167295181,
                                                                                                           0.0110041265474552, 0.00544217687074822, -0.00541271989174563,
                                                                                                           -0.00935374149659862, 0.0116738197424893, 0.00916341422026123,
                                                                                                           0.00504456028249556, -0.00150577212648495, -0.0102211796246648,
                                                                                                           -0.0135432537667174, -0.00858074480864934, 0.003635104725636,
                                                                                                           0.00569161779924121, 0.0157777396672953, -0.0182340030390005,
                                                                                                           0.012725709372313, 0.00950925454236717, 0.0218671152228762,
                                                                                                           0.00954732510288059, -0.000978314038806327, -0.00440672433491107,
                                                                                                           0.00508196721311482, -0.00505627140760079, -0.00786885245901636,
                                                                                                           0.00908790482485133, 0.00425740953004738, -0.00538072721343552,
                                                                                                           -0.000983606557377059, 0.0129635707253035, -0.00437388627895663,
                                                                                                           0.00325414904002597, -0.0311385014596172, 0.0132239705390023,
                                                                                                           -0.0079299520898729, -0.00932556203164026, 0.000840477391158112
                                                                          ), `10044` = c(NA, 0.0256410256410258, 0.00249999999999995,
                                                                                         -0.00249376558603487, -0.0083333333333333, 0.000840336134453779,
                                                                                         0.00923593618807717, 0.0391014975041597, -0.0120096076861489,
                                                                                         -0.00729335494327388, 0.00489795918367353, -0.00649878147847283,
                                                                                         0.017170891251022, -0.00401929260450151, 0.00322841000807106,
                                                                                         -0.00876910699919553, 0.0136352568785003, -0.00712627111858444,
                                                                                         0, 0.0024193548387097, -0.00321802091713586, -0.00968523002421318,
                                                                                         0.00407497962510184, -0.0089285714285714, -0.000819000819000992,
                                                                                         0.00491803278688518, -0.00407830342577475, 0.0141195741195741,
                                                                                         -0.00180901925313359, 0.00970873786407767, 0.00881410256410242,
                                                                                         -0.0015885623510723, 0.000795544948289484, -0.0174880763116058,
                                                                                         0.00404530744336573, -0.0112812248186946, -0.00488997555012216,
                                                                                         0.00654381654381653, -0.00681047038625204, -0.000507938588586221,
                                                                                         0.00327868852459035, -0.000816993464052285, -0.00899427636958305,
                                                                                         -0.00165016501650161, -0.00165289256198342, 0.0124172185430464,
                                                                                         -0.00368765331152898, -0.0102503918784725, -0.0074626865671642,
                                                                                         -0.0158730158730159, -0.00169779286926997, 0.056122448979592,
                                                                                         -0.0177133655394526, -0.012295081967213, 0, -0.0032365145228217,
                                                                                         0.00907501456997761, -0.0053877887788778, 0.00956473408711944,
                                                                                         -0.0115036976170912, 0, 0.00332502078137997, -0.00248550124275071,
                                                                                         0.00249169435215957, 0.0149130074565038, -0.0204081632653061,
                                                                                         0.00916666666666655, 0, -0.000825763831544202, 0.000371900826446225,
                                                                                         0.00293279358916121, -0.00329489291598029, 0.00543801652892562,
                                                                                         -0.00458662808857624, -0.00328654004954576, 0.000480522278007101,
                                                                                         -0.00960582974494861, -0.0142140468227426, 0.000848176420695568,
                                                                                         -0.0144067796610169, 0, -0.00343938091143603, 0.00690250215703192,
                                                                                         -0.00783204798628967, -0.00592472319624138, 0.00955690703735868,
                                                                                         -0.00413080895008611, -0.00535776011061173, -0.00086880973066894,
                                                                                         0.0057304347826086, -0.00310395213515591, -0.000867302688638327,
                                                                                         7.81249999999289e-05, 0.00426181982310414, 0.00172860847018153,
                                                                                         -0.00431406384814503, -0.00519930675909863, -0.00958188153310113,
                                                                                         0.00143359718557612, 0.00108902804247202, -0.00514971005465537,
                                                                                         -0.00352733686067008, 0.000884955752212369, -0.0141467727674625,
                                                                                         0.011659192825112, -0.0328014184397163, 0.00412465627864345,
                                                                                         -0.00502053856686446, -0.000917431192660523, -0.00183654729109284,
                                                                                         -0.00183992640294384, -0.00184331797235016, -0.000923361034164349,
                                                                                         0.0138632162661738, 0.0209662716499543, 0.00714285714285712,
                                                                                         0.00443262411347534, 0.00220653133274507, 0.00660501981505934,
                                                                                         -0.00349956255468054, -0.00482879719051799, -0.00926334362593739,
                                                                                         -0.0178094390026715, -0.00815956482320945, 0.0457038391224862
                                                                          )), class = c("data.table", "data.frame"), row.names = c(NA,
                                                                                                                                   -125L))


retDaily %>% 
  gather(stock, return, -date) %>% 
  as_tsibble(key = stock, index = date) %>% 
  group_by_key() %>% 
  index_by(year_month = ~ yearmonth(.)) %>% 
  summarise(data = list(return)) %>%
  mutate(skewness = pmap_dbl(list(data, lag(data), lag(data, 2)), function(x, y, z){skewness(c(x, y, z), na.rm = TRUE)})) %>%
  select(-data) %>%
  spread(stock, skewness)
#> # A tsibble: 6 x 5 [1M]
#>   year_month `10026` `10028` `10032` `10044`
#>        <mth>   <dbl>   <dbl>   <dbl>   <dbl>
#> 1   2018 Jan -1.11    -0.406   1.05    0.434
#> 2   2018 Feb -0.961   -0.262   0.636   0.527
#> 3   2018 Mar -0.964    0.240  -0.742   1.99 
#> 4   2018 Apr  0.0949   0.591  -2.55    2.19 
#> 5   2018 May -0.275    1.75   -2.82    2.59 
#> 6   2018 Jun -0.500    1.12   -3.51    0.968

Created on 2019-09-26 by the reprex package (v0.3.0)

1 Like

Hi @raytong. Thanks for your answer! This code comes put with the exact same numbers I have computed by hand, so my problem is solved now. Appreciate your efforts!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.