error in finding an already defined reference - openmx

32 Views Asked by At

I am a student trying to use R for the first time, and I have no idea how to fix my current code - the same error message keeps showing. All the codes before "fit <- mxRun(model)" work without error messages. I added "model$CovarianceMatrix <- cov_matrix" to solve the error but that didn't do the.

Could the error be something related with how my data is formulated, rather than the code itself?

I would appreciate any kind of help, I'm completely lost at this point. Thank you to all you clever people out there in advance :)

pheno2 <- read.csv("pheno2.csv")
library(OpenMx)

colnames(pheno2) <- gsub("\\.", "_", colnames(pheno2))

num_variables <- 7
cov_matrix <- mxMatrix(type = "Full", nrow = num_variables, ncol = num_variables, free = TRUE, values = 1, name = "cov")
means <- mxMatrix(type = "Full", nrow = 1, ncol = num_variables, free = TRUE, values = 0, name = "means")
variances <- mxMatrix(type = "Diag", nrow = num_variables, ncol = num_variables, free = TRUE, values = 1, name = "variances")

mxDataObject <- mxData(observed = as.matrix(pheno2), type = "raw")
expectation <- mxExpectationNormal(covariance = "predicted_cov", means = "means")

model <- mxModel("BasicModel",
                 cov_matrix,
                 means,
                 variances,
                 mxAlgebra(cov_matrix %*% t(variances) %*% cov_matrix, name = "predicted_cov"),
                 expectation,
                 mxDataObject)

model$CovarianceMatrix <- cov_matrix

model$fitFunction <- mxFitFunctionML()
fit <- mxRun(model)


Error: Unknown reference 'cov_matrix' detected in the entity 
  'predicted_cov' in model 'BasicModel'
1

There are 1 best solutions below

0
On

Remember to provide a minimal example with data when asking.

The naming is incorrect, it should be be "cov" here: t(variances) %*% cov