1
votes

This is my transaction data:

data:

id          from    to          date        amount  
<int>       <fctr>  <fctr>      <date>      <dbl>
19521       6644    6934        2005-01-01  700.0
19524       6753    8456        2005-01-01  600.0
19523       9242    9333        2005-01-01  1000.0
…           …       …           …           …
1056317     7819    7454        2010-12-31  60.2
1056318     6164    7497        2010-12-31  107.5
1056319     7533    7492        2010-12-31  164.1

Previously I asked this question, where I needed to form a network in specified time intervals, then perform network measures on the networks . Now, I need to go a little bit further.
What I want to do is as follows:

  • For example let's consider row 1 . In row 1, transaction date is "2005-01-01", so we need to extract data where date_trx lie in "2004-07-05"-"2005-01-01"(which is the last 6 month period prior to the date "2005-01-01") and then form the network on this extracted data to calculate the network's measures such as degree centrality, betweenness centrality of the accounts in the network. At the end, the first row of the new colunms degree_centrality and betweenness_centrality will contain degree centrality of the account 6644 and betweenness centrality of the account 6644 respectively.
  • Similarly, in row 2, transaction date again is "2005-01-01", so we need to extract data where date_trx lie in "2004-07-05"-"2005-01-01"(which is the last 6 month period prior to the date "2005-01-01") and then form the network on this extracted data to calculate the network's measures such as degree centrality, betweenness centrality of the accounts in the network. At the end, the second row of the new colunms degree_centrality and betweenness_centrality will contain degree centrality of the account 6753 and betweenness centrality of the account 6753 respectively.
  • And it goes like this for all the rows of the data.

PS:

  • Let's consider row 1, row 2 and row 3. Since the dates are the same, the networks that will be created by data whose dates lie in the range "2004-07-05"-"2005-01-01" will be exactly the same for all the three rows. However, since network measures such as degree centr. or betweenness centr. will be added to the data according to the accounts in from column, these network measures for 6644, 6753 and 9242 will more likely be different from each other.
  • Also, the boundaries of date intervals taken into account when we extract data between a date range to form a network. For example, the very first date in my data is "2005-01-01"( and there are 4 transactions made in that date in fact). But, let's say there is only one observation(transaction) on this very first date "2005-01-01" and there's not any observation before this date, now we want to create a network of data ranging between"2004-07-05"-"2005-01-01". Since as I said the boundaries also count, the only observation we can take -in this specified date range- to form the network is the one with date "2005-01-01". So, since a transaction consists of two accounts, thus only these two accounts(nodes) will be present in the network. So, at the end of the day, one observation is sufficient to build a network and calculate network metrics on it.

dput() output of the data sample:

structure(list(id = c(18529L, 13742L, 9913L, 956L, 2557L, 1602L, 
18669L, 35900L, 48667L, 51341L, 53713L, 60126L, 60545L, 65113L, 
66783L, 83324L, 87614L, 88898L, 89874L, 94765L, 100277L, 101587L, 
103444L, 108414L, 113319L, 121516L, 126607L, 130170L, 131771L, 
135002L, 149431L, 157403L, 157645L, 158831L, 162597L, 162680L, 
163901L, 165044L, 167082L, 168562L, 168940L, 172578L, 173031L, 
173267L, 177507L, 179167L, 182612L, 183499L, 188171L, 189625L, 
193940L, 198764L, 199342L, 200134L, 203328L, 203763L, 204733L, 
205651L, 209672L, 210242L, 210979L, 214532L, 214741L, 215738L, 
216709L, 220828L, 222140L, 222905L, 226133L, 226527L, 227160L, 
228193L, 231782L, 232454L, 233774L, 237836L, 237837L, 238860L, 
240223L, 245032L, 246673L, 247561L, 251611L, 251696L, 252663L, 
254410L, 255126L, 255230L, 258484L, 258485L, 259309L, 259910L, 
260542L, 262091L, 264462L, 264887L, 264888L, 266125L, 268574L, 
272959L, 273519L, 274064L, 274276L, 275121L, 277368L, 279699L, 
280009L, 280771L, 283004L, 285434L, 286555L, 287519L, 287672L, 
292464L, 292967L, 293761L, 298688L, 299828L, 301001L, 305967L, 
306733L, 307195L, 307690L, 312021L, 312387L, 313201L, 313617L, 
314821L, 319220L, 319431L, 319896L, 319898L, 320552L, 321169L, 
321489L, 323363L, 326642L, 327878L, 329300L, 329693L, 334956L, 
336257L, 337051L, 337453L, 341921L, 342486L, 343779L, 345045L, 
346051L, 350569L, 351775L, 351776L, 351982L, 352926L, 357909L, 
359279L, 360537L, 360625L, 360626L, 360893L, 366453L, 367527L, 
369371L, 372062L, 372399L, 374143L, 375554L, 376953L, 383066L, 
383834L, 388124L, 390941L, 391457L, 392392L, 393886L, 394712L, 
400161L, 400163L, 400293L, 400947L, 408385L, 408433L, 408770L, 
409130L, 409884L, 409957L, 411094L, 411473L, 417931L, 417933L, 
423987L, 426306L, 427859L, 427933L, 429862L, 437299L, 437302L, 
439673L, 440086L, 442868L, 445391L, 447715L, 449318L, 452670L, 
453274L, 458907L, 462533L, 463977L, 463978L, 465277L, 466227L, 
466334L, 468656L, 468984L, 469658L, 470878L, 471085L, 475595L, 
479825L, 480706L, 482107L, 482396L, 484029L, 487872L, 489049L, 
489244L, 491078L, 492645L, 494182L, 494770L, 499833L, 501560L, 
503281L, 506762L, 507155L, 510635L, 510636L, 511456L, 518516L, 
520256L, 522059L, 523879L, 526605L, 531419L, 541120L, 542984L, 
543255L, 544835L, 552145L, 552249L, 552599L, 556641L, 558437L, 
560182L, 560316L, 561661L, 562757L, 564655L, 566807L, 568924L, 
569579L, 572454L, 575342L, 583113L, 584735L, 586566L, 588467L, 
588650L, 590397L, 593263L, 596176L, 596991L, 598516L, 600036L, 
600890L, 601217L, 605716L, 608700L, 608986L, 610955L, 611554L, 
612369L, 612912L, 614413L, 614581L, 615689L, 617839L, 621218L, 
625643L, 631926L, 632402L, 632634L, 632971L, 633878L, 634037L, 
635879L, 635881L, 641722L, 644074L, 644363L, 647383L, 648449L, 
648705L, 651123L, 653520L, 653632L, 655184L, 657159L, 659304L, 
659383L, 664298L, 667950L, 670871L, 671575L, 674758L, 679149L, 
681217L, 683256L, 686397L, 690087L, 692048L, 693581L, 698475L, 
702839L, 703263L, 705359L, 707965L, 708472L, 709573L, 711538L, 
717942L, 724743L, 728282L, 728648L, 729681L, 731550L, 733199L, 
733615L, 735554L, 736414L, 738424L, 738745L, 740422L, 741730L, 
742188L, 744849L, 749384L, 752006L, 752873L, 755237L, 757632L, 
759299L, 759719L, 761893L, 763247L, 766302L, 773620L, 773833L, 
774422L, 776563L, 778547L, 779605L, 779717L, 781104L, 785381L, 
787524L, 796458L, 798356L, 799486L, 800502L, 801384L, 801958L, 
802663L, 806221L, 809515L, 810634L, 811948L, 811977L, 814180L, 
814464L, 817624L, 818373L, 823705L, 825916L, 827123L, 828005L, 
830594L, 835990L, 836925L, 837301L, 842136L, 848963L, 849984L, 
851164L, 853383L, 856965L, 857455L, 862780L, 868448L, 869178L, 
871528L, 872324L, 873608L, 874589L, 876773L, 877903L, 879305L, 
884153L, 888738L, 888830L, 889369L, 890395L, 891820L, 894176L, 
901068L, 902416L, 904059L, 904624L, 906822L, 916201L, 926644L, 
927983L, 929856L, 930224L, 931276L, 932214L, 932332L, 934410L, 
934979L, 935556L, 937221L, 937603L, 941771L, 944454L, 946767L, 
950291L, 950302L, 951398L, 953147L, 953606L, 955816L, 957054L, 
957102L, 957984L, 967407L, 973595L, 974085L, 978109L, 979048L, 
979900L, 981315L, 983423L, 985428L, 985680L, 986613L, 992879L, 
997651L, 999781L, 1004742L, 1006958L, 1009140L, 1009227L, 1009644L, 
1013484L, 1018535L, 1019671L, 1030067L, 1030636L, 1032335L, 1033712L, 
1035096L, 1036537L, 1038682L, 1042439L, 1045492L, 1050247L, 1054737L, 
1056136L), from = c("5370", "5370", "5370", "8605", "5370", "6390", 
"5370", "5370", "8934", "5370", "5635", "6046", "5680", "8026", 
"9037", "5370", "7816", "8046", "5492", "8756", "5370", "9254", 
"5370", "5370", "7078", "6615", "5370", "9817", "8228", "8822", 
"5735", "7058", "5370", "8667", "9315", "6053", "7990", "8247", 
"8165", "5656", "9261", "5929", "8251", "5370", "6725", "5370", 
"6004", "7022", "7442", "5370", "8679", "6491", "7078", "5370", 
"5370", "5370", "5658", "5370", "9296", "8386", "5370", "5370", 
"5370", "9535", "5370", "7541", "5370", "9621", "5370", "7158", 
"8240", "5370", "5370", "8025", "5370", "5370", "5370", "6989", 
"5370", "7059", "5370", "5370", "5370", "9121", "5608", "5370", 
"5370", "7551", "5370", "5370", "5370", "5370", "9163", "9362", 
"6072", "5370", "5370", "5370", "5370", "5370", "5370", "6169", 
"5370", "5370", "7034", "8313", "7930", "7151", "8914", "5370", 
"9277", "5370", "5370", "6376", "5939", "6253", "5370", "8629", 
"5370", "6778", "5370", "5370", "7783", "5370", "5370", "5370", 
"7319", "5370", "6644", "9221", "6820", "5370", "5370", "5370", 
"6326", "5370", "5370", "6495", "5370", "5370", "7818", "9788", 
"6831", "5370", "7986", "5370", "5700", "5370", "5679", "7074", 
"5370", "6125", "5370", "6806", "5370", "5841", "5370", "5370", 
"5370", "5370", "7177", "8369", "5370", "7420", "9686", "5370", 
"5875", "5370", "6196", "5370", "7538", "5370", "5370", "8761", 
"5370", "9837", "5370", "6214", "7755", "5370", "7824", "5370", 
"5370", "5370", "8115", "8941", "8582", "5370", "5583", "5370", 
"5990", "5370", "7497", "5370", "5370", "8336", "5370", "5370", 
"9847", "7269", "8479", "5370", "6973", "5370", "5370", "5870", 
"9500", "5370", "9406", "6806", "6644", "6255", "9135", "5370", 
"7008", "5370", "8197", "5370", "5370", "5370", "5921", "8951", 
"5370", "8726", "8384", "5370", "9446", "5370", "5370", "8435", 
"5370", "7220", "5370", "9427", "7664", "5370", "6996", "8811", 
"6455", "5370", "6022", "5370", "7827", "7290", "5370", "5409", 
"6988", "5370", "5698", "5844", "8754", "5370", "5370", "5370", 
"5370", "9003", "5370", "9024", "5370", "5370", "5370", "5370", 
"8650", "5370", "5370", "5370", "5924", "7815", "5370", "6876", 
"8445", "8524", "6799", "6951", "7606", "5950", "9421", "5370", 
"5370", "9301", "9409", "8810", "5370", "8892", "5370", "8843", 
"5370", "6891", "5370", "5370", "5370", "5370", "5370", "9869", 
"7880", "5370", "5370", "8346", "7427", "8308", "6452", "7104", 
"5370", "7228", "5370", "7694", "5370", "6296", "5370", "8624", 
"5393", "5982", "8619", "8494", "6354", "5370", "8954", "5370", 
"5370", "5370", "8356", "5370", "5370", "5370", "5370", "6342", 
"5370", "5636", "5370", "5370", "6195", "7265", "5370", "5370", 
"5370", "5370", "7504", "9289", "6814", "5370", "5370", "5370", 
"6649", "9706", "5370", "7385", "8110", "6359", "6557", "6338", 
"5370", "5370", "7937", "9238", "5370", "5370", "6511", "5370", 
"5370", "9038", "5370", "9646", "5370", "5370", "5370", "5516", 
"5370", "5370", "5370", "6020", "7005", "9824", "5370", "5370", 
"5370", "5370", "7131", "6629", "5370", "7683", "5370", "9720", 
"5370", "9586", "5370", "5370", "5370", "8984", "5505", "5789", 
"5370", "5370", "5370", "7272", "5370", "5370", "5370", "5907", 
"5370", "9731", "5370", "5370", "8359", "5370", "7879", "8048", 
"5370", "9458", "7931", "7918", "5983", "5939", "6592", "5370", 
"5765", "5370", "5370", "5898", "5370", "9558", "9372", "5370", 
"5936", "9580", "5370", "8458", "5370", "5370", "9692", "5370", 
"6290", "5703", "5747", "9788", "6540", "8600", "7420", "6459", 
"5370", "5370", "6768", "7523", "5370", "5370", "9177", "5370", 
"8898", "7257", "5370", "5370", "6395", "5370", "7168", "7671", 
"6158", "5372", "7983", "8919", "5370", "7614", "5370", "8631", 
"5370", "8342", "7227", "5370", "5370", "5370", "6700", "5370", 
"5370", "7078", "7349", "5370", "8210", "9427", "5370", "5613"
), to = c("9356", "5605", "8567", "5370", "5636", "5370", "8933", 
"8483", "5370", "7626", "5370", "5370", "5370", "5370", "5370", 
"9676", "5370", "5370", "5370", "5370", "9105", "5370", "9772", 
"6979", "5370", "5370", "7564", "5370", "5370", "5370", "5370", 
"5370", "8744", "5370", "5370", "5370", "5370", "5370", "5370", 
"5370", "5370", "5370", "5370", "7318", "5370", "8433", "5370", 
"5370", "5370", "7122", "5370", "5370", "5370", "8566", "6728", 
"9689", "5370", "8342", "5370", "5370", "5614", "5596", "5953", 
"5370", "7336", "5370", "7247", "5370", "7291", "5370", "5370", 
"6282", "7236", "5370", "8866", "8613", "9247", "5370", "6767", 
"5370", "9273", "7320", "9533", "5370", "5370", "8930", "9343", 
"5370", "9499", "7693", "7830", "5392", "5370", "5370", "5370", 
"7497", "8516", "9023", "7310", "8939", "9736", "5370", "8679", 
"5585", "5370", "5370", "5370", "5370", "5370", "7691", "5370", 
"7332", "9476", "5370", "5370", "5370", "9239", "5370", "6417", 
"5370", "8445", "6016", "5370", "7933", "6929", "7437", "5370", 
"6672", "5370", "5370", "5370", "9118", "6640", "7429", "5370", 
"9149", "7979", "5370", "5716", "9405", "5370", "5370", "5370", 
"6433", "5370", "8747", "5370", "6675", "5370", "5370", "6621", 
"5370", "8237", "5370", "6934", "5370", "9361", "7366", "8699", 
"9564", "5370", "5370", "5373", "5370", "5370", "8457", "5370", 
"6650", "5370", "7033", "5370", "9807", "6984", "5370", "6269", 
"5370", "6510", "5370", "5370", "9001", "5370", "6445", "6874", 
"8926", "5370", "5370", "5370", "9188", "5370", "5660", "5370", 
"6061", "5370", "7523", "8130", "5370", "9570", "6142", "5370", 
"5370", "5370", "9272", "5370", "8558", "7020", "5370", "5370", 
"8890", "5370", "5370", "5370", "5370", "5370", "5684", "5370", 
"6282", "5370", "8422", "7715", "6404", "5370", "5370", "7182", 
"5370", "5370", "9635", "5370", "7395", "9352", "5370", "6450", 
"5370", "5640", "5370", "5370", "8654", "5370", "5370", "5370", 
"6601", "5370", "6029", "5370", "5370", "9251", "5370", "5370", 
"9809", "5370", "5370", "5370", "7687", "5417", "8629", "6171", 
"5370", "8927", "5370", "9675", "8547", "8601", "6474", "5370", 
"9295", "9699", "7161", "5370", "5370", "9359", "5370", "5370", 
"5370", "5370", "5370", "5370", "5370", "5370", "8142", "7801", 
"5370", "5370", "5370", "8378", "5370", "5460", "5370", "9495", 
"5370", "8673", "8990", "6929", "7061", "9469", "5370", "5370", 
"8067", "7833", "5370", "5370", "5370", "5370", "5370", "7064", 
"5370", "8462", "5370", "6096", "5370", "6985", "5370", "5370", 
"5370", "5370", "5370", "5370", "7698", "5370", "5872", "5894", 
"9164", "5370", "5554", "5872", "5592", "5565", "5370", "6805", 
"5370", "8188", "8041", "5370", "5370", "7622", "8610", "9157", 
"8521", "5370", "5370", "5370", "7297", "5802", "6610", "5370", 
"5370", "6019", "5370", "5370", "5370", "5370", "5370", "6422", 
"7773", "5370", "5370", "6673", "7844", "5370", "7257", "5786", 
"5370", "7486", "5370", "8512", "7647", "6112", "5370", "6670", 
"9836", "9514", "5370", "5370", "5370", "7172", "8962", "6249", 
"9216", "5370", "5370", "8584", "5370", "7282", "5370", "8202", 
"5370", "8537", "9829", "8265", "5370", "5370", "5370", "7361", 
"8598", "9051", "5370", "9602", "5926", "5719", "5370", "9278", 
"5370", "8702", "8958", "5370", "5862", "5370", "5370", "7666", 
"5370", "5370", "5370", "5370", "5370", "5370", "6352", "5370", 
"6251", "5722", "5370", "8564", "5370", "5370", "8219", "5370", 
"5370", "9731", "5370", "5473", "6430", "5370", "8813", "5370", 
"5370", "5370", "5370", "5370", "5370", "5370", "5370", "5692", 
"6612", "5370", "5370", "7801", "8606", "5370", "9652", "5370", 
"5370", "7143", "7007", "5370", "7474", "5370", "5370", "5370", 
"5370", "5370", "5370", "6555", "5370", "9065", "5370", "5623", 
"5370", "5370", "6888", "8494", "6909", "5370", "5827", "5852", 
"5370", "5370", "6710", "5370", "5370", "7836", "5370"), date = structure(c(12934, 
13000, 13038, 13061, 13099, 13113, 13117, 13179, 13238, 13249, 
13268, 13296, 13299, 13309, 13314, 13391, 13400, 13404, 13409, 
13428, 13452, 13452, 13460, 13482, 13493, 13518, 13526, 13537, 
13542, 13544, 13596, 13616, 13617, 13626, 13633, 13633, 13639, 
13642, 13646, 13656, 13660, 13664, 13667, 13669, 13677, 13686, 
13694, 13694, 13707, 13716, 13725, 13738, 13739, 13746, 13756, 
13756, 13756, 13761, 13769, 13770, 13776, 13786, 13786, 13786, 
13791, 13799, 13806, 13813, 13817, 13817, 13817, 13822, 13829, 
13830, 13836, 13847, 13847, 13847, 13852, 13860, 13866, 13871, 
13878, 13878, 13878, 13882, 13883, 13883, 13887, 13887, 13888, 
13889, 13890, 13891, 13895, 13896, 13896, 13899, 13905, 13909, 
13909, 13909, 13910, 13914, 13918, 13922, 13923, 13926, 13938, 
13938, 13938, 13943, 13943, 13951, 13952, 13956, 13969, 13969, 
13974, 13982, 13983, 13986, 13989, 13999, 13999, 13999, 13999, 
14004, 14011, 14011, 14012, 14012, 14013, 14016, 14018, 14030, 
14030, 14030, 14035, 14036, 14043, 14046, 14050, 14052, 14060, 
14060, 14060, 14065, 14067, 14073, 14076, 14076, 14077, 14083, 
14091, 14091, 14096, 14096, 14096, 14097, 14104, 14106, 14117, 
14122, 14122, 14122, 14122, 14127, 14135, 14136, 14152, 14152, 
14152, 14152, 14157, 14158, 14165, 14165, 14165, 14166, 14183, 
14183, 14183, 14183, 14183, 14183, 14188, 14188, 14196, 14196, 
14213, 14213, 14213, 14213, 14218, 14226, 14226, 14231, 14232, 
14243, 14244, 14244, 14244, 14249, 14249, 14254, 14256, 14257, 
14257, 14258, 14259, 14259, 14263, 14263, 14264, 14266, 14267, 
14274, 14275, 14275, 14275, 14275, 14280, 14285, 14286, 14286, 
14288, 14291, 14299, 14302, 14303, 14303, 14308, 14312, 14313, 
14316, 14316, 14317, 14334, 14334, 14334, 14339, 14342, 14347, 
14364, 14364, 14364, 14369, 14377, 14377, 14377, 14391, 14395, 
14395, 14395, 14395, 14395, 14395, 14400, 14402, 14403, 14406, 
14408, 14425, 14425, 14425, 14425, 14425, 14430, 14433, 14436, 
14437, 14438, 14440, 14443, 14445, 14456, 14456, 14456, 14456, 
14458, 14461, 14461, 14463, 14463, 14464, 14466, 14469, 14484, 
14487, 14487, 14487, 14487, 14487, 14487, 14492, 14492, 14498, 
14500, 14500, 14508, 14513, 14514, 14517, 14517, 14517, 14517, 
14517, 14522, 14522, 14527, 14530, 14537, 14540, 14548, 14548, 
14548, 14553, 14556, 14560, 14561, 14563, 14578, 14578, 14578, 
14578, 14583, 14584, 14585, 14586, 14591, 14607, 14609, 14609, 
14609, 14609, 14609, 14609, 14611, 14612, 14614, 14614, 14615, 
14616, 14616, 14618, 14620, 14622, 14622, 14624, 14626, 14628, 
14629, 14631, 14633, 14637, 14640, 14640, 14640, 14640, 14645, 
14646, 14646, 14648, 14652, 14653, 14668, 14668, 14668, 14668, 
14670, 14673, 14673, 14676, 14679, 14680, 14681, 14681, 14685, 
14686, 14699, 14699, 14699, 14699, 14703, 14704, 14707, 14711, 
14712, 14712, 14727, 14729, 14729, 14729, 14734, 14737, 14738, 
14742, 14760, 14760, 14760, 14760, 14760, 14760, 14760, 14763, 
14765, 14769, 14773, 14773, 14773, 14774, 14777, 14784, 14790, 
14790, 14790, 14790, 14795, 14803, 14821, 14821, 14821, 14821, 
14824, 14826, 14826, 14828, 14829, 14829, 14830, 14831, 14834, 
14840, 14850, 14852, 14852, 14852, 14852, 14852, 14852, 14856, 
14856, 14857, 14865, 14882, 14882, 14882, 14882, 14882, 14882, 
14887, 14889, 14889, 14890, 14895, 14910, 14913, 14913, 14913, 
14918, 14918, 14919, 14922, 14926, 14927, 14943, 14943, 14943, 
14946, 14948, 14949, 14951, 14954, 14956, 14967, 14974, 14974
), class = "Date"), amount = c(24.4, 7618, 21971, 5245, 2921, 
8000, 169.2, 71.5, 14.6, 4214, 14.6, 13920, 14.6, 24640, 1600, 
261.1, 16400, 3500, 2700, 19882, 182, 14.6, 16927, 25653, 3059, 
2880, 9658, 4500, 12480, 14.6, 1000, 3679, 34430, 12600, 14.6, 
19.2, 4900, 826, 3679, 2100, 38000, 79, 11400, 21495, 3679, 200, 
14.6, 100.6, 3679, 5300, 108.9, 3679, 2696, 7500, 171.6, 14.6, 
99.2, 2452, 3679, 3218, 700, 69.7, 14.6, 91.5, 2452, 3679, 2900, 
17572, 14.6, 14.6, 90.5, 2452, 49752, 3679, 1900, 14.6, 870, 
85.2, 2452, 3679, 1600, 540, 14.6, 14.6, 79, 210, 2452, 28400, 
720, 180, 420, 44289, 489, 3679, 840, 2900, 150, 870, 420, 14.6, 
152.9, 64.1, 41464, 2452, 205, 3679, 2166, 100, 14.6, 14.6, 59.7, 
6588, 2452, 3679, 65, 200, 14.6, 65.7, 2452, 3679, 15792, 2000, 
2200, 99.7, 14.6, 140.3, 67.6, 2452, 5600, 873, 3679, 660, 36258, 
1900, 2002, 263, 14.6, 63.3, 2452, 14607, 3679, 1400, 7680, 360, 
14.6, 14.6, 65.5, 2452, 344, 3679, 990, 1000, 19652, 1080, 14.6, 
72.8, 22991, 2452, 1140, 9226, 3679, 2400, 1410, 18300, 14.6, 
14.6, 75.7, 2452, 3679, 1700, 14.6, 14.6, 65.4, 78.8, 2452, 6846, 
270, 3679, 6339, 3000, 14.6, 14.6, 358.3, 102.3, 14.6, 73.2, 
3869, 2452, 3679, 2800, 14.6, 14.6, 72.4, 228.2, 2452, 3679, 
3700, 180, 6900, 350, 14.6, 14.6, 59.2, 23131, 2452, 2880, 1200, 
630, 3679, 2400, 1680, 17100, 720, 390, 2520, 450, 1920, 840, 
137.9, 14.6, 14.6, 55.4, 2452, 5800, 1700, 24977, 3679, 570, 
630, 11700, 14.6, 58.3, 2452, 1347, 1455, 4600, 3679, 3300, 14.6, 
14.6, 51.9, 2452, 2038, 3679, 14.6, 49.6, 14.6, 2452, 9252, 3294, 
3679, 1170, 70.5, 70.1, 91.3, 14.6, 14.6, 54.1, 2452, 46181, 
15586, 74529, 3679, 189.5, 318.1, 14.6, 55.2, 14.6, 2452, 6446, 
1500, 6300, 3679, 8600, 300, 6500, 2600, 185.2, 14.6, 64.1, 11200, 
3041, 2452, 2895, 14037, 3103, 1200, 3679, 870, 14.6, 57.9, 93, 
109.1, 67.1, 14.6, 2452, 390, 13200, 7746, 3679, 2300, 2960, 
18292, 14.6, 136.9, 7500, 14.6, 65, 2452, 2974, 331, 3679, 1300, 
14600, 14.6, 14.6, 72.4, 2452, 1100, 10315, 3679, 39864, 94.1, 
101.2, 14.6, 72.4, 2452, 1469, 27132, 4700, 3679, 1000, 101.8, 
58.4, 76, 14.6, 14.6, 61.6, 38200, 570, 2400, 2452, 600, 400, 
100, 2412, 4485, 1320, 3679, 2500, 600, 810, 1800, 2400, 330, 
480, 14.6, 114, 14.6, 55.9, 2452, 500, 4356, 7045, 11889, 3679, 
146.1, 14.6, 288.5, 56.3, 1300, 3, 2452, 7156, 24784, 24423, 
3679, 822, 810, 20800, 2.2, 1800, 14.6, 63, 1050, 2452, 15976, 
4755, 11100, 3679, 180, 14.6, 166.6, 59.9, 2452, 5411, 13405, 
3679, 183.9, 14.6, 35.4, 10.8, 14.6, 14.6, 66.7, 1800, 2452, 
2277, 511, 800, 3679, 65, 5040, 450, 14.6, 14.6, 93.5, 71.5, 
2452, 3679, 14.6, 14.6, 14.6, 75.4, 270, 14, 2452, 4622, 145, 
24324, 1523, 372, 3679, 1400, 3100, 14.6, 14.6, 14.6, 77.9, 14.6, 
77.1, 14989, 6500, 2452, 3679, 247.3, 14.6, 14.6, 14.6, 190.6, 
58.7, 2452, 1398, 4268, 3597, 3679, 1800, 14.6, 14.6, 63.1, 2452, 
12400, 5106, 4633, 3679, 510, 14.6, 204.7, 67.1, 4500, 2452, 
5800, 1275, 6460, 3679, 6406, 78.7, 52.6)), row.names = c(NA, 
-478L), class = "data.frame")

EDIT:
According to @pseudospin 's solution, the following code:

library(data.table)
setDT(dt)
measure_graph <- function(dt) {
  network <- graph_from_data_frame(dt[,.(from, to)], directed=TRUE)
  accounts <- dt[date == max(date), from]
  list(
    account = accounts,
    degree = degree(network, mode = "all")[accounts],
    in_degree = degree(network, mode = "in")[accounts],
    out_degree = degree(network, mode = "out")[accounts]
  )
}

dt[, measure_graph(dt[(date >= end_date - 180) & (date <= end_date)]), .(end_date = date)]

when applied using the data provided above returns this output:

end_date    account     degree    in_degree    out_degree
<date>      <fctr>       <dbl>    <dbl>        <dbl>
2005-05-31  5370        1         0            1
2005-08-05  5370        2         0            2
2005-09-12  5370        3         0            3
2005-10-05  8605        NA        NA           NA
2005-11-12  5370        5         1            4
2005-11-26  6390        NA        NA           NA
2005-11-30  5370        6         2            4
2006-01-31  5370        7         2            5
2006-03-31  8934        NA        NA           NA
2006-04-11  5370        6         2            4
...

Here, why don't the accounts 8605,6390,8934 have degree=1, in_degree=0 and out_degree=1 but have NAs instead? Also, some values are correct, but some are not. For example, consider the date "2010-12-11" for the account 5370.
Filtering the output by this date and account gives:

end_date    account     degree   in_degree    out_degree
<date>      <fctr>      <dbl>    <dbl>        <dbl>
2010-12-11  5370        1        0            1

However, when I filter the sample data provided above by the last 6 month period (which is "2010-06-14"-"2010-12-11") prior to the date "2010-12-11" and the account 5370, by

data[(date <="2010-12-11" & date>=as.Date(as.numeric(as.Date("2010-12-11"))-180, origin="1970-01-01")) & (from==5370 | to==5370)]

it gives:

id          from    to          date        amount  
<int>       <fctr>  <fctr>      <date>      <dbl>
890395       5939   5370        2010-06-14  65.0
891820       6592   5370        2010-06-17  5040.0
894176       5370   6352        2010-06-24  450.0
901068       5765   5370        2010-06-30  14.6
902416       5370   6251        2010-06-30  14.6
904059       5370   5722        2010-06-30  93.5
904624       5898   5370        2010-06-30  71.5
...
1032335      6700   5370        2010-11-30  67.1
1033712      5370   5827        2010-12-03  4500.0
1035096      5370   5852        2010-12-05  2452.0
1036537      7078   5370        2010-12-06  5800.0
1038682      7349   5370        2010-12-08  1275.0
1042439      5370   6710        2010-12-11  6460.0  

(63 rows in total)

So, according to this, in fact 5370 should have degree=63, and if we only filter by to==5370 we'll see it should have in_degree=37 and similarly if we only filter by from==5370 we'll see it should have out_degree=26, for the specified date range "2010-06-14"-"2010-12-11", whereas the code computes it as 1,0 and 1 respectively. Why does the code give correct results for some, but wrong results for some others?

1
I copied your version of the code and it seems to work - I don't see the NAs you see. It's probably something to do with the name s of the datatables - you're using data, I was using dt both outside and inside the function. Try making them all different names and remove any old ones from your environment. Also, the sample data in the question stops at 2008 so I can't check the rest of the question.pseudospin
I have just noticed, from and to columns should be in character format, rather than factor, when forming the network. This way, all values are computed correctly. I think when these two columns are factors, errors happen since the number of accounts the levels of these factor columns contain is more than the number of accounts present to form the network. Converting these two columns into character solves the problem.Leyla Alkan

1 Answers

1
votes

I think you just have to do it as you describe. I'd arrange it something like this:

library(data.table)
setDT(dt)
measure_graph <- function(dt) {
  network <- graph_from_data_frame(dt[,.(from, to)], directed=TRUE)
  accounts <- dt[date == max(date), from]
  list(
    account = accounts,
    closeness = closeness(network, normalized = TRUE)[accounts],
    betweenness = betweenness(network, normalized = TRUE)[accounts]
  )
}

dt[, measure_graph(dt[(date >= end_date - 180) & (date <= end_date)]), .(end_date = date)]

This will only create the graphs once for each date and extract the stats you want for all the from accoutns on that date.