I don't know if I'm being an idiot, but I'm trying to pass arguments to the lavaan cfa() function using do.call(). I've done this successfully with other functions but in this case the arguments are mixed - i.e. the first is a character string, the second a data.frame, and the third a character string again. Take this simple example:
require(lavaan)
#simulating data
t <- rnorm(100,0,.8)
y1 <- t + rnorm(100,0,.2)
y2 <- t + rnorm(100,0,.2)
y3 <- t + rnorm(100,0,.2)
g <- c(0,1)
data <- data.frame(y1,y2,y3,g)
#setting up the lavaan model
model <- 'f1 =~ y1 + y2 + y3'
Now simply using these arguments works fine
cfa(model,data,group='g')
But when I try to use the do.call()
args <- list(model=model,data=data,group='g')
do.call(cfa,args)
I get this error message:
Error in as.character(mc[[1L]]) :
cannot coerce type 'closure' to vector of type 'character'
I tried using substitute(), any combination of quotation marks and the quote-Argument of do.call() but all to no avail. Because I want to use this for a sort-of mini-package I want to avoid using any other packages and would rather only use base functions. Is this possible?
Any help would be greatly appreciated!
Best, Martin
do.call
expects its first argument to be the 'name' of the function. Usually it works if this is entered as an R symbol (an expression without quotes) or expressed as a single element character value. In this case cfa
is a wrapper for lavaan
and the function name is converted to a list element named 'model.type' which succeeds when it is a character value but not when it is an R symbol-name of a function.
> do.call('cfa',args)
lavaan (0.5-15) converged normally after 56 iterations
Number of observations per group
0 50
1 50
Estimator ML
Minimum Function Test Statistic 0.000
Degrees of freedom 0
P-value (Chi-square) 0.000
Chi-square for each group:
0 0.000
1 0.000