【问题标题】:why a renamed function doesn't work like the original function in R为什么重命名的函数不像 R 中的原始函数那样工作
【发布时间】:2019-09-25 20:38:24
【问题描述】:

我正在使用 match.call 将名为 factanal 的 BASE R 函数重命名为 efa。但我想知道为什么只有在使用参数公式xefa 时会引发错误:object 'n.obs' not foundfactanal 工作正常?

注意:data.frame 用于x 时,efa 工作正常。

efa <- function(x, factors, data = NULL, covmat = NULL, n.obs = NA,
            subset, na.action, start = NULL, center = FALSE,
            scores = c("none", "regression", "Bartlett"),
            rotation = "varimax", control = NULL, ...)
 {

 fit <- factanal(x, factors, data = data, covmat, n.obs = n.obs,
              subset, na.action, start = start,
              scores = scores,
              rotation = rotation, control = control, ...)

 fit$call <- match.call(expand.dots = FALSE)

 return(fit)
}

# Example of use:

v1 <- c(1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,4,5,6)
v2 <- c(1,2,1,1,1,1,2,1,2,1,3,4,3,3,3,4,6,5)
v3 <- c(3,3,3,3,3,1,1,1,1,1,1,1,1,1,1,5,4,6)

factanal(~v1+v2+v3, factors = 1, data = data.frame(v1, v2, v3)) # Works fine
     efa(~v1+v2+v3, factors = 1, data = data.frame(v1, v2, v3)) # Error: object 'n.obs' not found

     efa(data.frame(v1, v2, v3), factors = 1) # Works fine

【问题讨论】:

    标签: r function dataframe formula


    【解决方案1】:

    我认为更常见的方法是先捕获调用,包装任何参数,然后评估它。像这样的

    efa <- function(x, factors, data = NULL, covmat = NULL, n.obs = NA,
                    subset, na.action, start = NULL, center = FALSE,
                    scores = c("none", "regression", "Bartlett"),
                    rotation = "varimax", control = NULL, ...)
    {
    
      cc <- match.call(expand.dots = FALSE)
      cc[[1]] <- quote(factanal)
      fit <- eval.parent(cc)
      fit$call <- match.call(expand.dots = FALSE)
    
      return(fit)
    }
    

    我们可以测试

    efa(~v1+v2+v3, factors = 1, data = data.frame(v1, v2, v3))
    # Call:
    # efa(x = ~v1 + v2 + v3, factors = 1, data = data.frame(v1, v2,     v3))
    # 
    # Uniquenesses:
    #    v1    v2    v3 
    # 0.005 0.114 0.739 
    # 
    # Loadings:
    #    Factor1
    # v1 0.998  
    # v2 0.941  
    # v3 0.511  
    # 
    #                Factor1
    # SS loadings      2.143
    # Proportion Var   0.714
    # 
    # The degrees of freedom for the model is 0 and the fit was 0.0609 
    

    否则此函数无法正常工作的原因是,当您将值传递给n.obs 时,它假定这是您的 data.frame 中包含您希望使用的值的变量的名称,它确实如此不要假设这是您当前环境中的变量。

    【讨论】:

    • @joran。对,和model.frame有关。如果您有dd &lt;- data.frame(x=1:5, y=1:5, n=1:5); model.frame(y~x, dd, n.obs=n),它将在您的输出中添加(n.obs) 列。是的,它也与惰性求值有关,因为它获取变量名,而不是当前变量值。所以它更多地与... om model.frame 函数有关。也许我只是没有解释清楚。
    • @joran。这里有帮助的部分原因是使用match.call,因此如果您最初没有传递参数,它也不会传递给model.frame。因此,使用subset= 可以允许subset=x&gt;3 之类的东西,但这是model.frame 的特殊参数,并且没有处理...
    • @joran 另一点是尽管 NA 是n.obs 的默认值,但传递该值也会触发错误。 factanal(~v1+v2+v3, factors = 1, data = data.frame(v1, v2, v3), n.obs=NA)。在这种情况下,它真的必须不通过。
    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多