我正在使用factanal
将名为efa
的BASE R函数重命名为match.call
。但是我想知道为什么仅在对公式x
使用公式时,efa
会引发错误:object 'n.obs' not found
但factanal
正常工作?
注意:将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
答案 0 :(得分:2)
我认为更通用的方法是先捕获调用,包装所有参数,然后对其求值。像这样
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中包含您要使用的值的变量的名称,并不能认为这是您当前环境中的变量。