kuriwaki / ddi

Computes Meng's d.d.i. (Data Defect Index) from samples that may not be i.i.d.
https://cran.r-project.org/web/packages/ddi/index.html
3 stars 0 forks source link

tidyeval #2

Closed kuriwaki closed 5 years ago

kuriwaki commented 5 years ago

quick fix. change to NSE

library(ddi)

data(g2016)
ddcs <- ddc(g2016, N = "tot_votes", n = "cces_n_vv", mu = "pct_djt_voters", muhat = "cces_pct_djt_vv")

Created on 2019-02-02 by the reprex package (v0.2.1)

kuriwaki commented 5 years ago

Now works with tidyeval, along with a few others:

library(ddi)
library(tibble)
library(dplyr)
data(g2016)

# 1. scalar input
select(g2016, cces_totdjt_vv, cces_n_vv, tot_votes, votes_djt) %>%
  summarize_all(sum)
#> # A tibble: 1 x 4
#>   cces_totdjt_vv cces_n_vv tot_votes votes_djt
#>            <dbl>     <dbl>     <dbl>     <dbl>
#> 1          12284     35829 136639786  62984824

## plug those numbers in
ddc(mu = 62984824/136639786, muhat = 12284/35829, N = 136639786, n = 35829)
#> [1] -0.003837163

# 2. vector input using "with"
with(g2016, ddc(mu = pct_djt_voters, muhat = cces_pct_djt_vv, N = tot_votes, n = cces_n_vv))
#>  [1] -0.0059541279 -0.0062341071 -0.0023488019 -0.0061097707 -0.0009864919
#>  [6] -0.0025746344 -0.0035362241 -0.0033951165  0.0014015382 -0.0029747918
#> [11] -0.0038228152 -0.0001757426 -0.0073716139 -0.0036437192 -0.0069956521
#> [16] -0.0058255411 -0.0059093759 -0.0057837854 -0.0040533230 -0.0047893714
#> [21] -0.0024905368 -0.0028280876 -0.0050296619 -0.0043292576 -0.0056626724
#> [26] -0.0069305025 -0.0046563153 -0.0075840944 -0.0047785897 -0.0037497506
#> [31] -0.0028289070 -0.0025619899 -0.0031936586 -0.0051968951 -0.0078308914
#> [36] -0.0057088185 -0.0065654840 -0.0030642004 -0.0039137353 -0.0039907269
#> [41] -0.0040871158 -0.0069019981 -0.0050741833 -0.0044884762 -0.0059634270
#> [46] -0.0034491625 -0.0040918085 -0.0024121681 -0.0075404659 -0.0051378753
#> [51] -0.0086086072

# 3. vector input in tidy tibble
transmute(g2016, st,
          ddc = ddc(mu = pct_djt_voters, muhat = cces_pct_djt_vv, N = tot_votes, n = cces_n_vv))
#> # A tibble: 51 x 2
#>    st          ddc
#>    <chr>     <dbl>
#>  1 AL    -0.00595 
#>  2 AK    -0.00623 
#>  3 AZ    -0.00235 
#>  4 AR    -0.00611 
#>  5 CA    -0.000986
#>  6 CO    -0.00257 
#>  7 CT    -0.00354 
#>  8 DE    -0.00340 
#>  9 DC     0.00140 
#> 10 FL    -0.00297 
#> # … with 41 more rows

Created on 2019-10-28 by the reprex package (v0.3.0)