|
- library(tidyverse)
- library(patchwork)
-
-
- # Graphique test statistique ----
-
- seq(100, 0) %>%
- map(function(x){
- ci <- function(m, s, n, z = 1.34)
- {
- dev <- z * (s / sqrt(n))
- c(m - dev, m + dev)
- }
-
- ztest <- function(m1, s1, n1, m2, s2, n2)
- {
- zscore <- abs(m2 - m1) / sqrt((s1^2 / n1) + (s2^2 / n2))
- 2 * (1 - pnorm(zscore))
- }
-
- tibble(m1 = x/10,
- m2 = - x/10,
- s = 10,
- n = 20) -> df
-
- df %>%
- ggplot() +
- aes(x = m1) +
- stat_function(fun = dnorm, args = list(mean = df$m1, sd = df$s), color = "blue", size = 1.5, n = 1000) +
- stat_function(fun = dnorm, args = list(mean = df$m2, sd = df$s), color = "red", size = 1.5, n = 1000) +
- geom_vline(xintercept = ci(df$m1, df$s, df$n), color = "blue") +
- geom_vline(xintercept = ci(df$m2, df$s, df$n), color = "red") +
- geom_text(x = -30, hjust = 0, y = .05, vjust = 1, label = str_c("p = ", ztest(df$m1, df$s, df$n, df$m2, df$s, df$n)), size = 4) +
- scale_x_continuous(limits = c(-30 , 30)) +
- scale_y_continuous(limits = c(0, .05)) +
- theme_classic() +
- xlab("x") -> p1
-
- df %>%
- ggplot() +
- aes(x = m1) +
- stat_function(fun = dnorm, args = list(mean = abs(df$m2 - df$m1), sd = sqrt(df$s^2 / df$n + df$s^2 / df$n))) +
- geom_vline(xintercept = 0) +
- geom_vline(xintercept = ci(abs(df$m2 - df$m1), sqrt(df$s^2 / df$n + df$s^2 / df$n), 1, 1.96)) +
- scale_x_continuous(limits = c(-10, 40)) +
- theme_classic() +
- xlab("x") -> p2
-
- p1 + p2 + plot_layout(ncol = 1, heights = c(8, 2))
- }) -> plots
-
- 1:101 %>%
- map(~ ggsave(plot = plots[[.]],
- filename = str_c("anim/", str_pad(., width = 3, pad = "0"), ".png"),
- unit = "cm",
- dpi = 200,
- width = 16,
- height = 9))
- # Un temps ----
-
- one <- tibble(color = rep(LETTERS[1:3], each = 10),
- y = map((-1:1), ~rnorm(10, ., .1)) %>% unlist,
- x = rnorm(30, 0, .1))
-
- one %>%
- ggplot() +
- geom_segment(x = -1, y = 0, xend = 1, yend = 0, color = "darkgrey", arrow = arrow(), size = 2) +
- geom_segment(jx = 0, y = -.1, xend = 0, yend = .1, color = "darkgrey", size = 2) +
- aes(x = x, y = y, color = color) +
- geom_point(size = 2) +
- scale_x_continuous(limits = c(-1, 1)) +
- scale_y_continuous(limits = c(-2, 2)) +
- theme_void() +
- theme(legend.position = "none",
- plot.background = element_rect(fill = "transparent", colour = "transparent")) ->
- oneplot
-
- ggsave(plot = oneplot, filename = "one.png", bg = "transparent")
-
- # Deux temps ----
-
- two <- tibble(x = map(c(-.5, .5), ~rnorm(10, ., .1)) %>% unlist,
- y = map(c(-1, 1), ~rnorm(10, ., .1)) %>% unlist,
- colour = rep("A", 20))
-
- two %>%
- ggplot() +
- geom_segment(x = -1, y = 0, xend = 1, yend = 0, color = "darkgrey", arrow = arrow(), size = 2) +
- geom_segment(x = -.5, y = -.1, xend = -.5, yend = .1, color = "darkgrey", size = 2) +
- geom_segment(x = .5, y = -.1, xend = .5, yend = .1, color = "darkgrey", size = 2) +
- aes(x = x, y = y, colour = colour) +
- geom_point(size = 2) +
- scale_x_continuous(limits = c(-1, 1)) +
- scale_y_continuous(limits = c(-2, 2)) +
- theme_void() +
- theme(legend.position = "none",
- plot.background = element_rect(fill = "transparent", colour = "transparent")) ->
- twoplot
-
- ggsave(plot = twoplot, filename = "two.png", bg = "transparent")
-
- # N temps ----
-
- many <- tibble(x = map(-3:3, ~rnorm(10, ., .1)) %>% unlist,
- y = runif(7, -1.5, 1.5) %>% map(~rnorm(10, ., .1)) %>% unlist,
- colour = rep("A", 70))
-
- many %>%
- ggplot() +
- geom_segment(x = -4, y = 0, xend = 4, yend = 0, color = "darkgrey", arrow = arrow(), size = 2) +
- geom_segment(x = -3, y = -.1, xend = -3, yend = .1, color = "darkgrey", size = 2) +
- geom_segment(x = -2, y = -.1, xend = -2, yend = .1, color = "darkgrey", size = 2) +
- geom_segment(x = -1, y = -.1, xend = -1, yend = .1, color = "darkgrey", size = 2) +
- geom_segment(x = 0, y = -.1, xend = 0, yend = .1, color = "darkgrey", size = 2) +
- geom_segment(x = 1, y = -.1, xend = 1, yend = .1, color = "darkgrey", size = 2) +
- geom_segment(x = 2, y = -.1, xend = 2, yend = .1, color = "darkgrey", size = 2) +
- geom_segment(x = 3, y = -.1, xend = 3, yend = .1, color = "darkgrey", size = 2) +
- aes(x = x, y = y, colour = colour) +
- geom_point(size = 2) +
- scale_x_continuous(limits = c(-4, 4)) +
- scale_y_continuous(limits = c(-2, 2)) +
- theme_void() +
- theme(legend.position = "none",
- plot.background = element_rect(fill = "transparent", colour = "transparent")) ->
- manyplot
-
- ggsave(plot = manyplot, filename = "many.png", bg = "transparent")
-
- # Série temporelle ----
-
- ## Figure introduction
- library(astsa)
- data(flu) #chargement de la base "flu" du package astsa
-
- ## Représentation graphique de la série temporelle
- tsplot(flu,xlab = "Time", ylab = "Deaths per 10,000 people",
- main="Monthly pneumonia and influenza deaths per 10,000 people \n in the United States for 11 years, 1968 to 1978")
-
- ## Simulation série
-
- ### Simulation des paramêtres temps, tendance, saisonalité, bruit
- t = time(cmort)
- m = 0.2*t+2.1
- s = cos(5*t)
- epsilon = rnorm(length(t),0,0.5)
-
- ### Simulation des séries temporelles comme modèle additif/multiplicatif/Hybride
- simu.ts.additif = ts(m+s+epsilon,frequency = 52, start = c(1970, 1) )
- tsplot(simu.ts.additif)
-
- simu.ts.multiplictif = ts(m*s*epsilon,frequency = 52, start = c(1970, 1) )
- tsplot(simu.ts.multiplictif)
-
- simu.ts.hybride = ts(m*s+epsilon*100,frequency = 52, start = c(1970, 1) )
- tsplot(simu.ts.hybride)
-
- #### Courbes
- plot(t,m, type = "l", main = "Tendance", xlab = "Temps", ylab = "")
- plot(t,s, type = "l", main = "Saisonnalité", xlab = "Temps", ylab = "")
- plot(t,epsilon, type = "l", main = "Bruit", xlab = "Temps", ylab = "")
-
- ### Décomposition des signaux simulés
- plot(decompose(simu.ts.additif, type="additive")) #décomposition trend/seasonnality/noise, modèle additif
- plot(stl(simu.ts.additif, "periodic"))
- plot(decompose(decompose(simu.ts.additif, type="additive")$random, type="additive"))
-
- ## Simulation modèles AR et MA
-
- ### Simulation AR(1) : X_t = 0.6X_{t???1} + ??_t
- X=arima.sim(n = 2400, list(ar = 0.6),sd = 1)
- plot(acf(X),lwd=2, main = "Acf AR(1)")
- plot(pacf(X),lwd=2, main = "Pacf AR(1)")
-
- ### Simulation AR(2) : Xt = 0.6X_{t???1} - 0.4X_{t???2} + ??t
- X=arima.sim(n = 2400, list(ar = c(0.6,-0.4)),sd = 1)
- plot(acf(X),lwd=2, main = "Acf AR(2)")
- plot(pacf(X),lwd=2, main = "Pacf AR(2)")
-
- ### Simulation MA(1) X_t = ??_t + 0.7??_{t???1}
- X=arima.sim(n = 2400, list(ma = .7),sd = 1)
- plot(acf(X),lwd=2, main = "Acf MA(1)")
- plot(pacf(X),lwd=2, main = "Pacf MA(1)")
|