On Thu, 2 Sep 2004, Richard Valliant wrote:
> Is there a way to use one of the functions in the survey package to get
> a table of estimated percentages (or proportions) and the standard error
> for each? For example, suppose that AGECODE AND SEX are two factors
> with 5 and 2 levels.
>
> The 5x2 AGECODE x SEX table would have the estimated percentage of
> persons in each cell,
>
The computations can be done easily enough with svymean or svrepmean, but
the layout isn't quite what you want
Example:
library(survey)
data(api)
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
> svymean(~interaction(stype,comp.imp),design=dclus1)
mean SE
interaction(stype, comp.imp)E.No 0.174863 0.0260
interaction(stype, comp.imp)H.No 0.038251 0.0161
interaction(stype, comp.imp)M.No 0.060109 0.0246
interaction(stype, comp.imp)E.Yes 0.612022 0.0417
interaction(stype, comp.imp)H.Yes 0.038251 0.0161
interaction(stype, comp.imp)M.Yes 0.076503 0.0217
Turning this into a 3x2 table with a mean and SE in each cell would take a
bit of work since R doesn't have a very general table layout system.
One approach is
ftable.svystat<-function(x,rownames){
m<-cbind(coef(x),sqrt(diag(vcov(x))))
if (is.null(rownames))
return(as.table(m))
rowdim<-sapply(rownames,length)
mm<-array(m,dim=c(rowdim,NCOL(m)),
dimnames=c(as.list(rownames),
list(c("coef","SE"))))
ftable(mm,row.vars=length(rowdim)+0:1)
}
which gives
a <- svymean(~interaction(stype,comp.imp),design=dclus1)
b <-
ftable(a,list(c("E","H","M"),c("No","Yes")))
round(b*100,1)
E H M
No coef 17.5 3.8 6.0
SE 2.6 1.6 2.5
Yes coef 61.2 3.8 7.7
SE 4.2 1.6 2.2
or with named dimnames
>
b<-ftable(a,list(stype=c("E","H","M"),comp.imp=c("No","Yes")))
> round(b*100,1)
stype E H M
comp.imp
No coef 17.5 3.8 6.0
SE 2.6 1.6 2.5
Yes coef 61.2 3.8 7.7
SE 4.2 1.6 2.2
-thomas