Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 202041

subscript out of bounds when using broom::tidy() on a svyglm with a non-varying term

$
0
0

I'm getting a subscript out of bounds error when using tidy() on a svyglm where one of the covariates does not vary:

library(broom)
library(tidyverse)
library(survey)

test.df <- tibble(
  id = c(1,2,3,4),
  weights = c(1,1,1,1),
  outcome = c(0,0,1,1),
  var1 = c(1,1,1,1),
  var2 = c(5,8,10,6)
)

svyglm(formula = outcome ~ var1 + var2,
       design = svydesign(ids = ~id, weights = ~weights, data = test.df),
       family = "binomial") %>%
  tidy(exp = TRUE,
       conf.int = TRUE)
#> Error in CI[piv, , drop = FALSE]: subscript out of bounds

The error seems to be sensitive to the order in which the covariates are specified in the formula, which was surprising to me. If the order is reversed, the error does not occur:

svyglm(formula = outcome ~ var2 + var1,
       design = svydesign(ids = ~id, weights = ~weights, data = test.df),
       family = "binomial") %>%
  tidy(exp = TRUE,
       conf.int = TRUE)
#> # A tibble: 2 x 7
#>   term        estimate std.error statistic p.value  conf.low conf.high
#>   <chr>          <dbl>     <dbl>     <dbl>   <dbl>     <dbl>     <dbl>
#> 1 (Intercept)   0.0403     4.05     -0.792   0.511 0.0000144    113.  
#> 2 var2          1.56       0.508     0.875   0.474 0.576          4.22

The error only occurs when requesting confidence intervals:

svyglm(formula = outcome ~ var1 + var2,
       design = svydesign(ids = ~id, weights = ~weights, data = test.df),
       family = "binomial") %>%
  tidy(exp = TRUE)
#> # A tibble: 2 x 5
#>   term        estimate std.error statistic p.value
#>   <chr>          <dbl>     <dbl>     <dbl>   <dbl>
#> 1 (Intercept)   0.0403     4.05     -0.792   0.511
#> 2 var2          1.56       0.508     0.875   0.474

Might this be an issue with broom, or am I missing something? Why would tidy() be sensitive to the order of covariates in the formula?


Viewing all articles
Browse latest Browse all 202041

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>