R4EPI / epitabulate

Tables for epidemiological analysis
https://R4EPI.github.io/epitabulate
GNU General Public License v3.0
8 stars 0 forks source link

tab_survey when all rows in one group #12

Closed aspina7 closed 4 years ago

aspina7 commented 4 years ago

tab_survey appears to throw an error if all rows are in one group of a categorical variable.

will get round to producing a reprex.

Error in UseMethod("pull") : 
  no applicable method for 'pull' applied to an object of class "c('simpleError', 'error', 'condition')"
aspina7 commented 4 years ago

in other news it will swap the levels of a factor


# A tibble: 166 x 4
   variable      value        n ci                   
   <chr>         <chr>    <dbl> <chr>                
 3 minagearvgrp  >19   63393.   91.5% (91.2--91.9)   
 4 minagearvgrp  0-10   4235.   6.1% (5.9--6.4)      
 5 minagearvgrp  11-19  1621.   2.3% (2.2--2.5) 
aspina7 commented 4 years ago

in other other news, when using na.rm = FALSE tab_linelist will add a "Missing" row tab_survey will add a "(Missing)" row - i.e. with brackets

zkamvar commented 4 years ago

tab_survey appears to throw an error if all rows are in one group of a categorical variable.

will get round to producing a reprex.

Error in UseMethod("pull") : 
  no applicable method for 'pull' applied to an object of class "c('simpleError', 'error', 'condition')"

Can you elaborate on what you mean by "when all rows in one group"?

aspina7 commented 4 years ago

tab_survey appears to throw an error if all rows are in one group of a categorical variable. will get round to producing a reprex.

Error in UseMethod("pull") : 
  no applicable method for 'pull' applied to an object of class "c('simpleError', 'error', 'condition')"

Can you elaborate on what you mean by "when all rows in one group"?

Sorry - took me a while to get back to this. See reprex below. If have a var with only one value throws error.

library("dplyr") 
#> Warning: package 'dplyr' was built under R version 3.6.1
#> 
#> 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("srvyr")
#> Warning: package 'srvyr' was built under R version 3.6.1
#> 
#> Attaching package: 'srvyr'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library("sitrep")

dummy <- tibble(banana = sample(c("eat", NA), 100, replace = TRUE), 
                weight = sample(1:3, 100, replace = TRUE))

dummy_weighted <- dummy %>% 
  as_survey_design(ids = 1,
                   weights = weight)

tab_survey(dummy_weighted, banana)
#> Warning: removing 47 missing value(s) from `banana`
#> Error in UseMethod("pull"): no applicable method for 'pull' applied to an object of class "c('simpleError', 'error', 'condition')"

Created on 2020-03-23 by the reprex package (v0.3.0)

zkamvar commented 4 years ago

So, it turns out that this was part of my attempt to be clever and catch an error that was plaguing us for a while, but not being smart enough to realize that the error could pop up without strata.

It's complaining because there is only one level in that category, so it cannot apply contrasts.

If you set na.rm = FALSE, then the missing data are treated as a second factor.

library("epibuffet")
dummy <- tibble::tibble(
  banana = sample(c("eat", NA), 100, replace = TRUE),
  weight = sample(1:3, 100, replace = TRUE)
)

dummy_weighted <- srvyr::as_survey_design(dummy, ids = 1, weights = weight)
tab_survey(dummy_weighted, banana)
#> Warning: removing 42 missing value(s) from `banana`
#> Error: contrasts can be applied only to factors with 2 or more levels
tab_survey(dummy_weighted, banana, na.rm = FALSE)
#> # A tibble: 2 x 4
#>   variable value         n ci                
#> * <chr>    <chr>     <dbl> <chr>             
#> 1 banana   eat         113 56.8% (46.0--66.9)
#> 2 banana   (Missing)    86 43.2% (33.1--54.0)

Created on 2020-04-12 by the reprex package (v0.3.0)

zkamvar commented 4 years ago

Fixed in #13