Search code examples
rdplyrhydrogof

How to get Nash-Sutcliffe efficiency by group using hydroGOF package?


I am trying to get NSE values grouped by a variable. I tried something similar to:

library(dplyr)
library(hydroGOF)

mtcars %>% group_by(cyl) %>% 
  NSE(wt,drat)

Why isn't it working? It doesn't find "wt". Thank you.


Solution

  • Here's an approach with summarise:

    mtcars %>% 
      group_by(cyl) %>%
      summarise(NSE = NSE(wt, drat))
    # A tibble: 3 x 2
        cyl    NSE
      <dbl>  <dbl>
    1     4 -30.2 
    2     6  -2.22
    3     8 -10.2 
    

    The reason yours wasn't working is because %>% redirects the output of the previous function into the first argument of the next. So yours was the equivalent of NSE(mtcars,wt,drat). And since wt isn't defined in the global environment, it wasn't found.