[R] svyby and make.formula

Muhuri, Pradip (SAMHSA/CBHSQ) Pradip.Muhuri at samhsa.hhs.gov
Wed Oct 3 06:14:11 CEST 2012


Dear Anthony,

Thank you very much for helping me resolve the issues.  I now got all the results, which I intended to generate.

Pradip Muhuri

________________________________________
From: Anthony Damico [ajdamico at gmail.com]
Sent: Tuesday, October 02, 2012 9:50 PM
To: Muhuri, Pradip (SAMHSA/CBHSQ)
Cc: R help
Subject: Re: svyby and make.formula

please double-check that you've got all of your parameters correct by typing ?svymean ?svyby  and ?make.formula before you send questions to r-help  :)


# you spelled design wrong and probably need to throw out your NA values.  try this

# percentile by SPD status
svyby(~dthage, ~xspd2, design=nhis, svyquantile,  c( 0 , .25 , .5 , .75 , 1 ),  keep.var = F, na.rm = TRUE)



# mean for each of the 3 variables

# this returns a logical vector, but make.formula requires a character vector
vars <- names(nhis) %in% c("dthage", "ypll_ler", "ypl_75")
vars
svymean(make.formula(vars),nhis,subset (nhis, mortstat==1), na.rm=TRUE)


# create a character vector instead

# note you also spelled the third variable wrong-- it will break unless you correct that
vars <- c("dthage", "ypll_ler", "ypll_75")

# this statement has two survey design parameters, which won't work.  which one do you want to use?
svymean(make.formula(vars),nhis,subset (nhis, mortstat==1), na.rm=TRUE)

# pick one
svymean(make.formula(vars),nhis, na.rm=TRUE)
svymean(make.formula(vars),subset(nhis, mortstat==1), na.rm=TRUE)
# all of the variables in vars are NA whenever mortstat isn't 1, so they give the same results



On Tue, Oct 2, 2012 at 7:51 PM, Muhuri, Pradip (SAMHSA/CBHSQ) <Pradip.Muhuri at samhsa.hhs.gov<mailto:Pradip.Muhuri at samhsa.hhs.gov>> wrote:

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<tel:339%20%C2%A0640.8950>        0
     NA         NA        NA     2   1      339  714.0662<tel:339%20%C2%A0714.0662>        0
     NA         NA        NA     2   1      339  714.0662<tel:339%20%C2%A0714.0662>        0
     NA         NA        NA     2   1      339  530.5263<tel:339%20%C2%A0530.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<tel:339%20%C2%A0592.3100>        1
     80    9.291881       0      2   2     339 1014.7387        1
     87    5.001076       0      2   2     339  853.4763<tel:339%20%C2%A0853.4763>        1
     87    5.001076       0      2   2     339  505.1475<tel:339%20%C2%A0505.1475>        1
     88    5.510514       0      2   2     339 1429.5963        1
     78    8.817926       0      2   2     339  592.3100<tel:339%20%C2%A0592.3100>        1
     80    9.291881       0      2   2     339 1014.7387        1
     87    5.001076       0      2   2     339  853.4763<tel:339%20%C2%A0853.4763>        1
     87    5.001076       0      2   2     339  505.1475<tel:339%20%C2%A0505.1475>        1
     88    5.510514       0      2   2     339 1429.5963        1"
newdata <- read.table (textConnection(xd1), header=TRUE, as.is<http://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<tel:240-276-1070>
Fax: 240-276-1260<tel:240-276-1260>
e-mail: Pradip.Muhuri at samhsa.hhs.gov<mailto:Pradip.Muhuri at samhsa.hhs.gov><mailto: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