des.addvars | R Documentation |
Modifies an analytic object by adding new variables to it.
des.addvars(design, ...)
design |
Object of class |
... |
|
This function adds to the data frame contained in design
the new variables defined by the tag = expr
arguments. A tag
can be specified either by means of an identifier or by a character string; expr
can be any expression that it makes sense to evaluate in the design
environment.
For each argument tag = expr
bound to the formal argument ...
the added column will have name given by the tag
value and values obtained by evaluating the expr
expression on design
. Any input expression not supplied with a tag
will be ignored and will therefore have no effect on the des.addvars
return value.
Variables to be added to the input object have to be new: namely it is not possible to use des.addvars
to modify the values in a pre-existing design
column. This an intentional feature meant to safeguard the integrity of the relations between survey data and sampling design metadata stored in design
.
An object of the same class of design
, containing new variables but supplied with exactly the same metadata.
Diego Zardetto
Zardetto, D. (2015) “ReGenesees: an Advanced R System for Calibration, Estimation and Sampling Error Assessment in Complex Sample Surveys”. Journal of Official Statistics, 31(2), 177-203. doi: https://doi.org/10.1515/jos-2015-0013.
e.svydesign
to bind survey data and sampling design metadata, e.calibrate
for calibrating weights.
data(data.examples)
# Creation of an analytic object:
des<-e.svydesign(data=example,ids=~towcod+famcod,strata=~SUPERSTRATUM,
weights=~weight)
# Adding the new 'ones' variable to estimate the number
# of final units in the population:
des<-des.addvars(des,ones=1)
svystatTM(des,~ones)
# Recoding a qualitative variable:
des<-des.addvars(des,agerange=factor(ifelse(age5c==1,
"young","not-young")))
svystatTM(des,~agerange,estimator="Mean")
svystatTM(des,~income,~agerange,estimator="Mean",conf.int=TRUE)
# Algebraic operations on numeric variables:
des<-des.addvars(des,z2=z^2)
svystatTM(des,~z2,estimator="Mean")
# A more interesting example: estimating the
# percentage of population with income below
# the poverty threshold (defined as 0.6 times
# the median income for the whole population):
Median.Income <- coef(svystatQ(des, ~income,probs=0.5))
Median.Income
des <- des.addvars(des,
status = factor(
ifelse(income < (0.6 * Median.Income),
"poor", "non-poor")
)
)
svystatTM(des,~status,estimator="Mean")
# Mean income for poor and non-poor:
svystatTM(des,~income,~status,estimator="Mean")
### NOTE: Procedure above yields *correct point estimates* of the share of poor
### population and their average income, while *variance estimation is
### approximated* since we neglected the sampling variability of the
### estimated poverty threshold.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.