srvyr survey_quantile - no output when using ci

I am not getting any output (0 rows) when specifying confidence interval with quantiles. Any ideas? Here's a reprex:

library(tidyverse) # for tidyverse
library(here) # for file paths
#> here() starts at C:/Users/sazimmer/AppData/Local/Temp/RtmpyMdwWk/reprex5a68704646a1
library(survey) # for some survey analysis
#> Loading required package: grid
#> Loading required package: Matrix
#> 
#> Attaching package: 'Matrix'
#> The following objects are masked from 'package:tidyr':
#> 
#>     expand, pack, unpack
#> Loading required package: survival
#> 
#> Attaching package: 'survey'
#> The following object is masked from 'package:graphics':
#> 
#>     dotchart
library(srvyr) # for tidy survey analysis
#> 
#> Attaching package: 'srvyr'
#> The following object is masked from 'package:stats':
#> 
#>     filter

# recs <- read_rds(here("Data", "recs.rds"))
# 
# recs %>%
#    select(NWEIGHT, BRRWT1:BRRWT20, TOTALDOL) %>%
#    mutate_if(is.numeric, round, 1) %>%
#    slice(1:10) %>%
#    dput()

recs <- structure(list(NWEIGHT = c(12085.2, 14410.2, 23326, 12165.3, 
                                   16717.7, 26058.9, 45952.8, 15281, 14034.7, 43359.2), BRRWT1 = c(16555.3, 
                                                                                                   21486.3, 12297.4, 18554, 8075.7, 37004.1, 28077.4, 22962.3, 6989.9, 
                                                                                                   73353.8), BRRWT2 = c(5938, 19147, 36659.8, 5209.1, 8427.3, 34907.2, 
                                                                                                                        71118.6, 7067.5, 6954.7, 19161.1), BRRWT3 = c(17725.6, 7320.4, 
                                                                                                                                                                      10476.2, 18041.5, 8335.6, 15314.9, 20106.9, 25747.7, 7097.5, 
                                                                                                                                                                      16672.1), BRRWT4 = c(17635.6, 23567.9, 10101.9, 5441.8, 23666.4, 
                                                                                                                                                                                           33343, 68172.2, 23842.5, 20873, 18551), BRRWT5 = c(6032.8, 21926.6, 
                                                                                                                                                                                                                                              10291.1, 5815.6, 7674.5, 15332.5, 64424.8, 7951.1, 8224.2, 81545.2
                                                                                                                                                                                           ), BRRWT6 = c(17095.4, 7477.4, 39732, 4936, 25586.8, 14433.9, 
                                                                                                                                                                                                         56769.9, 26022.5, 19524.2, 63728), BRRWT7 = c(6003.4, 21544, 
                                                                                                                                                                                                                                                       11547.8, 22136.1, 23442, 14590.9, 22868.6, 7984.6, 21250.8, 67869
                                                                                                                                                                                                         ), BRRWT8 = c(5994.1, 8189.4, 12678.5, 18412.7, 26866.4, 32685.3, 
                                                                                                                                                                                                                       18690.8, 6924.7, 19723.2, 19365.2), BRRWT9 = c(5403, 6616.4, 
                                                                                                                                                                                                                                                                      38550.9, 18910.6, 8263.6, 34548.4, 27477.9, 6676.5, 5853.1, 69789.9
                                                                                                                                                                                                                       ), BRRWT10 = c(17569.6, 7260.4, 10754.5, 6621.4, 8210.5, 33987.8, 
                                                                                                                                                                                                                                      84209.8, 23981.8, 6368.9, 64151.3), BRRWT11 = c(16621, 21417, 
                                                                                                                                                                                                                                                                                      44738.6, 20070.6, 8292.4, 14716.9, 22968.8, 23687.5, 7158.6, 
                                                                                                                                                                                                                                                                                      22341.6), BRRWT12 = c(18815.8, 21356.5, 36050.6, 23333.1, 25607.7, 
                                                                                                                                                                                                                                                                                                            32993.5, 24615.3, 24816.2, 22569.4, 67045.8), BRRWT13 = c(5660.4, 
                                                                                                                                                                                                                                                                                                                                                                      7196.1, 12305.6, 18647.6, 24152.6, 36403.8, 61313.4, 22493.8, 
                                                                                                                                                                                                                                                                                                                                                                      22462.3, 19394.3), BRRWT14 = c(20846.4, 8036.6, 36765.3, 5191.9, 
                                                                                                                                                                                                                                                                                                                                                                                                     25632.3, 34958.6, 24262.1, 6726.5, 22151.8, 77443.7), BRRWT15 = c(7251.4, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       21021.6, 10564.8, 17992.9, 27171.8, 14994.7, 55610, 25790, 19986.2, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                       83486.3), BRRWT16 = c(6305.8, 6465.2, 10107.8, 5299.3, 7484.6, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             33526.7, 31353, 24029.8, 7626.1, 75868), BRRWT17 = c(17556.9, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  7067.4, 10337, 5938.8, 24470.2, 15120.5, 22056.8, 6785.7, 23286.2, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  17688.3), BRRWT18 = c(5502.9, 19909.7, 40280.8, 4850.4, 8446.1, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        14267, 21275.6, 26481.6, 6040, 22775.7), BRRWT19 = c(17874.9, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             7107.3, 11448.9, 21861.2, 7888.2, 14766.8, 74685.1, 7270.7, 6770.2, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             21210.8), BRRWT20 = c(17451.2, 19018.8, 12917.4, 18002.6, 8830.7, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   32376.9, 80576.5, 6998.6, 7081.4, 74481), TOTALDOL = c(1406.1, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          1366, 2828.8, 2103, 910.9, 1040, 1015.2, 2384.1, 2485, 1973.7
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   )), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ))

recs_des <- recs %>%
   as_survey_rep(weights=NWEIGHT,
                 repweights=starts_with("BRRWT"),
                 type="Fay",
                 rho=0.5,
                 mse=TRUE)


### no output?
recs_des %>%
   summarize(
      Spent=survey_quantile(
         x = TOTALDOL,
         quantiles = c(.25, .75),
         vartype = "ci"
      )
   )
#> [1] Spent_q25     Spent_q75     Spent_q25_low Spent_q75_low Spent_q25_upp
#> [6] Spent_q75_upp
#> <0 rows> (or 0-length row.names)

recs_des %>%
   summarize(
      Spent=survey_quantile(
         x = TOTALDOL,
         quantiles = c(.25, .75),
         vartype = "se"
      )
   )
#>   Spent_q25 Spent_q75 Spent_q25_se Spent_q75_se
#> 1  999.7143  2068.924     20.65815     399.1772


recs_des %>%
   summarize(
      Spent=survey_mean(
         x = TOTALDOL,
         vartype = "ci"
      )
   )
#>      Spent Spent_low Spent_upp
#> 1 1674.696  1110.019  2239.373

Created on 2021-03-07 by the reprex package (v1.0.0)

class(recs_des)
[1] "tbl_svy"       "svyrep.design"
 print(recs_des)
Call: Called via srvyr
Fay's variance method (rho= 0.5 ) with 20 replicates and MSE variances.
Sampling variables:
 - repweights: `BRRWT1 + BRRWT2 + BRRWT3 + BRRWT4 + BRRWT5 + BRRWT6 + BRRWT7 + BRRWT8 + BRRWT9 + BRRWT10 + BRRWT11 + BRRWT12 + BRRWT13 + BRRWT14 + BRRWT15 + BRRWT16 + BRRWT17 + BRRWT18 + BRRWT19 + BRRWT20`
 - weights: NWEIGHT
Data variables: NWEIGHT (dbl), BRRWT1 (dbl), BRRWT2 (dbl), BRRWT3 (dbl), BRRWT4 (dbl),
  BRRWT5 (dbl), BRRWT6 (dbl), BRRWT7 (dbl), BRRWT8 (dbl), BRRWT9 (dbl), BRRWT10 (dbl),
  BRRWT11 (dbl), BRRWT12 (dbl), BRRWT13 (dbl), BRRWT14 (dbl), BRRWT15 (dbl), BRRWT16
  (dbl), BRRWT17 (dbl), BRRWT18 (dbl), BRRWT19 (dbl), BRRWT20 (dbl), TOTALDOL (dbl)

doesn't have an implicit print method.

That's not the issue - you can see it returns something with 0 rows and I expect 1 row. I found a bug on github so there's no solution.

OP code is consistent with the help(as_survey_rep) example, which shows a class list` return value.

suppressPackageStartupMessages({
  library(dplyr)
  library(survey)
  library(srvyr)
})

data(scd)
# use BRR replicate weights from Levy and Lemeshow
scd <- scd %>%
  mutate(rep1 = 2 * c(1, 0, 1, 0, 1, 0),
         rep2 = 2 * c(1, 0, 0, 1, 0, 1),
         rep3 = 2 * c(0, 1, 1, 0, 0, 1),
         rep4 = 2 * c(0, 1, 0, 1, 1, 0))

scdrep <- scd %>%
  as_survey_rep(type = "BRR", repweights = starts_with("rep"),
                combined_weights = FALSE)
#> Warning in svrepdesign.default(variables = variables, repweights = repweights, :
#> No sampling weights provided: equal probability assumed

print(scdrep)
#> Call: Called via srvyr
#> Balanced Repeated Replicates with 4 replicates.
#> Sampling variables:
#>  - repweights: `rep1 + rep2 + rep3 + rep4`
#> Data variables: ESA (int), ambulance (int), arrests (dbl), alive (dbl), rep1
#>   (dbl), rep2 (dbl), rep3 (dbl), rep4 (dbl)

str(scdrep)
#> List of 11
#>  $ type            : chr "BRR"
#>  $ scale           : num 0.25
#>  $ rscales         : num [1:4] 1 1 1 1
#>  $ rho             : NULL
#>  $ call            : 'quoteless_text' chr "Called via srvyr"
#>  $ combined.weights: logi FALSE
#>  $ variables       : tibble [6 × 8] (S3: tbl_df/tbl/data.frame)
#>   ..$ ESA      : int [1:6] 1 1 2 2 3 3
#>   ..$ ambulance: int [1:6] 1 2 1 2 1 2
#>   ..$ arrests  : num [1:6] 120 78 185 228 670 530
#>   ..$ alive    : num [1:6] 25 24 30 49 80 70
#>   ..$ rep1     : num [1:6] 2 0 2 0 2 0
#>   ..$ rep2     : num [1:6] 2 0 0 2 0 2
#>   ..$ rep3     : num [1:6] 0 2 2 0 0 2
#>   ..$ rep4     : num [1:6] 0 2 0 2 2 0
#>  $ pweights        : num [1:6] 1 1 1 1 1 1
#>  $ repweights      :'data.frame':    6 obs. of  4 variables:
#>   ..$ rep1: num [1:6] 2 0 2 0 2 0
#>   ..$ rep2: num [1:6] 2 0 0 2 0 2
#>   ..$ rep3: num [1:6] 0 2 2 0 0 2
#>   ..$ rep4: num [1:6] 0 2 0 2 2 0
#>   ..- attr(*, "terms")=Classes 'terms', 'formula'  language ~rep1 + rep2 + rep3 + rep4
#>   .. .. ..- attr(*, "variables")= language list(rep1, rep2, rep3, rep4)
#>   .. .. ..- attr(*, "factors")= int [1:4, 1:4] 1 0 0 0 0 1 0 0 0 0 ...
#>   .. .. .. ..- attr(*, "dimnames")=List of 2
#>   .. .. .. .. ..$ : chr [1:4] "rep1" "rep2" "rep3" "rep4"
#>   .. .. .. .. ..$ : chr [1:4] "rep1" "rep2" "rep3" "rep4"
#>   .. .. ..- attr(*, "term.labels")= chr [1:4] "rep1" "rep2" "rep3" "rep4"
#>   .. .. ..- attr(*, "order")= int [1:4] 1 1 1 1
#>   .. .. ..- attr(*, "intercept")= int 1
#>   .. .. ..- attr(*, "response")= int 0
#>   .. .. ..- attr(*, ".Environment")=<environment: 0x55e0245b7488> 
#>   .. .. ..- attr(*, "predvars")= language list(rep1, rep2, rep3, rep4)
#>   .. .. ..- attr(*, "dataClasses")= Named chr [1:4] "numeric" "numeric" "numeric" "numeric"
#>   .. .. .. ..- attr(*, "names")= chr [1:4] "rep1" "rep2" "rep3" "rep4"
#>  $ degf            : num 3
#>  $ mse             : logi FALSE
#>  - attr(*, "class")= chr [1:2] "tbl_svy" "svyrep.design"
#>  - attr(*, "survey_vars")=List of 3
#>   ..$ repweights:List of 1
#>   .. ..$ : symbol rep1 + rep2 + rep3 + rep4
#>   ..$ weights   : NULL
#>   ..$ fpc       : NULL
#>   ..- attr(*, "class")= chr "survey_vars"

I think you missed the question. It's about when getting quantile estimates and requesting confidence intervals and is related to this issue: https://github.com/gergness/srvyr/issues/101

suppressPackageStartupMessages({
   library(dplyr)
   library(survey)
   library(srvyr)
})

data(scd)
# use BRR replicate weights from Levy and Lemeshow
scd <- scd %>%
   mutate(rep1 = 2 * c(1, 0, 1, 0, 1, 0),
          rep2 = 2 * c(1, 0, 0, 1, 0, 1),
          rep3 = 2 * c(0, 1, 1, 0, 0, 1),
          rep4 = 2 * c(0, 1, 0, 1, 1, 0))

scdrep <- scd %>%
   as_survey_rep(type = "BRR", repweights = starts_with("rep"),
                 combined_weights = FALSE)
#> Warning in svrepdesign.default(variables = variables, repweights = repweights, :
#> No sampling weights provided: equal probability assumed

scdrep %>%
   summarise(
      arrest=survey_quantile(
         arrests,
         quantiles = c(.25, .75),
         vartype = "ci"
      )
   )
#> [1] arrest_q25     arrest_q75     arrest_q25_low arrest_q75_low arrest_q25_upp
#> [6] arrest_q75_upp
#> <0 rows> (or 0-length row.names)

Created on 2021-03-08 by the reprex package (v1.0.0)

1 Like

Yup. Sure did. Thanks on behalf of posterity.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.