[R] use of formula in survey analysis with replicated weights

Milan Bouchet-Valat nalimilan at club.fr
Fri Jun 28 19:20:35 CEST 2013


Le vendredi 28 juin 2013 à 17:44 +0200, LE TERTRE Alain a écrit :
> Hi there,
> I would like to use a formula inside a call to withReplicates in a survey analysis.
> If the initial call with formula expressed inside the function works as expected, defining the formula outside gives an error message.
> See example below, adapted from survey:withReplicates help page.
> 
> library(survey)
> library(quantreg)
> 
> data(api)
> ## one-stage cluster sample
> dclus1<-svydesign( id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
> ## convert to bootstrap
> bclus1<-as.svrepdesign( dclus1, type="bootstrap", replicates=100)
> 
> ## median regression
> withReplicates( bclus1, quote( coef( rq( api00~api99, tau=0.5, weights=.weights))))
>                theta     SE
> (Intercept) 87.78505 18.850
> api99        0.91589  0.028
> 
> # Defining formula outside
> Myformula <- as.formula( " api00~api99")
> # Rerun the same analysis
> withReplicates( bclus1, quote( coef( rq( formula= Myformula, tau=0.5, weights=.weights))))
> Erreur dans eval(expr, envir, enclos) : objet 'api00' introuvable
> 
> # I suspect the evaluation not done in the right environment. 
> #If you specify with data option in rq, the initial dataframe, formula is then correctly evaluated but .weights are not found.
> 
> withReplicates( bclus1, quote( coef( rq( formula= Myformula, tau=0.5, weights=.weights, data=apiclus1 ))))
> Erreur dans eval(expr, envir, enclos) : objet '.weights' introuvable
> 
> Any help greatly appreciated
Here is a workaround:
Myformula <- "api00 ~ api99"

withReplicates(bclus1, quote(coef(rq(formula(Myformula), tau=0.5, weights=.weights))))

This solution makes sure the formula uses the environment where the
weights are available. If you call as.formula() from outside the
function, it will use the global environment. If you pass a character
string, it will be converted to a formula object deep in a function and
will thus use an environment where the weights are not be available
either.

Note that the same problem happens when using lm().


Regards

> O__ ---- Alain Le Tertre
>  c/ /'_ --- Institut de Veille Sanitaire (InVS)/ Département Santé Environnement
> (*) \(*) -- Responsable de l'unité Statistiques & Outils
> ~~~~~~~~~~ - 12 rue du val d'Osne
> 94415 Saint Maurice cedex FRANCE
> Voice: 33 1 41 79 67 62 Fax: 33 1 41 79 67 68
> email: a.letertre at invs.sante.fr
>  
> 
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list