Using "survey" package to create a 2 by 4 table for chisquare test

Hello, I am trying to create a table from the data below

dataset<-data.frame(stringsAsFactors=FALSE,
                                                                                 status = c("low", "low", "ok", "low", "ok", "ok"),
                                                                               MCLUSTER = c(601, 601, 601, 511, 511, 829),
                                                                                MNUMBER = c(289, 151, 9, 94, 95, 234),
                                                                                    M01 = c(2, 2, 1, 2, 1, 1),
                                                                            unadjstatus = c("low", "ok", "ok", "low", "ok", "ok"),
                                                                                    wgt = c(0.995193, 0.995193, 0.995193, 1.117896, 1.117896, 1.477476),
                                                                                MREGION = c(2, 2, 2, 2, 2, 2),
                                                                                  MTYPE = c(2, 2, 2, 2, 2, 2),
                                                                                status2 = as.factor(c("low", "ok", "ok", "low", "ok", "ok")),
                                                                                status3 = as.factor(c("low", "low", "low", "low", "ok", "ok"))
                                                                         )

So far I have two methodologies which work to create tables, but they don't seem to work with the "survey" package.
I have tried this

> tb<-vapply(dataset[c(1,5,9:10)],table, FUN.VALUE = c("low"=0, "ok"=0))
> tb
    status unadjstatus status2 status3
low      3           2       2       4
ok       3           4       4       2

and this

> tb1<-dataset%>%dplyr::summarise_at(.vars=c(1,5,9:10), .funs = ~list(table(.))) %>%tidyr::unnest()
> tb1
  status unadjstatus status2 status3
1      3           2       2       4
2      3           4       4       2

but when I try to use these tables in "survey", I get these errors

wgtdata<-svydesign(ids =~MCLUSTER,weights =~wgt, data=dataset

> svychisq(~tb,wgtdata)
Error in formula[[2]][[2]] : object of type 'symbol' is not subsettable

> svychisq(~tb1,wgtdata)
Error in formula[[2]][[2]] : object of type 'symbol' is not subsettable

I have also tried using the survey table functions as below but keep getting errors (mostly because it's not making the table the way it ought to be for a chisquare test)

tb3<-ftable(dataset,colnames=list("unadjstatus","status","status2","status3"),
                          rownames=list())
> svychisq(~tb3,wgtdata)
Error in formula[[2]][[2]] : object of type 'symbol' is not subsettable


tb4<-svytable(~unadjstatus+status+status2+status3,metwgt)
svychisq(~tb4, ~wgtdata)
Error in UseMethod("svychisq", design) : 
  no applicable method for 'svychisq' applied to an object of class "formula"

does anyone have a suggestion on how to overcome this?

Hi!

To help us help you, could you please turn this into a self-contained reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

I've editted it, I hope it's clear enough now

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