0

While using tbl_svysummary, an error is thrown if the source data has missing values, whereas in tbl_summary, a simple warning is thrown (and the filtering of the missing data done in background)

Simple reprex:

database = data.frame(
  INDIV_AGE = rnorm(100, mean = 50, sd = 4),
  INDIV_GENDER = rbinom(n = 100, size=1, prob = 0.6),
  PAIN_SCALE = factor(sample(c("Low", "Elevated"), size = 100, replace = T)),
  FLOWER_COLOR = factor(sample(c("Blue", "Red"), size = 100, replace = T)),
  poids = rnorm(100, mean = 2, sd = 0.8)
)
database[1, "INDIV_GENDER"] = NA #if you remove this line it works fine

design = survey::svydesign(
  id = ~1, 
  weights = ~poids,
  data = database
)

design |> 
  gtsummary::tbl_svysummary(
    by = "INDIV_GENDER"
  )

Interestingly, the package does throw the usual warning, but also issues an error:

1 missing rows in the "INDIV_GENDER" column have been removed.
Error in model.frame.default(formula = weights ~ ..ard_total_n.., data = model.frame(design),  : 
  variable lengths differ (found for '..ard_total_n..')

I'm not sure this is the expected behaviour, should I file an issue on github?

2
  • The problem is caused by NAs. First you need to decide how to deal with the missing values (removing or imputing). Because the dataset remains 99 units long while the poids vector is still 100 units long. I think that's why the analysis can't be done. Commented Jun 20 at 20:25
  • Yes I figured that out, but since tbl_summary usually handles missing values on its own (see the first line of the output), I expected tbl_svysummary would handle NAs as well. It turns out there might be a mishandling of the filtering step. Here it says it filtered out the row with missing data, so it shouldn't be triggering any error afterwards Commented Jun 21 at 15:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.