Cumulative Variance

code
rtip
cumulative
sapply
lapply
Author

Steven P. Sanderson II, MPH

Published

October 24, 2022

Introducton

This is going to be a simple example on how we can make a function in #base #r that will crate a cumulative variance function. From base R we are going to use `seq_along()`, `stats::var()`, and `sapply()` inside of the function we will call `cvar` for cumulative variance.

Generate Data

The first thing we need to do in order to showcase this function is to generate some data. Lets do that below:

``````l <- list(
a = rnorm(50),
b = rnorm(50, 1),
c = rnorm(50, 2)
)

l``````
``````\$a
[1] -0.96548479  0.49276394  0.14030455  1.11786377 -1.47239834 -0.06906506
[7] -1.51133985  1.48910665  0.09444727 -0.01216806  0.35365683 -1.13562871
[13] -1.27899694  0.10963391 -0.00708945 -1.26718573  0.92143855  0.09716551
[19] -0.28025814 -0.18046616 -1.75919633  0.01686201 -0.10204673  0.91791398
[25] -1.70503761  1.50856724 -1.29433294  0.42665133 -0.78176459  0.16141529
[31]  1.42536506 -0.42168041 -0.30222269  0.05129043 -0.73717680 -1.60823604
[37] -0.11921815  0.08357566  0.23250949  0.50846618 -0.02674088  0.12101223
[43]  0.10390867 -1.11476987 -1.42201791 -1.35493159  0.35703193 -1.08176152
[49] -0.08189606  0.46341303

\$b
[1]  1.67636555  1.22588224  0.44445597  2.06992723  1.82473269 -0.03321279
[7]  1.29568923 -0.29542080  1.46614555  0.51617492  2.03383464  0.13835453
[13]  3.18982479 -0.38493278  0.67450796  1.69715532  1.19963387  1.17294403
[19]  0.83585415  1.49308994  0.53831112  1.76345465  1.80154859  0.47358491
[25]  1.40422472  2.50254552 -0.07376997  0.38077031  1.13606122 -0.26052567
[31]  0.88624336  1.89232197  1.37488657  2.53211686  1.77919794  3.42367520
[37] -0.59175356 -0.04816522  2.08963807  1.40124074 -0.73135934  0.65282741
[43]  0.87359580  0.14540086  1.52502012  0.52190806  2.29922084  0.62462975
[49]  2.94462210  1.06173482

\$c
[1]  1.01194711  1.36267530  1.37423091  1.14980487  1.39304340  3.26911528
[7]  1.71184232  1.88096194  2.90461886  1.39510407  1.86157191  1.14906542
[13]  1.90072693  1.78998258  1.61307934  0.76604158  2.92366827  2.32424523
[19]  2.94645235  2.73102591  0.87949048  3.31239943  1.05720691  1.42571354
[25]  1.79266828  1.84627335  0.81364549  0.25976918  1.48698512  1.10254109
[31]  1.60219278  1.84545465  1.93508206  2.13570750  2.32733075  2.53404107
[37]  1.25864169  3.28238628  1.98998276  1.44299079  2.26296491  3.86667748
[43]  1.84651988  3.24765507  0.18464631 -0.01404234  2.78432762 -0.05193538
[49]  0.35160392  2.58212054``````

Make Function

Now that we have our data, lets make the function:

``````cvar <- function(.x){
sapply(seq_along(.x), function(k, z) stats::var(z[1:k]), z = .x)
}``````

Ok, now that we have our function, lets take a look at it in use.

Use Function

``sapply(l, cvar)``
``````              a         b          c
[1,]        NA        NA         NA
[2,] 1.0632447 0.1014676 0.06150513
[3,] 0.5789145 0.3885272 0.04239889
[4,] 0.7633500 0.4867186 0.03075658
[5,] 1.1294646 0.4093271 0.02873772
[6,] 0.9043498 0.6932616 0.69685950
[7,] 1.0277904 0.5789892 0.58271798
[8,] 1.2918409 0.7813852 0.50862439
[9,] 1.1344452 0.7052323 0.62156290
[10,] 1.0088029 0.6580963 0.56764373
[11,] 0.9242084 0.6858993 0.51210764
[12,] 0.9418512 0.7024341 0.49623990
[13,] 0.9661294 1.0026509 0.45782344
[14,] 0.8992042 1.1041314 0.42295247
[15,] 0.8371837 1.0364119 0.39358167
[16,] 0.8556585 0.9929979 0.42396425
[17,] 0.8822275 0.9315646 0.49164277
[18,] 0.8344918 0.8770439 0.48215682
[19,] 0.7888762 0.8321667 0.52875406
[20,] 0.7473648 0.7964123 0.54171586
[21,] 0.8305355 0.7722667 0.56162921
[22,] 0.7940780 0.7564316 0.63535849
[23,] 0.7587189 0.7425071 0.63686713
[24,] 0.7802954 0.7290301 0.61692337
[25,] 0.8409644 0.7019443 0.59130379
[26,] 0.9248957 0.7464413 0.56765490
[27,] 0.9359290 0.7761117 0.58464117
[28,] 0.9159283 0.7676951 0.64765859
[29,] 0.8952420 0.7403040 0.62681485
[30,] 0.8690093 0.7773175 0.61856072
[31,] 0.9251735 0.7524213 0.59834912
[32,] 0.8976913 0.7499102 0.57961326
[33,] 0.8702922 0.7290409 0.56296663
[34,] 0.8452302 0.7678835 0.55094641
[35,] 0.8301013 0.7571526 0.54480209
[36,] 0.8638223 0.8786798 0.54627250
[37,] 0.8400510 0.9426489 0.53823917
[38,] 0.8195803 0.9560736 0.58478212
[39,] 0.8028106 0.9542482 0.57032971
[40,] 0.7943867 0.9312335 0.55895977
[41,] 0.7750385 0.9957722 0.55033290
[42,] 0.7581242 0.9766789 0.63799874
[43,] 0.7417073 0.9547108 0.62281006
[44,] 0.7453949 0.9533619 0.65240410
[45,] 0.7629119 0.9360654 0.70195202
[46,] 0.7747320 0.9223139 0.76179573
[47,] 0.7652088 0.9339432 0.76550157
[48,] 0.7645076 0.9188787 0.82293002
[49,] 0.7490587 0.9695572 0.84800554
[50,] 0.7434405 0.9498710 0.84419808``````
``lapply(l, cvar)``
``````\$a
[1]        NA 1.0632447 0.5789145 0.7633500 1.1294646 0.9043498 1.0277904
[8] 1.2918409 1.1344452 1.0088029 0.9242084 0.9418512 0.9661294 0.8992042
[15] 0.8371837 0.8556585 0.8822275 0.8344918 0.7888762 0.7473648 0.8305355
[22] 0.7940780 0.7587189 0.7802954 0.8409644 0.9248957 0.9359290 0.9159283
[29] 0.8952420 0.8690093 0.9251735 0.8976913 0.8702922 0.8452302 0.8301013
[36] 0.8638223 0.8400510 0.8195803 0.8028106 0.7943867 0.7750385 0.7581242
[43] 0.7417073 0.7453949 0.7629119 0.7747320 0.7652088 0.7645076 0.7490587
[50] 0.7434405

\$b
[1]        NA 0.1014676 0.3885272 0.4867186 0.4093271 0.6932616 0.5789892
[8] 0.7813852 0.7052323 0.6580963 0.6858993 0.7024341 1.0026509 1.1041314
[15] 1.0364119 0.9929979 0.9315646 0.8770439 0.8321667 0.7964123 0.7722667
[22] 0.7564316 0.7425071 0.7290301 0.7019443 0.7464413 0.7761117 0.7676951
[29] 0.7403040 0.7773175 0.7524213 0.7499102 0.7290409 0.7678835 0.7571526
[36] 0.8786798 0.9426489 0.9560736 0.9542482 0.9312335 0.9957722 0.9766789
[43] 0.9547108 0.9533619 0.9360654 0.9223139 0.9339432 0.9188787 0.9695572
[50] 0.9498710

\$c
[1]         NA 0.06150513 0.04239889 0.03075658 0.02873772 0.69685950
[7] 0.58271798 0.50862439 0.62156290 0.56764373 0.51210764 0.49623990
[13] 0.45782344 0.42295247 0.39358167 0.42396425 0.49164277 0.48215682
[19] 0.52875406 0.54171586 0.56162921 0.63535849 0.63686713 0.61692337
[25] 0.59130379 0.56765490 0.58464117 0.64765859 0.62681485 0.61856072
[31] 0.59834912 0.57961326 0.56296663 0.55094641 0.54480209 0.54627250
[37] 0.53823917 0.58478212 0.57032971 0.55895977 0.55033290 0.63799874
[43] 0.62281006 0.65240410 0.70195202 0.76179573 0.76550157 0.82293002
[49] 0.84800554 0.84419808``````

Voila!