Importation et chargement de la base

library(readxl)
library(ggplot2)
library(tidyr)

base_bot <- read_excel("C:/Users/DELL/Desktop/base_bot.xlsx", sheet = "Base")
head(base_bot)
## # A tibble: 6 × 4
##   Annee           C       PIB_r         RNB
##   <chr>       <dbl>       <dbl>       <dbl>
## 1 1990  3139079431. 5274313873. 5163084155.
## 2 1991  3364466918. 5667709608. 5712056669.
## 3 1992  3480302391. 5833040675. 5920640884.
## 4 1993  3575602576. 5944807979. 6208045254.
## 5 1994  3702263332. 6160480628. 5932196869.
## 6 1995  4142509121. 6593587690. 6546735502.
base_bot$Annee <- as.numeric(base_bot$Annee)
attach(base_bot)

2. Statistiques descriptives

summary(base_bot)
##      Annee            C                 PIB_r                RNB           
##  Min.   :1990   Min.   :3.139e+09   Min.   :5.274e+09   Min.   :5.163e+09  
##  1st Qu.:1998   1st Qu.:4.844e+09   1st Qu.:7.592e+09   1st Qu.:7.697e+09  
##  Median :2006   Median :6.168e+09   Median :1.031e+10   Median :1.021e+10  
##  Mean   :2006   Mean   :7.261e+09   Mean   :1.071e+10   Mean   :1.037e+10  
##  3rd Qu.:2014   3rd Qu.:1.031e+10   3rd Qu.:1.353e+10   3rd Qu.:1.308e+10  
##  Max.   :2022   Max.   :1.299e+10   Max.   :1.747e+10   Max.   :1.745e+10
sd(PIB_r)
## [1] 3599830167
sd(C)
## [1] 3159746217
sd(RNB)
## [1] 3475180701
data_long <- pivot_longer(base_bot, cols = c(PIB_r, C, RNB),
                          names_to = "Variable",
                          values_to = "Valeur")
# Tracer toutes les courbes sur un même graphique
ggplot(data_long, aes(x = Annee, y = Valeur, color = Variable)) +
  geom_line(size = 1.3) +
  labs(
    title = "Évolution du PIB, de la Consommation et du RNB au Botswana (1990–2022)",
    x = "Année",
    y = "Valeur (en USD constants)",
    color = "Variables macroéconomiques"
  ) +
  scale_color_manual(
    values = c(
      "PIB_r" = "#e75480",   # rose vif
      "C" = "#d896d8",   # rose mauve
      "RNB" = "#c71585"  # violet rosé foncé
    ),
    labels = c("PIB", "Consommation", "RNB")
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(face = "bold", size = 14),
    axis.title = element_text(size = 12),
    legend.title = element_text(size = 11),
    legend.text = element_text(size = 10)
  )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

3. Corrélations

library(corrplot)
## Warning: package 'corrplot' was built under R version 4.4.1
## corrplot 0.94 loaded
vars <- base_bot[, c("PIB_r", "C", "RNB")]

# Calcul de la matrice de corrélation
cor_matrix <- cor(vars, use = "complete.obs")

# Visualisation élégante
corrplot(cor_matrix, method = "color",
         type = "upper",           # Affiche la moitié supérieure
         col = colorRampPalette(c("#fbe1e7", "#fa8ea4", "#fa8eb2"))(200),  # Palette rose-violet
         tl.col = "black",         # Couleur des labels
         tl.cex = 1.2,             # Taille du texte
         addCoef.col = "black",   # Affiche les coefficients
         number.cex = 1.1,         # Taille des coefficients
         title = "Matrice de corrélation entre PIB_r, C et RNB",
         mar = c(0,0,2,0))         # Marge pour le ti

# 4. Régression pour la PMC (avec PIB)

model_pmc_pib <- lm(C ~ PIB_r)
summary(model_pmc_pib)
## 
## Call:
## lm(formula = C ~ PIB_r)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -1.346e+09 -3.530e+08  2.335e+08  4.026e+08  1.494e+09 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.903e+09  3.956e+08  -4.809 3.71e-05 ***
## PIB_r        8.558e-01  3.507e-02  24.398  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 714200000 on 31 degrees of freedom
## Multiple R-squared:  0.9505, Adjusted R-squared:  0.9489 
## F-statistic: 595.3 on 1 and 31 DF,  p-value: < 2.2e-16

5. Régression pour la PMC (avec RNB)

model_pmc_rnb <- lm(C ~ RNB)
summary(model_pmc_rnb)
## 
## Call:
## lm(formula = C ~ RNB)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -1.388e+09 -5.715e+08  1.294e+08  4.080e+08  1.067e+09 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.985e+09  3.527e+08  -5.629 3.53e-06 ***
## RNB          8.913e-01  3.228e-02  27.609  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 634600000 on 31 degrees of freedom
## Multiple R-squared:  0.9609, Adjusted R-squared:  0.9597 
## F-statistic: 762.3 on 1 and 31 DF,  p-value: < 2.2e-16

C =f(Y)

plot(C ~ PIB_r, main="Consommation en fonction du PIB", xlab="PIB", ylab="Consommation")
abline(lm(C ~ PIB_r, data=base_bot), col="#c71585")

plot(C ~ RNB, main="Consommation en fonction du RNB", xlab="RNB", ylab="Consommation")
abline(lm(C ~ RNB, data=base_bot), col="#c71585")

# AR ## Transformer en série temporelle

library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(urca)
library(forecast)
Y <- ts(PIB_r, start = 1990, frequency = 1)
# 2. Test de stationnarité
adf_result <- adf.test(Y)
print(adf_result)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  Y
## Dickey-Fuller = -3.0277, Lag order = 3, p-value = 0.1756
## alternative hypothesis: stationary
Y_diff <- diff(Y)
adf_resultdif <- adf.test(Y_diff)
## Warning in adf.test(Y_diff): p-value smaller than printed p-value
print(adf_resultdif)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  Y_diff
## Dickey-Fuller = -4.7363, Lag order = 3, p-value = 0.01
## alternative hypothesis: stationary

##correlogramme

par(mfrow = c(1,2))  # 2 graphiques côte à côte
acf(Y, main = "ACF du PIB ", col = "#c71585")
pacf(Y, main = "PACF du PIB ", col = "purple")

par(mfrow = c(1,1))  # Réinitialiser
par(mfrow = c(1,2))  # 2 graphiques côte à côte
acf(Y_diff, main = "ACF du PIB différencié", col = "#c71585")
pacf(Y_diff, main = "PACF du PIB différencié", col = "purple")

par(mfrow = c(1,1))  # Réinitialiser

Ajuster un modèle AR(2)

model_ar2 <- arima(Y, order = c(2, 1, 0))
summary(model_ar2)
## 
## Call:
## arima(x = Y, order = c(2, 1, 0))
## 
## Coefficients:
##          ar1     ar2
##       0.0135  0.0090
## s.e.  0.1792  0.1932
## 
## sigma^2 estimated as 5.866e+17:  log likelihood = -700.02,  aic = 1406.03
## 
## Training set error measures:
##                     ME      RMSE       MAE      MPE     MAPE      MASE
## Training set 362466878 754182779 598214339 3.357767 5.286656 0.9646854
##                    ACF1
## Training set -0.2901335
model_ar1 <- arima(Y, order = c(1, 1, 0))
model_ar1
## 
## Call:
## arima(x = Y, order = c(1, 1, 0))
## 
## Coefficients:
##          ar1
##       0.0128
## s.e.  0.1786
## 
## sigma^2 estimated as 5.866e+17:  log likelihood = -700.02,  aic = 1404.03
best_model <- auto.arima(Y)
summary(best_model)
## Series: Y 
## ARIMA(0,1,0) with drift 
## 
## Coefficients:
##           drift
##       381093145
## s.e.  102454471
## 
## sigma^2 = 4.557e+17:  log likelihood = -695.47
## AIC=1394.94   AICc=1395.35   BIC=1397.87
## 
## Training set error measures:
##                    ME      RMSE       MAE        MPE    MAPE      MASE
## Training set 148279.3 654291453 427449402 -0.3667388 3.70096 0.6893084
##                    ACF1
## Training set -0.2874421

Comparaison avec AR(1)

AIC(model_ar1); AIC(model_ar2)
## [1] 1404.033
## [1] 1406.03

Test de significativité des coefficients

coeftest <- function(model) {
  coef <- model$coef
  se <- sqrt(diag(model$var.coef))
  tval <- coef / se
  pval <- 2 * (1 - pnorm(abs(tval)))
  data.frame(Coefficient = coef, `Std. Error` = se, `t value` = tval, `Pr(>|t|)` = pval)
}
coeftest(model_ar2)
##     Coefficient Std..Error    t.value  Pr...t..
## ar1 0.013520358  0.1792000 0.07544843 0.9398579
## ar2 0.009046208  0.1931616 0.04683233 0.9626469
coeftest(model_ar1)
##     Coefficient Std..Error    t.value  Pr...t..
## ar1  0.01282455  0.1786411 0.07178946 0.9427695
coeftest(best_model)
##       Coefficient Std..Error  t.value     Pr...t..
## drift   381093145  102454471 3.719634 0.0001995116