Convenience Functions, Moving Window Statistics, and Graphics

Introduction

This package contains:

  1. Functions that do something convenient.
  2. Functions for calculating moving-window statistics.
  3. Functions for generating graphs.

Convenience functions

truerange

The base R function range returns the minimum and maximum of a vector, but the “range” is actually defined as the difference between the minimum and maximum. This function calculates the actual range. It is equivalent to the base R code diff(range(x)), but a bit simpler and much faster.

x <- rnorm(1000)
all.equal(diff(range(x)), truerange(x))
#> [1] TRUE
as.data.frame(print(microbenchmark(diff(range(x)), truerange(x), times = 500)))
#> Unit: microseconds
#>            expr   min    lq     mean median      uq    max neval
#>  diff(range(x)) 8.276 9.027 10.21259  9.458 11.0405 42.970   500
#>    truerange(x) 3.887 3.968  4.22657  4.158  4.2880 14.577   500
expr time
diff(range(x)) 42970
diff(range(x)) 14947
diff(range(x)) 11742
truerange(x) 14357
truerange(x) 4419
diff(range(x)) 12012
diff(range(x)) 11771
diff(range(x)) 11662
truerange(x) 4519
diff(range(x)) 11431
diff(range(x)) 11452
diff(range(x)) 11661
truerange(x) 4678
truerange(x) 4148
diff(range(x)) 11462
truerange(x) 4308
diff(range(x)) 12233
truerange(x) 4488
truerange(x) 4238
truerange(x) 4098
truerange(x) 4098
diff(range(x)) 11251
diff(range(x)) 10940
diff(range(x)) 11071
truerange(x) 4709
diff(range(x)) 12042
truerange(x) 4319
diff(range(x)) 10940
truerange(x) 4268
truerange(x) 4168
diff(range(x)) 10920
diff(range(x)) 11130
truerange(x) 4329
diff(range(x)) 11030
truerange(x) 4258
diff(range(x)) 11231
diff(range(x)) 11221
diff(range(x)) 22232
truerange(x) 4679
truerange(x) 4107
diff(range(x)) 11722
truerange(x) 4319
diff(range(x)) 11221
truerange(x) 4318
truerange(x) 4127
truerange(x) 4098
truerange(x) 4158
diff(range(x)) 11151
diff(range(x)) 11602
diff(range(x)) 11541
diff(range(x)) 11101
truerange(x) 4458
truerange(x) 4127
truerange(x) 4337
truerange(x) 4127
diff(range(x)) 11041
truerange(x) 4258
diff(range(x)) 11341
diff(range(x)) 10840
diff(range(x)) 10840
truerange(x) 4328
diff(range(x)) 11341
truerange(x) 4319
diff(range(x)) 11060
truerange(x) 4349
diff(range(x)) 11993
diff(range(x)) 11421
diff(range(x)) 11161
diff(range(x)) 11472
diff(range(x)) 11080
truerange(x) 4539
truerange(x) 4148
truerange(x) 4088
diff(range(x)) 11251
diff(range(x)) 11051
truerange(x) 4459
diff(range(x)) 10960
truerange(x) 4288
diff(range(x)) 11822
diff(range(x)) 11091
truerange(x) 4398
diff(range(x)) 11331
diff(range(x)) 11101
diff(range(x)) 11331
truerange(x) 4338
diff(range(x)) 10941
truerange(x) 4278
diff(range(x)) 11501
diff(range(x)) 11071
truerange(x) 4398
truerange(x) 4098
diff(range(x)) 11301
diff(range(x)) 11001
diff(range(x)) 11712
truerange(x) 4298
diff(range(x)) 11351
truerange(x) 4458
truerange(x) 4198
diff(range(x)) 11061
truerange(x) 4288
truerange(x) 4107
truerange(x) 4329
truerange(x) 4097
truerange(x) 4098
truerange(x) 4138
truerange(x) 4448
diff(range(x)) 10840
truerange(x) 4268
diff(range(x)) 11491
diff(range(x)) 11251
diff(range(x)) 10820
truerange(x) 4279
diff(range(x)) 11361
diff(range(x)) 10900
diff(range(x)) 11111
diff(range(x)) 11582
truerange(x) 4378
diff(range(x)) 13275
truerange(x) 4278
diff(range(x)) 11221
truerange(x) 4308
truerange(x) 4258
diff(range(x)) 10841
diff(range(x)) 10920
diff(range(x)) 11411
truerange(x) 4308
truerange(x) 4117
truerange(x) 4117
truerange(x) 4098
diff(range(x)) 13355
truerange(x) 4488
diff(range(x)) 11270
truerange(x) 4588
truerange(x) 4117
truerange(x) 4098
diff(range(x)) 11101
diff(range(x)) 11591
diff(range(x)) 11180
diff(range(x)) 11040
diff(range(x)) 11221
diff(range(x)) 10720
diff(range(x)) 10841
truerange(x) 4518
diff(range(x)) 11011
truerange(x) 4278
truerange(x) 4118
diff(range(x)) 11261
diff(range(x)) 11020
truerange(x) 4389
truerange(x) 4188
diff(range(x)) 10970
truerange(x) 4298
diff(range(x)) 13856
diff(range(x)) 11101
diff(range(x)) 11421
truerange(x) 4469
truerange(x) 4137
diff(range(x)) 11031
truerange(x) 4258
diff(range(x)) 11031
truerange(x) 14237
diff(range(x)) 11361
diff(range(x)) 11331
truerange(x) 4489
truerange(x) 4107
diff(range(x)) 13456
truerange(x) 4328
truerange(x) 4108
truerange(x) 4108
diff(range(x)) 11031
truerange(x) 4368
diff(range(x)) 11522
truerange(x) 4288
diff(range(x)) 11411
truerange(x) 4278
truerange(x) 4118
diff(range(x)) 8937
diff(range(x)) 9488
diff(range(x)) 9497
diff(range(x)) 8746
truerange(x) 4328
truerange(x) 3937
diff(range(x)) 9768
truerange(x) 4448
truerange(x) 3968
truerange(x) 3937
truerange(x) 3938
truerange(x) 3967
truerange(x) 3898
diff(range(x)) 12312
truerange(x) 4157
truerange(x) 4018
truerange(x) 3948
diff(range(x)) 9678
truerange(x) 4238
diff(range(x)) 8826
diff(range(x)) 9057
diff(range(x)) 8867
diff(range(x)) 8897
diff(range(x)) 9097
truerange(x) 4419
diff(range(x)) 12834
diff(range(x)) 9337
truerange(x) 4248
truerange(x) 4028
diff(range(x)) 9127
truerange(x) 4178
diff(range(x)) 9458
diff(range(x)) 9107
truerange(x) 4328
diff(range(x)) 8997
diff(range(x)) 8776
diff(range(x)) 9508
truerange(x) 4378
truerange(x) 4148
truerange(x) 3968
truerange(x) 3927
truerange(x) 3928
truerange(x) 3927
truerange(x) 3907
diff(range(x)) 9337
diff(range(x)) 9488
diff(range(x)) 8917
truerange(x) 4197
truerange(x) 3957
diff(range(x)) 9437
truerange(x) 4188
diff(range(x)) 9437
truerange(x) 4168
truerange(x) 3917
diff(range(x)) 11322
truerange(x) 4168
truerange(x) 4078
truerange(x) 3948
truerange(x) 3927
truerange(x) 3888
truerange(x) 3917
truerange(x) 3897
diff(range(x)) 9408
truerange(x) 4228
diff(range(x)) 9167
diff(range(x)) 10490
truerange(x) 4218
truerange(x) 3947
truerange(x) 3928
diff(range(x)) 9177
diff(range(x)) 8997
diff(range(x)) 9598
truerange(x) 4318
truerange(x) 3947
truerange(x) 3938
truerange(x) 3957
truerange(x) 3948
diff(range(x)) 9708
truerange(x) 4188
diff(range(x)) 9247
diff(range(x)) 8917
diff(range(x)) 9267
truerange(x) 4328
diff(range(x)) 8856
diff(range(x)) 8877
truerange(x) 4158
truerange(x) 3977
truerange(x) 4018
diff(range(x)) 8826
diff(range(x)) 8766
truerange(x) 4168
diff(range(x)) 9117
truerange(x) 4188
diff(range(x)) 9277
diff(range(x)) 8877
diff(range(x)) 8937
truerange(x) 4227
diff(range(x)) 9187
diff(range(x)) 8896
diff(range(x)) 8857
truerange(x) 4278
truerange(x) 3958
diff(range(x)) 8696
diff(range(x)) 9067
truerange(x) 4268
diff(range(x)) 9157
truerange(x) 4158
truerange(x) 3988
truerange(x) 3927
diff(range(x)) 8817
truerange(x) 4398
diff(range(x)) 8717
truerange(x) 4138
truerange(x) 3978
truerange(x) 4007
diff(range(x)) 9157
truerange(x) 4208
diff(range(x)) 8886
truerange(x) 4128
diff(range(x)) 8827
diff(range(x)) 9438
truerange(x) 4298
truerange(x) 4008
diff(range(x)) 9027
truerange(x) 4248
truerange(x) 4008
truerange(x) 3947
diff(range(x)) 8466
diff(range(x)) 8967
truerange(x) 4248
diff(range(x)) 8686
diff(range(x)) 9588
diff(range(x)) 9297
diff(range(x)) 19486
truerange(x) 4739
diff(range(x)) 9307
diff(range(x)) 10329
truerange(x) 4438
truerange(x) 3998
truerange(x) 3938
diff(range(x)) 9878
diff(range(x)) 10028
truerange(x) 4238
diff(range(x)) 9007
truerange(x) 4198
diff(range(x)) 9548
truerange(x) 4258
diff(range(x)) 8977
truerange(x) 4168
diff(range(x)) 9077
truerange(x) 4148
truerange(x) 3937
diff(range(x)) 8707
diff(range(x)) 9749
diff(range(x)) 8556
diff(range(x)) 9327
diff(range(x)) 9207
truerange(x) 4208
truerange(x) 4107
truerange(x) 3917
truerange(x) 3917
diff(range(x)) 9187
diff(range(x)) 8757
diff(range(x)) 9167
diff(range(x)) 8756
diff(range(x)) 8817
truerange(x) 4288
diff(range(x)) 9307
truerange(x) 4228
truerange(x) 4058
truerange(x) 3967
truerange(x) 3927
truerange(x) 3907
truerange(x) 3897
truerange(x) 3897
diff(range(x)) 9327
diff(range(x)) 8987
diff(range(x)) 9257
diff(range(x)) 8967
truerange(x) 4268
diff(range(x)) 9588
truerange(x) 4288
diff(range(x)) 9237
diff(range(x)) 9688
diff(range(x)) 8486
truerange(x) 4489
truerange(x) 3937
truerange(x) 4188
diff(range(x)) 9197
diff(range(x)) 8877
truerange(x) 4258
truerange(x) 3998
truerange(x) 3898
diff(range(x)) 9287
diff(range(x)) 9017
truerange(x) 4208
truerange(x) 3998
truerange(x) 3967
truerange(x) 3918
diff(range(x)) 9337
truerange(x) 4258
truerange(x) 4028
diff(range(x)) 8946
diff(range(x)) 9648
truerange(x) 4308
diff(range(x)) 9307
truerange(x) 4198
truerange(x) 3968
truerange(x) 3947
truerange(x) 3888
diff(range(x)) 10510
truerange(x) 4278
truerange(x) 3988
diff(range(x)) 9588
diff(range(x)) 8827
truerange(x) 4168
truerange(x) 3968
diff(range(x)) 9438
diff(range(x)) 9548
diff(range(x)) 8656
diff(range(x)) 9257
diff(range(x)) 8727
truerange(x) 4358
truerange(x) 4098
truerange(x) 4017
diff(range(x)) 9518
truerange(x) 4168
truerange(x) 3958
truerange(x) 3918
diff(range(x)) 9167
diff(range(x)) 12153
truerange(x) 5380
truerange(x) 3967
diff(range(x)) 8976
diff(range(x)) 11682
diff(range(x)) 8917
diff(range(x)) 9017
truerange(x) 4298
diff(range(x)) 8967
truerange(x) 4368
truerange(x) 3927
truerange(x) 3958
diff(range(x)) 9698
truerange(x) 4188
diff(range(x)) 8917
diff(range(x)) 9458
truerange(x) 4177
diff(range(x)) 9538
truerange(x) 4198
diff(range(x)) 9939
diff(range(x)) 9087
truerange(x) 4328
diff(range(x)) 9508
diff(range(x)) 10880
diff(range(x)) 10760
truerange(x) 4378
diff(range(x)) 8796
truerange(x) 4228
diff(range(x)) 9608
diff(range(x)) 8656
truerange(x) 4299
diff(range(x)) 9237
truerange(x) 4168
diff(range(x)) 9868
diff(range(x)) 9648
diff(range(x)) 9158
truerange(x) 4298
diff(range(x)) 9457
truerange(x) 4178
diff(range(x)) 9588
diff(range(x)) 8837
truerange(x) 4278
diff(range(x)) 9368
truerange(x) 4237
diff(range(x)) 9658
diff(range(x)) 9397
diff(range(x)) 20178
diff(range(x)) 9067
diff(range(x)) 9327
diff(range(x)) 8946
diff(range(x)) 9037
diff(range(x)) 8967
diff(range(x)) 9989
diff(range(x)) 8726
truerange(x) 4879
truerange(x) 4028
truerange(x) 4027
truerange(x) 3957
truerange(x) 3887
diff(range(x)) 9638
truerange(x) 4218
diff(range(x)) 9147
truerange(x) 4198
truerange(x) 3937
truerange(x) 4098
truerange(x) 3967
truerange(x) 3907
truerange(x) 3927
truerange(x) 3897
truerange(x) 3957
diff(range(x)) 9358
diff(range(x)) 8906
diff(range(x)) 11392
diff(range(x)) 8566
diff(range(x)) 9788
diff(range(x)) 8907
truerange(x) 4789
truerange(x) 3957
diff(range(x)) 9137
truerange(x) 4207
truerange(x) 4008
diff(range(x)) 9508
truerange(x) 4157
diff(range(x)) 9417
diff(range(x)) 9178
diff(range(x)) 8876
truerange(x) 4328
diff(range(x)) 9498
diff(range(x)) 9147
diff(range(x)) 8276
truerange(x) 4288
truerange(x) 4158
truerange(x) 3927
diff(range(x)) 9318
truerange(x) 4308
diff(range(x)) 9397
diff(range(x)) 8736
diff(range(x)) 9227
truerange(x) 4338
diff(range(x)) 10209
truerange(x) 4328
diff(range(x)) 9208
truerange(x) 4418
truerange(x) 4108
truerange(x) 3967
diff(range(x)) 9377
diff(range(x)) 9338
diff(range(x)) 9117
truerange(x) 4678
diff(range(x)) 9338
truerange(x) 4248
truerange(x) 4098
truerange(x) 3947
truerange(x) 3918
truerange(x) 3927
truerange(x) 4068
diff(range(x)) 8786
diff(range(x)) 8917
truerange(x) 4248
diff(range(x)) 9097
diff(range(x)) 9287
truerange(x) 4198
truerange(x) 3948
truerange(x) 3977
diff(range(x)) 9237
diff(range(x)) 8606
truerange(x) 4428
diff(range(x)) 8967
diff(range(x)) 8867
truerange(x) 4208
truerange(x) 3938
truerange(x) 4058
diff(range(x)) 10289
diff(range(x)) 9017
truerange(x) 4248
truerange(x) 3957
truerange(x) 3978
diff(range(x)) 8896
truerange(x) 4167
diff(range(x)) 9829
diff(range(x)) 8856
truerange(x) 4359
diff(range(x)) 9067
diff(range(x)) 9808
truerange(x) 4288
diff(range(x)) 9528
diff(range(x)) 9377
diff(range(x)) 8896
truerange(x) 4389
diff(range(x)) 9308
diff(range(x)) 9177
diff(range(x)) 9007
truerange(x) 4278
truerange(x) 4058
diff(range(x)) 9217
truerange(x) 4148
diff(range(x)) 9057
diff(range(x)) 8827
diff(range(x)) 8627
truerange(x) 4228
truerange(x) 3977
truerange(x) 3948
diff(range(x)) 9277
diff(range(x)) 9327
truerange(x) 4259
truerange(x) 4108
diff(range(x)) 8887
truerange(x) 4258
diff(range(x)) 8957
diff(range(x)) 9087
truerange(x) 4188
diff(range(x)) 9197
diff(range(x)) 8747
truerange(x) 4148
diff(range(x)) 9457
truerange(x) 4158
truerange(x) 4028
truerange(x) 4108
truerange(x) 3907
truerange(x) 4007
diff(range(x)) 8886
diff(range(x)) 8997
truerange(x) 4348
diff(range(x)) 9267
diff(range(x)) 9127
diff(range(x)) 8856
diff(range(x)) 9478
truerange(x) 4398
truerange(x) 4108
diff(range(x)) 8716
truerange(x) 4217
truerange(x) 3947
diff(range(x)) 19256
truerange(x) 4528
diff(range(x)) 10379
diff(range(x)) 9508
diff(range(x)) 8947
truerange(x) 4278
diff(range(x)) 10619
diff(range(x)) 8766
truerange(x) 4218
diff(range(x)) 9217
diff(range(x)) 9748
truerange(x) 4428
truerange(x) 3978
truerange(x) 4027
diff(range(x)) 8817
truerange(x) 4188
truerange(x) 3957
diff(range(x)) 9478
diff(range(x)) 8907
diff(range(x)) 8896
diff(range(x)) 9157
truerange(x) 4308
truerange(x) 3957
diff(range(x)) 9287
diff(range(x)) 9006
diff(range(x)) 8837
truerange(x) 4248
truerange(x) 4017
diff(range(x)) 9108
truerange(x) 4178
truerange(x) 4017
truerange(x) 4128
truerange(x) 4007
diff(range(x)) 9368
truerange(x) 4208
diff(range(x)) 9338
truerange(x) 4219
diff(range(x)) 9598
truerange(x) 4187
truerange(x) 3967
truerange(x) 3917
diff(range(x)) 9458
diff(range(x)) 9478
truerange(x) 4288
diff(range(x)) 9948
diff(range(x)) 8666
truerange(x) 4448
diff(range(x)) 9277
diff(range(x)) 10038
truerange(x) 4589
truerange(x) 4098
truerange(x) 4218
diff(range(x)) 9558
diff(range(x)) 9117
truerange(x) 4228
truerange(x) 4068
diff(range(x)) 12764
truerange(x) 4148
diff(range(x)) 8827
diff(range(x)) 8797
truerange(x) 4278
truerange(x) 3977
truerange(x) 3917
diff(range(x)) 9507
diff(range(x)) 8956
diff(range(x)) 8837
truerange(x) 4238
diff(range(x)) 9047
truerange(x) 4188
diff(range(x)) 9247
truerange(x) 4158
diff(range(x)) 8887
truerange(x) 4158
diff(range(x)) 9788
truerange(x) 4318
truerange(x) 4008
truerange(x) 3927
diff(range(x)) 8907
diff(range(x)) 9107
truerange(x) 4268
diff(range(x)) 9708
truerange(x) 4218
diff(range(x)) 8877
truerange(x) 4288
diff(range(x)) 8957
diff(range(x)) 9838
truerange(x) 4408
truerange(x) 3967
truerange(x) 3948
diff(range(x)) 9698
truerange(x) 4438
truerange(x) 4068
truerange(x) 3937
truerange(x) 3918
diff(range(x)) 9137
truerange(x) 4188
diff(range(x)) 10930
diff(range(x)) 9167
truerange(x) 4358
truerange(x) 3957
truerange(x) 3948
diff(range(x)) 8977
truerange(x) 4277
diff(range(x)) 10019
diff(range(x)) 8556
diff(range(x)) 8957
diff(range(x)) 8987
diff(range(x)) 8877
truerange(x) 4338
truerange(x) 3927
truerange(x) 3918
truerange(x) 3947
truerange(x) 4028
diff(range(x)) 9067
diff(range(x)) 9127
diff(range(x)) 8796
truerange(x) 4228
truerange(x) 3968
diff(range(x)) 8997
truerange(x) 4177
truerange(x) 3937
truerange(x) 3998
diff(range(x)) 9117
truerange(x) 4158
diff(range(x)) 8907
diff(range(x)) 9237
diff(range(x)) 9217
diff(range(x)) 8636
diff(range(x)) 8997
diff(range(x)) 9368
diff(range(x)) 10229
diff(range(x)) 9227
truerange(x) 4329
diff(range(x)) 30628
diff(range(x)) 12653
diff(range(x)) 12083
truerange(x) 4559
diff(range(x)) 10851
truerange(x) 4268
truerange(x) 4027
truerange(x) 4017
diff(range(x)) 24225
truerange(x) 4498
truerange(x) 3957
truerange(x) 3968
truerange(x) 3898
diff(range(x)) 13205
truerange(x) 4509
truerange(x) 3927
diff(range(x)) 12323
diff(range(x)) 10700
truerange(x) 4418
truerange(x) 3957
truerange(x) 3958
truerange(x) 4027
truerange(x) 4028
truerange(x) 3957
diff(range(x)) 14157
truerange(x) 4358
truerange(x) 3957
truerange(x) 3947
diff(range(x)) 12203
diff(range(x)) 12212
truerange(x) 4488
diff(range(x)) 10360
truerange(x) 4259
truerange(x) 3977
truerange(x) 4138
diff(range(x)) 13976
truerange(x) 4238
truerange(x) 3967
diff(range(x)) 12232
truerange(x) 4308
truerange(x) 3957
truerange(x) 3938
diff(range(x)) 12464
truerange(x) 4268
diff(range(x)) 10760
diff(range(x)) 12684
diff(range(x)) 13886
truerange(x) 4729
truerange(x) 3967
diff(range(x)) 12163
truerange(x) 4298
diff(range(x)) 11582
diff(range(x)) 12653
diff(range(x)) 10540
truerange(x) 4519
diff(range(x)) 11782
diff(range(x)) 11952
truerange(x) 4379
diff(range(x)) 12643
truerange(x) 4198
diff(range(x)) 14397
diff(range(x)) 11752
diff(range(x)) 10440
truerange(x) 4438
diff(range(x)) 12363
diff(range(x)) 11722
diff(range(x)) 12133
diff(range(x)) 10429
diff(range(x)) 12383
diff(range(x)) 12172
diff(range(x)) 13946
diff(range(x)) 10579
truerange(x) 4719
truerange(x) 3978
diff(range(x)) 12954
truerange(x) 4438
diff(range(x)) 12403
diff(range(x)) 12223
truerange(x) 4468
diff(range(x)) 10880
diff(range(x)) 12414
diff(range(x)) 10349
truerange(x) 4369
truerange(x) 4037
truerange(x) 4028
truerange(x) 3967
truerange(x) 4018
truerange(x) 3937
diff(range(x)) 12784
diff(range(x)) 13445
diff(range(x)) 13295
diff(range(x)) 12013
truerange(x) 4588
truerange(x) 3978
truerange(x) 3917
diff(range(x)) 11812
diff(range(x)) 11051
diff(range(x)) 13315
diff(range(x)) 11772
truerange(x) 4599
diff(range(x)) 12313
truerange(x) 4348
diff(range(x)) 12503
diff(range(x)) 12383
diff(range(x)) 11000
diff(range(x)) 12183
diff(range(x)) 12163
diff(range(x)) 8997
truerange(x) 4819
truerange(x) 4127
truerange(x) 3907
diff(range(x)) 8977
diff(range(x)) 9327
truerange(x) 4208
truerange(x) 3978
truerange(x) 3917
diff(range(x)) 9348
diff(range(x)) 9678
diff(range(x)) 8947
diff(range(x)) 8846
truerange(x) 4399
truerange(x) 3947
truerange(x) 4148
diff(range(x)) 9808
truerange(x) 4158
diff(range(x)) 9798
truerange(x) 4458
truerange(x) 3957
diff(range(x)) 8977
truerange(x) 4218
truerange(x) 4008
diff(range(x)) 9467
truerange(x) 14577
diff(range(x)) 10219
truerange(x) 4468
diff(range(x)) 10240
truerange(x) 4308
truerange(x) 4037
diff(range(x)) 9388
truerange(x) 4228
diff(range(x)) 10089
diff(range(x)) 9167
diff(range(x)) 9668
diff(range(x)) 9628
diff(range(x)) 9317
diff(range(x)) 9227
truerange(x) 4448
truerange(x) 4048
diff(range(x)) 8967
diff(range(x)) 9057
diff(range(x)) 9427
truerange(x) 4389
diff(range(x)) 9037
truerange(x) 4248
truerange(x) 4227
diff(range(x)) 9568
diff(range(x)) 9037
diff(range(x)) 9117
diff(range(x)) 9247
diff(range(x)) 9468
truerange(x) 4218
truerange(x) 3957
truerange(x) 3988
truerange(x) 3917
truerange(x) 3927
truerange(x) 3937
diff(range(x)) 8836
diff(range(x)) 8476
diff(range(x)) 9689
diff(range(x)) 9227
truerange(x) 4398
truerange(x) 3928
diff(range(x)) 9498
diff(range(x)) 9107
truerange(x) 4248
truerange(x) 3937
truerange(x) 4178
truerange(x) 3917
truerange(x) 3907
diff(range(x)) 9037
truerange(x) 4208
diff(range(x)) 9948
diff(range(x)) 8956
diff(range(x)) 8937
truerange(x) 4368
truerange(x) 3968
diff(range(x)) 9027
truerange(x) 4268
truerange(x) 3947
diff(range(x)) 10850
truerange(x) 4147
truerange(x) 3947
diff(range(x)) 9227
diff(range(x)) 9077
truerange(x) 4258
diff(range(x)) 9117
truerange(x) 4227
truerange(x) 3988
diff(range(x)) 11111
truerange(x) 4268
diff(range(x)) 8746
diff(range(x)) 9488
truerange(x) 4148
truerange(x) 3957
diff(range(x)) 9538
truerange(x) 4158
truerange(x) 4098
diff(range(x)) 9077
diff(range(x)) 9057
diff(range(x)) 8616
diff(range(x)) 8917
truerange(x) 4198
truerange(x) 3997
truerange(x) 3958
truerange(x) 3897
truerange(x) 3897
diff(range(x)) 9568
diff(range(x)) 9368
diff(range(x)) 9548
truerange(x) 4859
truerange(x) 4448
diff(range(x)) 9768
truerange(x) 4448
truerange(x) 3927
diff(range(x)) 9448
truerange(x) 4168
diff(range(x)) 9277
diff(range(x)) 9427
truerange(x) 4388
diff(range(x)) 8996
truerange(x) 4158
truerange(x) 4078
truerange(x) 4538
truerange(x) 4348
truerange(x) 4178
diff(range(x)) 9507
diff(range(x)) 8877
truerange(x) 4288
truerange(x) 4148
truerange(x) 3947
diff(range(x)) 9017
truerange(x) 4329
diff(range(x)) 9157
truerange(x) 4188
truerange(x) 3977
truerange(x) 3918
diff(range(x)) 9738
truerange(x) 4148
diff(range(x)) 8716
truerange(x) 4248
diff(range(x)) 9528
diff(range(x)) 8706
truerange(x) 4228
diff(range(x)) 9598
diff(range(x)) 9498
diff(range(x)) 9598
truerange(x) 4339
truerange(x) 4259
diff(range(x)) 9198
truerange(x) 4168
diff(range(x)) 9688
truerange(x) 4188
truerange(x) 4128
diff(range(x)) 8846
diff(range(x)) 8887
truerange(x) 4238
truerange(x) 3977
truerange(x) 3938
truerange(x) 3967

bmi3, bmi4

It isn’t hard to create body mass index (BMI) groups from continuous BMI values, but it is hard to remember how BMI values on the cutpoints get classified. The cutpoints according to the CDC are:

BMI values Classification
< 18.5 Underweight
[18.5, 25) Normal weight
[25, 30) Overweight
>= 30 Obese

The function bmi3 creates 3 groups (lumping the first two above into “Normal weight”), while bmi4 creates 4 groups. Both return factor variables, with or without labels depending on labels.

bmi <- round(runif(100, min = 15, max = 45), 1)
table(bmi3(bmi))
Normal weight Overweight Obese
32 21 47
table(bmi4(bmi, labels = FALSE))
[-Inf,18.5) [18.5,25) [25,30) [30, Inf)
8 24 21 47

sumsim

This function creates tables summarizing results of statistical simulations, providing common metrics of performance like mean bias, standard deviation, mean standard error, mean squared error, and confidence interval coverage.

To illustrate, suppose X1, ..., Xn ∼ N(μ, σ2), and we wish to compare two estimators for σ2: the MLE (n in denominator) vs. the sample variance (n − 1 in denominator).

MLE <- c()
s2 <- c()
for (ii in 1: 1000) {
   x <- rnorm(n = 25)
   MLE[ii] <- sum((x - mean(x))^2) / 25
   s2[ii] <- sum((x - mean(x))^2) / 24
 }
kable(sumsim(estimates = cbind(MLE, s2), truth = 1))
Mean bias SD MSE
MLE -0.053 0.280 0.081
s2 -0.013 0.292 0.085

You can request different performance metrics through the statistics input; some of them, like confidence interval coverage, require specifying ses with standard errors.

Moving window statistics

moving_mean

The function moving_mean is one of dozens of moving average functions available in R. I’m not sure it’s the absolute fastest, but it is much faster than roll_mean in RcppRoll.

library("RcppRoll")

lengths <- c(10, 100, 1000, 10000)
multiples1 <- multiples2 <- c()
for (ii in 1: 4) {
  n <- lengths[ii]
  x <- rnorm(n)
  medians <- summary(microbenchmark(roll_mean(x, 5), moving_mean(x, 5),
                                    roll_mean(x, n / 5), moving_mean(x, n / 5),
                                    times = 50))$median
  multiples1[ii] <- medians[1] / medians[2]
  multiples2[ii] <- medians[3] / medians[4]
}
par(mfrow = c(1, 2))
plot(1: 4, multiples1, type = "b", col = "blue", main = "5-unit MA", 
     ylab = "Speed multiple", xlab = "Vector length", xaxt = "n", 
     ylim = c(0, max(multiples1) * 1.05))
axis(side = 1, at = 1: 4, labels = lengths)
abline(h = 1)

plot(1: 4, multiples2, type = "b", col = "blue", main = "length(x)/5-unit MA", 
     ylab = "Speed multiple", xlab = "Vector length", xaxt = "n", 
     ylim = c(0, max(multiples2) * 1.05))
axis(side = 1, at = 1: 4, labels = lengths)
abline(h = 1)

Graphics

histo

This function is similar to the base R function hist, but with two added features:

  1. Can overlay one or more fitted probability density/mass functions (PDFs/PMFs) for any univariate distribution supported in R (see ?Distributions).

  2. Can generate more of a barplot type histogram, where each possible value gets its own bin centered over its value (useful for discrete variables with not too many possible values).

Here are two examples:

# Histogram for 1,000 values from Bin(8, 0.25)
x <- rbinom(n = 1000, size = 5, prob = 0.25)
histo(x, dis = "binom", size = 5, colors = "blue", points_list = list(type = "b"))


# Histogram for 10,000 values from lognormal(0, 0.35) and various fitted PDFs.
x <- rlnorm(n = 10000, meanlog = 0, sdlog = 0.35)
histo(x, c("lnorm", "norm", "gamma"), main = "X ~ Lognormal(0, 0.35)")

References

Eddelbuettel, Dirk. 2013. Seamless R and C++ Integration with Rcpp. New York: Springer. https://doi.org/10.1007/978-1-4614-6868-4.
Eddelbuettel, Dirk, and James Joseph Balamuta. 2017. Extending extitR with extitC++: A Brief Introduction to extitRcpp.” PeerJ Preprints 5 (August): e3188v1. https://doi.org/10.7287/peerj.preprints.3188v1.
Eddelbuettel, Dirk, and Romain François. 2011. Rcpp: Seamless R and C++ Integration.” Journal of Statistical Software 40 (8): 1–18. https://doi.org/10.18637/jss.v040.i08.
Ushey, Kevin. 2015. RcppRoll: Efficient Rolling / Windowed Operations. https://CRAN.R-project.org/package=RcppRoll.
Xie, Yihui. 2017. Printr: Automatically Print r Objects to Appropriate Formats According to the ’Knitr’ Output Format. https://CRAN.R-project.org/package=printr.