Can't solve dimname associated error

Posted on
No user picture. Jaewonee Joined: 10/02/2023
Hello. I am quite naive with OpenMx & R in general, and I can't find a way to solve this error. I'm trying to create a covariance matrix with my data.. I am aware that my code is probably very messy and has lots of other errors-I really need some help & advice on how to advance with this. Thank you so much in advance. This is my code:

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

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

matrix_data <- matrix(as.double(unlist(pheno2)), ncol = 7)

rownames(matrix_data) <- 1:nrow(matrix_data)

colnames(matrix_data) <- colnames(pheno2)

mxDataObject <- mxData(observed = matrix_data, type = "raw")

num_variables <- 7
cov_matrix <- mxMatrix(type = "Full", nrow = num_variables, ncol = num_variables, free = TRUE, values = 1, name = "cov_matrix")
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")

expectation <- mxExpectationNormal(covariance = "predicted_cov", means = "means")

model <- mxModel("MyModel",
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: The expected covariance matrix associated with MxExpectationNormal in model 'MyModel' does not contain dimnames.

---------------------
The attached file is my dataset - it has 63 entries & 7columns

Replied on Wed, 11/01/2023 - 13:21
Picture of user. AdminRobK Joined: 01/24/2014

This is pretty easy to fix. You can just provide the dimnames in your call to `mxAlgebra()`. Suppose you were analyzing five variables, with the following five column names: "x1","x2", "x3", "x4", and "x5". You could then do something like,

mxAlgebra(cov_matrix %*% t(variances) %*% cov_matrix, name = "predicted_cov", dimnames=c("x1","x2","x3","x4","x5")

. I notice that you don't have a header row in your dataset. You're going to have to create one, and use `read.csv()` with argument `header=TRUE`, so that OpenMx knows which variables it's going to be analyzing.
Replied on Thu, 11/02/2023 - 01:22
No user picture. Jaewonee Joined: 10/02/2023

In reply to by AdminRobK


pheno2 <- read.csv("pheno2.csv", header = TRUE)
library(OpenMx)

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

matrix_data <- matrix(as.double(unlist(pheno2)), ncol = 7)

rownames(matrix_data) <- 1:nrow(matrix_data)

colnames(matrix_data) <- colnames(pheno2)

# Now, create the OpenMx data object
mxDataObject <- mxData(observed = matrix_data, type = "raw")

num_variables <- 7
cov_matrix <- mxMatrix(type = "Full", nrow = num_variables, ncol = num_variables, free = TRUE, values = 1, name = "cov_matrix")
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")

expectation <- mxExpectationNormal(covariance = "predicted_cov", means = "means")

model <- mxModel("MyModel",cov_matrix,means,variances,mxAlgebra(cov_matrix %*% t(variances) %*% cov_matrix, name = "predicted_cov", dimnames=c("x1","x2","x3","x4","x5","x6","x7")), expectation, mxDataObject)

model$CovarianceMatrix <- cov_matrix

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

> model <- mxModel("MyModel",cov_matrix,means,variances,mxAlgebra(cov_matrix %*% t(variances) %*% cov_matrix, name = "predicted_cov", dimnames=c("x1","x2","x3","x4","x5","x6","x7")), expectation, mxDataObject)
Error in `dimnames<-`(`*tmp*`, value = dimnames) :
dimnames of MxAlgebra object must be either NULL or list of length 2

Replied on Thu, 11/02/2023 - 10:34
Picture of user. AdminRobK Joined: 01/24/2014

In reply to by Jaewonee

My mistake. `dimnames` is supposed to be a list of length 2; the first element provides row names, and the second provides column names. Try this:

dimnames=list(c("x1","x2","x3","x4","x5","x6","x7"),c("x1","x2","x3","x4","x5","x6","x7"))

Edit: actually, what might be easier is to provide this argument,

dimnames=c("x1","x2","x3","x4","x5","x6","x7")

, to `mxExpectationNormal()`.

Replied on Sun, 11/12/2023 - 11:53
No user picture. Jaewonee Joined: 10/02/2023

In reply to by AdminRobK


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

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

matrix_data <- matrix(as.double(unlist(pheno2)), ncol = 7)

# Set row names for each observation
rownames(matrix_data) <- 1:nrow(matrix_data)

# Set column names for each variable
colnames(matrix_data) <- colnames(pheno2)

# Now, create the OpenMx data object
mxDataObject <- mxData(observed = matrix_data, type = "raw")

num_variables <- 7
cov_matrix <- mxMatrix(type = "Full", nrow = num_variables, ncol = num_variables, free = TRUE, values = 1, name = "cov_matrix")
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")

expectation <- mxExpectationNormal(covariance = "predicted_cov", means = "means",dimnames=c("NS","RA","RD","P","SD","C","ST"))

model <- mxModel("MyModel",
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: The reference 'MyModel.fitfunction' does not exist. It is used by object MxComputeGradientDescent .

(I changed the column names to my header)

Replied on Sun, 11/12/2023 - 15:20
Picture of user. AdminHunter Joined: 03/01/2013

The below version of the model "works" in the sense that there are no errors. However, the model-implied covariance matrix at the starting values is not a valid covariance matrix.


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

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

matrix_data <- matrix(as.double(unlist(pheno2)), ncol = 7)

# Set row names for each observation
rownames(matrix_data) <- 1:nrow(matrix_data)

# Set column names for each variable
colnames(matrix_data) <- colnames(pheno2)

# Now, create the OpenMx data object
mxDataObject <- mxData(observed = matrix_data, type = "raw")

num_variables <- 7
cov_matrix <- mxMatrix(type = "Full", nrow = num_variables, ncol = num_variables, free = TRUE, values = 1, name = "cov_matrix")
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")

expectation <- mxExpectationNormal(covariance = "predicted_cov", means = "means", dimnames=c("NS","RA","RD","P","SD","C","ST"))

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


Running MyModel with 63 parameters
Error: The job for model 'MyModel' exited abnormally with the error message: fit is not finite (The continuous part of the model implied covariance (loc2) is not positive definite in data 'MyModel.data' row 1. Detail:
covariance = matrix(c( # 7x7
7, 7, 7, 7, 7, 7, 7
, 7, 7, 7, 7, 7, 7, 7
, 7, 7, 7, 7, 7, 7, 7
, 7, 7, 7, 7, 7, 7, 7
, 7, 7, 7, 7, 7, 7, 7
, 7, 7, 7, 7, 7, 7, 7
, 7, 7, 7, 7, 7, 7, 7), byrow=TRUE, nrow=7, ncol=7)
)
In addition: Warning message:
In model 'MyModel' Optimizer returned a non-zero status code 10. Starting values are not feasible. Consider mxTryHard()

The first question I have for you is: what do you mean by "I'm trying to create a covariance matrix with my data?"

Second, what are you hoping this algebra will do?


mxAlgebra(cov_matrix %*% t(variances) %*% cov_matrix, name = "predicted_cov")

Replied on Wed, 11/15/2023 - 01:27
No user picture. Jaewonee Joined: 10/02/2023

The attached file is the raw data for 7 dimensions of the TCI scale of 63 individuals. I'm trying to do a variance component analysis for 7 dimensions in the end. But for now as a trial I want to set the 1st column in my data (header:NS) as a dependent variable and the other 6 as random effects & calculate their contributions.

(P.S. I am aware that this is quite different from what I was doing with my original code, sorry for the confusion)

Replied on Wed, 11/15/2023 - 09:25
Picture of user. AdminHunter Joined: 03/01/2013

So, is your question solved for now?

The problem with my previous script was just that the starting values implied an improper covariance matrix: a 7x7 matrix of all 7s.

Replacing this line

cov_matrix <- mxMatrix(type = "Full", nrow = num_variables, ncol = num_variables, free = TRUE, values = 1, name = "cov_matrix")

with these lines

set.seed(41)
rvals <- matrix(runif(num_variables*num_variables), num_variables, num_variables)
rvals <- rvals %*% t(rvals)
cov_matrix <- mxMatrix(type = "Full", nrow = num_variables, ncol = num_variables, free = TRUE, values = rvals, name = "cov_matrix")

fixes the starting values issue.

Replied on Thu, 11/23/2023 - 09:33
No user picture. Jaewonee Joined: 10/02/2023


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

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

matrix_data <- matrix(as.double(unlist(pheno2)), ncol = 7)

rownames(matrix_data) <- 1:nrow(matrix_data)

colnames(matrix_data) <- colnames(pheno2)

mxDataObject <- mxData(observed = matrix_data, type = "raw")

num_variables <- 7
set.seed(41)
rvals <- matrix(runif(num_variables*num_variables), num_variables, num_variables)
rvals <- rvals %*% t(rvals)
cov_matrix <- mxMatrix(type = "Full", nrow = num_variables, ncol = num_variables, free = TRUE, values = rvals, name = "cov_matrix")
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")

expectation <- mxExpectationNormal(covariance = "predicted_cov", means = "means", dimnames=c("NS","RA","RD","P","SD","C","ST"))

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


Running MyModel with 63 parameters
Warning message:
In model 'MyModel' Optimizer returned a non-zero status code 5. The Hessian at the solution does not appear to be convex. See ?mxCheckIdentification for possible diagnosis (Mx status RED).

Are there any other parts I should change?

Replied on Sat, 11/25/2023 - 15:04
Picture of user. AdminHunter Joined: 03/01/2013

Are there any other parts I should change?

I really have no idea if there are parts you *should* change. It depends to a large extent on how the model you are specifying relates to the model you actually want to fit. I still don't know what model you'd like to fit and how this script relates to it.

This warning is generally saying "The optimum solution doesn't really look right (non-convex Hessian); consequently, don't trust your standard errors; your model might not be identified.". In this case, my guess is the model you specified has little to do with the model you want to specify and little to do with any reasonable model for these data. We can't help you with that unless you give us more information.

For the purposes of clarity, we have solved your initial question: you no longer have a problem with dimnames.

If you have further questions relating to a model you'd like to fit, we will all be happy to help you with them. Please start a new thread with whatever that question is. If you do so, please be as specific as you can about the model you are trying to fit and what questions you have.