4

Why is cummean(x) not equal to cumsum(x)/seq_along(x)?

set.seed(456)
x <- as.integer(runif(30)*300)
x

cummean(x)
cumsum(x)/seq_along(x)

[1]  26  63 219 255 236  99  24  85  71 115 111  65 226 246 179 195 252 135 215  87  53 216 271 133 251 211 285 192  22  76
 [1]  26.00000  26.00000  38.33333  83.50000 117.80000 137.50000 132.00000 118.50000 114.77778 110.40000 110.81818 110.83333 107.30769 115.78571 124.46667 127.87500 131.82353
[18] 138.50000 138.31579 142.15000 139.52381 135.59091 139.08696 144.58333 144.12000 148.23077 150.55556 155.35714 156.62069 152.13333

 [1]  26.0000  44.5000 102.6667 140.7500 159.8000 149.6667 131.7143 125.8750 119.7778 119.3000 118.5455 114.0833 122.6923 131.5000 134.6667 138.4375 145.1176 144.5556 148.2632
[20] 145.2000 140.8095 144.2273 149.7391 149.0417 153.1200 155.3462 160.1481 161.2857 156.4828 153.8000
Ahorn
  • 3,686
  • 1
  • 10
  • 17
Mervyn Lau
  • 43
  • 3

1 Answers1

3

This is actually an issue with the dplyr::cummean function as of dplyr 1.1.0 see here. Romain Francois pushed a fix four days ago so if you pull the dplyr version from github it should give the correct results, will try and update in a sec.

Example that was used in the issue mentioned above:

library(tidyverse)
x <- 1:5

# long(er) way
cumsum(x) / seq_along(x)
#> [1] 1.0 1.5 2.0 2.5 3.0

# dplyr 0.8.5 cummean()
cummean(x)
#> [1] 1.0 1.5 2.0 2.5 3.0

# dplyr 1.0.0 cummean()
cummean(x)
#> [1] 1.000000 1.000000 1.333333 1.750000 2.200000

What caused the bug (also from github issue linked above):

It looks like the indexing is off by one for dplyr_cummean in /src/funs.cpp, causing the first index to be repeated twice (and the last index to be dropped). I'll submit a pull request with a slight change which I think makes it work as intended.


Update: Current version on github (1.0.0.9000) gives correct result:

library(dplyr)
packageVersion("dplyr")
#[1] ‘1.0.0.9000’

set.seed(456)
x <- as.integer(runif(30)*300)

all(dplyr::cummean(x) == cumsum(x)/seq_along(x))
#[1] TRUE
Ahorn
  • 3,686
  • 1
  • 10
  • 17