Skip to content

Instantly share code, notes, and snippets.

@brshallo
Last active November 23, 2021 19:37
Show Gist options
  • Select an option

  • Save brshallo/fcbd30fd77916e10fcfed6bd50aae081 to your computer and use it in GitHub Desktop.

Select an option

Save brshallo/fcbd30fd77916e10fcfed6bd50aae081 to your computer and use it in GitHub Desktop.
library(tidyverse)

defaultW <- getOption("warn")
options(warn = -1)

data(lending_club, package = "modeldata")
df_ca <- filter(lending_club, addr_state == "CA")
df_tx <- filter(lending_club, addr_state == "TX")

tidy_wtd.t.test <- function(test){
  
  test %>% 
    map(enframe) %>% 
    map(~spread(.x, name, value)) %>% 
    bind_cols() %>% 
    janitor::clean_names()
}

weights::wtd.t.test(
  x = df_ca$int_rate,
  y = df_tx$int_rate,
  weight = df_ca$funded_amnt,
  weighty = df_tx$funded_amnt
) %>%
  tidy_wtd.t.test()
#> # A tibble: 1 x 8
#>   x1                         df p_value t_value difference mean_x mean_y std_err
#>   <chr>                   <dbl>   <dbl>   <dbl>      <dbl>  <dbl>  <dbl>   <dbl>
#> 1 Two Sample Weighted T-~ 1934.   0.481  -0.706     -0.156   12.9   13.0   0.221

options(warn = defaultW)

Created on 2021-11-23 by the reprex package (v2.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment