svycontrast.Rd
Computes linear or nonlinear contrasts of estimates produced by survey
functions (or any object with coef
and vcov
methods).
svycontrast(stat, contrasts, add=FALSE, ...)
object of class svrepstat
or svystat
A vector or list of vectors of coefficients, or a call or list of calls
keep all the coefficients of the input in the output?
For future expansion
Object of class svrepstat
or svystat
If contrasts
is a list, the element names are used as
names for the returned statistics.
If an element of contrasts
is shorter than coef(stat)
and has names, the
names are used to match up the vectors and the remaining elements of
contrasts
are assumed to be zero. If the names are not legal
variable names (eg 0.1
) they must be quoted (eg "0.1"
)
If contrasts
is a "call"
or list of "call"s
, and
stat
is a svrepstat
object including replicates, the
replicates are transformed and used to compute the variance. If
stat
is a svystat
object or a svrepstat
object
without replicates, the delta-method is used to compute variances, and
the calls must use only functions that deriv
knows how
to differentiate. If the names are not legal variable names they must
be quoted with backticks (eg `0.1`
).
regTermTest
, svyglm
data(api)
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
a <- svytotal(~api00+enroll+api99, dclus1)
svycontrast(a, list(avg=c(0.5,0,0.5), diff=c(1,0,-1)))
#> contrast SE
#> avg 3874804 873276
#> diff 230363 54921
## if contrast vectors have names, zeroes may be omitted
svycontrast(a, list(avg=c(api00=0.5,api99=0.5), diff=c(api00=1,api99=-1)))
#> contrast SE
#> avg 3874804 873276
#> diff 230363 54921
## nonlinear contrasts
svycontrast(a, quote(api00/api99))
#> nlcon SE
#> contrast 1.0613 0.0062
svyratio(~api00, ~api99, dclus1)
#> Ratio estimator: svyratio.survey.design2(~api00, ~api99, dclus1)
#> Ratios=
#> api99
#> api00 1.061273
#> SEs=
#> api99
#> api00 0.006230831
## Example: standardised skewness coefficient
moments<-svymean(~I(api00^3)+I(api00^2)+I(api00), dclus1)
svycontrast(moments,
quote((`I(api00^3)`-3*`I(api00^2)`*`I(api00)`+ 3*`I(api00)`*`I(api00)`^2-`I(api00)`^3)/
(`I(api00^2)`-`I(api00)`^2)^1.5))
#> nlcon SE
#> contrast -0.014253 0.2781
## Example: geometric means
## using delta method
meanlogs <- svymean(~log(api00)+log(api99), dclus1)
svycontrast(meanlogs,
list(api00=quote(exp(`log(api00)`)), api99=quote(exp(`log(api99)`))))
#> nlcon SE
#> api00 635.33 24.008
#> api99 596.18 24.529
## using delta method
rclus1<-as.svrepdesign(dclus1)
meanlogs <- svymean(~log(api00)+log(api99), rclus1)
svycontrast(meanlogs,
list(api00=quote(exp(`log(api00)`)),
api99=quote(exp(`log(api99)`))))
#> nlcon SE
#> api00 635.33 26.863
#> api99 596.18 27.302
## why is add=TRUE useful?
(totals<-svyby(~enroll,~stype,design=dclus1,svytotal,covmat=TRUE))
#> stype enroll se
#> E E 2109717.1 631349.4
#> H H 535594.9 226716.6
#> M M 759628.1 213635.5
totals1<-svycontrast(totals, list(total=c(1,1,1)), add=TRUE)
svycontrast(totals1, list(quote(E/total), quote(H/total), quote(M/total)))
#> nlcon SE
#> [1,] 0.6196 0.0657
#> [2,] 0.1573 0.0431
#> [3,] 0.2231 0.0412
totals2<-svycontrast(totals, list(total=quote(E+H+M)), add=TRUE)
all.equal(as.matrix(totals1),as.matrix(totals2))
#> [1] TRUE
## more complicated svyby
means <- svyby(~api00+api99, ~stype+sch.wide, design=dclus1, svymean,covmat=TRUE)
svycontrast(means, quote(`E.No:api00`-`E.No:api99`))
#> nlcon SE
#> contrast -5.3333 4.1177
svycontrast(means, quote(`E.No:api00`/`E.No:api99`))
#> nlcon SE
#> contrast 0.99114 0.0062
## transforming replicates
meanlogs_r <- svymean(~log(api00)+log(api99), rclus1, return.replicates=TRUE)
svycontrast(meanlogs_r,
list(api00=quote(exp(`log(api00)`)), api99=quote(exp(`log(api99)`))))
#> nlcon SE
#> api00 635.33 27.216
#> api99 596.18 27.659