stan-dev / posterior

The posterior R package
https://mc-stan.org/posterior/
Other
167 stars 24 forks source link

Bug: `autocorrelation()` doesn't return anything #375

Closed bschneidr closed 1 week ago

bschneidr commented 1 month ago

The function autocorrelation() doesn't return any values, unlike the function autocovariance(). Below is a reprex:

library(posterior)
#> This is posterior version 1.6.0
#> 
#> Attaching package: 'posterior'
#> The following objects are masked from 'package:stats':
#> 
#>     mad, sd, var
#> The following objects are masked from 'package:base':
#> 
#>     %in%, match

x <- c(1.0971608425717, 
       0.930412372942111, 
       1.38864114119356, 
       0.296248053385611)

# Yields expected output
  autocovariance(x)
#> [1]  0.15998048 -0.07238629  0.01909960 -0.02670355

# No output 
  autocorrelation(x)

Created on 2024-09-27 with reprex v2.1.1

This is because the autocorrelation() function ends with an assignment, and doesn't end with return() or by printing an object.

https://github.com/stan-dev/posterior/blob/20b6d3aad2d15e398dbf86787da01843f824df7f/R/convergence.R#L610

jgabry commented 1 month ago

Thanks for pointing this out. It does actually return the values if you assign it, e.g. ac <- autocorrelation(x) works fine. But yeah you're right that the assignment as the final line of the function code will result in it not printing anything.