21 Conditional Transition Probabilities
This chapter is a continuation of the LTA chapters.
Data Source: The data used to illustrate these analyses include elementary school student Science Attitude survey items collected during 7th and 10th grades from the Longitudinal Study of American Youth (LSAY; Miller, 2015).
To install package {rhdf5}
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
#BiocManager::install("rhdf5")Load packages
library(MplusAutomation)
library(rhdf5)
library(tidyverse)
library(here)
library(glue)
library(janitor)
library(gt)
library(reshape2)
library(cowplot)
library(ggrepel)
library(haven)
library(modelsummary)
library(corrplot)
library(DiagrammeR)
library(filesstrings)
library(PNWColors)21.0.0.1 Manually calculate transition probabilities by covariate
Transition probabilities can be calculated for each level of the covariate. For example, transition probabilities for males or females only, depending on the research question.
step3 <- mplusObject(
TITLE = "LTA (invariant)",
VARIABLE =
"usevar = ab39m ab39t ab39u ab39w ab39x ! 7th grade indicators
ga33a ga33h ga33i ga33k ga33l FEMALE;
categorical = ab39m-ab39x ga33a-ga33l;
classes = c1(4) c2(4);" ,
ANALYSIS =
"estimator = mlr;
type = mixture;
starts = 500 100;
processors = 10;",
MODEL =
"%overall%
c2 c1 on FEMALE;
c2#1 on c1#1 (b11);
c2#2 on c1#1 (b21);
c2#3 on c1#1 (b31);
c2#1 on c1#2 (b12);
c2#2 on c1#2 (b22);
c2#3 on c1#2 (b32);
c2#1 on c1#3 (b13);
c2#2 on c1#3 (b23);
c2#3 on c1#3 (b33);
[c2#1] (a1);
[c2#2] (a2);
[c2#3] (a3);
c2#1 ON female (b212);
c2#2 ON female (b222);
c2#3 ON female (b232);
c1#1 ON female (b112);
c1#2 ON female (b122);
c1#3 ON female (b132);
MODEL c1:
%c1#1%
[AB39M$1-AB39X$1] (1-5); !!! labels that are repeated will constrain parameters to equality !!!
%c1#2%
[AB39M$1-AB39X$1] (6-10);
%c1#3%
[AB39M$1-AB39X$1] (11-15);
%c1#4%
[AB39M$1-AB39X$1] (16-20);
MODEL c2:
%c2#1%
[GA33A$1-GA33L$1] (1-5);
%c2#2%
[GA33A$1-GA33L$1] (6-10);
%c2#3%
[GA33A$1-GA33L$1] (11-15);
%c2#4%
[GA33A$1-GA33L$1] (16-20);",
OUTPUT = "tech1 tech15 svalues;",
MODELCONSTRAINT = " ! Compute joint and marginal probabilities:
New(
t11 t12 t13 t14
t21 t22 t23 t24
t31 t32 t33 t34
t41 t42 t43 t44
t11B t12B t13B t14B
t21B t22B t23B t24B
t31B t32B t33B t34B
t41B t42B t43B t44B
diff_11_22 x
);
t11 = exp(a1 +b11)/(exp(a1+b11)+exp(a2+b21)+exp(a3+b31)+exp(0));
t12 = exp(a2 +b21)/(exp(a1+b11)+exp(a2+b21)+exp(a3+b31)+exp(0));
t13 = exp(a3 +b31)/(exp(a1+b11)+exp(a2+b21)+exp(a3+b31)+exp(0));
t14 = 1 - (t11+t12+t13);
t21 = exp(a1 +b12)/(exp(a1+b12)+exp(a2+b22)+exp(a3+b32)+exp(0));
t22 = exp(a2 +b22)/(exp(a1+b12)+exp(a2+b22)+exp(a3+b32)+exp(0));
t23 = exp(a3 +b32)/(exp(a1+b12)+exp(a2+b22)+exp(a3+b32)+exp(0));
t24 = 1 - (t21+t22+t23);
t31 = exp(a1 +b13)/(exp(a1+b13)+exp(a2+b23)+exp(a3+b33)+exp(0));
t32 = exp(a2 +b23)/(exp(a1+b13)+exp(a2+b23)+exp(a3+b33)+exp(0));
t33 = exp(a3 +b33)/(exp(a1+b13)+exp(a2+b23)+exp(a3+b33)+exp(0));
t34 = 1 - (t31+t32+t33);
t41 = exp(a1)/(exp(a1)+exp(a2)+exp(a3)+exp(0));
t42 = exp(a2)/(exp(a1)+exp(a2)+exp(a3)+exp(0));
t43 = exp(a3)/(exp(a1)+exp(a2)+exp(a3)+exp(0));
t44 = 1 - (t41+t42+t43);
!c1#3 ON female (b132);
x= 1 ; ! x=1 is female, x=0 males
t11B = exp(a1 +b11+b212*x)/(exp(a1+b11+b212*x)+exp(a2+b21+b222*x)+exp(a3+b31+b232*x)
+exp(0));
t12B = exp(a2 +b21+b222*x)/(exp(a1+b11+b212*x)+exp(a2+b21+b222*x)+exp(a3+b31+b232*x)
+exp(0));
t13B = exp(a3 +b31+b232*x)/(exp(a1+b11+b212*x)+exp(a2+b21+b222*x)+exp(a3+b31+b232*x)
+exp(0));
t14B = 1 - (t11B+t12B+t13B);
t21B = exp(a1 +b12+b212*x)/(exp(a1+b12+b212*x)+exp(a2+b22+b222*x)+exp(a3+b32+b232*x)
+exp(0));
t22B = exp(a2 +b22+b222*x)/(exp(a1+b12+b212*x)+exp(a2+b22+b222*x)+exp(a3+b32+b232*x)
+exp(0));
t23B = exp(a3 +b32+b232*x)/(exp(a1+b12+b212*x)+exp(a2+b22+b222*x)+exp(a3+b32+b232*x)
+exp(0));
t24B = 1 - (t21B+t22B+t23B);
t31B = exp(a1 +b13+b212*x)/(exp(a1+b13+b212*x)+exp(a2+b23+b222*x)+exp(a3+b33+b232*x)
+exp(0));
t32B = exp(a2 +b23+b222*x)/(exp(a1+b13+b212*x)+exp(a2+b23+b222*x)+exp(a3+b33+b232*x)
+exp(0));
t33B = exp(a3 +b33+b232*x)/(exp(a1+b13+b212*x)+exp(a2+b23+b222*x)+exp(a3+b33+b232*x)
+exp(0));
t34B = 1 - (t31B+t32B+t33B);
t41B = exp(a1+b212*x)/(exp(a1+b212*x)+exp(a2+b222*x)+exp(a3+b232*x)+exp(0));
t42B = exp(a2+b222*x)/(exp(a1+b212*x)+exp(a2+b222*x)+exp(a3+b232*x)+exp(0));
t43B = exp(a3+b232*x)/(exp(a1+b212*x)+exp(a2+b222*x)+exp(a3+b232*x)+exp(0));
t44B = 1 - (t41B+t42B+t43B);
diff_11_22= t11-t11B;",
usevariables = colnames(savedata),
rdata = savedata)
step3_fit <- mplusModeler(step3,
dataout=here("lta","cov_model","calc_tran.dat"),
modelout=here("lta","cov_model","calc_tran.inp"),
check=TRUE, run = TRUE, hashfilename = FALSE)Read invariance model and extract parameters (intercepts and multinomial regression coefficients)
lta_inv1 <- readModels(here("lta","cov_model","calc_tran.out" ), quiet = TRUE)
par <- as_tibble(lta_inv1[["parameters"]][["unstandardized"]]) %>%
select(1:3) %>%
filter(grepl('ON|Means|Intercept', paramHeader)) %>%
mutate(est = as.numeric(est),
label = c("b11", "b12", "b13", "b21", "b22", "b23", "b31", "b32", "b33", "b212", "b222", "b232", "b112", "b122", "b132", "a11", "a21", "a31", "a12", "a22", "a32"))Manual method to calculate transition probabilities by covariate:
# Name each parameter individually to make the subsequent calculations more readable
a1 <- unlist(par[19,3]);
a2 <- unlist(par[20,3]);
a3 <- unlist(par[21,3]);
b11 <- unlist(par[1,3]);
b21 <- unlist(par[4,3]);
b31 <- unlist(par[7,3]);
b12 <- unlist(par[2,3]);
b22 <- unlist(par[5,3]);
b32 <- unlist(par[8,3]);
b13 <- unlist(par[3,3]);
b23 <- unlist(par[6,3]);
b33 <- unlist(par[9,3]);
b212 <- unlist(par[10,3]);
b222 <- unlist(par[11,3]);
b232 <- unlist(par[12,3]);
b112 <- unlist(par[13,3]);
b122 <- unlist(par[14,3]);
b132 <- unlist(par[15,3]);
x <- 0 # x=1 is female, x=0 males
# Calculate transition probabilities from the logit parameters
t11B <- exp(a1 + b11 + b212*x) / (exp(a1 + b11 + b212*x) + exp(a2 + b21 + b222*x) + exp(a3 + b31 + b232*x) + exp(0))
t12B <- exp(a2 + b21 + b222*x) / (exp(a1 + b11 + b212*x) + exp(a2 + b21 + b222*x) + exp(a3 + b31 + b232*x) + exp(0))
t13B <- exp(a3 + b31 + b232*x) / (exp(a1 + b11 + b212*x) + exp(a2 + b21 + b222*x) + exp(a3 + b31 + b232*x) + exp(0))
t14B <- 1 - (t11B + t12B + t13B)
t21B <- exp(a1 + b12 + b212*x) / (exp(a1 + b12 + b212*x) + exp(a2 + b22 + b222*x) + exp(a3 + b32 + b232*x) + exp(0))
t22B <- exp(a2 + b22 + b222*x) / (exp(a1 + b12 + b212*x) + exp(a2 + b22 + b222*x) + exp(a3 + b32 + b232*x) + exp(0))
t23B <- exp(a3 + b32 + b232*x) / (exp(a1 + b12 + b212*x) + exp(a2 + b22 + b222*x) + exp(a3 + b32 + b232*x) + exp(0))
t24B <- 1 - (t21B + t22B + t23B)
t31B <- exp(a1 + b13 + b212*x) / (exp(a1 + b13 + b212*x) + exp(a2 + b23 + b222*x) + exp(a3 + b33 + b232*x) + exp(0))
t32B <- exp(a2 + b23 + b222*x) / (exp(a1 + b13 + b212*x) + exp(a2 + b23 + b222*x) + exp(a3 + b33 + b232*x) + exp(0))
t33B <- exp(a3 + b33 + b232*x) / (exp(a1 + b13 + b212*x) + exp(a2 + b23 + b222*x) + exp(a3 + b33 + b232*x) + exp(0))
t34B <- 1 - (t31B + t32B + t33B)
t41B <- exp(a1 + b212*x) / (exp(a1 + b212*x) + exp(a2 + b222*x) + exp(a3 + b232*x) + exp(0))
t42B <- exp(a2 + b222*x) / (exp(a1 + b212*x) + exp(a2 + b222*x) + exp(a3 + b232*x) + exp(0))
t43B <- exp(a3 + b232*x) / (exp(a1 + b212*x) + exp(a2 + b222*x) + exp(a3 + b232*x) + exp(0))
t44B <- 1 - (t41B + t42B + t43B)
x <- 1 # x=1 is female, x=0 males
# Calculate transition probabilities from the logit parameters
t11 <- exp(a1 + b11 + b212*x) / (exp(a1 + b11 + b212*x) + exp(a2 + b21 + b222*x) + exp(a3 + b31 + b232*x) + exp(0))
t12 <- exp(a2 + b21 + b222*x) / (exp(a1 + b11 + b212*x) + exp(a2 + b21 + b222*x) + exp(a3 + b31 + b232*x) + exp(0))
t13 <- exp(a3 + b31 + b232*x) / (exp(a1 + b11 + b212*x) + exp(a2 + b21 + b222*x) + exp(a3 + b31 + b232*x) + exp(0))
t14 <- 1 - (t11 + t12 + t13)
t21 <- exp(a1 + b12 + b212*x) / (exp(a1 + b12 + b212*x) + exp(a2 + b22 + b222*x) + exp(a3 + b32 + b232*x) + exp(0))
t22 <- exp(a2 + b22 + b222*x) / (exp(a1 + b12 + b212*x) + exp(a2 + b22 + b222*x) + exp(a3 + b32 + b232*x) + exp(0))
t23 <- exp(a3 + b32 + b232*x) / (exp(a1 + b12 + b212*x) + exp(a2 + b22 + b222*x) + exp(a3 + b32 + b232*x) + exp(0))
t24 <- 1 - (t21 + t22 + t23)
t31 <- exp(a1 + b13 + b212*x) / (exp(a1 + b13 + b212*x) + exp(a2 + b23 + b222*x) + exp(a3 + b33 + b232*x) + exp(0))
t32 <- exp(a2 + b23 + b222*x) / (exp(a1 + b13 + b212*x) + exp(a2 + b23 + b222*x) + exp(a3 + b33 + b232*x) + exp(0))
t33 <- exp(a3 + b33 + b232*x) / (exp(a1 + b13 + b212*x) + exp(a2 + b23 + b222*x) + exp(a3 + b33 + b232*x) + exp(0))
t34 <- 1 - (t31 + t32 + t33)
t41 <- exp(a1 + b212*x) / (exp(a1 + b212*x) + exp(a2 + b222*x) + exp(a3 + b232*x) + exp(0))
t42 <- exp(a2 + b222*x) / (exp(a1 + b212*x) + exp(a2 + b222*x) + exp(a3 + b232*x) + exp(0))
t43 <- exp(a3 + b232*x) / (exp(a1 + b212*x) + exp(a2 + b222*x) + exp(a3 + b232*x) + exp(0))
t44 <- 1 - (t41 + t42 + t43)Create table
21.0.1 Create Transition Table
The table below shows the transition probabilities estimated for girls only. These are hand calculated in Mplus using the OVERALL section of the model and Model Constraints command.
t_matrix <- tibble(
"Time1" = c("C1=Anti-Science","C1=Amb. w/ Elevated","C1=Amb. w/ Minimal","C1=Pro-Science"),
"C2=Anti-Science" = c(t11,t21,t31,t41),
"C2=Amb. w/ Elevated" = c(t12,t22,t32,t42),
"C2=Amb. w/ Minimal" = c(t13,t23,t33,t43),
"C2=Pro-Science" = c(t14,t24,t34,t44))
t_matrix %>%
gt(rowname_col = "Time1") %>%
tab_stubhead(label = "7th grade") %>%
tab_header(
title = md("**FEMALES: Student transitions from 7th grade (rows) to 10th grade (columns)**")) %>%
fmt_number(2:5,decimals = 3) %>%
tab_spanner(label = "10th grade",columns = 2:5) %>%
tab_footnote(
footnote = md(
"*Note.* Transition matrix values are the identical to Table 5, however Table 5
has the values rearranged by class for interpretation purposes. Classes may be arranged
directly through Mplus syntax using start values."),
locations = cells_title())| FEMALES: Student transitions from 7th grade (rows) to 10th grade (columns)1 | ||||
| 7th grade |
10th grade
|
|||
|---|---|---|---|---|
| C2=Anti-Science | C2=Amb. w/ Elevated | C2=Amb. w/ Minimal | C2=Pro-Science | |
| C1=Anti-Science | 0.302 | 0.261 | 0.097 | 0.341 |
| C1=Amb. w/ Elevated | 0.129 | 0.510 | 0.152 | 0.209 |
| C1=Amb. w/ Minimal | 0.164 | 0.308 | 0.273 | 0.255 |
| C1=Pro-Science | 0.177 | 0.187 | 0.094 | 0.542 |
| 1 Note. Transition matrix values are the identical to Table 5, however Table 5 has the values rearranged by class for interpretation purposes. Classes may be arranged directly through Mplus syntax using start values. | ||||
t_matrix <- tibble(
"Time1" = c("C1=Anti-Science","C1=Amb. w/ Elevated","C1=Amb. w/ Minimal","C1=Pro-Science"),
"C2=Anti-Science" = c(t11B,t21B,t31B,t41B),
"C2=Amb. w/ Elevated" = c(t12B,t22B,t32B,t42B),
"C2=Amb. w/ Minimal" = c(t13B,t23B,t33B,t43B),
"C2=Pro-Science" = c(t14B,t24B,t34B,t44B))
t_matrix %>%
gt(rowname_col = "Time1") %>%
tab_stubhead(label = "7th grade") %>%
tab_header(
title = md("**MALES: Student transitions from 7th grade (rows) to 10th grade (columns)**")) %>%
fmt_number(2:5,decimals = 3) %>%
tab_spanner(label = "10th grade",columns = 2:5) %>%
tab_footnote(
footnote = md(
"*Note.* Transition matrix values are the identical to Table 5, however Table 5
has the values rearranged by class for interpretation purposes. Classes may be arranged
directly through Mplus syntax using start values."),
locations = cells_title())| MALES: Student transitions from 7th grade (rows) to 10th grade (columns)1 | ||||
| 7th grade |
10th grade
|
|||
|---|---|---|---|---|
| C2=Anti-Science | C2=Amb. w/ Elevated | C2=Amb. w/ Minimal | C2=Pro-Science | |
| C1=Anti-Science | 0.306 | 0.273 | 0.085 | 0.336 |
| C1=Amb. w/ Elevated | 0.131 | 0.532 | 0.133 | 0.205 |
| C1=Amb. w/ Minimal | 0.170 | 0.329 | 0.245 | 0.256 |
| C1=Pro-Science | 0.181 | 0.198 | 0.083 | 0.539 |
| 1 Note. Transition matrix values are the identical to Table 5, however Table 5 has the values rearranged by class for interpretation purposes. Classes may be arranged directly through Mplus syntax using start values. | ||||