1
votes

I have a nested tibble in the following form:

library(purrr)
library(dplyr)

ex <- tibble(data = list(val = rnorm(12), 
                         val = rnorm(5), 
                         val = rep(NA, 5), 
                         val = c(rnorm(3), NA)), 
               p1 = rnorm(4),
               p2 = rnorm(4)) %>% 
         mutate(data = map(data, tibble))

and I would like a new column filled with p-values calculated from ks.test for each tibble in data compared to a normal distribution which parameters are stored in p1 and p2.

I tried something like

ex %>%
   mutate(ks_test = map(data, 
             ~tryCatch( #to avoid problems with tibbles filled only by NAs
                ks.test(x = .$val,
                        y = "pnorm",
                        mean = .$p1,
                        sd = .$p2), 
              error = function(e) list(p.value = NA))),
           ks_pvalue = map_dbl(ks_test, "p.value"))

which unfortunately fails to perform the test, providing only NAs.

Please, can you help me to fix this chunk of code? Thank you.

1

1 Answers

2
votes

I will refer you to purrr::possibly to handle your NA cases. As far as handling the vectors which are complete it helps to have a grouping column to nest by. Also a specific function for the ks_test helps keep things clean.

library(purrr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyverse)


val1 = data.frame(val = rnorm(12), group = 1)
val2 = data.frame(val = rnorm(5), group = 2)

ks_test_fun <- function(data, p1, p2, ...){
  data %>% 
  ks.test(x = .$val, y = "pnorm", mean = p1, sd = p2) %>% 
  broom::tidy() %>% 
  pull(p.value)
}

set.seed(4)
rbind(val1, val2) %>% 
  group_by(group) %>% 
  nest() %>% 
  mutate(p1 = rnorm(2), p2 = rnorm(2)) %>% 
  mutate(ks_test_pval = map(data, ks_test_fun, p1, p2)) %>% 
  unnest(ks_test_pval)
#> # A tibble: 2 x 5
#>   group data                  p1    p2 ks_test_pval
#>   <dbl> <list>             <dbl> <dbl>        <dbl>
#> 1     1 <tibble [12 x 1]>  0.217 0.891        0.137
#> 2     2 <tibble [5 x 1]>  -0.542 0.596        0.333

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