LAVAAN HELP

2,852 views
Skip to first unread message

roybo...@gmail.com

unread,
Mar 3, 2017, 7:28:57 AM3/3/17
to lavaan
Hello everyone,

I need some help! 

I'm working on a problem where I have 5 latent variables and 25 objective (measured) variables (these are distinct).  Each latent variable loads onto 5 objective variables.  There is missing data, coded as NA. 

I want to run an SEM in R and use FIML to handle the missing data. I'm using the lavaan package.  Here is how I have it coded (objectives is my data set): 


model <- '
temotion  ~~ TSomatic + TWorries + TUnhappy + TClingy + TAfraid
tconduct   ~~ TTantrum + TObeys + TFights + TLies + TSteals
thyper    ~~ TRestles + TFidgety + TDistrac + TReflect + TAttends
tpeer ~~ TLoner + TFriend + TPopular + TBullied + TOldBest
tprosoc ~~ TConsid + TShares + TCaring + TKind + THelpOut
'

fit <- sem(model, objectives, missing='fiml')


I KEEP GETTING THIS ERROR:  Error in if ((!is.matrix(model)) | ncol(model) != 3) stop("model argument must be a 3-column matrix") :
  argument is of length zero


Does anyone know how to fix this???? Please help!!!


Terrence Jorgensen

unread,
Mar 3, 2017, 9:12:37 AM3/3/17
to lavaan
I KEEP GETTING THIS ERROR:  Error in if ((!is.matrix(model)) | ncol(model) != 3) stop("model argument must be a 3-column matrix") :
  argument is of length zero

This does not look like lavaan.  Check which packages you have loaded:

sessionInfo()

Did you also load the "sem" package, which has a different sem() function?  If so, you can make sure to call lavaan's function explicitly:

fit <- lavaan::sem(model, objectives, missing='fiml')

If this isn't the problem, it may be necessary to post enough data to reproduce the error.

Terrence D. Jorgensen
Postdoctoral Researcher, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

Edward Rigdon

unread,
Mar 3, 2017, 9:43:30 AM3/3/17
to lav...@googlegroups.com
Look at your syntax. If the variables on the left hand side are supposed to be factors, then the operator between the two sides should be =~, not ~~. Without an =~, lavaan does not know that the left hand side variables are factors rather than observed variables.

model <- ' 

temotion  ~~ TSomatic + TWorries + TUnhappy + TClingy + TAfraid
tconduct   ~~ TTantrum + TObeys + TFights + TLies + TSteals
thyper    ~~ TRestles + TFidgety + TDistrac + TReflect + TAttends
tpeer ~~ TLoner + TFriend + TPopular + TBullied + TOldBest
tprosoc ~~ TConsid + TShares + TCaring + TKind + THelpOut
'

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to lav...@googlegroups.com.
Visit this group at https://groups.google.com/group/lavaan.
For more options, visit https://groups.google.com/d/optout.

Alexandra Kinnby

unread,
May 4, 2017, 5:11:52 AM5/4/17
to lavaan
Hi,
I get the same error message as posted above when I use the following model:

model <- '
Nutrients =~ Nitrate + Phosphate + Chl
NoAdvBran =~ Nutrients
Area =~ Nutrients
NoAdvBran ~ Area + Alkalinity + Salinity + Temperature
Area ~ Salinity + Temperature

fit <- sem(model, data = advWC)

Error in if ((!is.matrix(model)) | ncol(model) != 3) stop("model argument must be a 3-column matrix") : 
  argument is of length zero

Does anyone know what I'm doing wrong?




On Friday, March 3, 2017 at 3:43:30 PM UTC+1, Edward Rigdon wrote:
Look at your syntax. If the variables on the left hand side are supposed to be factors, then the operator between the two sides should be =~, not ~~. Without an =~, lavaan does not know that the left hand side variables are factors rather than observed variables.

model <- ' 

temotion  ~~ TSomatic + TWorries + TUnhappy + TClingy + TAfraid
tconduct   ~~ TTantrum + TObeys + TFights + TLies + TSteals
thyper    ~~ TRestles + TFidgety + TDistrac + TReflect + TAttends
tpeer ~~ TLoner + TFriend + TPopular + TBullied + TOldBest
tprosoc ~~ TConsid + TShares + TCaring + TKind + THelpOut
'
On Fri, Mar 3, 2017 at 9:12 AM, Terrence Jorgensen <tjorge...@gmail.com> wrote:
I KEEP GETTING THIS ERROR:  Error in if ((!is.matrix(model)) | ncol(model) != 3) stop("model argument must be a 3-column matrix") :
  argument is of length zero

This does not look like lavaan.  Check which packages you have loaded:

sessionInfo()

Did you also load the "sem" package, which has a different sem() function?  If so, you can make sure to call lavaan's function explicitly:

fit <- lavaan::sem(model, objectives, missing='fiml')

If this isn't the problem, it may be necessary to post enough data to reproduce the error.

Terrence D. Jorgensen
Postdoctoral Researcher, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam

--
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.

Mikko Rönkkö

unread,
May 4, 2017, 5:38:29 AM5/4/17
to lav...@googlegroups.com
Hi,

On 4 May 2017, at 12.11, Alexandra Kinnby <alexandr...@gmail.com> wrote:

Hi,
I get the same error message as posted above when I use the following model:

model <- '
Nutrients =~ Nitrate + Phosphate + Chl
NoAdvBran =~ Nutrients
Area =~ Nutrients
NoAdvBran ~ Area + Alkalinity + Salinity + Temperature
Area ~ Salinity + Temperature

fit <- sem(model, data = advWC)

Error in if ((!is.matrix(model)) | ncol(model) != 3) stop("model argument must be a 3-column matrix") : 
  argument is of length zero

Does anyone know what I'm doing wrong?


As pointed out by Terrence below, this is not a lavaan error message. It looks like you are trying to use the sem function from the sem package. You can try the following code


library(sem)
library(lavaan)

model <- ' 
  # latent variable definitions
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + a*y2 + b*y3 + c*y4
dem65 =~ y5 + a*y6 + b*y7 + c*y8

# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60

# residual correlations
y1 ~~ y5
y2 ~~ y4 + y6
y3 ~~ y7
y4 ~~ y8
y6 ~~ y8
'
lavaan::sem(model, data=PoliticalDemocracy)
sem::sem(model, data=PoliticalDemocracy

If you yse the sem function from lavaan, it works. The sem function from the sem package produces the error that you are get.

It is probably not a good idea to load both packages at the same time, but it you do, you should call the functions using their package names as demonstrated above to ensure that you are using the correct function.

Mikko
Message has been deleted

Mikko Rönkkö

unread,
May 4, 2017, 6:17:43 AM5/4/17
to lav...@googlegroups.com
Hi,

I do not know what your data are or what you are trying to do, but to me your model does not make much sense.

Here you are defining the same latent variable twice using just a different name for it.

NoAdvBran =~ Nutrients
Area =~ Nutrients

Is this really what you want to do? 

Here you define nutrients as measured by nitrate, phosphate, and chl

Nutrients =~ Nitrate + Phosphate + Chl

I do not know what Chl means, but it nitrate and phosphate are probably distinct types of nutrients instead of unidimensional measures of one underlying latent variable, which the model states. 


You would need to provide a lot more detail on what you are trying to do to get more help.

Mikko

On 4 May 2017, at 13.09, Alexandra Kinnby <alexandr...@gmail.com> wrote:

When I use sem::sem I get the same error as before and when I use lavaan::sem I get the following error:

Error in lav_model_estimate(lavmodel = lavmodel, lavsamplestats = lavsamplestats,  : 
  lavaan ERROR: initial model-implied matrix (Sigma) is not positive definite;
  check your model and/or starting parameters.
In addition: Warning messages:
1: In lav_data_full(data = data, group = group, cluster = cluster,  :
  lavaan WARNING: some observed variances are (at least) a factor 1000 times larger than others; use varTable(fit) to investigate
2: In lav_samplestats_icov(COV = cov[[g]], ridge = ridge, x.idx = x.idx[[g]],  :
  lavaan WARNING: sample covariance matrix is not positive-definite

Edward Rigdon

unread,
May 4, 2017, 6:28:36 AM5/4/17
to lav...@googlegroups.com
Are you sure this is lavaan's sem function and not the semester function in John Fox's sem package? Could the lavaan sem be masked by another function with the same name?

To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages