【问题标题】:Why does a substituted formula work for lm and oneway.test, but not aov?为什么替换公式适用于 lm 和 oneway.test,但不适用于 aov?
【发布时间】:2015-10-04 15:49:03
【问题描述】:
 example <- data.frame(
   var1 = c(1, 2, 3, 4, 5, 6, 7, 8),
   class = c(rep(1, 4), rep(2, 4))
 )
 example$class <- as.factor(example$class)

This question 提供了使用替代和 as.name 为 aov 创建公式的修复,但我不明白为什么该公式适用于 oneway.testlm。谁能解释一下?

 fm <- substitute(i ~ class, list(i = as.name('var1')))
 oneway.test(fm, example)

    One-way analysis of means (not assuming equal variances)

data:  var1 and class
F = 19.2, num df = 1, denom df = 6, p-value = 0.004659

 lm(fm, example)

Call:
lm(formula = fm, data = example)

Coefficients:
(Intercept)       class2  
        2.5          4.0  

 aov(fm, example)
Error in terms.default(formula, "Error", data = data) : 
  no terms component nor attribute

【问题讨论】:

    标签: r formula substitution


    【解决方案1】:

    问题在于substitute 正在返回一个未评估的调用,而不是一个公式。比较

    class(substitute(a~b))
    # [1] "call"
    class(a~b)
    # [1] "formula"
    

    如果你评估它(就像在另一个答案中所做的那样),两者都可以工作

    fm <- eval(substitute(i ~ class, list(i = as.name('var1'))))
    oneway.test(fm, example)
    aov(fm, example)
    

    您收到的错误消息来自aov() 调用的terms 函数。此函数需要对公式进行操作,而不是调用。这基本上就是发生的事情

    # ok
    terms(a~b)
    
    # doesn't work
    unf <- quote(a~b)  #same as substitute(a~b)
    terms(unf)
    # Error in terms.default(unf) : no terms component nor attribute
    
    # ok
    terms(eval(unf))
    

    【讨论】:

      【解决方案2】:

      差异的一个可能来源是fm 实际上是call 而不是formula,显然有些函数会进行转换,而其他函数则不会。

      如果你这样做:

      fm <- as.formula(fm)
      

      然后对aov 的调用将起作用。

      【讨论】:

        猜你喜欢
        • 2021-11-13
        • 2020-12-13
        • 1970-01-01
        • 1970-01-01
        • 2017-05-08
        • 1970-01-01
        • 1970-01-01
        • 2016-02-28
        • 2022-08-18
        相关资源
        最近更新 更多