1

I'd like to build a loop that takes appropriate subsamples from a fit object (why? long explanation, very boring). To take these subsamples I'm using ?subset.survey.design but I don't want to typethis manually, I want to do it in a loop (an incomplete version is given below where the index l is fixed at 1 for demo purposes). I'm struggling with the final bit (how to create my subset out of a command in a string).

    library(survey)
    data(api)
    dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
    fit<-(svyglm(api00 ~ ell, design = dstrat))
    group="sch.wide"
#######THE ABOVE IS ALL THE INFORMATION THE AUTOMATED SYNTAX BELOW WILL USE   

    cap<-capture.output(fit$survey.design)
    cap<-paste0(cap[2:length(cap)],collapse="")

    l=1
    levs<-unique(dstrat$variables[group])[[1]]
    ll<-as.character(levs[l])
    (paste0("subset(",cap,",",group,"=='",ll,"')",collapse=""))

The last statement (paste0....) should have all I need but I want to create an actual design item out of it i.e. something like

SubD<-eval((paste0("subset(",cap,",",group,"=='",ll,"')",collapse=""))) But this unfortunately doesn't create a svydesign object, but there must be a way to evaluate the string, and create SubD which then allows me to run

svyglm(api00 ~ ell, design =SubD)

Can anyone help?

Thank you

Mercelo
  • 231
  • 1
  • 5
  • 14
  • 1
    you need to read [this](http://stackoverflow.com/questions/17407852/how-to-pass-an-expression-through-a-function-for-the-subset-function-to-evaluate) :) – Anthony Damico Jan 07 '14 at 14:18

0 Answers0