DV:
B6 (political engagement)
IV:
G3 (democracy score), N1 (Taiwan identity), N4(cross-strait relation), P1 (PPID), P2 (party FT), Q1 (age), Q4 (education), Q16 (income)
Load the data and packages.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(haven)
library(ggplot2)
library(caret)
## 載入需要的套件:lattice
##
## 載入套件:'caret'
##
## 下列物件被遮斷自 'package:purrr':
##
## lift
data_2024 <- read_dta(
file = "C:/Users/Jack Chen/Downloads/TEDS/TEDS2024/Independence/TEDS2024_indQ.dta"
)
Data pre-processing.
## Subset.
subset_2024 <- data_2024[, c(
"B6m04",
"B6m06",
"B6m08",
"B6m09",
"B6m13",
"B6m14",
"G3",
"N1a",
"N4a",
"P1c",
"P2a",
"P2b",
"P2c",
"Q1a",
"Q4a",
"Q16")
]
subset_2024 <- as.data.frame(subset_2024)
## Convert FT scores to NPID using 1s and 0s.
## First, we convert 沒聽過(96), 不知道(98) and 拒答(95) to NAs.
subset_2024$P1c <- as.numeric(as.character(subset_2024$P1c))
subset_2024$P2a[subset_2024$P2a %in% c(95, 98)] <- NA
subset_2024$P2a[subset_2024$P2b %in% c(95, 98)] <- NA
subset_2024$P2a[subset_2024$P2c %in% c(95, 98)] <- NA
## We want to see if the respondents have PPID toward 3 major parties, so we remove other options.
subset_2024$P1c[
subset_2024$P1c %in% c(4, 9, 11, 17, 19, 20, 73, 95, 98, 99)
] <- NA
subset_2024$P2a <- ifelse(subset_2024[c("P2a")] <= 2, 1, 0)
subset_2024$P2b <- ifelse(subset_2024[c("P2b")] <= 2, 1, 0)
subset_2024$P2c <- ifelse(subset_2024[c("P2c")] <= 2, 1, 0)
## Convert to factor class.
as.factor(subset_2024$P2a)
## [1] <NA> 0 0 1 <NA> 0 0 0 0 0 0 1 0 <NA>
## [15] 0 0 0 1 1 0 0 1 0 1 0 0 0 1
## [29] 0 0 0 0 0 0 0 <NA> 0 0 0 0 0 0
## [43] 1 0 1 0 0 0 0 0 0 0 0 0 0 1
## [57] <NA> 0 0 0 0 0 0 0 0 0 0 0 1 0
## [71] 0 0 0 0 0 0 0 <NA> 0 <NA> <NA> 0 1 <NA>
## [85] 1 0 <NA> 0 1 0 1 0 0 1 0 1 0 0
## [99] 0 0 0 0 0 1 0 0 0 0 0 0 1 1
## [113] 0 0 0 1 1 <NA> 0 <NA> 1 0 0 0 0 0
## [127] 0 0 0 0 0 0 1 0 1 1 0 0 0 0
## [141] 0 0 0 0 0 1 0 0 0 0 0 0 0 1
## [155] 0 0 0 0 0 1 0 0 1 1 0 0 1 0
## [169] 0 1 0 1 0 1 1 0 0 0 0 <NA> 0 0
## [183] 0 0 <NA> 0 0 0 0 0 0 0 1 0 <NA> 0
## [197] 1 0 0 0 0 0 0 0 0 0 1 0 0 0
## [211] 0 1 0 0 0 0 0 1 1 0 0 0 1 1
## [225] 0 0 0 0 1 0 <NA> 0 0 0 1 <NA> 1 0
## [239] 1 <NA> 1 0 0 0 1 0 <NA> 0 1 0 0 <NA>
## [253] <NA> 0 0 0 0 1 1 <NA> 0 0 0 <NA> 0 0
## [267] 0 0 0 1 0 1 1 0 1 0 1 0 0 0
## [281] <NA> 0 0 0 0 0 0 0 0 0 0 1 0 0
## [295] 1 0 0 0 0 0 0 <NA> 0 0 0 0 0 0
## [309] 0 1 0 0 0 0 0 0 0 0 0 0 1 0
## [323] 0 0 0 0 0 0 0 0 0 1 0 0 0 0
## [337] 0 0 1 0 0 0 0 0 0 0 <NA> 0 0 0
## [351] 0 0 0 1 0 0 0 1 0 0 0 0 0 0
## [365] 0 0 1 0 1 1 0 0 0 0 1 0 0 0
## [379] 1 1 0 0 0 0 <NA> 0 0 <NA> 0 0 0 <NA>
## [393] 0 0 1 0 0 1 1 0 0 0 0 1 0 0
## [407] 0 0 0 0 0 1 0 0 0 1 0 0 0 0
## [421] 0 0 0 0 0 0 0 0 0 0 0 0 <NA> 1
## [435] 0 1 0 0 0 0 1 0 0 0 0 0 0 0
## [449] 0 0 1 0 1 0 0 1 1 0 0 0 0 0
## [463] 1 0 0 0 <NA> 0 <NA> 0 0 <NA> <NA> 0 1 0
## [477] 0 0 1 0 0 0 0 0 0 1 0 1 0 1
## [491] <NA> 1 0 0 0 0 0 0 0 0 0 1 0 0
## [505] 0 0 0 0 0 1 1 0 0 1 0 1 0 0
## [519] 0 0 0 0 0 0 1 0 0 0 0 0 1 0
## [533] <NA> 0 0 0 0 <NA> 0 1 0 1 0 0 0 0
## [547] <NA> 0 0 0 0 0 0 0 0 0 1 0 0 0
## [561] 0 0 0 1 1 0 <NA> 1 0 1 0 1 0 1
## [575] 0 1 0 1 0 0 0 1 0 0 0 0 0 0
## [589] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [603] 0 0 0 0 0 <NA> <NA> 0 0 <NA> 0 <NA> 1 0
## [617] <NA> 0 <NA> 0 0 0 0 1 0 0 0 <NA> 0 0
## [631] 0 0 0 0 1 0 0 0 0 0 0 0 0 0
## [645] 0 0 1 0 1 1 0 1 0 0 0 0 1 0
## [659] 1 0 0 1 0 0 0 0 0 0 0 0 0 0
## [673] 1 0 0 1 0 0 0 0 0 0 0 0 0 0
## [687] <NA> 0 0 0 1 0 0 1 1 0 0 0 0 <NA>
## [701] 0 0 <NA> 1 0 0 0 0 0 0 0 0 0 0
## [715] 0 0 1 0 0 0 0 0 0 0 0 <NA> 0 0
## [729] <NA> 0 0 0 0 0 0 0 1 1 0 0 <NA> <NA>
## [743] 0 0 0 0 0 1 <NA> 1 0 1 0 0 0 1
## [757] 1 0 0 0 0 1 0 0 1 0 1 1 0 0
## [771] 0 0 0 0 0 0 0 0 0 0 1 0 0 0
## [785] 1 1 0 1 0 0 0 0 <NA> 0 0 0 0 0
## [799] 0 0 1 0 0 0 0 0 <NA> 0 <NA> 0 0 0
## [813] 1 0 0 <NA> 0 0 0 0 0 0 0 0 0 0
## [827] 0 0 0 1 0 1 0 0 0 0 0 0 0 0
## [841] <NA> 0 0 0 0 0 0 0 0 1 1 0 <NA> 0
## [855] 0 0 0 1 0 0 0 1 0 1 0 <NA> 0 0
## [869] 0 0 0 0 1 0 <NA> 0 <NA> 0 1 0 1 0
## [883] 0 0 1 1 0 <NA> 0 0 0 1 0 <NA> 0 0
## [897] 0 0 0 1 0 1 1 <NA> 1 0 0 0 0 0
## [911] 1 0 0 0 0 0 0 0 0 1 0 0 0 0
## [925] 0 1 0 <NA> 1 0 0 <NA> <NA> 1 1 1 0 0
## [939] 0 0 1 0 0 1 0 0 0 0 0 <NA> 0 1
## [953] 0 <NA> 0 0 0 0 1 0 0 0 0 0 0 0
## [967] 1 0 0 0 0 1 1 0 0 0 0 0 0 1
## [981] <NA> 0 0 1 1 0 0 0 0 0 0 0 0 0
## [995] 0 <NA> 0 0 0 1 1 0 0 0 0 0 0 0
## [1009] 0 0 0 1 0 0 0 <NA> 1 0 1 <NA> 0 0
## [1023] 0 0 1 0 1 0 0 0 0 <NA> 0 1 0 0
## [1037] 0 1 0 0 0 0 0 1 0 0 0 1 0 1
## [1051] 0 0 0 1 0 0 0 0 0 0 0 0 1 1
## [1065] 0 1 0 1 0 0 0 0 0 0 1 0 1 0
## [1079] 1 1 0 0 1 0 <NA> 0 0 0 0 0 0 1
## [1093] 0 0 0 0 0 1 0 0 1 <NA> 0 0 0 0
## [1107] 0 0 0 0 0 0 0 0 1 0 0 1 0 1
## [1121] 0 0 0 0 0 0 0 0 1 1 <NA> 1 0 0
## [1135] 1 0 1 0 0 0 1 0 <NA> 0 0 0 0 1
## [1149] 0 1 0 1 0 0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 0 0 0 0 1 1 0 0 1 0 0
## [1111] 0 0 1 0 1 0 1 1 0 1 0 1 1 1 0 0 0 0 1 1 0 1 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0
## [1148] 1 0 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0 1 0
## [1185] 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 0 0 1 1 1 0 0
## Levels: 0 1
## OHE for PPID.
str(subset_2024$P1c)
## num [1:1206] 2 41 NA 2 NA 2 1 2 1 41 ...
subset_2024_ohe <- subset_2024 %>%
mutate(value = 1) %>%
spread(P1c, value, fill = 0)
subset_2024_ohe <- subset_2024_ohe %>%
rename(KMT = `1`) %>%
rename(DPP = `2`) %>%
rename(TPP = `41`)
colnames(subset_2024_ohe)
## [1] "B6m04" "B6m06" "B6m08" "B6m09" "B6m13" "B6m14" "G3" "N1a" "N4a"
## [10] "P2a" "P2b" "P2c" "Q1a" "Q4a" "Q16" "KMT" "DPP" "TPP"
## [19] "<NA>"
subset_2024_ohe <- subset_2024_ohe[, -which(names(subset_2024_ohe) == "<NA>")]
## Convert matrix class to numeric class.
sapply(subset_2024_ohe, class) ## Which one is matrix?
## $B6m04
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $B6m06
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $B6m08
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $B6m09
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $B6m13
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $B6m14
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $G3
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $N1a
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $N4a
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $P2a
## [1] "matrix" "array"
##
## $P2b
## [1] "matrix" "array"
##
## $P2c
## [1] "matrix" "array"
##
## $Q1a
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $Q4a
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $Q16
## [1] "haven_labelled" "vctrs_vctr" "double"
##
## $KMT
## [1] "numeric"
##
## $DPP
## [1] "numeric"
##
## $TPP
## [1] "numeric"
subset_2024_ohe <- subset_2024_ohe %>%
mutate(across(where(is.matrix), ~ as.numeric(.)))
## Remove all irrevelant values.
for (n in names(subset_2024)) {
subset_2024[[n]][subset_2024[[n]] %in% c(95, 96, 97, 98, 99)] <- NA
}
Model fitting.
## First Model fitting.
## B6m04 是否擔任助選員或義工?
logit.B6m04 <- glm(B6m04 ~ P2a + P2b + P2c + KMT + DPP + TPP,
data = subset_2024_ohe,
family = binomial(link = "logit"),
na.action = na.omit
)
## B6m06 參加候選人的後援會?
logit.B6m06 <- glm(B6m06 ~ P2a + P2b + P2c + KMT + DPP + TPP,
data = subset_2024_ohe,
family = binomial(link = "logit"),
na.action = na.omit
)
## B6m08 遊說或勸說別人投票給某位候選人?
logit.B6m08 <- glm(B6m08 ~ P2a + P2b + P2c + KMT + DPP + TPP,
data = subset_2024_ohe,
family = binomial(link = "logit"),
na.action = na.omit
)
## B6m09 捐款?
logit.B6m09 <- glm(B6m09 ~ P2a + P2b + P2c + KMT + DPP + TPP,
data = subset_2024_ohe,
family = binomial(link = "logit"),
na.action = na.omit
)
## B6m013 主動參加造勢活動?
logit.B6m13 <- glm(B6m13 ~ P2a + P2b + P2c + KMT + DPP + TPP,
data = subset_2024_ohe,
family = binomial(link = "logit"),
na.action = na.omit
)
## B6m014 瀏覽候選人網站?
logit.B6m14 <- glm(B6m14 ~ P2a + P2b + P2c + KMT + DPP + TPP,
data = subset_2024_ohe,
family = binomial(link = "logit"),
na.action = na.omit
)
summary(logit.B6m04)
##
## Call:
## glm(formula = B6m04 ~ P2a + P2b + P2c + KMT + DPP + TPP, family = binomial(link = "logit"),
## data = subset_2024_ohe, na.action = na.omit)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -5.0303 0.7302 -6.889 5.61e-12 ***
## P2a -1.0414 0.6820 -1.527 0.1268
## P2b 0.6961 0.6618 1.052 0.2929
## P2c 0.7381 0.5095 1.449 0.1474
## KMT 0.4364 0.8872 0.492 0.6228
## DPP 1.3991 0.8109 1.725 0.0845 .
## TPP 0.8750 0.9531 0.918 0.3586
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 208.73 on 1122 degrees of freedom
## Residual deviance: 200.00 on 1116 degrees of freedom
## (因為不存在,82 個觀察量被刪除了)
## AIC: 214
##
## Number of Fisher Scoring iterations: 7
summary(logit.B6m06)
##
## Call:
## glm(formula = B6m06 ~ P2a + P2b + P2c + KMT + DPP + TPP, family = binomial(link = "logit"),
## data = subset_2024_ohe, na.action = na.omit)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -4.327595 0.526664 -8.217 <2e-16 ***
## P2a -0.742515 0.605441 -1.226 0.2200
## P2b 0.934085 0.562539 1.660 0.0968 .
## P2c 0.375042 0.454025 0.826 0.4088
## KMT 0.146492 0.670572 0.218 0.8271
## DPP 0.906274 0.623259 1.454 0.1459
## TPP 0.007885 0.804240 0.010 0.9922
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 254.65 on 1122 degrees of freedom
## Residual deviance: 247.54 on 1116 degrees of freedom
## (因為不存在,82 個觀察量被刪除了)
## AIC: 261.54
##
## Number of Fisher Scoring iterations: 7
summary(logit.B6m08)
##
## Call:
## glm(formula = B6m08 ~ P2a + P2b + P2c + KMT + DPP + TPP, family = binomial(link = "logit"),
## data = subset_2024_ohe, na.action = na.omit)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -3.7769 0.3963 -9.530 <2e-16 ***
## P2a 0.5980 0.3186 1.877 0.0606 .
## P2b 0.4857 0.3442 1.411 0.1582
## P2c -0.1634 0.3296 -0.496 0.6201
## KMT 0.6506 0.4861 1.338 0.1808
## DPP 1.0362 0.4532 2.287 0.0222 *
## TPP 1.1952 0.4800 2.490 0.0128 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 507.68 on 1122 degrees of freedom
## Residual deviance: 489.56 on 1116 degrees of freedom
## (因為不存在,82 個觀察量被刪除了)
## AIC: 503.56
##
## Number of Fisher Scoring iterations: 6
summary(logit.B6m09)
##
## Call:
## glm(formula = B6m09 ~ P2a + P2b + P2c + KMT + DPP + TPP, family = binomial(link = "logit"),
## data = subset_2024_ohe, na.action = na.omit)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -5.9973 1.0594 -5.661 1.51e-08 ***
## P2a 0.3628 0.5316 0.682 0.4950
## P2b 0.9002 0.5701 1.579 0.1144
## P2c 0.5452 0.8099 0.673 0.5009
## KMT -0.4765 1.4501 -0.329 0.7424
## DPP 1.2295 1.1405 1.078 0.2810
## TPP 2.8942 1.0982 2.635 0.0084 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 192.69 on 1122 degrees of freedom
## Residual deviance: 162.45 on 1116 degrees of freedom
## (因為不存在,82 個觀察量被刪除了)
## AIC: 176.45
##
## Number of Fisher Scoring iterations: 8
summary(logit.B6m13)
##
## Call:
## glm(formula = B6m13 ~ P2a + P2b + P2c + KMT + DPP + TPP, family = binomial(link = "logit"),
## data = subset_2024_ohe, na.action = na.omit)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -6.1914 1.0315 -6.002 1.94e-09 ***
## P2a 0.3981 0.3401 1.171 0.24178
## P2b 1.1648 0.4355 2.674 0.00749 **
## P2c 0.7457 0.3400 2.193 0.02830 *
## KMT 2.1273 1.0607 2.006 0.04490 *
## DPP 3.1119 1.0433 2.983 0.00286 **
## TPP 2.9215 1.0644 2.745 0.00605 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 444.98 on 1122 degrees of freedom
## Residual deviance: 399.22 on 1116 degrees of freedom
## (因為不存在,82 個觀察量被刪除了)
## AIC: 413.22
##
## Number of Fisher Scoring iterations: 8
summary(logit.B6m14)
##
## Call:
## glm(formula = B6m14 ~ P2a + P2b + P2c + KMT + DPP + TPP, family = binomial(link = "logit"),
## data = subset_2024_ohe, na.action = na.omit)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -2.16945 0.20248 -10.714 < 2e-16 ***
## P2a 0.49589 0.20173 2.458 0.01396 *
## P2b 0.02001 0.22079 0.091 0.92778
## P2c -0.14890 0.20810 -0.716 0.47428
## KMT 0.25303 0.27461 0.921 0.35684
## DPP 0.79914 0.24618 3.246 0.00117 **
## TPP 1.64279 0.26064 6.303 2.92e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1113.9 on 1122 degrees of freedom
## Residual deviance: 1041.3 on 1116 degrees of freedom
## (因為不存在,82 個觀察量被刪除了)
## AIC: 1055.3
##
## Number of Fisher Scoring iterations: 4
## Define a function for the next step.
fitting_logit <- function(data, y, x) {
formula <- as.formula(paste(y, "~", paste(x, collapse = " + ")))
glm(formula,
data = data,
family = binomial(link = "logit"),
na.action = na.omit
)
}
## Define Y variable.
y_vars <- c("B6m04", "B6m06", "B6m08", "B6m09", "B6m13", "B6m14")
all_vars <- names(subset_2024_ohe)
x_pool <- setdiff(all_vars, y_vars)
## Second Model fitting.
model_list <- list()
for (y in y_vars) {
model_data <- subset_2024_ohe[, c(y, x_pool)]
model_list[[paste0("full_logit_", y)]] <- fitting_logit(
model_data, y = y, x = x_pool
)
}
## Warning: glm.fit:擬合機率算出來是數值零或一
## Warning: glm.fit:擬合機率算出來是數值零或一
## Warning: glm.fit:擬合機率算出來是數值零或一
## Warning: glm.fit:擬合機率算出來是數值零或一
library(modelsummary)
modelsummary(model_list, stars = T)
| full_logit_B6m04 | full_logit_B6m06 | full_logit_B6m08 | full_logit_B6m09 | full_logit_B6m13 | full_logit_B6m14 | |
|---|---|---|---|---|---|---|
| + p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001 | ||||||
| (Intercept) | -3.831* | -1.360 | -3.281*** | -4.421* | -5.371*** | -7.271*** |
| (1.754) | (1.241) | (0.916) | (2.022) | (1.330) | (0.687) | |
| G3 | -0.103 | 0.003 | -0.003 | -0.156 | -0.012 | -0.030 |
| (0.110) | (0.021) | (0.021) | (0.123) | (0.029) | (0.030) | |
| N1a | -0.043 | -0.037 | 0.018* | 0.034** | 0.008 | 0.011 |
| (0.178) | (0.125) | (0.009) | (0.012) | (0.012) | (0.007) | |
| N4a | -0.212 | -0.276+ | -0.316** | -0.206 | -0.037 | -0.018 |
| (0.178) | (0.145) | (0.106) | (0.216) | (0.068) | (0.015) | |
| P2a | -1.012 | -0.770 | 0.622+ | 0.321 | 0.415 | 0.439* |
| (0.686) | (0.617) | (0.323) | (0.557) | (0.346) | (0.214) | |
| P2b | 0.436 | 0.687 | 0.358 | 0.432 | 1.133* | -0.013 |
| (0.707) | (0.576) | (0.354) | (0.634) | (0.447) | (0.238) | |
| P2c | 0.798 | 0.157 | -0.174 | 0.528 | 0.599+ | 0.241 |
| (0.525) | (0.468) | (0.342) | (0.838) | (0.350) | (0.227) | |
| Q1a | -0.001 | -0.051** | -0.005 | -0.024 | -0.029** | 0.037*** |
| (0.017) | (0.016) | (0.009) | (0.018) | (0.011) | (0.006) | |
| Q4a | 0.105 | 0.139+ | 0.146* | 0.193 | 0.132* | 0.277*** |
| (0.098) | (0.079) | (0.059) | (0.123) | (0.060) | (0.045) | |
| Q16 | -0.028 | 0.006 | -0.006 | -0.002 | 0.002 | -0.007+ |
| (0.026) | (0.006) | (0.006) | (0.011) | (0.005) | (0.004) | |
| KMT | 0.266 | -0.270 | 0.457 | -1.079 | 1.802+ | 0.656* |
| (0.914) | (0.688) | (0.505) | (1.512) | (1.070) | (0.303) | |
| DPP | 1.245 | 0.684 | 0.808+ | 1.333 | 3.020** | 0.935*** |
| (0.845) | (0.649) | (0.462) | (1.206) | (1.054) | (0.269) | |
| TPP | 0.649 | 0.252 | 0.920+ | 2.838* | 2.883** | 1.262*** |
| (0.965) | (0.826) | (0.490) | (1.114) | (1.072) | (0.278) | |
| Num.Obs. | 1123 | 1123 | 1123 | 1123 | 1123 | 1123 |
| AIC | 218.3 | 255.3 | 494.7 | 175.1 | 414.1 | 930.2 |
| BIC | 283.6 | 320.6 | 560.0 | 240.4 | 479.4 | 995.5 |
| Log.Lik. | -96.135 | -114.646 | -234.343 | -74.553 | -194.055 | -452.081 |
| RMSE | 0.13 | 0.15 | 0.23 | 0.12 | 0.21 | 0.36 |