I'm running into a problem with lavTestWald.
It says:
Error in UseMethod("vcov") :
no applicable method for 'vcov' applied to an object of class "lavaan"
Even when I run the example in help.
Here's my full session transcript.
> library(lavaan)
Loading required package: stats4
Loading required package: mnormt
Loading required package: pbivnorm
Loading required package: quadprog
Loading required package: numDeriv
This is lavaan 0.5-23.1097
lavaan is BETA software! Please report any bugs.
> data(HolzingerSwineford1939)
> HS.model <- '
+ visual =~ x1 + b1*x2 + x3
+ textual =~ x4 + b2*x5 + x6
+ speed =~ x7 + b3*x8 + x9
+ '
>
> fit <- cfa(HS.model, data=HolzingerSwineford1939)
>
> # test 1: test about a single parameter
> # this is the 'chi-square' version of the
> # z-test from the summary() output
> lavTestWald(fit, constraints = "b1 == 0")
Error in UseMethod("vcov") :
no applicable method for 'vcov' applied to an object of class "lavaan"
Weirdly, if I patch in the lavTestWald function from github, it works:
> lavTestWald(fit, constraints = "b1 == 1")
$stat
[1] 20.0704
$df
[1] 1
$p.value
[1] 7.464313e-06
$se
[1] "standard"
> lavaan::lavTestWald(fit, constraints = "b1 == 1")
Error in UseMethod("vcov") :
no applicable method for 'vcov' applied to an object of class "lavaan"
Even more weirdly, if I copy the contents of the function from typing:
lavTestWald,
call it ltw2 and run that, it also works.
ltw2 <- function (object, constraints = NULL, verbose = FALSE)
{
if (object@optim$npar > 0L && !object@optim$converged)
stop("lavaan ERROR: model did not converge")
if (is.null(constraints) || nchar(constraints) == 0L) {
stop("lavaan ERROR: constraints are empty")
}
PT <- as.data.frame(object@ParTable, stringsAsFactors = FALSE)
eq.idx <- which(PT$op == "==")
if (length(eq.idx) > 0L) {
PT <- PT[-eq.idx, ]
}
partable <- as.list(PT)
FLAT <- lavParseModelString(constraints)
CON <- attr(FLAT, "constraints")
LIST <- list()
if (length(CON) > 0L) {
lhs = unlist(lapply(CON, "[[", "lhs"))
op = unlist(lapply(CON, "[[", "op"))
rhs = unlist(lapply(CON, "[[", "rhs"))
LIST$lhs <- c(LIST$lhs, lhs)
LIST$op <- c(LIST$op, op)
LIST$rhs <- c(LIST$rhs, rhs)
}
else {
stop("lavaan ERROR: no equality constraints found in constraints argument")
}
theta <- object@optim$x
ceq.function <- lav_partable_constraints_ceq(partable = partable,
con = LIST, debug = FALSE)
JAC <- try(lav_func_jacobian_complex(func = ceq.function,
x = theta), silent = TRUE)
if (inherits(JAC, "try-error")) {
JAC <- lav_func_jacobian_simple(func = ceq.function,
x = theta)
}
if (verbose) {
cat("Restriction matrix (jacobian):\n")
print(JAC)
cat("\n")
}
theta.r <- ceq.function(theta)
if (verbose) {
cat("Restricted theta values:\n")
print(theta.r)
cat("\n")
}
VCOV <- vcov(object, labels = FALSE)
info.r <- JAC %*% VCOV %*% t(JAC)
Wald <- as.numeric(t(theta.r) %*% solve(info.r) %*% theta.r)
Wald.df <- nrow(JAC)
Wald.pvalue <- 1 - pchisq(Wald, df = Wald.df)
list(stat = Wald, df = Wald.df, p.value = Wald.pvalue, se = object@Options$se)
}
ltw2(fit, constraints = "b1 == 1" )
> ltw2(fit, constraints = "b1 == 1" )
$stat
[1] 20.0704
$df
[1] 1
$p.value
[1] 7.464313e-06
$se
[1] "standard"
>