简体   繁体   中英

In R Latent Variable Analysis understanding problem and get error Lavaan package

My problem statement is to identify the factors that affects net promoter score

I am using lavaan package testing with sample data

Below is the code

library(lavaan)
age=c(24,56,34)
weight=c(76,55,66)
nps=c(9,4,5)
df=c(age,weight,nps)
mat1=matrix(c(cov(abs(scale(df)))),3,3,byrow=TRUE)
mod2 <- "weight ~ age \n        weight ~ nps"
mod1 <- "nps ~ age \n        nps ~ weight"
mat1=matrix(c(cor(abs(scale(df)))),3,3,byrow=TRUE)
colnames(mat1) <- rownames(mat1) <- c("age", "weight", "nps")
mod1fit <- sem(mod1, sample.cov = mat1, sample.nobs = 100)

From above example can anyone help in understanding nobs[Number of Observations=100] . Usually in ML observations says about number of rows but I don't know the meaning here of nobs parameter .

I have used below link to learn

https://www.jaredknowles.com/journal/2013/9/1/latent-variable-analysis-with-r-getting-setup-with-lavaan

When I run above code I get error as below

Error in lav_samplestats_icov(COV = cov[[g]], ridge = ridge, x.idx = x.idx[[g]],  : 
  lavaan ERROR: sample covariance matrix is not positive-definite

The lavaan manual (that you can access from within the R console via the command ?sem ) states that the argument sample.nobs refers to

Number of observations if the full data frame is missing and only sample moments are given. For a multiple group analysis, a list or a vector with the number of observations for each group.

Considering the error message: I'm not really sure what you are trying to acchieve with that following line of code

mat1=matrix(c(cov(abs(scale(df)))),3,3,byrow=TRUE)

This however leads to a non-positive definite sample covariance matrix that looks like this

> mat1
       age weight nps
age      1      1   1
weight   1      1   1
nps      1      1   1

If age , weight and nps are factors (for which you have three observations each) then

mat1 <- cor(data.frame(age,weight,nps))

might produce the intended covariance matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM