'Svyby returns error although arguments match documentation

I've defined a survey object using the survey package (that all worked fine) and added two columns to it as follows:

anes_svy <- update( #the update fn adds columns to a survey object
  anes_svy, #object to add variables to
  one = 1,
  undoc_kids =
            factor( V161195x , levels = 1:6 , labels =
                c( 'should sent back - favor a great deal' ,
                   'should sent back - favor a moderate amount' ,
                   'should sent back - favor a little' ,
                   'should allow to stay - favor a little' ,
                   'should allow to stay - favor a moderate amount' ,
                   'should allow to stay - favor a great deal' )
            )
)

Now when I run

svyby( formula = ~one , by = ~undoc_kids , design = anes_svy , FUN = unwtd.count )

I get the error

Error in sum(sapply(covmats, ncol)) : invalid 'type' (list) of argument

The documentation asks for a vector for 'formula', a list of factors for 'by', a svydesign object for 'design', and a function for FUN (unwtd.count is inbuilt). What am I doing wrong?



Solution 1:[1]

I get this same error when I make a mistake coding the factor

This does not work- Gear is 3:5, but the levels is set at 0:2. I ended up with the same error you observed

library( survey)
m <- mtcars
m$gear <- factor( m$gear, levels=c(0:2), labels=c("L","M","H"))
table( m$gear )

f <- svydesign( ~1 , strata = ~cyl , weights = ~wt , data= m )
svyby( ~mpg , ~gear , f, svymean, na.rm=T)

This does

m <- mtcars
m$gear <- factor( mtcars$gear, levels=c(3:5), labels=c("L","M","H"))
table( m$gear )
f <- svydesign( ~1 , strata = ~cyl , weights = ~wt , data= m )
svyby( ~mpg , ~gear , f, svymean, na.rm=T)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MatthewR