"Error in Mg %*% COV : requires numeric/complex matrix/vector arguments" - Not sure how to proceed

476 views
Skip to first unread message

Jesse Goldshear

unread,
Mar 12, 2023, 7:46:48 PM3/12/23
to lavaan
Hi all,

I'm running a relatively simple model and would like to use the SAM framework that is included in the lavaan package. However, when I run my code, I receive the error in the title of this post. Has anyone else seen this error in this context (and knows what to do about it)? Thanks!

model <- ' hardship =~ EAT + CLOTH + WASH + RESTRM
                  WASH ~~ RESTRM
                  hardship ~ TNTMOVRC '

fit.lsam <- sam(model, data = dat,
                            mm.list = "hardship",
                            ordered = c("EAT", "CLOTH", "WASH", "RESTRM"),
                            std.lv = TRUE,
                            sam.method = "local")

summary(fit.lsam, fit.lsam.measures = TRUE, standardized = TRUE)

Edward Rigdon

unread,
Mar 12, 2023, 9:24:55 PM3/12/23
to lav...@googlegroups.com
This won't help you, but ... when I get an error like that from R, it is because an R object does not have the attributes that R expects. For example, the object might have class matrix when R expects it to have class data.frame. This does not help you because the objects mentioned in the error message are not objects that you directly created. I have no experience with the SAM method--so error, yes, context, no.

--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lavaan/4ed61dc2-2b59-41c7-8aaa-521b167b6003n%40googlegroups.com.

Keith Markus

unread,
Mar 13, 2023, 8:51:31 AM3/13/23
to lavaan
Gold...,
I agree with Ed (and am similarly unfamiliar with sam).  I just want to elaborate that before the error in your subject heading, you should see an error traceback.  This shows step-by-step how R got from your code to the function containing the code where the error occurred.  If you follow the footsteps back, you can find the objects that were passed to the function.  This may offer some clues as to which is not appropriate.  It could be that an object that should contain numeric values is NULL or full of NAs, or is a scalar instead of a matrix.

Keith
------------------------
Keith A. Markus
John Jay College of Criminal Justice, CUNY
http://jjcweb.jjay.cuny.edu/kmarkus
Frontiers of Test Validity Theory: Measurement, Causation and Meaning.
http://www.routledge.com/books/details/9781841692203/


Jesse Goldshear

unread,
Mar 15, 2023, 6:37:05 PM3/15/23
to lavaan
Thanks Edward and Keith!

Ultimately I haven't quite been able to figure out why the code I'm using isn't outputting the correct matrix arguments. Given that, I'm going back to the method I was previously using that seemed to work just fine. 

- jesse

Terrence Jorgensen

unread,
Mar 16, 2023, 2:11:35 AM3/16/23
to lavaan
Maybe you just need to define your predictor as a factor with a perfectly reliable single indicator. There might be something odd happening with lavaan’s default behavior of treating such an exogenous variable as fixed. 

Yves Rosseel

unread,
Mar 20, 2023, 1:20:43 PM3/20/23
to lav...@googlegroups.com
I cannot replicate this, so it may be something related to this
particular dataset. Would you be able to send me the data, or a snippet
of the data? Just enough to reproduce the error message.

Yves.
> --
> You received this message because you are subscribed to the Google
> Groups "lavaan" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to lavaan+un...@googlegroups.com
> <mailto:lavaan+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/lavaan/4ed61dc2-2b59-41c7-8aaa-521b167b6003n%40googlegroups.com <https://groups.google.com/d/msgid/lavaan/4ed61dc2-2b59-41c7-8aaa-521b167b6003n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Floor Burghoorn

unread,
Nov 10, 2023, 6:32:36 AM11/10/23
to lavaan
Hi all, 

Just chiming in a little late because I am currently experiencing the same issue, and am suspecting that it has to do with the ordered() function. 
I am using the data from example 1 from the '"Structural-After-Measurement" (SAM) approach to SEM' paper by Rosseel & Loh (2022) and their OSF page.  
I used the same model as given in the example and accompanying R script, which is as follows: 

model <- '
  # measurement part
    ost   =~ y1 + y2 + y3 + y4
    self  =~ x1 + x2 + x3
    other =~ x4 + x5 + x6
  # structural part
    ost   ~ b1*self + b2*other + c*sol + age + gender
    self  ~ a1*sol + age + gender
    other ~ a2*sol + age + gender
  # residual correlations
    self ~~ other '

I used the same syntax for fitting the model, except that I specified the indicators as ordered (and removed the 'estimator = "ML"', since I expected the estimator to become "WLSMV"): 

fit.lsam_o <- sam(model, data = Data, sam.method = "local", ordered = c("x1", "x2", "x3", "x4", "x5", "x6", "y1", "y2", "y3", "y4"))
summary(fit.lsam_o)

I receive the same error as Jesse: Error in Mg %*% COV : requires numeric/complex matrix/vector arguments

Changing the variable structure in the dataset to be ordered (using as.ordered), or simply specifying "ordered = TRUE" in the syntax, did not help. Running the model without the ordered() function does work. Omitting the ordered part in the syntax but setting the estimator to "WLSMV" does work — but I am not sure whether this really treats the indicators as categorical. 

Although the Rosseel & Loh (2022) paper states that "we will focus on settings where all latent variables and their indicators are continuous", I wonder whether it might still be possible to run the model with ordered indicators? The study I will be running has Likert-scale items, which are ordinal. It would be great if it were possible to respect the ordinal nature of Likert-scale items while running a SAM. 

Or is there perhaps another reason for this error that has nothing to do with the ordered() function? 

Thanks very much in advance, 

Kind regards, 
Floor Burghoorn




Op maandag 20 maart 2023 om 18:20:43 UTC+1 schreef yros...@gmail.com:

Floor Burghoorn

unread,
Nov 10, 2023, 7:11:52 AM11/10/23
to lavaan
Small addition my post above: I assume that estimator = "MLR" does work with sam(), correct? Because that could be a back-up option for Likert items, if the ordered + WLSMV approach does not work. The model runs fine with MLR, but because the summary output still indicates that the estimator was ML (which I know it always does when you use MLR, similar to displaying DWLS when estimating with WLSMV), I was not 100% sure. 

Op vrijdag 10 november 2023 om 12:32:36 UTC+1 schreef Floor Burghoorn:
Reply all
Reply to author
Forward
0 new messages