ebbr icon indicating copy to clipboard operation
ebbr copied to clipboard

ebbr doesn't allow for custom functions

Open michaelgaunt404 opened this issue 4 years ago • 1 comments

Hi, I'm trying to make a custom function to make ebbr_fit_prior estimates on many columns in a dataframe.

I'm running into a lot of issues in passing variable column names to ebbr when it is in a custom function - I've tried using !!as.symbol() but some people on reddit said this might not work given R's base NSE that you might be using to build this code.

Can you suggest a way by which to do this?

`library(tidyverse) library(Lahman) library(ebbr)

career <- Batting %>% filter(AB > 0) %>% anti_join(Pitching, by = "playerID") %>% group_by(playerID) %>% summarize(H = sum(H), AB = sum(AB)) %>% mutate(average = H / AB)

#this works career %>%
ebbr::ebb_fit_prior(H, AB)

#function that i can use to make a bunch of estimates make_eb_estimate = function(data, success, total, method = "mle"){ fitted = data %>%
ebb_fit_prior(x = success, n = total, method = method) %>%
augment() %>% .$.fitted }

#this does not work career %>%
make_eb_estimate("H", "AB")`

michaelgaunt404 avatar May 24 '21 17:05 michaelgaunt404

sorry this can be closed!

reddit hive mind was able to provide a solution

make_eb_estimate = function(data, success, total, method = "mle"){ eb_call = call2(.fn = expr(ebb_fit_prior), tbl = enexpr(data), x = as.name(success), # Assuming a character vector n = as.name(total), # Assuming a character vector method = method) fitted = eval_tidy(eb_call) }

michaelgaunt404 avatar May 24 '21 21:05 michaelgaunt404