I pasted the function here for reference (highlighted in red the portion where I set the equality constraints I want to weight
it should print out an example where x was measured at 3, 4.5, and 5.2 years and y was measured at 2.9, 4.4, and 6 years.
riclpm_formula <- function(var1='dep', var2='cmr',
n_ocs=NULL, meas_time=list(c(), c()),
stationarity=TRUE,
verbose=TRUE) {
# Check input ----------------------------------------------------------------
if (is.null(n_ocs) & (length(meas_time[[1]])==0 | length(meas_time[[1]])==0)) {
stop('Provide total number of occasions or time points of measurement!') }
if (length(meas_time[[1]]) != length(meas_time[[2]])) {
stop('You need the same number of time points for each of the two variables.') }
if (!is.null(n_ocs) & !length(meas_time[[1]]) %in% c(0, n_ocs)) { # contraddictory info
stop('Number of occations and measurement times provided do not agree.') }
# ----------------------------------------------------------------------------
# How many occasions
if (length(meas_time[[1]]) > 0) {
temp_var1 = meas_time[[1]]
temp_var2 = meas_time[[2]]
} else {
temp_var1 = temp_var2 = 1:n_ocs
}
if (is.null(n_ocs)) { n_ocs = length(temp_var1) }
# Create between components (random intercepts)
random_intercepts = paste0('# Random intercepts\n',
'ri_',var1,' =~ ', paste0('1*',var1,1:n_ocs, collapse=' + '), '\n',
'ri_',var2,' =~ ', paste0('1*',var2,1:n_ocs, collapse=' + '), '\n')
# Create within-components (or within-person centered variables)
# Note: factor loadings are set to 1 in non-stationary model and freely estimated in stationary models
if (stationarity) { l = 'NA*' } else { l = '1*' }
impulses = paste0('# Impulses\n',
paste0('w_',var1,1:n_ocs,' =~ ',l,var1,1:n_ocs, collapse='\n'), '\n',
paste0('w_',var2,1:n_ocs,' =~ ',l,var2,1:n_ocs, collapse='\n'), '\n')
# Estimate lagged effects between within-person centered variables
regressions = '# Regressions\n'
for (i in 2:n_ocs) {
# if (n_strata > 1) { ... ar_1 <- paste0('c(', paste0(rep(paste0('AR_',var1,i-1),strata), collapse=', '), ')') #define strata
ar_1 <- paste0('AR_',var1,i-1)
ar_2 <- paste0('AR_',var2,i-1)
cl_1 <- paste0('CL_',var1,i-1)
cl_2 <- paste0('CL_',var2,i-1)
regressions = paste0(regressions,
'w_',var1,i,' ~ ',ar_1,' * w_',var1,i-1,' + ',cl_1,' * w_',var2,i-1,'\n',
'w_',var2,i,' ~ ',ar_2,' * w_',var2,i-1,' + ',cl_2,' * w_',var1,i-1,'\n')
}
# Estimate covariance between within-person centered variables
covariances = paste0('# Covariances\n',
# Estimate correlation between within-components at first wave
'w_',var1,'1 ~~ cor1 * w_',var2,'1\n',
# Estimate residual covariances (between residuals of within-person vars, i.e., innovations)
paste0('w_',var1,2:n_ocs,' ~~ rcov',2:n_ocs,' * w_',var2,2:n_ocs, collapse='\n'), '\n',
# Estimate covariance of the random intercepts
'ri_',var1,' ~~ covRI * ri_',var2,'\n')
if (stationarity) { w1 = '1*' } else { w1 = '' }
variances = paste0('# Variances\n',
# Set variances of within-components at first wave to 1
paste0('w_',var1,'1 ~~ ',w1,'w_',var1,'1\n', 'w_',var2,'1 ~~ ',w1,'w_',var2,'1\n'),
# Estimate (and label) residual variances of within-person centered variables
paste0('w_',var1,2:n_ocs,' ~~ rvar_',var1,2:n_ocs,'*w_',var1,2:n_ocs, collapse='\n'),'\n',
paste0('w_',var2,2:n_ocs,' ~~ rvar_',var2,2:n_ocs,'*w_',var2,2:n_ocs, collapse='\n'),'\n',
# Estimate variance of random intercepts
'ri_',var1,' ~~ ri_',var1,'\nri_',var2,' ~~ ri_',var2, '\n')
constraints = ar_var1_con = ar_var2_con = cl_var1_con = cl_var2_con = cor_con = rvar_con = ''
if (stationarity) {
constraints = '# Constraints\n'
# paste0('# Constrain grand means over time\n',
# paste0(var1,1:n_ocs, collapse=' + '), ' ~ mean_',var1,'*1\n',
# paste0(var2,1:n_ocs, collapse=' + '), ' ~ mean_',var2,'*1\n')
# Weight for unequal temporal gaps
rel='^' # '*'
i = 1:(n_ocs-2) # iterator
prec=2 # precision
# NOTE: round to nearest integer for the moment
ar_var1_con <- paste0('AR_',var1,i,rel,round(temp_var1[i+1]-temp_var1[i], prec), ' == AR_',var1,i+1,rel,round(temp_var1[i+2]-temp_var1[i+1], prec),
collapse='\n')
ar_var2_con <- paste0('AR_',var2,i,rel,round(temp_var2[i+1]-temp_var2[i], prec), ' == AR_',var2,i+1,rel,round(temp_var2[i+2]-temp_var2[i+1], prec),
collapse='\n')
cl_var1_con <- paste0('CL_',var1,i,rel,round(temp_var1[i+1]-temp_var2[i], prec), ' == CL_',var1,i+1,rel,round(temp_var1[i+2]-temp_var2[i+1], prec),
collapse='\n')
cl_var2_con <- paste0('CL_',var2,i,rel,round(temp_var2[i+1]-temp_var1[i], prec), ' == CL_',var2,i+1,rel,round(temp_var2[i+2]-temp_var1[i+1], prec),
collapse='\n')
# Compute correlations between the within-components themselves at each wave
i = 1:(n_ocs-1) # new iterator
cor_con <- paste0('cor',2:n_ocs, ' := AR_',var1,i,'*CL_',var2,i,' + ', 'CL_',var1,i,'*AR_',var2,i,' + ',
'AR_',var1,i,'*AR_',var2,i,'*cor',i,' + ', 'CL_',var1,i,'*CL_',var2,i,'*cor',i,' + rcov',2:n_ocs, collapse='\n')
# Constrain residual variances of within-components such that the total variance of each
# within-component equals 1
rvar_con <- paste0('rvar_',var1,2:n_ocs,' == 1 - (AR_',var1,i,'*AR_',var1,i,' + ', 'CL_',var1,i,'*CL_',var1,i,' + 2*AR_',var1,i,'*CL_',var1,i,'*cor',i,')\n',
'rvar_',var2,2:n_ocs,' == 1 - (AR_',var2,i,'*AR_',var2,i,' + ', 'CL_',var2,i,'*CL_',var2,i,' + 2*AR_',var2,i,'*CL_',var2,i,'*cor',i,')', collapse = '\n')
}
constraints = paste0(constraints, ar_var1_con,'\n', ar_var2_con,'\n', cl_var1_con,'\n', cl_var2_con, '\n', cor_con,'\n',rvar_con)
f = paste0(random_intercepts, impulses, regressions, covariances, variances, constraints)
if (verbose) { cat(f) }
return(f)
}