[R] offlist Re: Svy function doesn't work nested in user-defined function
David Winsemius
dwinsemius at comcast.net
Fri May 21 18:52:33 CEST 2010
On May 21, 2010, at 11:48 AM, Thomas Lumley wrote:
> 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)
>
I certainly can see the reason for your preference. Much cleaner. I
also looked at the help page for update.formula as suggested by the
update help page. So, when the R interpreter encounters the "~" in the
test call, it acts in a manner something akin to encountering back-
quote or "$" and defers evaluation of "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
>
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list