I have a question regarding calling variables in svycontrast()
function with survey
package.
I'm trying to automate some contrast against a fixed parameter. I can do that no problem like this:
library(survey)
data(api)
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
diff <- svyby(~enroll, by = ~cnum, dclus1, na.rm.all = FALSE, svymean, covmat = T, vartype = "se")
parameter <- 550
svycontrast(diff, quote(`1` - parameter))
# nlcon SE
# contrast 2.8182 0
However, I have been for hours trying to figure out how to call that rowname
`1`, but with different approaches I keep getting mostly the following error message:
row <- quote(1)
svycontrast(diff, quote(row - parameter))
Error in row - parameter : non-numeric argument to binary operator
Any help would be very much appreciated.
The most natural way to pass arguments from the command line is to use the function commandArgs. This function scans the arguments which have been supplied when the current R session was invoked.
It happens quite often that we have an expression, which is stored as character class in R. If we want to evaluate such an expression, we need to convert our data object from character class to expression class. I’ll show an example.
For surveys this means the data and the survey meta- data. The way to specify variables from a data frame or object in R is a formula ~a + b + I(c < 5*d) The survey packagealwaysuses formulas to specify variables in a survey data set. Basic estimation ideas
R Survey package Version 3.21-1 is current, containing approximately 11000 lines of interpreted R code. Version 2.3 was published in Journal of Statistical Software.
You could use substitute
combined with as.symbol
:
row <- 1
substitute(row - parameter, list(row = as.symbol(row)))
# `1` - parameter
svycontrast(diff, substitute(row - parameter, list(row = as.symbol(row))))
# nlcon SE
#contrast 2.8182 0
I think you can use bquote
instead of quote
here
> row <- 1
> svycontrast(diff, bquote(.(as.name(row)) - parameter))
nlcon SE
contrast 2.8182 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With