Attachment | Size |
---|---|
data.csv [6] | 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