Small context note. I am working with Latent Class models, and there is a common phenomenon called label switching which makes that sometimes some of the classes flip across models. This is what I want to correct, I want consistency across the models. So I want to take one parameter, say forest_mu_x where x is the label of the class (with x being either a, b, or c), and sort the parameter estimates from the largest to smallest and then flip all the rest of the classes accordingly. Hence, class a is the one that corresponds to the highest values, class b is the second largest, and so forth.
Below you might find the data.frame that is the result of the estimation of several Latent Class models. As you will see, in the last column (LC-MIXL-3-A-5) the label switch occurred, and classes a and b switched (with respect to the results of all the other models).
Do you have any suggestions to get rid of this unfortunate behavior?
df
Variables LC-MIXL-3-A-0 LC-MIXL-3-A-1 LC-MIXL-3-A-2 LC-MIXL-3-A-3 LC-MIXL-3-A-4 LC-MIXL-3-A-5
1 asc1_a 0.03(0.25) 0.06(0.25) 0.08(0.25) 0.08(0.25) 0.05(0.25) -3.47(0.54)***
2 asc2_a 0.38(0.07)*** 0.37(0.07)*** 0.37(0.07)*** 0.37(0.07)*** 0.37(0.07)*** -0.08(0.11)
3 asc1_b -3.35(0.50)*** 1.91(0.74)** -3.46(0.51)*** -3.48(0.52)*** -3.47(0.53)*** 0.06(0.26)
4 asc2_b -0.11(0.11) -0.08(0.22) -0.07(0.11) -0.07(0.11) -0.08(0.11) 0.37(0.07)***
5 asc1_c 1.94(0.73)** -3.39(0.52)*** 1.89(0.74)* 1.88(0.75)* 1.90(0.74)* 1.89(0.75)*
6 asc2_c -0.07(0.22) -0.07(0.11) -0.08(0.22) -0.08(0.22) -0.06(0.23) -0.07(0.23)
7 forest_mu_a -0.04(0.03) -0.04(0.03) -0.04(0.03) -0.04(0.03) -0.04(0.03) -0.07(0.03)*
8 forest_mu_b -0.07(0.03)* -0.49(0.09)*** -0.07(0.03)* -0.07(0.03)* -0.07(0.03)* -0.04(0.03)
9 forest_mu_c -0.49(0.09)*** -0.07(0.03)* -0.48(0.10)*** -0.48(0.10)*** -0.48(0.10)*** -0.48(0.10)***
10 forest_sd_a -0.15(0.03)*** -0.14(0.02)*** 0.14(0.02)*** 0.14(0.02)*** -0.14(0.02)*** -0.12(0.04)**
11 forest_sd_b -0.11(0.05)* 0.54(0.08)*** -0.12(0.03)*** -0.12(0.03)*** -0.12(0.04)*** -0.14(0.02)***
12 forest_sd_c 0.55(0.08)*** 0.12(0.04)** 0.54(0.08)*** 0.54(0.08)*** 0.53(0.09)*** 0.53(0.09)***
13 morbidity_mu_a -0.10(0.14) -0.08(0.14) -0.10(0.13) -0.10(0.13) -0.09(0.13) -0.44(0.14)**
14 morbidity_mu_b -0.43(0.16)** -3.28(0.49)*** -0.43(0.14)** -0.43(0.14)** -0.44(0.14)** -0.09(0.14)
15 morbidity_mu_c -3.27(0.50)*** -0.44(0.14)** -3.27(0.50)*** -3.27(0.50)*** -3.24(0.53)*** -3.24(0.54)***
16 morbidity_sd_a -0.61(0.16)*** -0.53(0.10)*** 0.54(0.09)*** 0.54(0.09)*** -0.55(0.10)*** -0.78(0.16)***
17 morbidity_sd_b 0.73(0.21)*** -1.61(0.27)*** 0.77(0.15)*** 0.78(0.15)*** -0.78(0.16)*** -0.55(0.10)***
18 morbidity_sd_c -1.57(0.27)*** -0.79(0.15)*** -1.60(0.28)*** -1.60(0.28)*** -1.58(0.28)*** -1.58(0.29)***
19 cost_scale_a -0.03(0.02) -0.03(0.02) -0.03(0.02) -0.03(0.02) -0.03(0.02) -0.23(0.04)***
20 cost_scale_b -0.23(0.04)*** -2.45(0.43)*** -0.22(0.04)*** -0.22(0.04)*** -0.23(0.04)*** -0.03(0.02)
21 cost_scale_c -2.40(0.42)*** -0.22(0.04)*** -2.47(0.44)*** -2.48(0.45)*** -2.42(0.50)*** -2.43(0.53)***
22 land_a -0.01(0.01) -0.01(0.01) -0.01(0.01) -0.01(0.01) -0.01(0.01) -0.02(0.01)*
23 land_b -0.02(0.01)* 0.00(0.02) -0.02(0.01)* -0.02(0.01)* -0.02(0.01)* -0.01(0.01)
24 land_c 0.00(0.02) -0.02(0.01)* 0.00(0.02) 0.00(0.02) 0.00(0.02) 0.00(0.02)
25 location_a 0.20(0.14) 0.20(0.14) 0.18(0.14) 0.18(0.14) 0.19(0.14) -0.77(0.17)***
26 location_b -0.84(0.17)*** -1.39(0.37)*** -0.75(0.17)*** -0.76(0.17)*** -0.77(0.17)*** 0.19(0.14)
27 location_c -1.38(0.36)*** -0.77(0.17)*** -1.38(0.37)*** -1.38(0.37)*** -1.36(0.37)*** -1.36(0.37)***
28 delta_b -0.41(0.19)* -0.40(0.15)** 1.06(0.47)* 1.25(0.60)* 1.04(0.61) -1.05(0.64)
29 delta_c -0.38(0.15)* -0.64(0.19)*** -0.50(0.45) -0.19(0.51) -0.48(0.53) -1.50(0.70)*
30 gamma_b_nvisits 0.34(0.41) 1.56(0.44)*** 1.57(0.44)*** 1.44(0.46)** -1.44(0.46)**
31 gamma_b_age -0.04(0.01)*** -0.04(0.01)*** -0.04(0.01)*** 0.04(0.01)***
32 gamma_b_elec_bill -0.01(0.01) -0.01(0.01) 0.01(0.01)
33 gamma_b_signed_oath 0.58(0.33) -0.58(0.34)
34 gamma_b_income 0.00(0.04)
35 gamma_c_nvisits 1.33(0.41)** 0.33(0.41) 0.35(0.42) 0.20(0.42) -1.23(0.40)**
36 gamma_c_age 0.00(0.01) 0.00(0.01) 0.00(0.01) 0.04(0.01)**
37 gamma_c_elec_bill -0.01(0.01) -0.01(0.01) 0.00(0.01)
38 gamma_c_signed_oath 0.83(0.28)** 0.26(0.34)
39 gamma_c_income -0.01(0.05)
40 N 4395 4395 4395 4395 4395 4395
41 LL -3647.36 -3639.15 -3629.54 -3629.03 -3624.06 -3624.04
42 Num.Params 29 31 33 35 37 39
43 AIC 7352.71 7340.31 7325.08 7328.05 7322.12 7326.08
44 BIC 7537.97 7538.34 7535.89 7551.64 7558.49 7575.22
45 class_a \\%42.5 \\%41.12 \\%41.4 \\%41.57 \\%41.69 \\%29.37
46 class_b \\%28.31 \\%29.13 \\%29.73 \\%29.58 \\%29.34 \\%41.7
47 class_c \\%29.2 \\%29.75 \\%28.87 \\%28.85 \\%28.97 \\%28.93
>
Original data
df <- structure(list(Variables = c("asc1_a", "asc2_a", "asc1_b", "asc2_b","asc1_c", "asc2_c", "forest_mu_a", "forest_mu_b", "forest_mu_c", "forest_sd_a", "forest_sd_b", "forest_sd_c", "morbidity_mu_a", "morbidity_mu_b", "morbidity_mu_c", "morbidity_sd_a", "morbidity_sd_b",
"morbidity_sd_c", "cost_scale_a", "cost_scale_b", "cost_scale_c","land_a", "land_b", "land_c", "location_a", "location_b", "location_c", "delta_b", "delta_c", "gamma_b_nvisits", "gamma_b_age", "gamma_b_elec_bill","gamma_b_signed_oath", "gamma_b_income", "gamma_c_nvisits", "gamma_c_age",
"gamma_c_elec_bill", "gamma_c_signed_oath", "gamma_c_income", "N", "LL", "Num.Params", "AIC", "BIC", "class_a", "class_b", "class_c"), `LC-MIXL-3-A-0` = structure(c(" 0.03(0.25) ", " 0.38(0.07)***", "-3.35(0.50)***", "-0.11(0.11) ", " 1.94(0.73)** ", "-0.07(0.22) ", "-0.04(0.03) ", "-0.07(0.03)* ", "-0.49(0.09)***", "-0.15(0.03)***",
"-0.11(0.05)* ", " 0.55(0.08)***", "-0.10(0.14) ", "-0.43(0.16)** ", "-3.27(0.50)***", "-0.61(0.16)***", " 0.73(0.21)***", "-1.57(0.27)***", "-0.03(0.02) ", "-0.23(0.04)***", "-2.40(0.42)***", "-0.01(0.01) ", "-0.02(0.01)* ", " 0.00(0.02) ", " 0.20(0.14) ", "-0.84(0.17)***",
"-1.38(0.36)***", "-0.41(0.19)* ", "-0.38(0.15)* ", "", "", "", "", "", "", "", "", "", "", "4395", "-3647.36", "29", "7352.71", "7537.97", "\\%42.5", "\\%28.31", "\\%29.2"), dim = c(47L, 1L), dimnames = list(NULL, NULL)), `LC-MIXL-3-A-1` = structure(c(" 0.06(0.25) ",
" 0.37(0.07)***", " 1.91(0.74)** ", "-0.08(0.22) ", "-3.39(0.52)***","-0.07(0.11) ", "-0.04(0.03) ", "-0.49(0.09)***", "-0.07(0.03)* ", "-0.14(0.02)***", " 0.54(0.08)***", " 0.12(0.04)** ", "-0.08(0.14) ","-3.28(0.49)***", "-0.44(0.14)** ", "-0.53(0.10)***", "-1.61(0.27)***",
"-0.79(0.15)***", "-0.03(0.02) ", "-2.45(0.43)***", "-0.22(0.04)***","-0.01(0.01) ", " 0.00(0.02) ", "-0.02(0.01)* ", " 0.20(0.14) ", "-1.39(0.37)***", "-0.77(0.17)***", "-0.40(0.15)** ", "-0.64(0.19)***"," 0.34(0.41) ", "", "", "", "", " 1.33(0.41)** ", "", "", "",
"", "4395", "-3639.15", "31", "7340.31", "7538.34", "\\%41.12", "\\%29.13", "\\%29.75"), dim = c(47L, 1L), dimnames = list(NULL, NULL)), `LC-MIXL-3-A-2` = structure(c(" 0.08(0.25) ", " 0.37(0.07)***","-3.46(0.51)***", "-0.07(0.11) ", " 1.89(0.74)* ", "-0.08(0.22) ",
"-0.04(0.03) ", "-0.07(0.03)* ", "-0.48(0.10)***", " 0.14(0.02)***", "-0.12(0.03)***", " 0.54(0.08)***", "-0.10(0.13) ", "-0.43(0.14)** ", "-3.27(0.50)***", " 0.54(0.09)***", " 0.77(0.15)***", "-1.60(0.28)***", "-0.03(0.02) ", "-0.22(0.04)***", "-2.47(0.44)***", "-0.01(0.01) ",
"-0.02(0.01)* ", " 0.00(0.02) ", " 0.18(0.14) ", "-0.75(0.17)***", "-1.38(0.37)***", " 1.06(0.47)* ", "-0.50(0.45) ", " 1.56(0.44)***", "-0.04(0.01)***", "", "", "", " 0.33(0.41) ", " 0.00(0.01) ", "", "", "", "4395", "-3629.54", "33", "7325.08", "7535.89", "\\%41.4",
"\\%29.73", "\\%28.87"), dim = c(47L, 1L), dimnames = list(NULL, NULL)), `LC-MIXL-3-A-3` = structure(c(" 0.08(0.25) ", " 0.37(0.07)***", "-3.48(0.52)***", "-0.07(0.11) ", " 1.88(0.75)* ", "-0.08(0.22) ", "-0.04(0.03) ", "-0.07(0.03)* ", "-0.48(0.10)***", " 0.14(0.02)***",
"-0.12(0.03)***", " 0.54(0.08)***", "-0.10(0.13) ", "-0.43(0.14)** ", "-3.27(0.50)***", " 0.54(0.09)***", " 0.78(0.15)***", "-1.60(0.28)***", "-0.03(0.02) ", "-0.22(0.04)***", "-2.48(0.45)***", "-0.01(0.01) ", "-0.02(0.01)* ", " 0.00(0.02) ", " 0.18(0.14) ", "-0.76(0.17)***",
"-1.38(0.37)***", " 1.25(0.60)* ", "-0.19(0.51) ", " 1.57(0.44)***", "-0.04(0.01)***", "-0.01(0.01) ", "", "", " 0.35(0.42) ", " 0.00(0.01) ", "-0.01(0.01) ", "", "", "4395", "-3629.03", "35", "7328.05", "7551.64", "\\%41.57", "\\%29.58", "\\%28.85"
), dim = c(47L, 1L), dimnames = list(NULL, NULL)), `LC-MIXL-3-A-4` = structure(c(" 0.05(0.25) "," 0.37(0.07)***", "-3.47(0.53)***", "-0.08(0.11) ", " 1.90(0.74)* ",
"-0.06(0.23) ", "-0.04(0.03) ", "-0.07(0.03)* ", "-0.48(0.10)***", "-0.14(0.02)***", "-0.12(0.04)***", " 0.53(0.09)***", "-0.09(0.13) ", "-0.44(0.14)** ", "-3.24(0.53)***", "-0.55(0.10)***", "-0.78(0.16)***", "-1.58(0.28)***", "-0.03(0.02) ", "-0.23(0.04)***", "-2.42(0.50)***",
"-0.01(0.01) ", "-0.02(0.01)* ", " 0.00(0.02) ", " 0.19(0.14) ", "-0.77(0.17)***", "-1.36(0.37)***", " 1.04(0.61) ", "-0.48(0.53) ", " 1.44(0.46)** ", "-0.04(0.01)***", "-0.01(0.01) ", " 0.58(0.33) ", "", " 0.20(0.42) ", " 0.00(0.01) ", "-0.01(0.01) ", " 0.83(0.28)** ",
"", "4395", "-3624.06", "37", "7322.12", "7558.49", "\\%41.69", "\\%29.34", "\\%28.97"), dim = c(47L, 1L), dimnames = list(NULL, NULL)), `LC-MIXL-3-A-5` = structure(c("-3.47(0.54)***", "-0.08(0.11) ", " 0.06(0.26) ", " 0.37(0.07)***", " 1.89(0.75)* ", "-0.07(0.23) ",
"-0.07(0.03)* ", "-0.04(0.03) ", "-0.48(0.10)***", "-0.12(0.04)** ", "-0.14(0.02)***", " 0.53(0.09)***", "-0.44(0.14)** ", "-0.09(0.14) ", "-3.24(0.54)***", "-0.78(0.16)***", "-0.55(0.10)***", "-1.58(0.29)***", "-0.23(0.04)***", "-0.03(0.02) ", "-2.43(0.53)***", "-0.02(0.01)* ",
"-0.01(0.01) ", " 0.00(0.02) ", "-0.77(0.17)***", " 0.19(0.14) ", "-1.36(0.37)***", "-1.05(0.64) ", "-1.50(0.70)* ", "-1.44(0.46)** ", " 0.04(0.01)***", " 0.01(0.01) ", "-0.58(0.34) ", " 0.00(0.04) ", "-1.23(0.40)** ", " 0.04(0.01)** ", " 0.00(0.01) ", " 0.26(0.34) ",
"-0.01(0.05) ", "4395", "-3624.04", "39", "7326.08", "7575.22", "\\%29.37", "\\%41.7", "\\%28.93"), dim = c(47L, 1L), dimnames = list(NULL, NULL))), row.names = c(NA, -47L), class = "data.frame")