通过公式在R中运行? [英] Pass formula to function in R?

查看:327
本文介绍了通过公式在R中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何与此有关的帮助将非常感激。我正在使用Lumley调查软件包,并试图简化我的代码,但遇到了一些小问题。



包中的svymean函数在我的代码中调用如下,第一个参数是一个公式,指明我想要哪些变量,第二个参数是该数据集:

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

我试图创建一个函数,分类变量的平均值(比例)和标准错误,所以我做了以下函数:

  stats<  - 函数(repstat,num){
estmean < - as.numeric(round(100 * repstat [num],digits = 0))
estse < - round(100 * sqrt(attributes(repstat)
返回(list(mean = estmean,se = estse))
}

这是有效的,所以当我拉出第一个类别的均值和标准时,例如,我使用:

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

我想要做的就是将其简化为更短的内容,也许我只需要编写它:

  stats(FraSvy,hq_ehla,1)$ mean 

或者类似的东西。问题是,我无法弄清楚如何使用变量名将一个公式传递给一个函数。

解决方案

您可以使用 reconform 来构造你的公式,并在你的函数中调用 svymean 。使用 ... na.rm 或其他参数传递给 svymean

  stats<  - 函数(terms,data,num,...){
.formula < - reconulate(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

查看这个答案以获得关于编程创建更多细节的信息公式对象

或者,您可以在函数中传递一个公式对象。

  
repstat< - svymean(公式,数据,...)
estmean< - as.numeric(round(100 * repstat [num],digits = 0))
estse& lt; - round(100 * sqrt(属性(repstat)$ var [num,num]),digits = 1)
return(list(mean = estmean,se = estse))
}


stats2(data = FraSvy,公式=〜hq_ehla,1,na.rm = TRUE)$ mean


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.

解决方案

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

这篇关于通过公式在R中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆