Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass expression as argument in R Survey package

Tags:

r

survey

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.

like image 263
David Jorquera Avatar asked Jun 02 '21 20:06

David Jorquera


People also ask

How to pass arguments from the command line in R?

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.

How to evaluate an expression stored as character class in R?

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.

How to estimate survey data in R?

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

What is the current version of the R survey package?

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.


Video Answer


2 Answers

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
like image 59
Waldi Avatar answered Oct 12 '22 23:10

Waldi


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
like image 45
ThomasIsCoding Avatar answered Oct 12 '22 21:10

ThomasIsCoding