#Load Libraries
# Install necessary packages if you haven't already
# install.packages("readxl")
# install.packages("MSwM")
# install.packages("dplyr")
# install.packages("ggplot2")
# Load the libraries
library(readxl)
library(MSwM)
## Loading required package: parallel
library(dplyr)
##
## 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
library(ggplot2)
library(tseries)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(readxl)
GDP_Growth <- read_excel("/Users/nanaakuaasare/Downloads/GDP Growth for selecterd SSA countries_Working Data.xlsx",
sheet = "Quarterly Growth Rate")
#View(GDP_Growth)
head(GDP_Growth)
## # A tibble: 6 × 11
## Quarterly CAMEROON DRC GABON GHANA IVOR_COAST KENYA NIGERIA RWANDA SENEGAL
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1961Q1 0.191 2.12 4.12 0.829 3.17 -2.98 -0.109 -2.29 0.955
## 2 1961Q2 0.233 2.10 3.95 0.841 2.89 -2.57 -0.0464 -1.81 0.872
## 3 1961Q3 0.316 2.08 3.61 0.863 2.35 -1.73 0.0788 -0.831 0.706
## 4 1961Q4 0.440 2.05 3.09 0.897 1.53 -0.490 0.267 0.632 0.457
## 5 1962Q1 0.606 2.00 2.40 0.942 0.434 1.17 0.517 2.58 0.126
## 6 1962Q2 0.734 1.70 1.91 0.994 -0.0539 2.31 0.820 3.46 -0.0711
## # ℹ 1 more variable: TANZANIA <dbl>
colnames(GDP_Growth)
## [1] "Quarterly" "CAMEROON" "DRC" "GABON" "GHANA"
## [6] "IVOR_COAST" "KENYA" "NIGERIA" "RWANDA" "SENEGAL"
## [11] "TANZANIA"
# Load necessary libraries
library(readxl)
library(dplyr)
library(tidyr)
library(tseries)
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
data <- GDP_Growth
# Display the structure of the dataset
str(data)
## tibble [248 × 11] (S3: tbl_df/tbl/data.frame)
## $ Quarterly : chr [1:248] "1961Q1" "1961Q2" "1961Q3" "1961Q4" ...
## $ CAMEROON : num [1:248] 0.191 0.233 0.316 0.44 0.606 ...
## $ DRC : num [1:248] 2.12 2.1 2.08 2.05 2 ...
## $ GABON : num [1:248] 4.12 3.95 3.61 3.09 2.4 ...
## $ GHANA : num [1:248] 0.829 0.841 0.863 0.897 0.942 ...
## $ IVOR_COAST: num [1:248] 3.165 2.892 2.346 1.526 0.434 ...
## $ KENYA : num [1:248] -2.98 -2.57 -1.73 -0.49 1.17 ...
## $ NIGERIA : num [1:248] -0.109 -0.0464 0.0788 0.2667 0.5171 ...
## $ RWANDA : num [1:248] -2.295 -1.807 -0.831 0.632 2.584 ...
## $ SENEGAL : num [1:248] 0.955 0.872 0.706 0.457 0.126 ...
## $ TANZANIA : num [1:248] NA NA NA NA NA NA NA NA NA NA ...
# Reshape the data to long format for easier processing
data_long <- data %>%
gather(key = "Country", value = "GDP_Growth", -Quarterly)
# Summary statistics for each country
summary_stats <- data_long %>%
group_by(Country) %>%
summarize(
Mean = mean(GDP_Growth, na.rm = TRUE),
Median = median(GDP_Growth, na.rm = TRUE),
Std_Dev = sd(GDP_Growth, na.rm = TRUE),
Min = min(GDP_Growth, na.rm = TRUE),
Max = max(GDP_Growth, na.rm = TRUE),
Q1 = quantile(GDP_Growth, 0.25, na.rm = TRUE),
Q3 = quantile(GDP_Growth, 0.75, na.rm = TRUE),
N = n()
)
summary_stats
## # A tibble: 10 × 9
## Country Mean Median Std_Dev Min Max Q1 Q3 N
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
## 1 CAMEROON 0.905 0.971 1.39 -3.29 5.94 0.623 1.27 248
## 2 DRC 0.854 0.749 1.63 -2.92 6.52 -0.0266 1.95 248
## 3 GABON 1.01 0.984 2.36 -7.15 10.8 0.176 1.62 248
## 4 GHANA 0.920 1.07 1.10 -3.78 3.68 0.555 1.43 248
## 5 IVOR_COAST 1.07 1.11 1.39 -3.23 5.49 0.0841 1.98 248
## 6 KENYA 1.15 1.12 1.16 -2.98 6.62 0.482 1.58 248
## 7 NIGERIA 0.919 0.990 1.80 -4.23 7.16 0.0866 1.69 248
## 8 RWANDA 1.20 1.61 2.64 -15.6 12.1 0.387 2.28 248
## 9 SENEGAL 0.787 0.868 0.902 -2.21 2.76 0.355 1.44 248
## 10 TANZANIA 1.28 1.35 0.497 0.123 2.04 1.01 1.68 248
# Function to perform the unit root test (ADF) and homoscedasticity test (Breusch-Pagan)
perform_tests <- function(df) {
adf_result <- adf.test(df$GDP_Growth, na.action = na.omit)
bp_test <- bptest(GDP_Growth ~ 1, data = df)
return(data.frame(
ADF_Test_Statistic = adf_result$statistic,
ADF_p_value = adf_result$p.value,
BP_Test_Statistic = bp_test$statistic,
BP_p_value = bp_test$p.value
))
}
# Summary statistics for each country
summary_stats <- data_long %>%
group_by(Country) %>%
summarize(
Mean = mean(GDP_Growth, na.rm = TRUE),
Median = median(GDP_Growth, na.rm = TRUE),
Std_Dev = sd(GDP_Growth, na.rm = TRUE),
Min = min(GDP_Growth, na.rm = TRUE),
Max = max(GDP_Growth, na.rm = TRUE),
Q1 = quantile(GDP_Growth, 0.25, na.rm = TRUE),
Q3 = quantile(GDP_Growth, 0.75, na.rm = TRUE),
N = n()
)
# Function to perform the unit root test (ADF) and homoscedasticity test (Breusch-Pagan)
perform_tests <- function(df) {
# Remove NA values for the tests
df <- na.omit(df)
adf_result <- adf.test(df$GDP_Growth)
# Convert Quarterly to numeric for the regression
df$Quarterly <- as.numeric(as.factor(df$Quarterly))
bp_test <- bptest(GDP_Growth ~ Quarterly, data = df)
return(data.frame(
ADF_Test_Statistic = adf_result$statistic,
ADF_p_value = adf_result$p.value,
BP_Test_Statistic = bp_test$statistic,
BP_p_value = bp_test$p.value
))
}
# Perform tests for each country
test_results <- data_long %>%
group_by(Country) %>%
do(perform_tests(.))
## Warning in adf.test(df$GDP_Growth): p-value smaller than printed p-value
## Warning in adf.test(df$GDP_Growth): p-value smaller than printed p-value
## Warning in adf.test(df$GDP_Growth): p-value smaller than printed p-value
# Combine summary statistics and test results
combined_results <- summary_stats %>%
left_join(test_results, by = "Country")
# Print the combined results
print(combined_results)
## # A tibble: 10 × 13
## Country Mean Median Std_Dev Min Max Q1 Q3 N
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
## 1 CAMEROON 0.905 0.971 1.39 -3.29 5.94 0.623 1.27 248
## 2 DRC 0.854 0.749 1.63 -2.92 6.52 -0.0266 1.95 248
## 3 GABON 1.01 0.984 2.36 -7.15 10.8 0.176 1.62 248
## 4 GHANA 0.920 1.07 1.10 -3.78 3.68 0.555 1.43 248
## 5 IVOR_COAST 1.07 1.11 1.39 -3.23 5.49 0.0841 1.98 248
## 6 KENYA 1.15 1.12 1.16 -2.98 6.62 0.482 1.58 248
## 7 NIGERIA 0.919 0.990 1.80 -4.23 7.16 0.0866 1.69 248
## 8 RWANDA 1.20 1.61 2.64 -15.6 12.1 0.387 2.28 248
## 9 SENEGAL 0.787 0.868 0.902 -2.21 2.76 0.355 1.44 248
## 10 TANZANIA 1.28 1.35 0.497 0.123 2.04 1.01 1.68 248
## # ℹ 4 more variables: ADF_Test_Statistic <dbl>, ADF_p_value <dbl>,
## # BP_Test_Statistic <dbl>, BP_p_value <dbl>
# Optionally, save the combined results to an Excel file
#write.xlsx(combined_results, "path/to/save/summary_statistics_unit_root_homoscedasticity.xlsx", row.names = FALSE)
# Set seed for reproducibility
set.seed(123)
#Fit Markov-Switching Models ## Fixed Transition Probabilities (FTP) Model
# Fit a linear model as a base model
model <- lm(CAMEROON ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to CAMEROON data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE))
##
## AIC BIC logLik
## 540.8543 558.908 -268.4272
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.9719 0.0230 42.257 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2339457
## Multiple R-squared: 5.63e-30
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -5.179522e-01 -6.307453e-02 -7.669691e-37 7.228803e-02 4.945360e-01
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.8272 0.1883 4.393 1.118e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.016339
## Multiple R-squared: 4.851e-32
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -4.11289655 -0.03873180 0.00352952 0.08922131 5.11404760
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.96061021 0.03684917
## Regime 2 0.03938979 0.96315083
# Extract smoothed probabilities
smoothed_probs <- data.frame(
Quarterly = GDP_Growth$Quarterly[1:nrow(msm_ftp@Fit@smoProb)],
Regime1 = msm_ftp@Fit@smoProb[,1],
Regime2 = msm_ftp@Fit@smoProb[,2]
)
#Caneroon
# Fit a linear model as a base model
model <- lm(CAMEROON ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to CAMEROON data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE))
##
## AIC BIC logLik
## 540.8543 558.908 -268.4272
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.9719 0.0230 42.257 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2339457
## Multiple R-squared: 3.603e-30
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -5.179522e-01 -6.307453e-02 -7.669682e-37 7.228803e-02 4.945360e-01
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.8272 0.1882 4.3953 1.106e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.016339
## Multiple R-squared: 1.213e-32
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -4.11289655 -0.03873181 0.00352952 0.08922131 5.11404760
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.96061021 0.03684916
## Regime 2 0.03938979 0.96315084
# Plot the smoothed probabilities of the regimes
plotProb(msm_ftp, which = 1)
# Fit a linear model as a base model
model <- lm(CAMEROON ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to CAMEROON data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, p = 0, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE), p = 0)
##
## AIC BIC logLik
## 540.8543 558.908 -268.4272
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.8272 0.1882 4.3953 1.106e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.016339
## Multiple R-squared: 1.94e-31
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -4.11289655 -0.03873181 0.00352952 0.08922131 5.11404760
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.9719 0.0230 42.257 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2339457
## Multiple R-squared: 2.027e-30
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -5.179522e-01 -6.307453e-02 -7.669685e-37 7.228803e-02 4.945360e-01
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.96315084 0.03938979
## Regime 2 0.03684916 0.96061021
# Extract smoothed probabilities
smoothed_probs <- data.frame(
Quarterly = GDP_Growth$Quarterly[1:nrow(msm_ftp@Fit@smoProb)],
Regime1 = msm_ftp@Fit@smoProb[,1],
Regime2 = msm_ftp@Fit@smoProb[,2]
)
# Convert Quarterly to Date format
GDP_Growth$Date <- as.Date(paste0(substr(GDP_Growth$Quarterly, 1, 4), "-",
(as.numeric(substr(GDP_Growth$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
smoothed_probs$Date <- as.Date(paste0(substr(smoothed_probs$Quarterly, 1, 4), "-",
(as.numeric(substr(smoothed_probs$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
# Ensure both data frames have the correct number of rows
smoothed_probs <- smoothed_probs[1:nrow(GDP_Growth), ]
# Plot the actual GDP growth rate and the inferred regimes for CAMEROON
ggplot(GDP_Growth, aes(x = Date)) +
geom_line(aes(y = CAMEROON), color = "blue") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime1 * max(GDP_Growth$CAMEROON), color = "Regime 1"), linetype = "dashed") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime2 * max(GDP_Growth$CAMEROON), color = "Regime 2"), linetype = "dotted") +
labs(title = "GDP Growth Rate and Inferred Regimes for CAMEROON",
x = "Date",
y = "GDP Growth Rate / Regime Probability") +
theme_minimal()
# DRC
# Fit a linear model as a base model
model <- lm(DRC ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to DRC data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE))
##
## AIC BIC logLik
## 861.0235 879.0772 -428.5118
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.1384 0.3534 0.3916 0.6954
##
## Residual standard error: 2.284103
## Multiple R-squared: 5.906e-34
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -3.0605708 -0.1122525 0.1075989 0.2782024 6.3862095
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 1.2660 0.1245 10.169 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.8343038
## Multiple R-squared: 2.833e-31
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -1.42889489 -0.59489893 -0.05107403 0.50577280 1.55686979
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.8930295 0.05981622
## Regime 2 0.1069705 0.94018378
# Plot the smoothed probabilities of the regimes
plotProb(msm_ftp, which = 1)
# Convert Quarterly to Date format
GDP_Growth$Date <- as.Date(paste0(substr(GDP_Growth$Quarterly, 1, 4), "-",
(as.numeric(substr(GDP_Growth$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
smoothed_probs$Date <- as.Date(paste0(substr(smoothed_probs$Quarterly, 1, 4), "-",
(as.numeric(substr(smoothed_probs$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
# Ensure both data frames have the correct number of rows
smoothed_probs <- smoothed_probs[1:nrow(GDP_Growth), ]
# Plot the actual GDP growth rate and the inferred regimes for CAMEROON
ggplot(GDP_Growth, aes(x = Date)) +
geom_line(aes(y = DRC), color = "blue") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime1 * max(GDP_Growth$DRC), color = "Regime 1"), linetype = "dashed") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime2 * max(GDP_Growth$DRC), color = "Regime 2"), linetype = "dotted") +
labs(title = "GDP Growth Rate and Inferred Regimes for DRC",
x = "Date",
y = "GDP Growth Rate / Regime Probability") +
theme_minimal()
# Fit a linear model as a base model
model <- lm(GABON ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to DRC data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE))
##
## AIC BIC logLik
## 855.9556 874.0093 -425.9778
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.8405 0.0699 12.024 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.8564853
## Multiple R-squared: 1.075e-30
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -2.01024628 -0.36956175 0.01152309 0.51107767 1.57523740
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 1.6505 0.6626 2.4909 0.01274 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.77331
## Multiple R-squared: 7.79e-32
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -8.799034181 -0.062172755 -0.016395863 -0.001096506 9.169682263
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.97673557 0.06663729
## Regime 2 0.02326443 0.93336271
# Plot the smoothed probabilities of the regimes
plotProb(msm_ftp, which = 1)
# Convert Quarterly to Date format
GDP_Growth$Date <- as.Date(paste0(substr(GDP_Growth$Quarterly, 1, 4), "-",
(as.numeric(substr(GDP_Growth$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
smoothed_probs$Date <- as.Date(paste0(substr(smoothed_probs$Quarterly, 1, 4), "-",
(as.numeric(substr(smoothed_probs$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
# Ensure both data frames have the correct number of rows
smoothed_probs <- smoothed_probs[1:nrow(GDP_Growth), ]
# Plot the actual GDP growth rate and the inferred regimes for CAMEROON
ggplot(GDP_Growth, aes(x = Date)) +
geom_line(aes(y = DRC), color = "blue") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime1 * max(GDP_Growth$GABON), color = "Regime 1"), linetype = "dashed") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime2 * max(GDP_Growth$GABON), color = "Regime 2"), linetype = "dotted") +
labs(title = "GDP Growth Rate and Inferred Regimes for GABON",
x = "Date",
y = "GDP Growth Rate / Regime Probability") +
theme_minimal()
#GHANA
# Fit a linear model as a base model
model <- lm(GHANA ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to DRC data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE))
##
## AIC BIC logLik
## 509.8177 527.8714 -252.9088
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 1.1449 0.0225 50.884 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.222911
## Multiple R-squared: 2.481e-29
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -4.710480e-01 -3.359472e-02 -6.099398e-14 2.821340e-02 5.586324e-01
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.7328 0.1259 5.8205 5.867e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.451642
## Multiple R-squared: 2.866e-31
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -4.509964383 -0.177106570 0.005384045 0.270535283 2.942876520
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.9739964 0.02380021
## Regime 2 0.0260036 0.97619979
# Plot the smoothed probabilities of the regimes
plotProb(msm_ftp, which = 1)
# Convert Quarterly to Date format
GDP_Growth$Date <- as.Date(paste0(substr(GDP_Growth$Quarterly, 1, 4), "-",
(as.numeric(substr(GDP_Growth$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
smoothed_probs$Date <- as.Date(paste0(substr(smoothed_probs$Quarterly, 1, 4), "-",
(as.numeric(substr(smoothed_probs$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
# Ensure both data frames have the correct number of rows
smoothed_probs <- smoothed_probs[1:nrow(GDP_Growth), ]
# Plot the actual GDP growth rate and the inferred regimes for CAMEROON
ggplot(GDP_Growth, aes(x = Date)) +
geom_line(aes(y = DRC), color = "blue") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime1 * max(GDP_Growth$GHANA), color = "Regime 1"), linetype = "dashed") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime2 * max(GDP_Growth$GHANA), color = "Regime 2"), linetype = "dotted") +
labs(title = "GDP Growth Rate and Inferred Regimes for GHANA",
x = "Date",
y = "GDP Growth Rate / Regime Probability") +
theme_minimal()
#IVORY_COAST
# Fit a linear model as a base model
model <- lm(IVOR_COAST ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to DRC data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE))
##
## AIC BIC logLik
## 748.6868 766.7406 -372.3434
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 2.0512 0.0736 27.87 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6706564
## Multiple R-squared: 1.754e-30
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -1.2515197463 -0.2235208941 -0.0198549040 -0.0001433616 1.4848285721
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.2994 0.1218 2.4581 0.01397 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.310813
## Multiple R-squared: 1.793e-33
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -3.52925080 -0.21535050 0.07609383 0.31596388 5.19073598
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.95416035 0.03640898
## Regime 2 0.04583965 0.96359102
# Plot the smoothed probabilities of the regimes
plotProb(msm_ftp, which = 1)
# Convert Quarterly to Date format
GDP_Growth$Date <- as.Date(paste0(substr(GDP_Growth$Quarterly, 1, 4), "-",
(as.numeric(substr(GDP_Growth$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
smoothed_probs$Date <- as.Date(paste0(substr(smoothed_probs$Quarterly, 1, 4), "-",
(as.numeric(substr(smoothed_probs$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
# Ensure both data frames have the correct number of rows
smoothed_probs <- smoothed_probs[1:nrow(GDP_Growth), ]
# Plot the actual GDP growth rate and the inferred regimes for CAMEROON
ggplot(GDP_Growth, aes(x = Date)) +
geom_line(aes(y = DRC), color = "blue") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime1 * max(GDP_Growth$IVOR_COAST), color = "Regime 1"), linetype = "dashed") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime2 * max(GDP_Growth$IVOR_COAST), color = "Regime 2"), linetype = "dotted") +
labs(title = "GDP Growth Rate and Inferred Regimes for IVORY_COAST",
x = "Date",
y = "GDP Growth Rate / Regime Probability") +
theme_minimal()
# KENYA
# Fit a linear model as a base model
model <- lm(KENYA ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to DRC data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE))
##
## AIC BIC logLik
## 601.9779 620.0316 -298.989
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.9798 0.0515 19.025 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6136227
## Multiple R-squared: 1.178e-30
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -1.32021552 -0.34459544 0.05252642 0.43575291 1.21917801
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 2.0333 0.3816 5.3284 9.908e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.299905
## Multiple R-squared: 9.321e-31
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -5.01354279 -0.07411568 -0.02421199 -0.01733682 4.58578036
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.98372456 0.05829412
## Regime 2 0.01627544 0.94170588
# Plot the smoothed probabilities of the regimes
plotProb(msm_ftp, which = 1)
# Convert Quarterly to Date format
GDP_Growth$Date <- as.Date(paste0(substr(GDP_Growth$Quarterly, 1, 4), "-",
(as.numeric(substr(GDP_Growth$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
smoothed_probs$Date <- as.Date(paste0(substr(smoothed_probs$Quarterly, 1, 4), "-",
(as.numeric(substr(smoothed_probs$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
# Ensure both data frames have the correct number of rows
smoothed_probs <- smoothed_probs[1:nrow(GDP_Growth), ]
# Plot the actual GDP growth rate and the inferred regimes for CAMEROON
ggplot(GDP_Growth, aes(x = Date)) +
geom_line(aes(y = DRC), color = "blue") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime1 * max(GDP_Growth$KENYA), color = "Regime 1"), linetype = "dashed") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime2 * max(GDP_Growth$KENYA), color = "Regime 2"), linetype = "dotted") +
labs(title = "GDP Growth Rate and Inferred Regimes for KENYA",
x = "Date",
y = "GDP Growth Rate / Regime Probability") +
theme_minimal()
#NIGERIA
# Fit a linear model as a base model
model <- lm(NIGERIA ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to DRC data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE))
##
## AIC BIC logLik
## 842.5217 860.5754 -419.2608
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.9904 0.0972 10.189 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7894524
## Multiple R-squared: 0
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -1.571624e+00 -3.958431e-01 -9.579633e-06 4.710653e-01 1.428939e+00
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.7558 0.3794 1.9921 0.04636 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.008305
## Multiple R-squared: 1.226e-32
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -4.98564187 -0.09567266 0.01578043 0.09457793 6.40665464
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.96677054 0.07552644
## Regime 2 0.03322946 0.92447356
# Plot the smoothed probabilities of the regimes
plotProb(msm_ftp, which = 1)
# Convert Quarterly to Date format
GDP_Growth$Date <- as.Date(paste0(substr(GDP_Growth$Quarterly, 1, 4), "-",
(as.numeric(substr(GDP_Growth$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
smoothed_probs$Date <- as.Date(paste0(substr(smoothed_probs$Quarterly, 1, 4), "-",
(as.numeric(substr(smoothed_probs$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
# Ensure both data frames have the correct number of rows
smoothed_probs <- smoothed_probs[1:nrow(GDP_Growth), ]
# Plot the actual GDP growth rate and the inferred regimes for CAMEROON
ggplot(GDP_Growth, aes(x = Date)) +
geom_line(aes(y = DRC), color = "blue") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime1 * max(GDP_Growth$NIGERIA), color = "Regime 1"), linetype = "dashed") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime2 * max(GDP_Growth$NIGERIA), color = "Regime 2"), linetype = "dotted") +
labs(title = "GDP Growth Rate and Inferred Regimes for NIGERIA",
x = "Date",
y = "GDP Growth Rate / Regime Probability") +
theme_minimal()
#RWANDA
# Fit a linear model as a base model
model <- lm(RWANDA ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to DRC data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE))
##
## AIC BIC logLik
## 904.2144 922.2681 -450.1072
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 1.6955 0.0738 22.974 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9077772
## Multiple R-squared: 1.496e-30
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -1.830697420 -0.557912059 -0.001221437 0.442035017 2.095022398
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) -0.5952 0.7026 -0.8471 0.3969
##
## Residual standard error: 4.982816
## Multiple R-squared: 7.943e-33
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -15.03436031 0.07495206 0.10478667 0.24374853 12.65182106
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.96195984 0.118451
## Regime 2 0.03804016 0.881549
# Plot the smoothed probabilities of the regimes
plotProb(msm_ftp, which = 1)
# Convert Quarterly to Date format
GDP_Growth$Date <- as.Date(paste0(substr(GDP_Growth$Quarterly, 1, 4), "-",
(as.numeric(substr(GDP_Growth$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
smoothed_probs$Date <- as.Date(paste0(substr(smoothed_probs$Quarterly, 1, 4), "-",
(as.numeric(substr(smoothed_probs$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
# Ensure both data frames have the correct number of rows
smoothed_probs <- smoothed_probs[1:nrow(GDP_Growth), ]
# Plot the actual GDP growth rate and the inferred regimes for CAMEROON
ggplot(GDP_Growth, aes(x = Date)) +
geom_line(aes(y = DRC), color = "blue") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime1 * max(GDP_Growth$RWANDA), color = "Regime 1"), linetype = "dashed") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime2 * max(GDP_Growth$RWANDA), color = "Regime 2"), linetype = "dotted") +
labs(title = "GDP Growth Rate and Inferred Regimes for RWANDA",
x = "Date",
y = "GDP Growth Rate / Regime Probability") +
theme_minimal()
#SENEGAL
# Fit a linear model as a base model
model <- lm(SENEGAL ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to DRC data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE))
##
## AIC BIC logLik
## 554.4447 572.4985 -275.2224
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 1.1507 0.0643 17.896 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5643157
## Multiple R-squared: 1.548e-31
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -0.8586419 -0.4069317 -0.1479950 0.2908478 1.6078269
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) -0.3702 0.2001 -1.8501 0.0643 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7824113
## Multiple R-squared: 2.467e-31
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -1.84004291 0.05554908 0.10788504 0.26974866 0.68005485
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.93267264 0.2142697
## Regime 2 0.06732736 0.7857303
# Plot the smoothed probabilities of the regimes
plotProb(msm_ftp, which = 1)
# Convert Quarterly to Date format
GDP_Growth$Date <- as.Date(paste0(substr(GDP_Growth$Quarterly, 1, 4), "-",
(as.numeric(substr(GDP_Growth$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
smoothed_probs$Date <- as.Date(paste0(substr(smoothed_probs$Quarterly, 1, 4), "-",
(as.numeric(substr(smoothed_probs$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
# Ensure both data frames have the correct number of rows
smoothed_probs <- smoothed_probs[1:nrow(GDP_Growth), ]
# Plot the actual GDP growth rate and the inferred regimes for CAMEROON
ggplot(GDP_Growth, aes(x = Date)) +
geom_line(aes(y = DRC), color = "blue") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime1 * max(GDP_Growth$SENEGAL), color = "Regime 1"), linetype = "dashed") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime2 * max(GDP_Growth$SENEGAL), color = "Regime 2"), linetype = "dotted") +
labs(title = "GDP Growth Rate and Inferred Regimes for SENEGAL",
x = "Date",
y = "GDP Growth Rate / Regime Probability") +
theme_minimal()
#TANZANIA
# Fit a linear model as a base model
model <- lm(TANZANIA ~ 1, data = GDP_Growth)
# Fit a Markov-Switching model to DRC data with fixed transition probabilities
msm_ftp <- msmFit(model, k = 2, sw = c(TRUE, TRUE))
# Summarize the model
summary(msm_ftp)
## Markov Switching Model
##
## Call: msmFit(object = model, k = 2, sw = c(TRUE, TRUE))
##
## AIC BIC logLik
## 56.19241 71.84303 -26.0962
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.8292 0.0495 16.752 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3692079
## Multiple R-squared: 1.447e-30
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -0.706390025 0.007192186 0.011767141 0.147404594 0.419853457
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 1.6448 0.0227 72.458 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.1863317
## Multiple R-squared: 2.272e-29
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -0.3407641425 -0.0450097768 -0.0006673555 0.0339662618 0.3995802719
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.94967697 0.04088305
## Regime 2 0.05032303 0.95911695
# Plot the smoothed probabilities of the regimes
plotProb(msm_ftp, which = 1)
# Convert Quarterly to Date format
GDP_Growth$Date <- as.Date(paste0(substr(GDP_Growth$Quarterly, 1, 4), "-",
(as.numeric(substr(GDP_Growth$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
smoothed_probs$Date <- as.Date(paste0(substr(smoothed_probs$Quarterly, 1, 4), "-",
(as.numeric(substr(smoothed_probs$Quarterly, 6, 6)) - 1) * 3 + 1, "-01"), format="%Y-%m-%d")
# Ensure both data frames have the correct number of rows
smoothed_probs <- smoothed_probs[1:nrow(GDP_Growth), ]
# Plot the actual GDP growth rate and the inferred regimes for CAMEROON
ggplot(GDP_Growth, aes(x = Date)) +
geom_line(aes(y = DRC), color = "blue") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime1 * max(GDP_Growth$TANZANIA), color = "Regime 1"), linetype = "dashed") +
geom_line(data = smoothed_probs, aes(x = Date, y = Regime2 * max(GDP_Growth$TANZANIA), color = "Regime 2"), linetype = "dotted") +
labs(title = "GDP Growth Rate and Inferred Regimes for TANZANIA",
x = "Date",
y = "GDP Growth Rate / Regime Probability") +
theme_minimal()
## Warning: Removed 248 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Removed 248 rows containing missing values or values outside the scale range
## (`geom_line()`).
data <- GDP_Growth
# Define a function to count expansions and recessions
count_phases <- function(df, country) {
expansion_count <- sum(df[[country]] > 0, na.rm = TRUE)
recession_count <- sum(df[[country]] <= 0, na.rm = TRUE)
return(list(expansion_count = expansion_count, recession_count = recession_count))
}
# List of countries
countries <- colnames(data)[-1]
# Create a data frame to store the results
results <- data.frame(
Country = character(),
Expansions = numeric(),
Recessions = numeric(),
stringsAsFactors = FALSE
)
# Calculate expansions and recessions for each country
for (country in countries) {
counts <- count_phases(data, country)
results <- rbind(results, data.frame(
Country = country,
Expansions = counts$expansion_count,
Recessions = counts$recession_count
))
}
# Print the results
print(results)
## Country Expansions Recessions
## 1 CAMEROON 205 43
## 2 DRC 184 64
## 3 GABON 196 52
## 4 GHANA 210 38
## 5 IVOR_COAST 192 56
## 6 KENYA 230 18
## 7 NIGERIA 189 59
## 8 RWANDA 204 44
## 9 SENEGAL 209 39
## 10 TANZANIA 136 0
## 11 Date 211 37
# Save the results to an Excel file
#write.xlsx(results, "path/to/save/expansion_recession_counts.xlsx", row.names = FALSE)