Svy function doesn't work nested in user-defined function

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Svy function doesn't work nested in user-defined function

Sabatier, Jennifer F. (CDC/OID/NCHHSTP)
 
Hi R-help,

I posted about this late yesterday but got no response. I may have put
TMI in the original request.  Not to mention I couldn't cut and paste
yesterday because I was working R off a non-network computer while
asking for help on a network computer.


Essentially, I have this user-defined function:

test <- function(X){
                 
 
            chisq <- svychisq(~X + SEX, design=audit,
statisic="adjWald", round=4)
                       
}

test(con)



"con" is a data variable in my design object audit.

When I just run:

chisq <- svychisq(~X + SEX, design=audit, statisic="adjWald", round=4)

It works just fine.  

It's only when it's nested in the function test() that it falls apart.  

I get this error:

Error in `[.data.frame`(design$variables, , as.character(rows)) :
  undefined columns selected


How can I solve this problem?

Thanks,

Jen (obviously a new R-user, as of late 2009)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Svy function doesn't work nested in user-defined function

David Freedman 2
I'm not sure that this is the problem, but are you certain that the variable 'con' is in audit ?  You check outside the function just really tells you that X and SEX are in audit.

hth, david freedman (good to see another CDCer using R)
Reply | Threaded
Open this post in threaded view
|

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

David Winsemius
In reply to this post by Sabatier, Jennifer F. (CDC/OID/NCHHSTP)

On May 20, 2010, at 11:24 AM, Sabatier, Jennifer F. (CDC/OID/NCHHSTP)  
wrote:

>
> Hi R-help,
>
> I posted about this late yesterday but got no response. I may have put
> TMI in the original request.  Not to mention I couldn't cut and paste
> yesterday because I was working R off a non-network computer while
> asking for help on a network computer.
>
>
> Essentially, I have this user-defined function:
>
> test <- function(X){
>
>  
>             chisq <- svychisq(~X + SEX, design=audit,

Did you really have a variable name "X" inside the object that you are  
giving a temporary name of "X" to. Seems confusing if that were your  
choice.

> statisic="adjWald", round=4)
>
> }
>
> test(con)
>

I do not know the answer, but I'm not sure that anyone including  
Lumley could answer it with the proffered information:

Perhaps you should include the code used to create "audit" as well as  
the results of str() on the data argument to design when audit was  
created. I am guessing that "SEX" is not available inside the function  
and that you should have written:

test <- function(Z){
            chisq <- svychisq(~X + SEX, design=Z,
                                     statistic="adjWald", round=4)
                       
}


--
David.


>
>
> "con" is a data variable in my design object audit.
>
> When I just run:
>
> chisq <- svychisq(~X + SEX, design=audit, statisic="adjWald", round=4)
>
> It works just fine.
>
> It's only when it's nested in the function test() that it falls apart.
>
> I get this error:
>
> Error in `[.data.frame`(design$variables, , as.character(rows)) :
>  undefined columns selected
>
>
> How can I solve this problem?
>
> Thanks,
>
> Jen (obviously a new R-user, as of late 2009)
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

David Winsemius

On May 20, 2010, at 4:32 PM, David Winsemius wrote:

>
> On May 20, 2010, at 11:24 AM, Sabatier, Jennifer F. (CDC/OID/
> NCHHSTP) wrote:
>
>>
>> Hi R-help,
>>
>> I posted about this late yesterday but got no response. I may have  
>> put
>> TMI in the original request.  Not to mention I couldn't cut and paste
>> yesterday because I was working R off a non-network computer while
>> asking for help on a network computer.
>>
>>
>> Essentially, I have this user-defined function:
>>
>> test <- function(X){
>>
>>
>>           chisq <- svychisq(~X + SEX, design=audit,
>
> Did you really have a variable name "X" inside the object that you  
> are giving a temporary name of "X" to. Seems confusing if that were  
> your choice.
>
>> statisic="adjWald", round=4)
>>
>> }
>>
>> test(con)
>>
>
> I do not know the answer, but I'm not sure that anyone including  
> Lumley could answer it with the proffered information:
>
> Perhaps you should include the code used to create "audit" as well  
> as the results of str() on the data argument to design when audit  
> was created. I am guessing that "SEX" is not available inside the  
> function and that you should have written:
>
> test <- function(Z){
>           chisq <- svychisq(~X + SEX, design=Z,
>                                    statistic="adjWald", round=4)
>
> }

Oh, heck. Forgot to trim the address list. Looking at this again, I  
think I see the OP's intent better. I still do not have the answer,  
but believe the OP needs to construct a valid formula object if she  
intends to pass only a column name to her function. Perhaps something  
along the lines of:

test <- function(Z){
           fmla <- as.formula( paste(" ~ ", paste(X, "SEX", collapse=  
"+"))))
           chisq <- svychisq( fmla, design=audit,
                                    statistic="adjWald", round=4)
                       
}

test(con)


>
>
> --
> David.
>
>
>>
>>
>> "con" is a data variable in my design object audit.
>>
>> When I just run:
>>
>> chisq <- svychisq(~X + SEX, design=audit, statisic="adjWald",  
>> round=4)
>>
>> It works just fine.
>>
>> It's only when it's nested in the function test() that it falls  
>> apart.
>>
>> I get this error:
>>
>> Error in `[.data.frame`(design$variables, , as.character(rows)) :
>> undefined columns selected
>>
>>
>> How can I solve this problem?
>>
>> Thanks,
>>
>> Jen (obviously a new R-user, as of late 2009)
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

David Winsemius

On May 20, 2010, at 4:44 PM, David Winsemius wrote:

>
> On May 20, 2010, at 4:32 PM, David Winsemius wrote:
>
>>
>> On May 20, 2010, at 11:24 AM, Sabatier, Jennifer F. (CDC/OID/
>> NCHHSTP) wrote:
>>
>>>
>>> Hi R-help,
>>>
>>> I posted about this late yesterday but got no response. I may have  
>>> put
>>> TMI in the original request.  Not to mention I couldn't cut and  
>>> paste
>>> yesterday because I was working R off a non-network computer while
>>> asking for help on a network computer.
>>>
>>>
>>> Essentially, I have this user-defined function:
>>>
>>> test <- function(X){
>>>
>>>
>>>           chisq <- svychisq(~X + SEX, design=audit,
>>
>> Did you really have a variable name "X" inside the object that you  
>> are giving a temporary name of "X" to. Seems confusing if that were  
>> your choice.
>>
>>> statisic="adjWald", round=4)
>>>
>>> }
>>>
>>> test(con)
>>>
>>
>> I do not know the answer, but I'm not sure that anyone including  
>> Lumley could answer it with the proffered information:
>>
>> Perhaps you should include the code used to create "audit" as well  
>> as the results of str() on the data argument to design when audit  
>> was created. I am guessing that "SEX" is not available inside the  
>> function and that you should have written:
>>
>> test <- function(Z){
>>           chisq <- svychisq(~X + SEX, design=Z,
>>                                   statistic="adjWald", round=4)
>>
>> }
>
> Oh, heck. Forgot to trim the address list. Looking at this again, I  
> think I see the OP's intent better. I still do not have the answer,  
> but believe the OP needs to construct a valid formula object if she  
> intends to pass only a column name to her function. Perhaps  
> something along the lines of:
>
> test <- function(Z){
>          fmla <- as.formula( paste(" ~ ", paste(X, "SEX", collapse=  
> "+"))))

Er, Better might be:

fmla <- as.formula( paste(" ~ ", paste(c(Z, "SEX"), collapse= "+")))

>          chisq <- svychisq( fmla, design=audit,
>                                   statistic="adjWald", round=4)
>
> }
>
> test(con)
>
>
>>
>>
>> --
>> David.
>>
>>
>>>
>>>
>>> "con" is a data variable in my design object audit.
>>>
>>> When I just run:
>>>
>>> chisq <- svychisq(~X + SEX, design=audit, statisic="adjWald",  
>>> round=4)
>>>
>>> It works just fine.
>>>
>>> It's only when it's nested in the function test() that it falls  
>>> apart.
>>>
>>> I get this error:
>>>
>>> Error in `[.data.frame`(design$variables, , as.character(rows)) :
>>> undefined columns selected
>>>
>>>
>>> How can I solve this problem?
>>>
>>> Thanks,
>>>
>>> Jen (obviously a new R-user, as of late 2009)
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> David Winsemius, MD
> West Hartford, CT
>

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

David Winsemius

On May 20, 2010, at 4:47 PM, David Winsemius wrote:

>
> On May 20, 2010, at 4:44 PM, David Winsemius wrote:
>
>>
>> On May 20, 2010, at 4:32 PM, David Winsemius wrote:
>>
>>>
>>> On May 20, 2010, at 11:24 AM, Sabatier, Jennifer F. (CDC/OID/
>>> NCHHSTP) wrote:
>>>
>>>>
>>>> Hi R-help,
>>>>
>>>> I posted about this late yesterday but got no response. I may  
>>>> have put
>>>> TMI in the original request.  Not to mention I couldn't cut and  
>>>> paste
>>>> yesterday because I was working R off a non-network computer while
>>>> asking for help on a network computer.
>>>>
>>>>
>>>> Essentially, I have this user-defined function:
>>>>
>>>> test <- function(X){
>>>>
>>>>
>>>>         chisq <- svychisq(~X + SEX, design=audit,
>>>
>>> Did you really have a variable name "X" inside the object that you  
>>> are giving a temporary name of "X" to. Seems confusing if that  
>>> were your choice.
>>>
>>>> statisic="adjWald", round=4)
>>>>
>>>> }
>>>>
>>>> test(con)
>>>>
>>>
>>> I do not know the answer, but I'm not sure that anyone including  
>>> Lumley could answer it with the proffered information:
>>>
>>> Perhaps you should include the code used to create "audit" as well  
>>> as the results of str() on the data argument to design when audit  
>>> was created. I am guessing that "SEX" is not available inside the  
>>> function and that you should have written:
>>>
>>> test <- function(Z){
>>>         chisq <- svychisq(~X + SEX, design=Z,
>>>                                  statistic="adjWald", round=4)
>>>
>>> }
>>
>> Oh, heck. Forgot to trim the address list. Looking at this again, I  
>> think I see the OP's intent better. I still do not have the answer,  
>> but believe the OP needs to construct a valid formula object if she  
>> intends to pass only a column name to her function. Perhaps  
>> something along the lines of:
>>
>> test <- function(Z){
>>         fmla <- as.formula( paste(" ~ ", paste(X, "SEX", collapse=  
>> "+"))))
>
> Er, Better might be:
>
> fmla <- as.formula( paste(" ~ ", paste(c(Z, "SEX"), collapse= "+")))
>
>>         chisq <- svychisq( fmla, design=audit,
>>                                  statistic="adjWald", round=4)
>>
>> }
>>
>> test(con)

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.

--


>>
>>
>>>
>>>
>>> --
>>> David.
>>>
>>>
>>>>
>>>>
>>>> "con" is a data variable in my design object audit.
>>>>
>>>> When I just run:
>>>>
>>>> chisq <- svychisq(~X + SEX, design=audit, statisic="adjWald",  
>>>> round=4)
>>>>
>>>> It works just fine.
>>>>
>>>> It's only when it's nested in the function test() that it falls  
>>>> apart.
>>>>
>>>> I get this error:
>>>>
>>>> Error in `[.data.frame`(design$variables, , as.character(rows)) :
>>>> undefined columns selected
>>>>
>>>>
>>>> How can I solve this problem?
>>>>
>>>> Thanks,
>>>>
>>>> Jen (obviously a new R-user, as of late 2009)
>

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

Thomas Lumley
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
[hidden email] University of Washington, Seattle

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

David Winsemius

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
> [hidden email] University of Washington, Seattle
>

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.