Pass formula to function in R?
Asked Answered
J

2

11

Any help with this would be really appreciated. I am using the Lumley survey package and am trying to simplify my code, but have hit a slight snag.

The svymean function from the package is called as follows in my code, where the first argument is a formula indicating which variables I want, and the second argument is that dataset:

svymean(~hq_ehla, FraSvy, na.rm=TRUE)

I'm trying to create a function that will pull out the mean (proportions) and standard errors for categorical variables, so I've made the following function:

stats <- function(repstat, num) {
    estmean <- as.numeric(round(100 * repstat[num], digits=0))
    estse <- round(100 * sqrt(attributes(repstat)$var[num,num]), digits=1)
    return(list(mean=estmean, se=estse))
}

This works, so when I'm pulling out the mean and se of my first category, for example, I use:

stats(svymean(~hq_ehla, FraSvy, na.rm=TRUE), 1)$mean
stats(svymean(~hq_ehla, FraSvy, na.rm=TRUE), 1)$se

What I'd like to be able to do is simplify this to something much shorter, where maybe I'd only have to write:

stats(FraSvy, "hq_ehla", 1)$mean

Or something like that. Problem is that I can't figure out how to pass a formula to a function using a variable name.

Jaunita answered 3/2, 2013 at 23:49 Comment(0)
C
14

You can use reformulate to construct your formula and call svymean within your function. Use ... to pass na.rm or other arguments to svymean

stats <- function(terms, data,  num, ...) {
  .formula <- reformulate(terms)
  repstat <- svymean(.formula, data, ...)
  estmean <- as.numeric(round(100 * repstat[num], digits=0))
  estse <- round(100 * sqrt(attributes(repstat)$var[num,num]), digits=1)
  return(list(mean=estmean, se=estse))
}

stats(data = FraSvy, terms = "hq_ehla", 1, na.rm = TRUE)$mean

Have a look at this answer for more details on programmitically create formula objects

Or, you could pass a formula object within the function.

stats2 <- function(formula, data,  num, ...) {

  repstat <- svymean(formula, data, ...)
  estmean <- as.numeric(round(100 * repstat[num], digits=0))
  estse <- round(100 * sqrt(attributes(repstat)$var[num,num]), digits=1)
  return(list(mean=estmean, se=estse))
}


stats2(data = FraSvy, formula = ~hq_ehla, 1, na.rm = TRUE)$mean
Claypool answered 3/2, 2013 at 23:59 Comment(0)
S
0

the coef and SE functions might make your life easier..

# construct a function that takes the equation part of svymean as a string
# instead of as a formula.  everything else gets passed in the same
# as seen by the `...`
fun <- function( var , ... ) svymean( reformulate( var ) , ... )

# test it out.
result <- fun( "hq_ehla" , FraSvy , na.rm = TRUE )

# print the results to the screen
result

# also your components
coef( result )
SE( result )

# and round it
round( 100 * coef( result ) )
round( 100 * SE( result ) )
Shakitashako answered 4/2, 2013 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.