lavTestWald problem

379 views
Skip to first unread message

Jeremy Miles

unread,
Mar 2, 2017, 3:03:01 PM3/2/17
to lavaan

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"

>


--
--
My employer has nothing to do with this email. 

Yves Rosseel

unread,
Mar 3, 2017, 6:19:08 AM3/3/17
to lav...@googlegroups.com
On 03/02/2017 09:02 PM, Jeremy Miles wrote:
> 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"

It would seem that the dispatching of 'vcov' failed for some reason. I
have no explanation for this. Maybe, a variable 'vcov' was created? Or
something else replaced the generic of vcov? It must be something local
as I can not reproduce this on any machine.

In any case, I just changed lavTestWald() so that vcov() is replaced by
a lavaan-specific version, avoiding the dispatch. In dev lavaan 0.6-1.1110

Yves.
Reply all
Reply to author
Forward
0 new messages