Error in getting weighted proportions using svytable()

,

I am working with weighted data and the survey + srvyr packages. I am able to create a design object, but getting an error when I go to use svytable(). The reprex isn’t capturing anything beyond creating my data frame, so I will be uploading the code and describing the error below. Thank you in advance for anybody able to help!

The code:

# Install and Load Packages -----------------------------------------------

pacman::p_load(
  janitor,      # for clean names
  freqtables,   # for descriptive statistics
  MatchIt,      # for matching data
  cobalt,       # for covariate analysis
  marginaleffects, # for communicating models
  rstatix,      # for assessing variance
  survey,       # for survey functions
  srvyr,        # dplyr wrapper for survey package
  gtsummary,    # wrapper for survey package to produce tables
  skimr,        # for scanning data quality
  datapasta,    # for creating reproducible error data
  reprex,       # for creating reprex session
  tidyverse     # for dplyr, mostly, but other functions too
)


# Import data -------------------------------------------------------------

demo_data <- data.frame(
  stringsAsFactors = FALSE,
  matchit_id = c("21","236","296",
                 "23","198","230","30","28","94","34","178",
                 "37","173","354","39"),
  weights = c(1,0.5,0.5,1,0.5,
              0.5,1,0.5,0.5,1,1,1,0.5,0.5,1),
  employment_status = c("Full Time",
                        "Full Time","Full Time","Retired","Retired","Retired",
                        "Full Time","Full Time","Full Time",
                        "Part Time","Part Time","Retired","Retired","Retired",
                        "Full Time"),
  weight_loss_med_intake = c("Yes","No","No",
                             "Yes","No","No","Yes","No","No","Yes","No",
                             "Yes","No","No","Yes"),
  subclass = as.factor(c("1","1","1","2","2","2","3","3",
                         "3","4","4","5","5","5","6"))
)

# Create svydesign object -------------------------------------------------

design <- svydesign(ids = ~1, weights = ~weights, data = demo_data)

# Create weighted outcome -------------------------------------------------

weighted_tab <- svytable(~employment_status + ~weight_loss_med_intake, design, na.rm = T)
print(weighted_tab)

The error I am getting is:

> weighted_tab <- svytable(~employment_status + ~weight_loss_med_intake, design, na.rm = T)
Error in terms.formula(formula, data = data) : invalid model formula

Hey William!

The error seems to be in the formula syntax. svytable() takes a single formula with variables joined by +, not two separate formulas each starting with ~. ~employment_status + ~weight_loss_med_intake is invalid because R sees a second ~ inside the formula and can’t parse it as one term.

weighted_tab <- svytable(~employment_status + weight_loss_med_intake, design, na.rm = TRUE)
print(weighted_tab)

Just drop the second ~. The formula only needs one tilde at the start, and every variable after it (joined by +) becomes a dimension of the cross tab, the same way table() works with multiple vectors.

When it runs, try troubbleshootin these:

class(weighted_tab) # should be "svytable" "table"
sum(weighted_tab)   # should equal sum(demo_data$weights)

Thanks! That was so much easier than I imagined.

1 Like