# =========================
# 0. 載入套件
# =========================
library (readxl)
library (tidyverse)
Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'purrr' was built under R version 4.5.2
Warning: package 'dplyr' was built under R version 4.5.2
Warning: package 'lubridate' was built under R version 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.0 ✔ readr 2.1.5
✔ forcats 1.0.1 ✔ stringr 1.5.2
✔ ggplot2 4.0.2 ✔ tibble 3.3.0
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.1
── 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
Attaching package: 'janitor'
The following objects are masked from 'package:stats':
chisq.test, fisher.test
library (ggplot2)
library (corrplot)
library (broom)
library (plm)
Attaching package: 'plm'
The following objects are masked from 'package:dplyr':
between, lag, lead
# =========================
# 1. 讀取資料
# =========================
df <- read_excel ("~/Desktop/data/Strategic Ambiguity.xlsx" ) %>%
clean_names ()
names (df)
[1] "country" "year" "political_stability"
[4] "democracy" "scs" "post2022"
[7] "trade_china" "sec_us" "strategic_ambiguity"
# =========================
# 2. 戰略模糊折線圖
# =========================
ggplot (df, aes (x = year, y = strategic_ambiguity,
color = country, group = country)) +
geom_line (linewidth = 1.1 ) +
geom_point (size = 2 ) +
labs (
title = "Strategic Ambiguity Trends among ASEAN Countries" ,
x = "Year" ,
y = "Strategic Ambiguity" ,
color = "Country"
) +
theme_minimal (base_size = 13 ) +
theme (
plot.title = element_text (face = "bold" , size = 15 ),
legend.position = "right"
)
ggplot (df, aes (x = year, y = strategic_ambiguity,
color = country, group = country)) +
geom_line (linewidth = 1.1 ) +
geom_point (size = 2 ) +
geom_vline (xintercept = 2022 , linetype = "dashed" , color = "gray40" ) +
annotate ("text" , x = 2022.1 , y = max (df$ strategic_ambiguity, na.rm = TRUE ),
label = "Post-2022" , hjust = 0 , size = 4 ) +
labs (
title = "Strategic Ambiguity before and after 2022" ,
x = "Year" ,
y = "Strategic Ambiguity" ,
color = "Country"
) +
theme_minimal (base_size = 13 )
# =========================
# 3. 相關係數圖
# =========================
cor_data <- df %>%
select (
strategic_ambiguity,
political_stability,
democracy,
scs,
post2022,
trade_china,
sec_us
) %>%
mutate (across (everything (), as.numeric))
cor_matrix <- cor (cor_data, use = "complete.obs" )
corrplot (
cor_matrix,
method = "color" ,
type = "upper" ,
addCoef.col = "black" ,
tl.col = "black" ,
tl.srt = 45 ,
number.cex = 0.8 ,
title = "Correlation Matrix" ,
mar = c (0 , 0 , 2 , 0 )
)
cor_long <- as.data.frame (cor_matrix) %>%
rownames_to_column ("var1" ) %>%
pivot_longer (- var1, names_to = "var2" , values_to = "correlation" )
ggplot (cor_long, aes (x = var1, y = var2, fill = correlation)) +
geom_tile (color = "white" ) +
geom_text (aes (label = round (correlation, 2 )), size = 4 ) +
scale_fill_gradient2 (
low = "#2166AC" ,
mid = "white" ,
high = "#B2182B" ,
midpoint = 0 ,
limits = c (- 1 , 1 )
) +
labs (
title = "Correlation Heatmap" ,
x = "" ,
y = "" ,
fill = "Correlation"
) +
theme_minimal (base_size = 13 ) +
theme (
axis.text.x = element_text (angle = 45 , hjust = 1 ),
plot.title = element_text (face = "bold" , size = 15 )
)
# =========================
# 6. 政治穩定度與戰略模糊散佈圖
# =========================
ggplot (df, aes (x = political_stability, y = strategic_ambiguity)) +
geom_point (aes (color = country), size = 3 , alpha = 0.8 ) +
geom_smooth (method = "lm" , se = TRUE , color = "black" ) +
labs (
title = "Political Stability and Strategic Ambiguity" ,
x = "Political Stability" ,
y = "Strategic Ambiguity" ,
color = "Country"
) +
theme_minimal (base_size = 13 ) +
theme (
plot.title = element_text (face = "bold" , size = 15 )
)
`geom_smooth()` using formula = 'y ~ x'
# =========================
# 7. Panel Data
# =========================
p_df <- pdata.frame (df, index = c ("country" , "year" ))
model_fe <- plm (
strategic_ambiguity ~ political_stability + democracy + post2022,
data = p_df,
model = "within" ,
effect = "individual"
)
summary (model_fe)
Oneway (individual) effect Within Model
Call:
plm(formula = strategic_ambiguity ~ political_stability + democracy +
post2022, data = p_df, effect = "individual", model = "within")
Balanced Panel: n = 8, T = 5, N = 40
Residuals:
Min. 1st Qu. Median 3rd Qu. Max.
-23.5751 -9.4914 -1.0830 6.3004 36.1416
Coefficients:
Estimate Std. Error t-value Pr(>|t|)
political_stability -0.21692 1.68704 -0.1286 0.89858
democracy 1.35218 12.55351 0.1077 0.91496
post2022 10.64700 5.59222 1.9039 0.06689 .
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Total Sum of Squares: 7958.8
Residual Sum of Squares: 6935
R-Squared: 0.12863
Adj. R-Squared: -0.17184
F-statistic: 1.42698 on 3 and 29 DF, p-value: 0.25505
fe_result <- tidy (model_fe, conf.int = TRUE )
ggplot (fe_result, aes (x = estimate, y = reorder (term, estimate))) +
geom_point (size = 3 ) +
geom_errorbarh (aes (xmin = conf.low, xmax = conf.high), height = 0.2 ) +
geom_vline (xintercept = 0 , linetype = "dashed" , color = "gray40" ) +
labs (
title = "Fixed Effects Regression Coefficients" ,
x = "Coefficient Estimate" ,
y = "Variables"
) +
theme_minimal (base_size = 13 )
Warning: `geom_errorbarh()` was deprecated in ggplot2 4.0.0.
ℹ Please use the `orientation` argument of `geom_errorbar()` instead.
`height` was translated to `width`.
# 1. 載入套件
library (readxl)
library (tidyverse)
library (plm)
library (stargazer)
Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
# 2. 讀取資料
df <- read_excel ("~/Desktop/data/Strategic Ambiguity.xlsx" )
# 3. 整理欄位名稱
names (df) <- names (df) %>%
str_replace_all ("_" , "_" ) %>%
str_replace_all (" " , "_" ) %>%
str_to_lower ()
# 4. 確認欄位名稱
names (df)
[1] "country" "year" "political_stability"
[4] "democracy" "scs" "post2022"
[7] "trade_china" "sec_us" "strategic_ambiguity"
# 5. 確保變數型態正確
df <- df %>%
mutate (
country = as.factor (country),
year = as.numeric (year),
strategic_ambiguity = as.numeric (strategic_ambiguity),
political_stability = as.numeric (political_stability),
democracy = as.numeric (democracy),
scs = as.factor (scs),
post2022 = as.factor (post2022)
)
# 6. OLS 模型
ols_model <- lm (
strategic_ambiguity ~ political_stability + democracy + scs + post2022,
data = df
)
summary (ols_model)
Call:
lm(formula = strategic_ambiguity ~ political_stability + democracy +
scs + post2022, data = df)
Residuals:
Min 1Q Median 3Q Max
-42.770 -10.692 3.055 12.505 35.446
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 34.8375 25.0269 1.392 0.172705
political_stability 0.0058 0.3129 0.019 0.985314
democracy 7.0189 1.7532 4.004 0.000309 ***
scs1 -14.8289 7.3357 -2.021 0.050929 .
post20221 10.4480 6.5032 1.607 0.117129
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 20.1 on 35 degrees of freedom
Multiple R-squared: 0.3594, Adjusted R-squared: 0.2862
F-statistic: 4.909 on 4 and 35 DF, p-value: 0.003005
# 7. 建立 panel data
p_df <- pdata.frame (df, index = c ("country" , "year" ))
# 8. Fixed Effects 模型
fe_model <- plm (
strategic_ambiguity ~ political_stability + democracy + post2022,
data = p_df,
effect = "individual" ,
model = "within"
)
summary (fe_model)
Oneway (individual) effect Within Model
Call:
plm(formula = strategic_ambiguity ~ political_stability + democracy +
post2022, data = p_df, effect = "individual", model = "within")
Balanced Panel: n = 8, T = 5, N = 40
Residuals:
Min. 1st Qu. Median 3rd Qu. Max.
-23.5751 -9.4914 -1.0830 6.3004 36.1416
Coefficients:
Estimate Std. Error t-value Pr(>|t|)
political_stability -0.21692 1.68704 -0.1286 0.89858
democracy 1.35218 12.55351 0.1077 0.91496
post20221 10.64700 5.59222 1.9039 0.06689 .
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Total Sum of Squares: 7958.8
Residual Sum of Squares: 6935
R-Squared: 0.12863
Adj. R-Squared: -0.17184
F-statistic: 1.42698 on 3 and 29 DF, p-value: 0.25505
# 9. Random Effects 模型
re_model <- plm (
strategic_ambiguity ~ political_stability + democracy + post2022,
data = p_df,
model = "random"
)
summary (re_model)
Oneway (individual) effect Random Effect Model
(Swamy-Arora's transformation)
Call:
plm(formula = strategic_ambiguity ~ political_stability + democracy +
post2022, data = p_df, model = "random")
Balanced Panel: n = 8, T = 5, N = 40
Effects:
var std.dev share
idiosyncratic 239.14 15.46 0.44
individual 304.36 17.45 0.56
theta: 0.6315
Residuals:
Min. 1st Qu. Median 3rd Qu. Max.
-26.2114 -11.3538 -0.6278 8.7296 35.5464
Coefficients:
Estimate Std. Error z-value Pr(>|z|)
(Intercept) 22.40334 44.87890 0.4992 0.61764
political_stability 0.18419 0.56014 0.3288 0.74228
democracy 5.72589 3.31872 1.7253 0.08447 .
post20221 10.15012 4.93221 2.0579 0.03960 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Total Sum of Squares: 9875.1
Residual Sum of Squares: 8175.2
R-Squared: 0.17214
Adj. R-Squared: 0.10315
Chisq: 7.48575 on 3 DF, p-value: 0.057926
# 10. Hausman test:比較 FE / RE
phtest (fe_model, re_model)
Hausman Test
data: strategic_ambiguity ~ political_stability + democracy + post2022
chisq = 0.18499, df = 3, p-value = 0.98
alternative hypothesis: one model is inconsistent
# 11. OLS + FE + RE 結果整理成表格
stargazer (
ols_model, fe_model, re_model,
type = "text" ,
title = "Regression Results: Strategic Ambiguity" ,
dep.var.labels = "Strategic Ambiguity" ,
covariate.labels = c (
"Political Stability" ,
"Democracy" ,
"SCS Claimant" ,
"Post-2022"
),
omit.stat = c ("ser" ),
digits = 3
)
Regression Results: Strategic Ambiguity
=====================================================================
Dependent variable:
-------------------------------------------------
Strategic Ambiguity
OLS panel
linear
(1) (2) (3)
---------------------------------------------------------------------
Political Stability 0.006 -0.217 0.184
(0.313) (1.687) (0.560)
Democracy 7.019*** 1.352 5.726*
(1.753) (12.554) (3.319)
SCS Claimant -14.829*
(7.336)
Post-2022 10.448 10.647* 10.150**
(6.503) (5.592) (4.932)
Constant 34.837 22.403
(25.027) (44.879)
---------------------------------------------------------------------
Observations 40 40 40
R2 0.359 0.129 0.172
Adjusted R2 0.286 -0.172 0.103
F Statistic 4.909*** (df = 4; 35) 1.427 (df = 3; 29) 7.486*
=====================================================================
Note: *p<0.1; **p<0.05; ***p<0.01
ggplot (df, aes (x = democracy, y = strategic_ambiguity, label = country)) +
geom_point (size = 3 ) +
geom_smooth (method = "lm" , se = TRUE ) +
ggrepel:: geom_text_repel (size = 3 ) +
labs (
title = "Democracy and Strategic Ambiguity" ,
x = "Democracy" ,
y = "Strategic Ambiguity"
) +
theme_minimal ()
`geom_smooth()` using formula = 'y ~ x'
Warning: The following aesthetics were dropped during statistical transformation: label.
ℹ This can happen when ggplot fails to infer the correct grouping structure in
the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
variable into a factor?
df %>%
mutate (period = ifelse (year < 2022 , "Before 2022" , "Post-2022" )) %>%
group_by (country, period) %>%
summarise (mean_ambiguity = mean (strategic_ambiguity, na.rm = TRUE ), .groups = "drop" ) %>%
ggplot (aes (x = period, y = mean_ambiguity, group = country)) +
geom_line () +
geom_point (size = 3 ) +
facet_wrap (~ country) +
labs (
title = "Changes in Strategic Ambiguity Before and After 2022" ,
x = "" ,
y = "Average Strategic Ambiguity"
) +
theme_minimal ()
library (tidyverse)
library (marginaleffects)
Warning: package 'marginaleffects' was built under R version 4.5.2
m_dem <- lm (
strategic_ambiguity ~ political_stability * democracy +
scs + post2022 + trade_china + sec_us,
data = df
)
summary (m_dem)
Call:
lm(formula = strategic_ambiguity ~ political_stability * democracy +
scs + post2022 + trade_china + sec_us, data = df)
Residuals:
Min 1Q Median 3Q Max
-40.134 -11.197 4.241 10.049 31.564
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 104.7533 183.0311 0.572 0.5711
political_stability -1.1327 2.3752 -0.477 0.6367
democracy -8.7921 30.6290 -0.287 0.7759
scs1 -17.9451 8.0802 -2.221 0.0336 *
post20221 9.9828 6.9495 1.436 0.1606
trade_china -5.9151 56.5985 -0.105 0.9174
sec_us 27.5880 25.4438 1.084 0.2863
political_stability:democracy 0.2174 0.4165 0.522 0.6053
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 20.58 on 32 degrees of freedom
Multiple R-squared: 0.386, Adjusted R-squared: 0.2517
F-statistic: 2.874 on 7 and 32 DF, p-value: 0.01907
plot_slopes (
m_dem,
variables = "political_stability" ,
condition = "democracy"
) +
labs (
title = "Marginal Effect of Political Stability across Democracy" ,
x = "Democracy" ,
y = "Marginal Effect of Political Stability on Strategic Ambiguity"
) +
theme_minimal ()