Can't solve dimname associated error
Attachment | Size |
---|---|
pheno2.csv | 1.25 KB |
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
This is pretty easy to fix.
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.
Log in or register to post comments
In reply to This is pretty easy to fix. by AdminRobK
it seems like there's still an error with 'dimnames'?
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
Log in or register to post comments
In reply to it seems like there's still an error with 'dimnames'? by Jaewonee
my mistake
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()`.
Log in or register to post comments
In reply to my mistake by AdminRobK
New error?
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)
Log in or register to post comments
What are you trying to do?
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")
Log in or register to post comments
VCA with first column as dependent variable
(P.S. I am aware that this is quite different from what I was doing with my original code, sorry for the confusion)
Log in or register to post comments
Solved?
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.
Log in or register to post comments
warning?
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?
Log in or register to post comments
New thread
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.
Log in or register to post comments