Matherion / userfriendlyscience

The R userfriendlyscience package
https://userfriendlyscience.com
14 stars 7 forks source link

confIntV function not working when one of the levels is not present #8

Closed IndrajeetPatil closed 5 years ago

IndrajeetPatil commented 6 years ago

Hi,

The function userfriendlyscience::confIntV seems to have difficulty handling situations where one of the categorical variables doesn't have any values for one of the levels of the other categorical variable. I have provided a fully reproducible example below. I also toyed around with another function that doesn't fail even in such contexts,

# libraries needed
library(userfriendlyscience)
library(DescTools)
library(jmv)

# for reproducible output from bootstrapping
set.seed(420)

# dataframe
data <- structure(list(dilemma = structure(c(2L, 3L, 1L, 2L, 3L, 1L, 
                                             2L, 1L, 3L, 2L, 3L, 1L, 1L, 2L, 3L, 2L, 3L, 1L, 3L, 1L, 2L, 2L, 
                                             3L, 1L, 3L, 2L, 1L, 2L, 3L, 1L, 3L, 1L, 2L, 3L, 2L, 1L, 2L, 3L, 
                                             1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 3L, 2L, 3L, 1L, 3L, 2L, 1L, 
                                             2L, 3L, 1L, 2L, 1L, 3L, 3L, 1L, 2L, 2L, 1L, 3L, 1L, 3L, 2L, 3L, 
                                             2L, 1L, 2L, 1L, 3L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 3L, 2L, 
                                             1L, 3L, 1L, 2L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
                                             2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 3L, 1L, 2L, 2L, 
                                             3L, 1L, 2L, 1L, 3L, 1L, 2L, 3L, 2L, 1L, 3L, 1L, 2L, 3L, 3L, 1L, 
                                             2L, 3L, 2L, 1L, 2L, 3L, 1L), .Label = c("non-moral", "impersonal", 
                                                                                     "personal"), class = "factor"), experienced = structure(c(1L, 
                                                                                                                                               1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 
                                                                                                                                               1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                               1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                               1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                               1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                               1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                               1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                               1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                               1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("no", 
                                                                                                                                                                                                           "yes"), class = "factor")), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                           ), row.names = c(NA, -141L), .Names = c("dilemma", "experienced"
                                                                                                                                                                                                           ))                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
# converting to dataframe
data <- as.data.frame(data)

# making sure we can get Cramer's V first
jmv::contTables(
  data = data,
  rows = 'experienced' ,
  cols = 'dilemma',
  phiCra = TRUE
)
#> 
#>  CONTINGENCY TABLES
#> 
#>  Contingency Tables                                              
#>  --------------------------------------------------------------- 
#>    experienced    non-moral    impersonal    personal    Total   
#>  --------------------------------------------------------------- 
#>    no                    46            47          47      140   
#>    yes                    1             0           0        1   
#>    Total                 47            47          47      141   
#>  --------------------------------------------------------------- 
#> 
#> 
#>  <U+03C7>² Tests                       
#>  ------------------------------ 
#>          Value    df    p       
#>  ------------------------------ 
#>    <U+03C7>²     2.01     2    0.365   
#>    N       141                  
#>  ------------------------------ 
#> 
#> 
#>  Nominal                      
#>  ---------------------------- 
#>                       Value   
#>  ---------------------------- 
#>    Phi-coefficient      NaN   
#>    Cramer's V         0.120   
#>  ----------------------------

# trying to get confidence intervals using userfriendlyscience package function
userfriendlyscience::confIntV(
  x = data$experienced,
  y = data$dilemma,
  conf.level = 0.95,
  samples = 500,
  digits = 5,
  method = "bootstrap",
  storeBootstrappingData = FALSE
)
#> Error in chisq.test(x, y, correct = FALSE): 'x' and 'y' must have at least 2 levels

# using DescTools function
DescTools::CramerV(x = data$experienced,
                   y = data$dilemma, 
                   conf.level = 0.95)
#>  Cramer V    lwr.ci    upr.ci 
#> 0.1195229 0.0000000 0.2642163

Created on 2018-03-08 by the reprex package (v0.2.0).