---
title: "arma-estimation"
output:
  html_notebook: null
  html_document:
    df_print: paged
---

Les notes de cours sont disponibles [ici](https://le-mans.adjemian.eu/s%C3%A9ries-temporelles/estimation-arma/Estimation%20des%20mod%C3%A8les%20ARMA.pdf).

## Simulation d'un modèle ARMA(2,1)

```{r}
y <- arima.sim(model = list(ar = c(0.9, -0.2), ma = 0.8), rand.gen=rnorm, sd = sqrt(0.1), n = 10000)
plot.ts(y)
```
## Estimation des fonctions d'autocovariance et d'autocovariance partielle

```{r}
y_s_acf <- acf(y, type="correlation", lag.max=21)
y_s_pacf <- acf(y, type="partial", lag.max=21)
```
## Comparaison avec les moments théoriques
```{r}
require(ggplot2)


y_acf <- ARMAacf(ar=c(0.9, -0.2), ma=0.8, lag.max=21)

h = seq(0,21,1)
theoretical_acf = array(y_acf)
simulated_acf = y_s_acf$acf

df <- data.frame(h,theoretical_acf,simulated_acf)

g <- ggplot(df, aes(h))
g <- g + geom_point(aes(y=theoretical_acf), colour="red")
g <- g + geom_point(aes(y=simulated_acf), colour="black")
g <- g + labs(y = "Autocorrelation")
g

y_pacf <- ARMAacf(ar=c(0.9, -0.2), ma=0.8, lag.max=21, pacf=TRUE)

h = seq(1,21,1)
theoretical_pacf = array(y_pacf)
simulated_pacf = y_s_pacf$acf

df <- data.frame(h,theoretical_pacf,simulated_pacf)

g <- ggplot(df, aes(h))
g <- g + geom_point(aes(y=theoretical_pacf), colour="red")
g <- g + geom_point(aes(y=simulated_pacf), colour="black")
g <- g + labs(y = "Partial autocorrelation")
g <- g +theme(legend.position="bottom")
g

```
## Estimation de modèles ARMA
```{r}
y <- arima.sim(model = list(ar = c(0.9, -0.2), ma = 0.8), rand.gen=rnorm, sd = sqrt(0.1), n = 500)
arma10 <- arima(y, order = c(1, 0, 0), include.mean = FALSE)
arma20 <- arima(y, order = c(2, 0, 0), include.mean = FALSE)
arma30 <- arima(y, order = c(3, 0, 0), include.mean = FALSE)
arma40 <- arima(y, order = c(4, 0, 0), include.mean = FALSE)
arma50 <- arima(y, order = c(5, 0, 0), include.mean = FALSE)
arma11 <- arima(y, order = c(1, 0, 1), include.mean = FALSE)
arma21 <- arima(y, order = c(2, 0, 1), include.mean = FALSE)
arma31 <- arima(y, order = c(3, 0, 1), include.mean = FALSE)
arma41 <- arima(y, order = c(4, 0, 1), include.mean = FALSE)
arma51 <- arima(y, order = c(5, 0, 1), include.mean = FALSE)
arma12 <- arima(y, order = c(1, 0, 2), include.mean = FALSE)
arma22 <- arima(y, order = c(2, 0, 2), include.mean = FALSE)
arma32 <- arima(y, order = c(3, 0, 2), include.mean = FALSE)
arma42 <- arima(y, order = c(4, 0, 2), include.mean = FALSE)
arma52 <- arima(y, order = c(5, 0, 2), include.mean = FALSE)
arma13 <- arima(y, order = c(1, 0, 3), include.mean = FALSE)
arma23 <- arima(y, order = c(2, 0, 3), include.mean = FALSE)
arma33 <- arima(y, order = c(3, 0, 3), include.mean = FALSE)
arma43 <- arima(y, order = c(4, 0, 3), include.mean = FALSE)
arma53 <- arima(y, order = c(5, 0, 3), include.mean = FALSE)
arma14 <- arima(y, order = c(1, 0, 4), include.mean = FALSE)
arma24 <- arima(y, order = c(2, 0, 4), include.mean = FALSE)
arma34 <- arima(y, order = c(3, 0, 4), include.mean = FALSE)
arma44 <- arima(y, order = c(4, 0, 4), include.mean = FALSE)
arma54 <- arima(y, order = c(5, 0, 4), include.mean = FALSE)
arma15 <- arima(y, order = c(1, 0, 5), include.mean = FALSE)
arma25 <- arima(y, order = c(2, 0, 5), include.mean = FALSE)
arma35 <- arima(y, order = c(3, 0, 5), include.mean = FALSE)
arma45 <- arima(y, order = c(4, 0, 5), include.mean = FALSE)
arma55 <- arima(y, order = c(5, 0, 5), include.mean = FALSE)
arma01 <- arima(y, order = c(0, 0, 1), include.mean = FALSE)
arma02 <- arima(y, order = c(0, 0, 2), include.mean = FALSE)
arma03 <- arima(y, order = c(0, 0, 3), include.mean = FALSE)
arma04 <- arima(y, order = c(0, 0, 4), include.mean = FALSE)
arma05 <- arima(y, order = c(0, 0, 5), include.mean = FALSE)
aic <- AIC(arma01,arma02,arma03,arma04,arma05,
    arma11,arma12,arma13,arma14,arma15,
    arma21,arma22,arma23,arma24,arma25,
    arma31,arma32,arma33,arma34,arma35,
    arma41,arma42,arma43,arma44,arma45,
    arma51,arma52,arma53,arma54,arma55)
bic <- BIC(arma01,arma02,arma03,arma04,arma05,
    arma11,arma12,arma13,arma14,arma15,
    arma21,arma22,arma23,arma24,arma25,
    arma31,arma32,arma33,arma34,arma35,
    arma41,arma42,arma43,arma44,arma45,
    arma51,arma52,arma53,arma54,arma55)


aic[which(aic$AIC==min(aic$AIC)),]
bic[which(bic$BIC==min(bic$BIC)),]
arma21
```

