[R] offlist Re: Svy function doesn't work nested in user-defined function

Thomas Lumley tlumley at u.washington.edu
Fri May 21 17:48:08 CEST 2010


On Thu, 20 May 2010, David Winsemius wrote:

> Almost. Testing with the examples on svytable help page:
>
> test <- function(Z){ 			
>   fmla <- as.formula( paste(" ~ ", paste(c(Z, "stype"), collapse= "+")))
>   chisq <- svychisq( fmla, design=rclus1,
>                                 statistic="adjWald", round=4)
> }
>
> reslt <- test("sch.wide")
> reslt
>
> 	Design-based Wald test of association
>
> data:  svychisq(fmla, design = rclus1, statistic = "adjWald", round = 4)
> F = 2.2296, ndf = 2, ddf = 13, p-value = 0.1471
>
> The "con" object probably does not exist, so one needs to pass a character 
> name to the formula.
>

That works, but you can see why I prefer

test<-function(formula){
     svychisq(update(formula, ~.+stype), dclus1)
}

test(~sch.wide)

Model formulas were invented for precisely this purpose. You can work around them using character strings or using bare symbol names and substitute, but it's really easier just to work with the formula.

     -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



More information about the R-help mailing list