dgrtwo / ebbr

Empirical Bayes binomial estimation
Other
69 stars 13 forks source link

ebbr doesn't allow for custom functions #11

Open michaelgaunt404 opened 3 years ago

michaelgaunt404 commented 3 years ago

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 commented 3 years ago

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) }