0

I have a dataset from a survey that looks something like this.

library(dplyr)
library(modelsummary)
library(Hmisc)

set.seed(123)
df<- data.frame(var1=runif(1000),
                  var2=runif(1000),
                  var3=runif(1000),
                  particip=rbinom(100, size=1, p=0.1)) %>% 
mutate(refusal=ifelse(particip==1,0,rbinom(100, size=1, p=0.06)),
       sampled=ifelse(particip==1|refusal==1,"Sampled","Not Sampled")) %>% 
  arrange(desc(particip))
describe(df[,4:6])

My aim is to check for covariate balance (var1:var3) across different subset of the data.

  • particip are respondents that were sampled and replied to the survey
  • refusal are respondents that were sampled but did not respond to the survey
  • sampled are all the respondents that were sampled regardless of whether they refused or accepted to take the survey.

I would like to create a single balance table that compares the means of var1:var3 across multiple subsamples. In particular, I would like to compare the means for the whole universe of respondents (all 1000 possible respondents), to the means of the sampled respondents, to the means of the respondents that refused to take the survey to the means of the respondents that eventually took part in the survey.

I have tried using the function datasummary_balance from the package modelsummary but I am only able to compare the means of one group at a time (participated, sampled, or refusals). Instead, I would like to create a single table, will all of the means by these three different groups.

datasummary_balance(~particip,
                    fmt=3,
                    data=df,
                    output = "markdown")
datasummary_balance(~refusal,
                    fmt=3,
                    data=df,
                    output = "markdown")
datasummary_balance(~sampled,
                    fmt=3,
                    data=df,
                    output = "markdown")

If anyone know how to do this it would be of great help

aynber
  • 22,380
  • 8
  • 50
  • 63
Alex
  • 1,207
  • 9
  • 25

0 Answers0