research0812

library(readxl)
data <- read_excel("~/Desktop/data/research0812.xlsx")
data[data == "NA"] <- NA
data[data == ""] <- NA
names(data) <- trimws(names(data))
data$Year <- as.numeric(data$Year)

data$`Political stability` <- as.numeric(data$`Political stability`)
data$`FSI(100-FSI)` <- as.numeric(data$`FSI(100-FSI)`)
data$Democracy <- as.numeric(data$Democracy)

data$Post2022 <- as.numeric(data$Post2022)
data$SCS <- as.numeric(data$SCS)

data$`Trade China` <- as.numeric(data$`Trade China`)
data$`Sec US` <- as.numeric(data$`Sec US`)

data$`Dis Elite Preference` <- as.numeric(data$`Dis Elite Preference`)
data$`Dis Unga Alignment` <- as.numeric(data$`Dis Unga Alignment`)
model_elite <- lm(

`Dis Elite Preference` ~

Democracy +
Post2022 +
SCS +

`Political stability` +
`FSI(100-FSI)` +
`Trade China` +
`Sec US`,

data = data

)

summary(model_elite)

Call:
lm(formula = `Dis Elite Preference` ~ Democracy + Post2022 + 
    SCS + `Political stability` + `FSI(100-FSI)` + `Trade China` + 
    `Sec US`, data = data)

Residuals:
   Min     1Q Median     3Q    Max 
-34.85 -10.16  -3.21  11.65  43.05 

Coefficients:
                       Estimate Std. Error t value Pr(>|t|)  
(Intercept)            90.44805   48.92342   1.849   0.0737 .
Democracy              -7.26603    3.47952  -2.088   0.0448 *
Post2022               -9.19763    6.83639  -1.345   0.1880  
SCS                    17.79026    8.65478   2.056   0.0481 *
`Political stability`  -0.11770    0.74422  -0.158   0.8753  
`FSI(100-FSI)`        0.02064    0.53026   0.039   0.9692  
`Trade China`         -17.09603   36.37006  -0.470   0.6415  
`Sec US`              -24.36829   27.06405  -0.900   0.3746  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 20.67 on 32 degrees of freedom
  (40 observations deleted due to missingness)
Multiple R-squared:  0.3808,    Adjusted R-squared:  0.2454 
F-statistic: 2.811 on 7 and 32 DF,  p-value: 0.02121
library(broom)
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.5.2
####################################################
## Forest Plot for Elite Preference Distance
####################################################

# 提取迴歸結果
coef_elite <- tidy(model_elite, conf.int = TRUE)

# 移除截距
coef_elite <- subset(
  coef_elite,
  term != "(Intercept)"
)

# 修改變數名稱
coef_elite$term <- c(
  "Democracy",
  "Post-2022",
  "South China Sea Claimant",
  "Political Stability",
  "Government Legitimacy",
  "Trade Dependence on China",
  "Security Ties with the U.S."
)

# 設定顯示順序
coef_elite$term <- factor(
  coef_elite$term,
  levels = rev(c(
    "Democracy",
    "Post-2022",
    "South China Sea Claimant",
    "Political Stability",
    "Government Legitimacy",
    "Trade Dependence on China",
    "Security Ties with the U.S."
  ))
)

# 畫森林圖
elite_plot <- ggplot(
  coef_elite,
  aes(
    x = estimate,
    y = term
  )
) +

  geom_vline(
    xintercept = 0,
    linetype = "dashed",
    colour = "grey65",
    linewidth = 0.6
  ) +

  geom_errorbarh(
    aes(
      xmin = conf.low,
      xmax = conf.high
    ),
    height = 0.16,
    linewidth = 0.65
  ) +

  geom_point(
    size = 2.6
  ) +

  labs(
    title = "OLS Estimates for Elite Preference Distance",
    x = "Coefficient Estimate (95% Confidence Interval)",
    y = NULL
  ) +

  theme_classic(base_size = 14) +

  theme(
    plot.title = element_text(
      face = "bold",
      hjust = 0.5,
      size = 18
    ),

    axis.text.y = element_text(
      size = 12
    ),

    axis.title.x = element_text(
      size = 13
    ),

    # 增加四周留白,尤其左側
    plot.margin = margin(
      t = 20,
      r = 30,
      b = 20,
      l = 80
    )
  )
Warning: `geom_errorbarh()` was deprecated in ggplot2 4.0.0.
ℹ Please use the `orientation` argument of `geom_errorbar()` instead.
# 顯示圖
elite_plot
`height` was translated to `width`.

# 輸出 PDF
ggsave(
  filename = "elite_forest.pdf",
  plot = elite_plot,
  width = 10,
  height = 6.5,
  units = "in"
)
`height` was translated to `width`.
library(broom)
library(ggplot2)

####################################################
## Forest Plot for Elite Preference Distance
####################################################

# 提取迴歸結果
coef_elite <- tidy(model_elite, conf.int = TRUE)

# 移除截距
coef_elite <- subset(
  coef_elite,
  term != "(Intercept)"
)

# 修改變數名稱
coef_elite$term <- c(
  "Democracy",
  "Post-2022",
  "South China Sea Claimant",
  "Political Stability",
  "Government Legitimacy",
  "Trade Dependence on China",
  "Security Ties with the U.S."
)

# 設定顯示順序
coef_elite$term <- factor(
  coef_elite$term,
  levels = rev(c(
    "Democracy",
    "Post-2022",
    "South China Sea Claimant",
    "Political Stability",
    "Government Legitimacy",
    "Trade Dependence on China",
    "Security Ties with the U.S."
  ))
)

# 畫森林圖
elite_plot <- ggplot(
  coef_elite,
  aes(
    x = estimate,
    y = term
  )
) +

  geom_vline(
    xintercept = 0,
    linetype = "dashed",
    colour = "grey65",
    linewidth = 0.6
  ) +

  geom_errorbarh(
    aes(
      xmin = conf.low,
      xmax = conf.high
    ),
    height = 0.16,
    linewidth = 0.65
  ) +

  geom_point(
    size = 2.6
  ) +

  labs(
    title = "OLS Estimates for Elite Preference Distance",
    x = "Coefficient Estimate (95% Confidence Interval)",
    y = NULL
  ) +

  theme_classic(base_size = 14) +

  theme(
    plot.title = element_text(
      face = "bold",
      hjust = 0.5,
      size = 18
    ),

    axis.text.y = element_text(
      size = 12
    ),

    axis.title.x = element_text(
      size = 13
    ),

    # 增加四周留白,尤其左側
    plot.margin = margin(
      t = 20,
      r = 30,
      b = 20,
      l = 80
    )
  )

# 顯示圖
elite_plot
`height` was translated to `width`.

# 輸出 PDF
ggsave(
  filename = "elite_forest.pdf",
  plot = elite_plot,
  width = 10,
  height = 6.5,
  units = "in"
)
`height` was translated to `width`.
model_unga <- lm(

`Dis Unga Alignment` ~

Democracy +
Post2022 +
SCS +

`Political stability` +
`FSI(100-FSI)` +
`Trade China` +
`Sec US`,

data = data

)

summary(model_unga)

Call:
lm(formula = `Dis Unga Alignment` ~ Democracy + Post2022 + SCS + 
    `Political stability` + `FSI(100-FSI)` + `Trade China` + 
    `Sec US`, data = data)

Residuals:
     Min       1Q   Median       3Q      Max 
-1.07942 -0.25982  0.02215  0.21699  0.71498 

Coefficients:
                       Estimate Std. Error t value Pr(>|t|)    
(Intercept)            2.828934   0.479241   5.903 1.08e-07 ***
Democracy             -0.084478   0.032768  -2.578   0.0120 *  
Post2022              -1.165356   0.095427 -12.212  < 2e-16 ***
SCS                    0.205679   0.086026   2.391   0.0194 *  
`Political stability`  0.007634   0.006340   1.204   0.2324    
`FSI(100-FSI)`      -0.006109   0.004570  -1.337   0.1855    
`Trade China`         -0.001305   0.412716  -0.003   0.9975    
`Sec US`               0.181308   0.296237   0.612   0.5424    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.3286 on 72 degrees of freedom
Multiple R-squared:  0.7275,    Adjusted R-squared:  0.701 
F-statistic: 27.46 on 7 and 72 DF,  p-value: < 2.2e-16
library(broom)
library(ggplot2)

####################################################
## Forest Plot for UNGA Alignment
####################################################

# 提取迴歸結果
coef_unga <- tidy(model_unga, conf.int = TRUE)

# 移除截距
coef_unga <- subset(
  coef_unga,
  term != "(Intercept)"
)

# 修改變數名稱
coef_unga$term <- c(
  "Democracy",
  "Post-2022",
  "South China Sea Claimant",
  "Political Stability",
  "Government Legitimacy",
  "Trade Dependence on China",
  "Security Ties with the U.S."
)

# 設定顯示順序
coef_unga$term <- factor(
  coef_unga$term,
  levels = rev(c(
    "Democracy",
    "Post-2022",
    "South China Sea Claimant",
    "Political Stability",
    "Government Legitimacy",
    "Trade Dependence on China",
    "Security Ties with the U.S."
  ))
)

# 畫森林圖
ggplot(
  coef_unga,
  aes(
    x = estimate,
    y = term
  )
) +

  geom_vline(
    xintercept = 0,
    linetype = "dashed",
    colour = "grey50"
  ) +

  geom_errorbarh(
    aes(
      xmin = conf.low,
      xmax = conf.high
    ),
    height = 0.18,
    linewidth = 0.8
  ) +

  geom_point(
    size = 3
  ) +

  labs(
    title = "OLS Estimates for UNGA Alignment",
    x = "Coefficient Estimate (95% Confidence Interval)",
    y = NULL
  ) +

  theme_classic(base_size = 14) +

  theme(
    plot.title = element_text(
      face = "bold",
      hjust = 0.5
    ),
    axis.text.y = element_text(size = 12)
  )
`height` was translated to `width`.

library(ggplot2)
library(broom)

####################################################
## Polished Coefficient Plot: UNGA Alignment
####################################################

# 取得模型結果
coef_unga <- tidy(
  model_unga,
  conf.int = TRUE
)

# 保留截距(如果你不想顯示截距,可把下面這行取消註解)
# coef_unga <- subset(coef_unga, term != "(Intercept)")

# 重新命名
coef_unga$variable <- coef_unga$term

coef_unga$variable[coef_unga$term == "(Intercept)"] <- "(Intercept)"
coef_unga$variable[coef_unga$term == "Democracy"] <- "Democracy"
coef_unga$variable[coef_unga$term == "Post2022"] <- "Post-2022"
coef_unga$variable[coef_unga$term == "SCS"] <- "South China Sea Claimant"
coef_unga$variable[grepl("Political", coef_unga$term)] <- "Political Stability"
coef_unga$variable[grepl("FSI", coef_unga$term)] <- "Government Legitimacy"
coef_unga$variable[grepl("Trade", coef_unga$term)] <- "Trade Dependence on China"
coef_unga$variable[grepl("Sec", coef_unga$term)] <- "Security Ties with the U.S."

# 設定顯示順序
coef_unga$variable <- factor(
  coef_unga$variable,
  levels = rev(c(
    "(Intercept)",
    "Democracy",
    "Post-2022",
    "South China Sea Claimant",
    "Political Stability",
    "Government Legitimacy",
    "Trade Dependence on China",
    "Security Ties with the U.S."
  ))
)

# 建立右側文字
coef_unga$estimate_ci <- sprintf(
  "%.3f (%.2f, %.2f)",
  coef_unga$estimate,
  coef_unga$conf.low,
  coef_unga$conf.high
)

# 找出右側文字位置
text_x <- max(coef_unga$conf.high, na.rm = TRUE) + 0.25

####################################################
## Plot
####################################################

ggplot(
  coef_unga,
  aes(
    x = estimate,
    y = variable
  )
) +

  # 0 線
  geom_vline(
    xintercept = 0,
    linetype = "dashed",
    colour = "grey50",
    linewidth = 0.6
  ) +

  # 信賴區間
  geom_errorbarh(
    aes(
      xmin = conf.low,
      xmax = conf.high
    ),
    height = 0.12,
    linewidth = 0.7
  ) +

  # 點估計
  geom_point(
    size = 2.8
  ) +

  # 右側 Estimate (95% CI)
  geom_text(
    aes(
      x = text_x,
      label = estimate_ci
    ),
    hjust = 0,
    size = 4
  ) +

  # 右側欄標題
  annotate(
    "text",
    x = text_x,
    y = length(levels(coef_unga$variable)) + 0.7,
    label = "Estimate (95% CI)",
    hjust = 0,
    fontface = "bold",
    size = 4.2
  ) +

  labs(
    title = "(A) Coefficient Plot",
    x = "Estimate",
    y = NULL,
    caption =
      "Notes: Points are OLS estimates with 95% confidence intervals.\n* p < 0.05, ** p < 0.01, *** p < 0.001."
  ) +

  # 留右側空間放數值
  coord_cartesian(
    clip = "off"
  ) +

  theme_classic(
    base_size = 14
  ) +

  theme(
    plot.title = element_text(
      face = "bold",
      hjust = 0.5,
      size = 16
    ),

    axis.text.y = element_text(
      size = 12
    ),

    axis.title.x = element_text(
      size = 13
    ),

    plot.caption = element_text(
      hjust = 0,
      size = 10
    ),

    plot.margin = margin(
      t = 15,
      r = 220,
      b = 15,
      l = 15
    )
  )
`height` was translated to `width`.

####################################################
## Packages
####################################################

library(readxl)
library(ggplot2)
library(broom)
library(fixest)
Warning: package 'fixest' was built under R version 4.5.2
model_elite_fe <- feols(

`Dis Elite Preference` ~

Democracy +
Post2022 +
SCS +

`Political stability` +
`FSI(100-FSI)` +
`Trade China` +
`Sec US`

|

Country,

data=data

)
NOTE: 40 observations removed because of NA values (LHS: 40).
The variable 'SCS' has been removed because of collinearity (see $collin.var).
summary(model_elite_fe)
OLS estimation, Dep. Var.: `Dis Elite Preference`
Observations: 40
Fixed-effects: Country: 8
Standard-errors: IID 
                        Estimate Std. Error   t value Pr(>|t|) 
Democracy               0.272489   14.50574  0.018785  0.98516 
Post2022               -8.025541    7.97910 -1.005821  0.32377 
`Political stability`   0.100143    1.88754  0.053055  0.95809 
`FSI(100-FSI)`       -0.694496    2.04657 -0.339346  0.73708 
`Trade China`         -11.743266  108.37094 -0.108362  0.91454 
`Sec US`              -10.185578   25.81177 -0.394610  0.69635 
... 1 variable was removed because of collinearity (SCS)
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 13.1     Adj. R2: 0.533708
             Within R2: 0.13795 
model_unga_fe <- feols(

`Dis Unga Alignment` ~

Democracy +
Post2022 +
SCS +

`Political stability` +
`FSI(100-FSI)` +
`Trade China` +
`Sec US`

|

Country,

data=data

)
The variable 'SCS' has been removed because of collinearity (see $collin.var).
summary(model_unga_fe)
OLS estimation, Dep. Var.: `Dis Unga Alignment`
Observations: 80
Fixed-effects: Country: 8
Standard-errors: IID 
                       Estimate Std. Error    t value  Pr(>|t|)    
Democracy              0.002838   0.090813   0.031249   0.97517    
Post2022              -1.222573   0.102816 -11.890880 < 2.2e-16 ***
`Political stability` -0.009651   0.015518  -0.621949   0.53612    
`FSI(100-FSI)`       0.001467   0.006891   0.212934   0.83203    
`Trade China`          1.417899   1.097875   1.291495   0.20104    
`Sec US`               0.066962   0.283074   0.236552   0.81374    
... 1 variable was removed because of collinearity (SCS)
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 0.270795     Adj. R2: 0.753905
                 Within R2: 0.752271
library(fixest)

model <- feols(

`Dis Unga Alignment`

~

Democracy
+
Post2022
+
SCS

+
`Political stability`
+
`FSI(100-FSI)`
+
`Trade China`
+
`Sec US`,

cluster = ~Country,

data=data

)

summary(model)
OLS estimation, Dep. Var.: `Dis Unga Alignment`
Observations: 80
Standard-errors: Clustered (Country) 
                       Estimate Std. Error    t value   Pr(>|t|)    
(Intercept)            2.828934   0.475447   5.950055 5.7003e-04 ***
Democracy             -0.084478   0.029329  -2.880375 2.3639e-02 *  
Post2022              -1.165356   0.115638 -10.077603 2.0331e-05 ***
SCS                    0.205679   0.085352   2.409784 4.6789e-02 *  
`Political stability`  0.007634   0.006533   1.168676 2.8079e-01    
`FSI(100-FSI)`      -0.006109   0.005089  -1.200306 2.6906e-01    
`Trade China`         -0.001305   0.528418  -0.002469 9.9810e-01    
`Sec US`               0.181308   0.159126   1.139400 2.9202e-01    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 0.311764   Adj. R2: 0.70099
library(fixest)

model <- feols(

`Dis Elite Preference`

~

Democracy
+
Post2022
+
SCS

+
`Political stability`
+
`FSI(100-FSI)`
+
`Trade China`
+
`Sec US`,

cluster = ~Country,

data=data

)
NOTE: 40 observations removed because of NA values (LHS: 40).
summary(model)
OLS estimation, Dep. Var.: `Dis Elite Preference`
Observations: 40
Standard-errors: Clustered (Country) 
                        Estimate Std. Error   t value Pr(>|t|)    
(Intercept)            90.448053  61.744159  1.464884 0.186370    
Democracy              -7.266026   2.874404 -2.527838 0.039357 *  
Post2022               -9.197635   5.980917 -1.537830 0.167977    
SCS                    17.790264   8.950939  1.987531 0.087208 .  
`Political stability`  -0.117698   0.687905 -0.171096 0.868989    
`FSI(100-FSI)`        0.020637   0.516190  0.039980 0.969226    
`Trade China`         -17.096027  41.901784 -0.408002 0.695463    
`Sec US`              -24.368290  17.577559 -1.386330 0.208196    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 18.5   Adj. R2: 0.245355
library(ggplot2)
library(dplyr)
Warning: package 'dplyr' was built under R version 4.5.2

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
elite_trend <- data %>%
  filter(!is.na(`Dis Elite Preference`)) %>%
  group_by(Year) %>%
  summarise(
    Mean = mean(`Dis Elite Preference`, na.rm = TRUE),
    SD = sd(`Dis Elite Preference`, na.rm = TRUE),
    N = n(),
    SE = SD/sqrt(N)
  )

ggplot(elite_trend,
       aes(x = Year,
           y = Mean)) +
  geom_line(linewidth = 1.2,
            color = "#2C7FB8") +
  geom_point(size = 3,
             color = "#2C7FB8") +
  geom_errorbar(aes(ymin = Mean-SE,
                    ymax = Mean+SE),
                width = .15) +
  geom_vline(xintercept = 2022,
             linetype = "dashed") +
  scale_x_continuous(
    breaks = 2019:2023
  ) +
  labs(
    title = "Elite Preference Distance",
    x = "Year",
    y = "Average Elite Preference Distance"
  ) +
  theme_classic(base_size = 14)

unga_trend <- data %>%
  group_by(Year) %>%
  summarise(
    Mean = mean(`Dis Unga Alignment`, na.rm = TRUE),
    SD = sd(`Dis Unga Alignment`, na.rm = TRUE),
    N = n(),
    SE = SD/sqrt(N)
  )

ggplot(unga_trend,
       aes(x = Year,
           y = Mean)) +
  geom_line(linewidth = 1.2,
            color = "#D95F02") +
  geom_point(size = 3,
             color = "#D95F02") +
  geom_errorbar(aes(ymin = Mean-SE,
                    ymax = Mean+SE),
                width = .15) +
  geom_vline(xintercept = 2022,
             linetype = "dashed") +
  scale_x_continuous(
    breaks = 2014:2023
  ) +
  labs(
    title = "UNGA Alignment",
    x = "Year",
    y = "Average UNGA Alignment"
  ) +
  theme_classic(base_size = 14)

ggplot(
  data %>% filter(!is.na(`Dis Elite Preference`)),
  aes(
    Year,
    `Dis Elite Preference`,
    color = Country,
    group = Country
  )
) +
  geom_line(linewidth = 1) +
  geom_point(size = 2) +
  geom_vline(xintercept = 2022,
             linetype = "dashed") +
  scale_x_continuous(breaks = 2019:2023) +
  labs(
    title = "Elite Preference Distance",
    x = "Year",
    y = "Elite Preference Distance"
  ) +
  theme_classic(base_size = 14)

ggplot(
  data,
  aes(
    Year,
    `Dis Unga Alignment`,
    color = Country,
    group = Country
  )
) +
  geom_line(linewidth = 1) +
  geom_point(size = 2) +
  geom_vline(xintercept = 2022,
             linetype = "dashed") +
  scale_x_continuous(breaks = 2014:2023) +
  labs(
    title = "UNGA Alignment",
    x = "Year",
    y = "UNGA Alignment"
  ) +
  theme_classic(base_size = 14)

library(ggplot2)
library(dplyr)

ggplot(
  data %>%
    filter(
      Country %in% c("Vietnam", "Malaysia"),
      !is.na(`Dis Elite Preference`)
    ),
  aes(
    x = Year,
    y = `Dis Elite Preference`,
    color = Country,
    group = Country
  )
) +
  geom_line(linewidth = 1.2) +
  geom_point(size = 3) +
  geom_vline(xintercept = 2022, linetype = "dashed") +
  scale_x_continuous(breaks = 2019:2023) +
  labs(
    title = "Elite Preference Distance",
    subtitle = "Vietnam vs. Malaysia",
    x = "Year",
    y = "Elite Preference Distance"
  ) +
  theme_classic(base_size = 14)

ggplot(
  data %>%
    filter(Country %in% c("Vietnam", "Malaysia")),
  aes(
    x = Year,
    y = `Dis Unga Alignment`,
    color = Country,
    group = Country
  )
) +
  geom_line(linewidth = 1.2) +
  geom_point(size = 3) +
  geom_vline(xintercept = 2022, linetype = "dashed") +
  scale_x_continuous(breaks = 2014:2023) +
  labs(
    title = "UNGA Alignment",
    subtitle = "Vietnam vs. Malaysia",
    x = "Year",
    y = "UNGA Alignment"
  ) +
  theme_classic(base_size = 14)

data$Period <- ifelse(data$Year < 2022,
                      "Pre-2022",
                      "Post-2022")
ggplot(
  subset(data, !is.na(`Dis Elite Preference`)),
  aes(x = Period,
      y = `Dis Elite Preference`,
      fill = Period)
) +
  geom_boxplot(alpha = .7) +
  geom_jitter(aes(color = Country),
              width = .12,
              size = 2,
              alpha = .7) +
  theme_classic(base_size = 14)

library(ggplot2)

data$Period <- ifelse(data$Year < 2022,
                      "Pre-2022",
                      "Post-2022")

ggplot(
  subset(data, !is.na(`Dis Elite Preference`)),
  aes(x = Period,
      y = `Dis Elite Preference`,
      fill = Period)
) +
  geom_boxplot(width = 0.6,
               alpha = 0.7,
               outlier.size = 2) +
  geom_jitter(width = 0.12,
              alpha = 0.6,
              size = 2) +
  labs(
    title = "Elite Preference Distance Before and After 2022",
    x = "",
    y = "Elite Preference Distance"
  ) +
  theme_classic(base_size = 14) +
  theme(legend.position = "none")

library(ggplot2)

data$Period <- ifelse(data$Year < 2022,
                      "Pre-2022",
                      "Post-2022")

ggplot(
  subset(data, !is.na(`Dis Elite Preference`)),
  aes(x = Period,
      y = `Dis Elite Preference`)
) +
  geom_boxplot(
    fill = "white",
    colour = "black",
    width = 0.55,
    outlier.shape = NA
  ) +
  geom_jitter(
    colour = "grey40",
    width = 0.10,
    size = 2,
    alpha = 0.7
  ) +
  labs(
    x = "",
    y = "Elite Preference Distance"
  ) +
  theme_classic(base_size = 14)

data$Period <- ifelse(data$Year < 2022,
                      "Pre-2022",
                      "Post-2022")

data$Period <- factor(
  data$Period,
  levels = c("Pre-2022", "Post-2022")

)
ggplot(
  data,
  aes(x = Period,
      y = `Dis Unga Alignment`)
) +
  geom_boxplot(
    fill = "white",
    colour = "black",
    width = 0.55,
    outlier.shape = NA
  ) +
  geom_jitter(
    colour = "grey40",
    width = 0.10,
    size = 2,
    alpha = 0.7
  ) +
  labs(
    x = "",
    y = "UNGA Alignment"
  ) +
  theme_classic(base_size = 14)

data$Period <- ifelse(data$Year < 2022,
                      "Pre-2022",
                      "Post-2022")

data$Period <- factor(
  data$Period,
  levels = c("Pre-2022", "Post-2022")
)
library(ggplot2)
library(broom)

coef <- tidy(model_unga,
             conf.int = TRUE)

coef <- subset(coef,
               term != "(Intercept)")
coef$term <- factor(
  coef$term,
  levels = rev(c(
    "Democracy",
    "Post2022",
    "SCS",
    "Political stability",
    "FSI(100-FSI)",
    "Trade China",
    "Sec US"
  )),
  labels = rev(c(
    "Democracy",
    "Post-2022",
    "South China Sea Claimant",
    "Political Stability",
    "Government Legitimacy",
    "Trade Dependence on China",
    "Security Ties with the U.S."
  ))
)
ggplot(coef,
       aes(x = estimate,
           y = term)) +

  geom_vline(xintercept = 0,
             linetype = "dashed",
             colour = "grey50") +

  geom_errorbarh(
      aes(xmin = conf.low,
          xmax = conf.high),
      height = .18,
      linewidth = .7
  ) +

  geom_point(size = 2.8) +

  labs(
      title = "Coefficient Plot",
      subtitle = "Dependent Variable: UNGA Alignment",
      x = "Coefficient Estimate (95% CI)",
      y = NULL
  ) +

  theme_classic(base_size = 14) +

  theme(
      plot.title = element_text(
          face = "bold",
          hjust = 0.5
      ),
      plot.subtitle = element_text(
          hjust = 0.5
      )
  )
`height` was translated to `width`.

labs(
    title = "(A) Coefficient Plot for UNGA Alignment",
    x = "Coefficient Estimate (95% CI)",
    y = NULL
)
<ggplot2::labels> List of 3
 $ x    : chr "Coefficient Estimate (95% CI)"
 $ y    : NULL
 $ title: chr "(A) Coefficient Plot for UNGA Alignment"
library(broom)
library(ggplot2)

# 擷取 Elite Preference Distance 模型結果
coef_elite <- tidy(
  model_elite,
  conf.int = TRUE
)

# 移除截距
coef_elite <- subset(
  coef_elite,
  term != "(Intercept)"
)

# 美化變數名稱
coef_elite$term <- factor(
  coef_elite$term,
  levels = rev(c(
    "Democracy",
    "Post2022",
    "SCS",
    "Political stability",
    "FSI(100-FSI)",
    "Trade China",
    "Sec US"
  )),
  labels = rev(c(
    "Democracy",
    "Post-2022",
    "South China Sea Claimant",
    "Political Stability",
    "Government Legitimacy",
    "Trade Dependence on China",
    "Security Ties with the U.S."
  ))
)

# 畫森林圖
ggplot(
  coef_elite,
  aes(
    x = estimate,
    y = term
  )
) +
  geom_vline(
    xintercept = 0,
    linetype = "dashed",
    colour = "grey50"
  ) +
  geom_errorbarh(
    aes(
      xmin = conf.low,
      xmax = conf.high
    ),
    height = 0.18,
    linewidth = 0.7
  ) +
  geom_point(size = 3) +
  labs(
    title = "OLS Estimates for Elite Preference Distance",
    x = "Coefficient Estimate (95% Confidence Interval)",
    y = NULL
  ) +
  theme_classic(base_size = 14) +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5)
  )
`height` was translated to `width`.

library(broom)
library(ggplot2)

# 擷取 UNGA Alignment 模型結果
coef_unga <- tidy(
  model_unga,
  conf.int = TRUE
)

# 移除截距
coef_unga <- subset(
  coef_unga,
  term != "(Intercept)"
)

# 美化變數名稱
coef_unga$term <- factor(
  coef_unga$term,
  levels = rev(c(
    "Democracy",
    "Post2022",
    "SCS",
    "Political stability",
    "FSI(100-FSI)",
    "Trade China",
    "Sec US"
  )),
  labels = rev(c(
    "Democracy",
    "Post-2022",
    "South China Sea Claimant",
    "Political Stability",
    "Government Legitimacy",
    "Trade Dependence on China",
    "Security Ties with the U.S."
  ))
)

# 畫森林圖
ggplot(
  coef_unga,
  aes(
    x = estimate,
    y = term
  )
) +
  geom_vline(
    xintercept = 0,
    linetype = "dashed",
    colour = "grey50"
  ) +
  geom_errorbarh(
    aes(
      xmin = conf.low,
      xmax = conf.high
    ),
    height = 0.18,
    linewidth = 0.7
  ) +
  geom_point(size = 3) +
  labs(
    title = "OLS Estimates for UNGA Alignment",
    x = "Coefficient Estimate (95% Confidence Interval)",
    y = NULL
  ) +
  theme_classic(base_size = 14) +
  theme(
    plot.title = element_text(
      face = "bold",
      hjust = 0.5
    )
  )
`height` was translated to `width`.

library(ggplot2)
library(broom)

coef_unga <- tidy(
  model_unga,
  conf.int = TRUE
)

# 移除截距
coef_unga <- subset(
  coef_unga,
  term != "(Intercept)"
)

# 重新命名所有變數
name_map <- c(
  "Democracy" = "Democracy",
  "Post2022" = "Post-2022",
  "SCS" = "South China Sea Claimant",
  "Political stability" = "Political Stability",
  "FSI(100-FSI)" = "Government Legitimacy",
  "Trade China" = "Trade Dependence on China",
  "Sec US" = "Security Ties with the U.S."
)

coef_unga$variable <- name_map[coef_unga$term]

# 指定圖上順序
coef_unga$variable <- factor(
  coef_unga$variable,
  levels = rev(c(
    "Democracy",
    "Post-2022",
    "South China Sea Claimant",
    "Political Stability",
    "Government Legitimacy",
    "Trade Dependence on China",
    "Security Ties with the U.S."
  ))
)

# 森林圖
ggplot(
  coef_unga,
  aes(
    x = estimate,
    y = variable
  )
) +
  geom_vline(
    xintercept = 0,
    linetype = "dashed",
    colour = "grey50"
  ) +
  geom_errorbarh(
    aes(
      xmin = conf.low,
      xmax = conf.high
    ),
    height = 0.18,
    linewidth = 0.7
  ) +
  geom_point(size = 3) +
  labs(
    title = "OLS Estimates for UNGA Alignment",
    x = "Coefficient Estimate (95% Confidence Interval)",
    y = NULL
  ) +
  theme_classic(base_size = 14) +
  theme(
    plot.title = element_text(
      face = "bold",
      hjust = 0.5
    )
  )
`height` was translated to `width`.

names(data) <- trimws(names(data))
library(fixest)

model_elite <- feols(
  `Dis Elite Preference` ~
    Democracy +
    Post2022 +
    SCS +
    `Political stability` +
    `FSI(100-FSI)` +
    `Trade China` +
    `Sec US`,
  cluster = ~Country,
  data = data
)
NOTE: 40 observations removed because of NA values (LHS: 40).
model_unga <- feols(
  `Dis Unga Alignment` ~
    Democracy +
    Post2022 +
    SCS +
    `Political stability` +
    `FSI(100-FSI)` +
    `Trade China` +
    `Sec US`,
  cluster = ~Country,
  data = data
)
coef_unga <- tidy(
  model_unga,
  conf.int = TRUE
) %>%
  filter(term != "(Intercept)") %>%
  mutate(
    variable = recode(
      term,
      "Democracy" = "Democracy",
      "Post2022" = "Post-2022",
      "SCS" = "South China Sea Claimant",
      "`Political stability`" = "Political Stability",
      "`FSI(100-FSI)`" = "Government Legitimacy",
      "`Trade China`" = "Trade Dependence on China",
      "`Sec US`" = "Security Ties with the U.S."
    )
  )

coef_unga$variable <- factor(
  coef_unga$variable,
  levels = rev(c(
    "Democracy",
    "Post-2022",
    "South China Sea Claimant",
    "Political Stability",
    "Government Legitimacy",
    "Trade Dependence on China",
    "Security Ties with the U.S."
  ))
)

ggplot(
  coef_unga,
  aes(
    x = estimate,
    y = variable
  )
) +
  geom_vline(
    xintercept = 0,
    linetype = "dashed",
    colour = "grey50"
  ) +
  geom_errorbarh(
    aes(
      xmin = conf.low,
      xmax = conf.high
    ),
    height = 0.18,
    linewidth = 0.7
  ) +
  geom_point(size = 3) +
  labs(
    title = "OLS Estimates for UNGA Alignment",
    x = "Coefficient Estimate (95% Confidence Interval)",
    y = NULL
  ) +
  theme_classic(base_size = 14) +
  theme(
    plot.title = element_text(
      face = "bold",
      hjust = 0.5
    )
  )
`height` was translated to `width`.

summary(model_elite)
OLS estimation, Dep. Var.: `Dis Elite Preference`
Observations: 40
Standard-errors: Clustered (Country) 
                        Estimate Std. Error   t value Pr(>|t|)    
(Intercept)            90.448053  61.744159  1.464884 0.186370    
Democracy              -7.266026   2.874404 -2.527838 0.039357 *  
Post2022               -9.197635   5.980917 -1.537830 0.167977    
SCS                    17.790264   8.950939  1.987531 0.087208 .  
`Political stability`  -0.117698   0.687905 -0.171096 0.868989    
`FSI(100-FSI)`        0.020637   0.516190  0.039980 0.969226    
`Trade China`         -17.096027  41.901784 -0.408002 0.695463    
`Sec US`              -24.368290  17.577559 -1.386330 0.208196    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
RMSE: 18.5   Adj. R2: 0.245355
coef(model_elite)
          (Intercept)             Democracy              Post2022 
          90.44805314           -7.26602621           -9.19763463 
                  SCS `Political stability`      `FSI(100-FSI)` 
          17.79026415           -0.11769774            0.02063707 
        `Trade China`              `Sec US` 
         -17.09602686          -24.36829028 
library(broom)

coef_elite <- tidy(model_elite, conf.int = TRUE)

coef_elite
# A tibble: 8 × 7
  term                  estimate std.error statistic p.value conf.low conf.high
  <chr>                    <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
1 (Intercept)            90.4       61.7      1.46    0.186    -55.6    236.   
2 Democracy              -7.27       2.87    -2.53    0.0394   -14.1     -0.469
3 Post2022               -9.20       5.98    -1.54    0.168    -23.3      4.94 
4 SCS                    17.8        8.95     1.99    0.0872    -3.38    39.0  
5 `Political stability`  -0.118      0.688   -0.171   0.869     -1.74     1.51 
6 `FSI(100-FSI)`        0.0206     0.516    0.0400  0.969     -1.20     1.24 
7 `Trade China`         -17.1       41.9     -0.408   0.695   -116.      82.0  
8 `Sec US`              -24.4       17.6     -1.39    0.208    -65.9     17.2  
####################################################
## Correlation Matrix
####################################################

corr_data <- data[, c(
  "Dis Elite Preference",
  "Dis Unga Alignment",
  "Democracy",
  "Post2022",
  "SCS",
  "Political stability",
  "FSI(100-FSI)",
  "Trade China",
  "Sec US"
)]

corr_matrix <- cor(
  corr_data,
  use = "pairwise.complete.obs",
  method = "pearson"
)

round(corr_matrix, 2)
                     Dis Elite Preference Dis Unga Alignment Democracy Post2022
Dis Elite Preference                 1.00               0.22     -0.47    -0.21
Dis Unga Alignment                   0.22               1.00     -0.26    -0.79
Democracy                           -0.47              -0.26      1.00    -0.02
Post2022                            -0.21              -0.79     -0.02     1.00
SCS                                  0.10               0.05      0.38     0.00
Political stability                  0.03              -0.04     -0.28     0.08
FSI(100-FSI)                      -0.21              -0.28      0.31     0.15
Trade China                          0.19              -0.03     -0.47     0.16
Sec US                              -0.17              -0.05      0.40     0.04
                       SCS Political stability FSI(100-FSI) Trade China
Dis Elite Preference  0.10                0.03          -0.21        0.19
Dis Unga Alignment    0.05               -0.04          -0.28       -0.03
Democracy             0.38               -0.28           0.31       -0.47
Post2022              0.00                0.08           0.15        0.16
SCS                   1.00               -0.37          -0.07       -0.31
Political stability  -0.37                1.00           0.69        0.48
FSI(100-FSI)       -0.07                0.69           1.00        0.20
Trade China          -0.31                0.48           0.20        1.00
Sec US                0.44               -0.51          -0.23       -0.46
                     Sec US
Dis Elite Preference  -0.17
Dis Unga Alignment    -0.05
Democracy              0.40
Post2022               0.04
SCS                    0.44
Political stability   -0.51
FSI(100-FSI)        -0.23
Trade China           -0.46
Sec US                 1.00
pairs(

corr_data,

pch = 19,

cex = 0.6,

col = "black"

)

####################################################
## Correlation Matrix Table
####################################################

# 選取三個主要自變數 + 四個控制變數
corr_data <- data[, c(
  "Democracy",
  "Post2022",
  "SCS",
  "Political stability",
  "FSI(100-FSI)",
  "Trade China",
  "Sec US"
)]

# 改成論文中較清楚的名稱
names(corr_data) <- c(
  "Democracy",
  "Post-2022",
  "SCS Claimant",
  "Political Stability",
  "Government Legitimacy",
  "Trade Dependence on China",
  "Security Ties with U.S."
)

# Pearson correlation matrix
corr_table <- cor(
  corr_data,
  use = "pairwise.complete.obs",
  method = "pearson"
)

# 四捨五入到小數點後兩位
corr_table <- round(corr_table, 2)

# 顯示
corr_table
                          Democracy Post-2022 SCS Claimant Political Stability
Democracy                      1.00     -0.02         0.38               -0.28
Post-2022                     -0.02      1.00         0.00                0.08
SCS Claimant                   0.38      0.00         1.00               -0.37
Political Stability           -0.28      0.08        -0.37                1.00
Government Legitimacy          0.31      0.15        -0.07                0.69
Trade Dependence on China     -0.47      0.16        -0.31                0.48
Security Ties with U.S.        0.40      0.04         0.44               -0.51
                          Government Legitimacy Trade Dependence on China
Democracy                                  0.31                     -0.47
Post-2022                                  0.15                      0.16
SCS Claimant                              -0.07                     -0.31
Political Stability                        0.69                      0.48
Government Legitimacy                      1.00                      0.20
Trade Dependence on China                  0.20                      1.00
Security Ties with U.S.                   -0.23                     -0.46
                          Security Ties with U.S.
Democracy                                    0.40
Post-2022                                    0.04
SCS Claimant                                 0.44
Political Stability                         -0.51
Government Legitimacy                       -0.23
Trade Dependence on China                   -0.46
Security Ties with U.S.                      1.00
library(broom)
library(ggplot2)

# Elite
elite <- tidy(model_elite, conf.int = TRUE)
elite$model <- "Elite Preference Distance"

# UNGA
unga <- tidy(model_unga, conf.int = TRUE)
unga$model <- "UNGA Alignment"

# 合併
coef_all <- rbind(elite, unga)

# 去掉截距
coef_all <- subset(
  coef_all,
  term != "(Intercept)"
)

# 重新命名
coef_all$term <- factor(
  coef_all$term,
  levels = rev(c(
    "Sec US",
    "Trade China",
    "FSI(100-FSI)",
    "Political stability",
    "SCS",
    "Post2022",
    "Democracy"
  )),
  labels = rev(c(
    "Security Ties with the U.S.",
    "Trade Dependence on China",
    "Government Legitimacy",
    "Political Stability",
    "South China Sea Claimant",
    "Post-2022",
    "Democracy"
  ))
)

ggplot(
  coef_all,
  aes(
    estimate,
    term
  )
) +

geom_vline(
  xintercept = 0,
  linetype = "dashed",
  colour = "grey60"
) +

geom_errorbarh(
  aes(
    xmin = conf.low,
    xmax = conf.high
  ),
  height = 0.15
) +

geom_point(
  size = 2.8
) +

facet_wrap(
  ~model,
  nrow = 1,
  scales = "free_x"

) +

labs(

title = "Comparison of Regression Coefficients",

x = "Coefficient Estimate (95% CI)",

y = NULL

) +

theme_classic(base_size = 14) +

theme(

plot.title = element_text(
  hjust = 0.5,
  face = "bold"
),

strip.text = element_text(
  face = "bold",
  size = 13
)

)
`height` was translated to `width`.

library(ggplot2)
library(broom)

####################################################
## 1. Extract regression results
####################################################

elite <- tidy(
  model_elite,
  conf.int = TRUE
)

elite$model <- "Elite Preference Distance"


unga <- tidy(
  model_unga,
  conf.int = TRUE
)

unga$model <- "UNGA Alignment"


####################################################
## 2. Combine two models
####################################################

coef_all <- rbind(
  elite,
  unga
)

# Remove intercept
coef_all <- coef_all[
  coef_all$term != "(Intercept)",
]


####################################################
## 3. Rename variables
####################################################

# 先保留原始名稱,避免任何變數被變成 NA
coef_all$variable <- coef_all$term

coef_all$variable[
  coef_all$term == "Democracy"
] <- "Democracy"

coef_all$variable[
  coef_all$term == "Post2022"
] <- "Post-2022"

coef_all$variable[
  coef_all$term == "SCS"
] <- "South China Sea Claimant"

coef_all$variable[
  grepl("Political stability", coef_all$term, fixed = TRUE)
] <- "Political Stability"

coef_all$variable[
  grepl("FSI", coef_all$term, fixed = TRUE)
] <- "Government Legitimacy"

coef_all$variable[
  grepl("Trade China", coef_all$term, fixed = TRUE)
] <- "Trade Dependence on China"

coef_all$variable[
  grepl("Sec US", coef_all$term, fixed = TRUE)
] <- "Security Ties with the U.S."


####################################################
## 4. Set variable order
####################################################

coef_all$variable <- factor(
  coef_all$variable,

  levels = rev(c(
    "Democracy",
    "Post-2022",
    "South China Sea Claimant",
    "Political Stability",
    "Government Legitimacy",
    "Trade Dependence on China",
    "Security Ties with the U.S."
  ))
)


####################################################
## 5. Check whether any NA remains
####################################################

print(
  coef_all[, c(
    "term",
    "variable",
    "model",
    "estimate",
    "conf.low",
    "conf.high"
  )]
)
# A tibble: 14 × 6
   term                  variable              model estimate conf.low conf.high
   <chr>                 <fct>                 <chr>    <dbl>    <dbl>     <dbl>
 1 Democracy             Democracy             Elit… -7.27e+0 -1.41e+1  -0.469  
 2 Post2022              Post-2022             Elit… -9.20e+0 -2.33e+1   4.94   
 3 SCS                   South China Sea Clai… Elit…  1.78e+1 -3.38e+0  39.0    
 4 `Political stability` Political Stability   Elit… -1.18e-1 -1.74e+0   1.51   
 5 `FSI(100-FSI)`      Government Legitimacy Elit…  2.06e-2 -1.20e+0   1.24   
 6 `Trade China`         Trade Dependence on … Elit… -1.71e+1 -1.16e+2  82.0    
 7 `Sec US`              Security Ties with t… Elit… -2.44e+1 -6.59e+1  17.2    
 8 Democracy             Democracy             UNGA… -8.45e-2 -1.54e-1  -0.0151 
 9 Post2022              Post-2022             UNGA… -1.17e+0 -1.44e+0  -0.892  
10 SCS                   South China Sea Clai… UNGA…  2.06e-1  3.85e-3   0.408  
11 `Political stability` Political Stability   UNGA…  7.63e-3 -7.81e-3   0.0231 
12 `FSI(100-FSI)`      Government Legitimacy UNGA… -6.11e-3 -1.81e-2   0.00593
13 `Trade China`         Trade Dependence on … UNGA… -1.30e-3 -1.25e+0   1.25   
14 `Sec US`              Security Ties with t… UNGA…  1.81e-1 -1.95e-1   0.558  
####################################################
## 6. Comparison coefficient plot
####################################################

ggplot(
  coef_all,
  aes(
    x = estimate,
    y = variable
  )
) +

  geom_vline(
    xintercept = 0,
    linetype = "dashed",
    colour = "grey55",
    linewidth = 0.6
  ) +

  geom_errorbarh(
    aes(
      xmin = conf.low,
      xmax = conf.high
    ),
    height = 0.16,
    linewidth = 0.7
  ) +

  geom_point(
    size = 2.8
  ) +

  facet_wrap(
    ~model,
    nrow = 1,
    scales = "free_x"
  ) +

  labs(
    title = "Comparison of Regression Coefficients",
    x = "Coefficient Estimate (95% CI)",
    y = NULL
  ) +

  theme_classic(
    base_size = 14
  ) +

  theme(
    plot.title = element_text(
      face = "bold",
      hjust = 0.5
    ),

    strip.text = element_text(
      face = "bold",
      size = 13
    ),

    axis.text.y = element_text(
      size = 11
    )
  )
`height` was translated to `width`.