1## 2## This is how NCHS does it: postStratify to a table where proportions for by= are specified and then are applied within each cell of over= 3## 4svystandardize<-function(design, by, over=~1, population, excluding.missing=NULL){ 5 6 if (!is.null(excluding.missing)){ 7 mf<-model.frame(excluding.missing, model.frame(design),na.action=na.omit) 8 naa<-attr(mf,"na.action") 9 if(!is.null(naa)) design<-design[-naa,] 10 } 11 12 if(is.data.frame(population)) population<-population$Freq 13 14 if (isTRUE(all.equal(over,~1))){ 15 freemargins<-data.frame(`_one_`=1, Freq=sum(weights(design, "sampling"))) 16 } else { 17 freemargins<-as.data.frame(svytable(over, design)) 18 } 19 fixedmargins<-as.data.frame(svytable(by,design)) 20 fixedmargins$Freq<-as.vector(population)/sum(as.vector(population)) 21 combined<-make.formula(c(attr(terms(by),"term.labels"), attr(terms(over),"term.labels"))) 22 allmargins<-as.data.frame(svytable(combined,design)) 23 allmargins$Freq<-as.vector(outer(fixedmargins$Freq, freemargins$Freq)) 24 25 design<-postStratify(design, combined, allmargins,partial=TRUE) 26 design$call<-sys.call() 27 design 28} 29