[R] svyby and make.formula
Muhuri, Pradip (SAMHSA/CBHSQ)
Pradip.Muhuri at samhsa.hhs.gov
Wed Oct 3 01:51:23 CEST 2012
Hello,
Although my R code for the svymean () and svyquantile () functions works fine, I am stuck with the svyby () and make.formula () functions. I got the following error messages.
- Error: object of type 'closure' is not subsettable # svyby ()
- Error in xx[[1]] : subscript out of bounds # make.formula ()
A reproducible example is appended below.
I would appreciate if someone could help me.
Thank you in advance.
Pradip Muhuri
####Below is a reproducible example ##########################################
setwd ("E:/RDATA")
library (survey)
xd1 <-
"dthage ypll_ler ypll_75 xspd2 psu stratum wt8 mortstat
NA NA NA 2 1 1 1683.7387 0
NA NA NA 2 1 1 640.8950 0
NA NA NA 2 1 1 714.0662 0
NA NA NA 2 1 1 714.0662 0
NA NA NA 2 1 1 530.5263 0
NA NA NA 2 1 1 2205.2863 0
NA NA NA 2 1 339 1683.7387 0
NA NA NA 2 1 339 640.8950 0
NA NA NA 2 1 339 714.0662 0
NA NA NA 2 1 339 714.0662 0
NA NA NA 2 1 339 530.5263 0
NA NA NA 2 1 339 2205.2863 0
78 8.817926 0 2 2 1 592.3100 1
80 9.291881 0 2 2 1 1014.7387 1
87 5.001076 0 2 2 1 853.4763 1
87 5.001076 0 2 2 1 505.1475 1
88 5.510514 0 2 2 1 1429.5963 1
78 8.817926 0 2 2 339 592.3100 1
80 9.291881 0 2 2 339 1014.7387 1
87 5.001076 0 2 2 339 853.4763 1
87 5.001076 0 2 2 339 505.1475 1
88 5.510514 0 2 2 339 1429.5963 1
78 8.817926 0 2 2 339 592.3100 1
80 9.291881 0 2 2 339 1014.7387 1
87 5.001076 0 2 2 339 853.4763 1
87 5.001076 0 2 2 339 505.1475 1
88 5.510514 0 2 2 339 1429.5963 1"
newdata <- read.table (textConnection(xd1), header=TRUE, as.is=TRUE)
dim (newdata)
# make the grouping variable (xspd)2
newdata$xspd2 <- factor(newdata$xspd2,levels=c (1,2),labels=c('SPD', 'No SPD'), ordered=TRUE)
nhis <- svydesign (id=~psu,strat=~stratum, weights=~wt8, data=newdata, nest=TRUE)
# mean age at death - nationwide
svymean( ~dthage, data=nhis , subset (nhis, mortstat==1))
# mean by SPD status
svyby(~dthage, ~xspd2 , design=nhis, svymean )
#percentile
svyquantile(~dthage, data = nhis , subset (nhis, mortstat==1), c( 0 , .25 , .5 , .75 , 1 ) )
# percentile by SPD status
svyby(~dthage, ~xspd2, desin=nhis, svyquantile, c( 0 , .25 , .5 , .75 , 1 ), keep.var = F)
# mean for each of the 3 variables
vars <- names(nhis) %in% c("dthage", "ypll_ler", "ypl_75")
vars
svymean(make.formula(vars),nhis,subset (nhis, mortstat==1), na.rm=TRUE)
#############################################
Pradip K. Muhuri
Statistician
Substance Abuse & Mental Health Services Administration
The Center for Behavioral Health Statistics and Quality
Division of Population Surveys
1 Choke Cherry Road, Room 2-1071
Rockville, MD 20857
Tel: 240-276-1070
Fax: 240-276-1260
e-mail: Pradip.Muhuri at samhsa.hhs.gov<mailto:Pradip.Muhuri at samhsa.hhs.gov>
The Center for Behavioral Health Statistics and Quality your feedback. Please click on the following link to complete a brief customer survey: http://cbhsqsurvey.samhsa.gov
More information about the R-help
mailing list