有比字符串操作更好的替代方法来以编程方式构建公式吗? [英] Is there a better alternative than string manipulation to programmatically build formulas?

查看:45
本文介绍了有比字符串操作更好的替代方法来以编程方式构建公式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人的函数似乎都使用公式对象,然后对内部深处的对象执行暗法术,我很嫉妒.

Everyone else's functions seem to take formula objects and then do dark magic to them somewhere deep inside and I'm jealous.

我正在编写一个适合多个模型的函数.这些模型的部分公式保持不变,并且一部分从一个模型更改为另一个模型.笨拙的方法是让用户将公式部分作为字符串输入,对其进行一些字符处理,然后使用as.formula.

I'm writing a function that fits multiple models. Parts of the formulas for these models remain the same and part change from one model to the next. The clumsy way would be to have the user input the formula parts as character strings, do some character manipulation on them, and then use as.formula.

但是在我走这条路线之前,我只想确保自己没有忽略一些更简洁的方法,该方法允许函数接受标准R格式的公式(例如,从其他使用公式的对象中提取的公式) ).

But before I go that route, I just want to make sure that I'm not overlooking some cleaner way of doing it that would allow the function to accept formulas in the standard R format (e.g. extracted from other formula-using objects).

我想要类似...

> LHS <- y~1; RHS <- ~a+b; c(LHS,RHS);
y ~ a + b
> RHS2 <- ~c;
> c(LHS, RHS, RHS2);
y ~ a + b + c

或...

> LHS + RHS;
y ~ a + b
> LHS + RHS + RHS2;
y ~ a + b + c

...但是很遗憾,这两种语法都不起作用.有人知道是否有事吗?谢谢.

...but unfortunately neither syntax works. Does anybody know if there is something that does? Thanks.

推荐答案

reformulate将做您想要的事情.

reformulate(termlabels = c('x','z'), response = 'y')
## y ~ x + z

或者没有拦截

reformulate(termlabels = c('x','z'), response = 'y', intercept = FALSE)
## y ~ x + z - 1

请注意,您不能构造具有多个reponses的公式,例如x+y ~z+b

Note that you cannot construct formulae with multiple reponses such as x+y ~z+b

reformulate(termlabels = c('x','y'), response = c('z','b'))
z ~ x + y

要从现有的formula中提取字词(根据您的示例)

To extract the terms from an existing formula (given your example)

attr(terms(RHS), 'term.labels')
## [1] "a" "b"

获得响应略有不同,这是一种简单的方法(对于单个变量响应).

To get the response is slightly different, a simple approach (for a single variable response).

as.character(LHS)[2]
## [1] 'y'


combine_formula <- function(LHS, RHS){
  .terms <- lapply(RHS, terms)
  new_terms <- unique(unlist(lapply(.terms, attr, which = 'term.labels')))
  response <- as.character(LHS)[2]

  reformulate(new_terms, response)


}


combine_formula(LHS, list(RHS, RHS2))

## y ~ a + b + c
## <environment: 0x577fb908>

我认为将响应指定为字符向量会更明智,例如

I think it would be more sensible to specify the response as a character vector, something like

combine_formula2 <- function(response, RHS, intercept = TRUE){
  .terms <- lapply(RHS, terms)
  new_terms <- unique(unlist(lapply(.terms, attr, which = 'term.labels')))
  response <- as.character(LHS)[2]

  reformulate(new_terms, response, intercept)


}
combine_formula2('y', list(RHS, RHS2))

您还可以定义+运算符以使用公式(更新为公式对象设置新方法)

you could also define a + operator to work with formulae (update setting an new method for formula objects)

`+.formula` <- function(e1,e2){
  .terms <- lapply(c(e1,e2), terms)
  reformulate(unique(unlist(lapply(.terms, attr, which = 'term.labels'))))
}

RHS + RHS2
## ~a + b + c

您也可以明智地使用.来使用update.formula

You can also use update.formula using . judiciously

 update(~a+b, y ~ .)
 ##  y~a+b

这篇关于有比字符串操作更好的替代方法来以编程方式构建公式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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