Attachment | Size |
---|---|
data.csv | 43.75 KB |
I am trying to fit a structured latent growth curve model where the actual days of observation are inserted into the latent variable loadings using definition variables. Unfortunately, I run into the following error:
Error in value[rows[[i]], cols[[i]]] <- startValue : incorrect number of subscripts on matrix
The model I am trying to fit is provided below (with the data file attached in the post). Note that I am not estimating covariances and only estimating fixed- and random-effect parameters along with a residual parameter (nine parameters in total).
data_wide <- read_csv(file = 'data.csv') #variable names manifest_vars <- names(data_wide)[str_detect(string = names(data_wide), pattern = 'obs_score')] time_vars <- names(data_wide)[str_detect(string = names(data_wide), pattern = 'actual')] latent_vars <- c('theta', 'alpha', 'beta', 'gamma') measurement_days <- as.numeric(str_extract(string = names(data_wide[ 2:ncol(data_wide)]), pattern = '[^_]*$'))[1:length(time_vars)] model <- mxModel(model = 'Definition model', type = 'RAM', independent = T, mxData(observed = data_wide, type = 'raw'), manifestVars = manifest_vars, latentVars = latent_vars, #Residual variances; by using one label, they are assumed to all be equal (homogeneity of variance) mxPath(from = manifest_vars, arrows=2, free=TRUE, labels='epsilon', values = 0.04, lbound = 0), #Latent variable covariances and variances mxPath(from = latent_vars, connect='unique.pairs', arrows=2, #aa(diff_rand), ab(cov_diff_beta), ac(cov_diff_gamma), bb(beta_rand), bc(var_beta_gamma), cc(gamma_rand) free = c(TRUE,FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE), values=c(0.04, NA, NA, NA, 0.07, NA, NA, 15, NA, 5.6), labels=c('theta_rand', 'NA(cov_theta_alpha)', 'NA(cov_theta_beta)', 'NA(cov_theta_gamma)', 'alpha_rand','NA(cov_alpha_beta)', 'NA(cov_alpha_gamma)', 'beta_rand', 'NA(cov_beta_gamma)', 'gamma_rand'), lbound = c(1e-3, NA, NA, NA, 1e-3, NA, NA, 1, NA, 1), ubound = c(2, NA, NA, NA, 2, NA, NA, 90^2, NA, 45^2)), #Latent variable means (linear parameters). Note that the nonlinear parameters of beta and gamma do not have estimated means mxPath(from = 'one', to = c('theta', 'alpha'), free = c(TRUE, TRUE), arrows = 1, labels = c('theta_fixed', 'alpha_fixed'), lbound = 0, ubound = 7, values = c(2.9, 3.3)), #Functional constraints mxMatrix(type = 'Full', nrow = length(manifest_vars), ncol = 1, free = TRUE, labels = 'theta_fixed', name = 't', lbound = 0, ubound = 7, values = 2.9), mxMatrix(type = 'Full', nrow = length(manifest_vars), ncol = 1, free = TRUE, labels = 'alpha_fixed', name = 'a', lbound = 0, ubound = 7, values = 3.3), mxMatrix(type = 'Full', nrow = length(manifest_vars), ncol = 1, free = TRUE, labels = 'beta_fixed', name = 'b', lbound = 1, ubound = 360, values = 171), mxMatrix(type = 'Full', nrow = length(manifest_vars), ncol = 1, free = TRUE, labels = 'gamma_fixed', name = 'g', lbound = 1, ubound = 360, values = 15), ##Specifies time matrix that assumes time-structured data (model runs with this time matrix) #mxMatrix(type = 'Full', nrow = length(time_vars), ncol = 1, free = FALSE, # values = measurement_days[1:7], # name = 'time'), #Specifies time matrix with definition variables that can account for varying measurements points (i.e., time-unstructured data) ##Error occurs with this time matrix mxMatrix(type = 'Full', nrow = length(manifest_vars), ncol = 1, free = FALSE, labels = c("data.actual_measurement_day_0", "data.actual_measurement_day_60", "data.actual_measurement_day_120", "data.actual_measurement_day_180", "data.actual_measurement_day_240", "data.actual_measurement_day_300", "data.actual_measurement_day_360"), name = 'time'), #Algebra specifying first partial derivatives; mxAlgebra(expression = 1 - 1/(1 + exp((b - time)/g)), name="Tl"), mxAlgebra(expression = 1/(1 + exp((b - time)/g)), name = 'Al'), mxAlgebra(expression = -((a - t) * (exp((b - time)/g) * (1/g))/(1 + exp((b - time)/g))^2), name = 'Bl'), mxAlgebra(expression = (a - t) * (exp((b - time)/g) * ((b - time)/g^2))/(1 + exp((b -time)/g))^2, name = 'Gl'), #Factor loadings; all fixed and, importantly, constrained to change according to their partial derivatives (i.e., nonlinear functions) mxPath(from = 'theta', to = manifest_vars, arrows=1, free=FALSE, labels = c("Tl[1,1]", "Tl[2,1]", "Tl[3,1]", "Tl[4,1]", "Tl[5,1]", "Tl[6,1]", "Tl[7,1]")), mxPath(from = 'alpha', to = manifest_vars, arrows=1, free=FALSE, labels = c("Al[1,1]", "Al[2,1]", "Al[3,1]", "Al[4,1]", "Al[5,1]", "Al[6,1]", "Al[7,1]")), mxPath(from='beta', to = manifest_vars, arrows=1, free=FALSE, labels = c("Bl[1,1]", "Bl[2,1]", "Bl[3,1]", "Bl[4,1]", "Bl[5,1]", "Bl[6,1]", "Bl[7,1]")), mxPath(from='gamma', to = manifest_vars, arrows=1, free=FALSE, labels = c("Gl[1,1]", "Gl[2,1]", "Gl[3,1]", "Gl[4,1]", "Gl[5,1]", "Gl[6,1]", "Gl[7,1]")), mxFitFunctionML(vector = FALSE) ) model_results <- mxRun(model) #only get error when fitting time matrix specified with definition variables
I was unable to replicate your error: model ran fine! I've copy-pasted your results.
-Ryne
Hi!
There was a bug in OpenMx around using
tibbles
with definition variables.This forum post had the same issue: https://openmx.ssri.psu.edu/comment/9559#comment-9559
The temporary fix on your end is to hand OpenMx a
data.frame
instead of atibble
. For example, useThe ultimate fix is already solved in the OpenMx development version on GitHub with this issue: https://github.com/OpenMx/OpenMx/issues/345. The solution will be part of the next release of OpenMx.
Cheers!
Thank you very much! I forgot about the
data.frame
requirement. Just have one more question: Is convergence more difficult to obtain with definition variables? My understanding of using definition variables in this case is, by using the actual time at which each measurement is recorded for each person, an individual model has to be fit for each person. Thus, although using definition variables allows a model to account for individually varying measurement times, convergence becomes more difficult because the optimization problem of fitting models for each person's data is inherently more complex.Just asking this question because I have repeatedly generated data sets and noticed that convergence codes of 0 (i.e.,
code = 0
can only be obtained by usingmxTryHard()
and convergence time has also increased considerably (from almost instant when not using definition variables to roughly 8 seconds).> Is convergence more difficult to obtain with definition variables?
In general, no. Models with definition variables do not fit a separate model for each person. Rather, these models allow the expected means and covariances to differ in some systematic way for every person. In the case of a growth model with definition variables on the loadings that allows different times of measurement across people, every person has the same model with the same parameters; however, the expected means and covariances differ for each person depending on their particular times of measurement. If the model is "true" and the times of measurement differ across people, then not accounting for this difference amounts to model misspecification. In this case of misspecification, the model should fit more poorly, converge less frequently, and have biased estimates (particularly of the residual error variance).
As far as the problems you're encountering, I have two thoughts. First, models with definition variables are generally slower to estimate. Allowing the means and covariances to differ across people is more computationally expensive, so it takes a bit longer. What you reported is not unusual. Second, for the model convergence, there are a lot of possible factors, but if it's solved by
mxTryHard()
then that's a sufficient solution without going into detail on all the possibilities.Ok. Thank you very much for that answer! If I wanted to dig a bit deeper into the parameter estimation procedure with definition variables, are there any resources that you might suggest?
These two chapters from the user's guide go into some detail:
Definition Means Path Spec
Definition Means Matrix Spec