function gives warning when run through dplyr::summarise, not otherwise

111 views Asked by At

I'm using fitdistrplur::fitdist to fit distributions. The results look fine but I am getting lots of warnings about dnorm() (or whichever distribution I use) returning NaN, but only when I run the analysis through dplyr:summarise(). If I call fitdist() without using summarise() there is no warning.

Here is a reprex. This is with R 4.3.

suppressPackageStartupMessages(library(dplyr))
library(fitdistrplus)
#> Loading required package: MASS
#> 
#> Attaching package: 'MASS'
#> The following object is masked from 'package:dplyr':
#> 
#>     select
#> Loading required package: survival
fun <- function(data, distr = "norm"){
  fitdist(
        data = data,
        distr = distr,
        keepdata = FALSE, method = "mme"
      )

return(1)
}


tibble(x = rnorm(100)) |> 
  summarise(fun(x))
#> Warning: There were 2 warnings in `summarise()`.
#> The first warning was:
#> ℹ In argument: `fun(x)`.
#> Caused by warning in `dnorm()`:
#> ! NaNs produced
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
#> # A tibble: 1 × 1
#>   `fun(x)`
#>      <dbl>
#> 1        1

fun(tibble(x = 1:100)$x)
#> [1] 1

Created on 2023-05-11 with reprex v2.0.2

I've tried the full range of debugging techniques I know - setting the debugger on dnorm etc, and cannot find what is causing the problem.

Currently I am using the workaround of wrapping fitdist() in suppressWarnings() but I don't think this is a great solution.

0

There are 0 answers