[R] problem with svyglm()

Thomas Lumley tlumley at u.washington.edu
Mon Apr 6 11:44:44 CEST 2009


On Thu, 2 Apr 2009, Federico Calboli wrote:

> Hello,
>
> I'm trying to use the function svyglm in the library survey.
>
> I create a data survey object:
>
> data_svy<- svydesign(id=~PSU, strata=~sample_domain,
> weights=~sample_weight, data=data, nest=TRUE)
>
> and I try to use svyglm() with little success:
>
> R<-svyglm(data_svy[,4]~(data_svy[,iCol]==listModality[[iVar]]
> [iMod]),design=data_svy, family=binomial(link="logit")
>
> Error in svyglm.survey.design(data_svy[, 4] ~ (data_svy[, iCol] ==
> listModality[[iVar]][iMod]),  :
>         all variables must be in design= argument

I don't know what you are trying to do here, but svyglm() wants a formula 
where the variables are all in the design object. Also, data_svy[,iCol] 
won't do what you probably expect, since data_svy isn't a data frame.


> Puzzlingly the following code works:
>
> R<-glm(data[,4]~(data[,iCol]==listModality[[iVar]][iMod]),
> family=binomial(link="logit")
>

There are two differences here. The first is that glm() doesn't require 
variables to be in the data= argument, because it doesn't have to carry 
around the design meta-data.  The second is that data[,4] is a 
well-defined variable, whereas data_svy[,4] is meaningless.

It's hard to guess exactly what you are trying to do here. If you are 
trying to do a loop over regression models then you have to do something 
like

for(v in vnames) {
   models[[v]] <- eval(bquote( svyglm(y~x+.(as.name(v)), design=data_svy)))
}

 	-thomas




More information about the R-help mailing list