正确附加到公式的方法,其中要附加的公式和内容均为参数 [英] Proper method to append to a formula where both formula and stuff to be appended are arguments

查看:95
本文介绍了正确附加到公式的方法,其中要附加的公式和内容均为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SO上,我已经做了大量的阅读,并且了解到我通常应该避免将formula objects作为字符串进行操作,但是我还没有找到如何安全地做到这一点的方法:

I've done a fair amount of reading here on SO and learned that I should generally avoid manipulation of formula objects as strings, but I haven't quite found how to do this in a safe manner:

tf <- function(formula = NULL, data = NULL, groups = NULL, ...) {
# Arguments are unquoted and in the typical form for lm etc
# Do some plotting with lattice using formula & groups (works, not shown)
# Append 'groups' to 'formula':
# Change y ~ x as passed in argument 'formula' to
# y ~ x * gr where gr is the argument 'groups' with
# scoping so it will be understood by aov
new_formula <- y ~ x * gr
# Now do some anova (could do if formula were right)
model <- aov(formula = new_formula, data = data)
# And print the aov table on the plot (can do)
print(summary(model)) # this will do for testing
}

也许我最接近的是使用reformulate,但这只在RHS上给出了+,而不是*.我想使用这样的功能:

Perhaps the closest I came was to use reformulate but that only gives + on the RHS, not *. I want to use the function like this:

p <- tf(carat ~ color, groups = clarity, data = diamonds)

,并具有克拉〜颜色*净度的aov结果.预先感谢.

and have the aov results for carat ~ color * clarity. Thanks in Advance.

解决方案

这是一个基于@Aaron的评论的工作版本,它演示了正在发生的事情:

Here is a working version based on @Aaron's comment which demonstrates what's happening:

tf <- function(formula = NULL, data = NULL, groups = NULL, ...) {
print(deparse(substitute(groups)))
f <- paste(".~.*", deparse(substitute(groups)))
new_formula <- update.formula(formula, f)
print(new_formula)
model <- aov(formula = new_formula, data = data)
print(summary(model))
}

推荐答案

我认为update.formula可以解决您的问题,但是我在函数调用中遇到了麻烦.它将按照我在下面的代码进行工作,但是请注意,我将列传递给组,而不是变量名.然后,将该列添加到函数数据集中,然后进行更新.

I think update.formula can solve your problem, but I've had trouble with update within function calls. It will work as I've coded it below, but note that I'm passing the column to group, not the variable name. You then add that column to the function dataset, then update works.

我也不知道它是否完全按照第二个方程式运行,但是请查看帮助文件中的update.formula并弄乱它.

I also don't know if it's doing exactly what you want in the second equation, but take a look at the help file for update.formula and mess around with it a bit.

http://stat. ethz.ch/R-manual/R-devel/library/stats/html/update.formula.html

tf <- function(formula,groups,d){
  d$groups=groups
  newForm = update(formula,~.*groups)
  mod = lm(newForm,data=d)
}

dat  = data.frame(carat=rnorm(10,0,1),color=rnorm(10,0,1),color2=rnorm(10,0,1),clarity=rnorm(10,0,1))
m = tf(carat~color,dat$clarity,d=dat)
m2 = tf(carat~color+color2,dat$clarity,d=dat)

tf2 <- function(formula, group, d) {
  f <- paste(".~.*", deparse(substitute(group)))
  newForm <- update.formula(formula, f)
  lm(newForm, data=d)
}
mA = tf2(carat~color,clarity,d=dat)
m2A = tf2(carat~color+color2,clarity,d=dat)

正如@Aaron指出的那样,是deparsesubstitute解决了我的问题:我在代码示例中添加了tf2作为更好的选择,以便您了解两者的工作原理.

As @Aaron pointed out, it's deparse and substitute that solve my problem: I've added tf2 as the better option to the code example so you can see how both work.

这篇关于正确附加到公式的方法,其中要附加的公式和内容均为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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