Calculate a median from a survey
create_analysis_median.Rd
Calculate a median from a survey
Arguments
- design
design survey
- group_var
dependent variable(s), variable to group by. If no dependent variable, it should be NA or empty string. If more than one variable, it should be one string with each variable separated by comma, e.g. "groupa, groupb" to group for groupa and groupb. NA is default for no grouping.
- analysis_var
the independent variable, variable to summarise
- level
the confidence level. 0.95 is default
Note
Default from survey/srvyr is "math" methodology. In case of odds number, it will return the lower value. Default for stats::median will calculate the mean between the two points. If there is a set of c(1,2), median(1,2) will return 1.5; survey_mean() will return 1 by default. create_analysis_median has the "school" methodology set as default, the results will match the default results from stats::median, pandas.median If want to calculate with the "math" methodology, you should run your own analysis with survey_median.
Examples
somedata <- data.frame(
aa = 1:10,
bb = rep(c("a", "b"), 5),
weights = rep(c(.5, 1.5), 5)
)
me_design <- srvyr::as_survey(somedata)
create_analysis_median(me_design, analysis_var = "aa")
#> analysis_type analysis_var analysis_var_value group_var group_var_value stat
#> 1 median aa <NA> <NA> <NA> 5.5
#> stat_low stat_upp n n_total n_w n_w_total analysis_key
#> 1 2 9 10 10 10 10 median @/@ aa %/% NA @/@ NA %/% NA
create_analysis_median(me_design, group_var = "bb", analysis_var = "aa")
#> # A tibble: 2 × 13
#> analysis_type analysis_var analysis_var_value group_var group_var_value stat
#> <chr> <chr> <chr> <chr> <chr> <dbl>
#> 1 median aa NA bb a 5
#> 2 median aa NA bb b 6
#> # ℹ 7 more variables: stat_low <dbl>, stat_upp <dbl>, n <int>, n_total <dbl>,
#> # n_w <dbl>, n_w_total <dbl>, analysis_key <chr>
me_design_w <- srvyr::as_survey(somedata, weights = weights)
create_analysis_median(me_design_w, analysis_var = "aa")
#> analysis_type analysis_var analysis_var_value group_var group_var_value stat
#> 1 median aa <NA> <NA> <NA> 6
#> stat_low stat_upp n n_total n_w n_w_total analysis_key
#> 1 2 NaN 10 10 10 10 median @/@ aa %/% NA @/@ NA %/% NA
create_analysis_median(me_design_w, group_var = "bb", analysis_var = "aa")
#> # A tibble: 2 × 13
#> analysis_type analysis_var analysis_var_value group_var group_var_value stat
#> <chr> <chr> <chr> <chr> <chr> <dbl>
#> 1 median aa NA bb a 5
#> 2 median aa NA bb b 6
#> # ℹ 7 more variables: stat_low <dbl>, stat_upp <dbl>, n <int>, n_total <dbl>,
#> # n_w <dbl>, n_w_total <dbl>, analysis_key <chr>