You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
6.7KB

  1. library(tidyverse)
  2. library(patchwork)
  3. # Graphique test statistique ----
  4. seq(100, 0) %>%
  5. map(function(x){
  6. ci <- function(m, s, n, z = 1.34)
  7. {
  8. dev <- z * (s / sqrt(n))
  9. c(m - dev, m + dev)
  10. }
  11. ztest <- function(m1, s1, n1, m2, s2, n2)
  12. {
  13. zscore <- abs(m2 - m1) / sqrt((s1^2 / n1) + (s2^2 / n2))
  14. 2 * (1 - pnorm(zscore))
  15. }
  16. tibble(m1 = x/10,
  17. m2 = - x/10,
  18. s = 10,
  19. n = 20) -> df
  20. df %>%
  21. ggplot() +
  22. aes(x = m1) +
  23. stat_function(fun = dnorm, args = list(mean = df$m1, sd = df$s), color = "blue", size = 1.5, n = 1000) +
  24. stat_function(fun = dnorm, args = list(mean = df$m2, sd = df$s), color = "red", size = 1.5, n = 1000) +
  25. geom_vline(xintercept = ci(df$m1, df$s, df$n), color = "blue") +
  26. geom_vline(xintercept = ci(df$m2, df$s, df$n), color = "red") +
  27. 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) +
  28. scale_x_continuous(limits = c(-30 , 30)) +
  29. scale_y_continuous(limits = c(0, .05)) +
  30. theme_classic() +
  31. xlab("x") -> p1
  32. df %>%
  33. ggplot() +
  34. aes(x = m1) +
  35. stat_function(fun = dnorm, args = list(mean = abs(df$m2 - df$m1), sd = sqrt(df$s^2 / df$n + df$s^2 / df$n))) +
  36. geom_vline(xintercept = 0) +
  37. geom_vline(xintercept = ci(abs(df$m2 - df$m1), sqrt(df$s^2 / df$n + df$s^2 / df$n), 1, 1.96)) +
  38. scale_x_continuous(limits = c(-10, 40)) +
  39. theme_classic() +
  40. xlab("x") -> p2
  41. p1 + p2 + plot_layout(ncol = 1, heights = c(8, 2))
  42. }) -> plots
  43. 1:101 %>%
  44. map(~ ggsave(plot = plots[[.]],
  45. filename = str_c("anim/", str_pad(., width = 3, pad = "0"), ".png"),
  46. unit = "cm",
  47. dpi = 200,
  48. width = 16,
  49. height = 9))
  50. # Un temps ----
  51. one <- tibble(color = rep(LETTERS[1:3], each = 10),
  52. y = map((-1:1), ~rnorm(10, ., .1)) %>% unlist,
  53. x = rnorm(30, 0, .1))
  54. one %>%
  55. ggplot() +
  56. geom_segment(x = -1, y = 0, xend = 1, yend = 0, color = "darkgrey", arrow = arrow(), size = 2) +
  57. geom_segment(jx = 0, y = -.1, xend = 0, yend = .1, color = "darkgrey", size = 2) +
  58. aes(x = x, y = y, color = color) +
  59. geom_point(size = 2) +
  60. scale_x_continuous(limits = c(-1, 1)) +
  61. scale_y_continuous(limits = c(-2, 2)) +
  62. theme_void() +
  63. theme(legend.position = "none",
  64. plot.background = element_rect(fill = "transparent", colour = "transparent")) ->
  65. oneplot
  66. ggsave(plot = oneplot, filename = "one.png", bg = "transparent")
  67. # Deux temps ----
  68. two <- tibble(x = map(c(-.5, .5), ~rnorm(10, ., .1)) %>% unlist,
  69. y = map(c(-1, 1), ~rnorm(10, ., .1)) %>% unlist,
  70. colour = rep("A", 20))
  71. two %>%
  72. ggplot() +
  73. geom_segment(x = -1, y = 0, xend = 1, yend = 0, color = "darkgrey", arrow = arrow(), size = 2) +
  74. geom_segment(x = -.5, y = -.1, xend = -.5, yend = .1, color = "darkgrey", size = 2) +
  75. geom_segment(x = .5, y = -.1, xend = .5, yend = .1, color = "darkgrey", size = 2) +
  76. aes(x = x, y = y, colour = colour) +
  77. geom_point(size = 2) +
  78. scale_x_continuous(limits = c(-1, 1)) +
  79. scale_y_continuous(limits = c(-2, 2)) +
  80. theme_void() +
  81. theme(legend.position = "none",
  82. plot.background = element_rect(fill = "transparent", colour = "transparent")) ->
  83. twoplot
  84. ggsave(plot = twoplot, filename = "two.png", bg = "transparent")
  85. # N temps ----
  86. many <- tibble(x = map(-3:3, ~rnorm(10, ., .1)) %>% unlist,
  87. y = runif(7, -1.5, 1.5) %>% map(~rnorm(10, ., .1)) %>% unlist,
  88. colour = rep("A", 70))
  89. many %>%
  90. ggplot() +
  91. geom_segment(x = -4, y = 0, xend = 4, yend = 0, color = "darkgrey", arrow = arrow(), size = 2) +
  92. geom_segment(x = -3, y = -.1, xend = -3, yend = .1, color = "darkgrey", size = 2) +
  93. geom_segment(x = -2, y = -.1, xend = -2, yend = .1, color = "darkgrey", size = 2) +
  94. geom_segment(x = -1, y = -.1, xend = -1, yend = .1, color = "darkgrey", size = 2) +
  95. geom_segment(x = 0, y = -.1, xend = 0, yend = .1, color = "darkgrey", size = 2) +
  96. geom_segment(x = 1, y = -.1, xend = 1, yend = .1, color = "darkgrey", size = 2) +
  97. geom_segment(x = 2, y = -.1, xend = 2, yend = .1, color = "darkgrey", size = 2) +
  98. geom_segment(x = 3, y = -.1, xend = 3, yend = .1, color = "darkgrey", size = 2) +
  99. aes(x = x, y = y, colour = colour) +
  100. geom_point(size = 2) +
  101. scale_x_continuous(limits = c(-4, 4)) +
  102. scale_y_continuous(limits = c(-2, 2)) +
  103. theme_void() +
  104. theme(legend.position = "none",
  105. plot.background = element_rect(fill = "transparent", colour = "transparent")) ->
  106. manyplot
  107. ggsave(plot = manyplot, filename = "many.png", bg = "transparent")
  108. # Série temporelle ----
  109. ## Figure introduction
  110. library(astsa)
  111. data(flu) #chargement de la base "flu" du package astsa
  112. ## Représentation graphique de la série temporelle
  113. tsplot(flu,xlab = "Time", ylab = "Deaths per 10,000 people",
  114. main="Monthly pneumonia and influenza deaths per 10,000 people \n in the United States for 11 years, 1968 to 1978")
  115. ## Simulation série
  116. ### Simulation des paramêtres temps, tendance, saisonalité, bruit
  117. t = time(cmort)
  118. m = 0.2*t+2.1
  119. s = cos(5*t)
  120. epsilon = rnorm(length(t),0,0.5)
  121. ### Simulation des séries temporelles comme modèle additif/multiplicatif/Hybride
  122. simu.ts.additif = ts(m+s+epsilon,frequency = 52, start = c(1970, 1) )
  123. tsplot(simu.ts.additif)
  124. simu.ts.multiplictif = ts(m*s*epsilon,frequency = 52, start = c(1970, 1) )
  125. tsplot(simu.ts.multiplictif)
  126. simu.ts.hybride = ts(m*s+epsilon*100,frequency = 52, start = c(1970, 1) )
  127. tsplot(simu.ts.hybride)
  128. #### Courbes
  129. plot(t,m, type = "l", main = "Tendance", xlab = "Temps", ylab = "")
  130. plot(t,s, type = "l", main = "Saisonnalité", xlab = "Temps", ylab = "")
  131. plot(t,epsilon, type = "l", main = "Bruit", xlab = "Temps", ylab = "")
  132. ### Décomposition des signaux simulés
  133. plot(decompose(simu.ts.additif, type="additive")) #décomposition trend/seasonnality/noise, modèle additif
  134. plot(stl(simu.ts.additif, "periodic"))
  135. plot(decompose(decompose(simu.ts.additif, type="additive")$random, type="additive"))
  136. ## Simulation modèles AR et MA
  137. ### Simulation AR(1) : X_t = 0.6X_{t???1} + ??_t
  138. X=arima.sim(n = 2400, list(ar = 0.6),sd = 1)
  139. plot(acf(X),lwd=2, main = "Acf AR(1)")
  140. plot(pacf(X),lwd=2, main = "Pacf AR(1)")
  141. ### Simulation AR(2) : Xt = 0.6X_{t???1} - 0.4X_{t???2} + ??t
  142. X=arima.sim(n = 2400, list(ar = c(0.6,-0.4)),sd = 1)
  143. plot(acf(X),lwd=2, main = "Acf AR(2)")
  144. plot(pacf(X),lwd=2, main = "Pacf AR(2)")
  145. ### Simulation MA(1) X_t = ??_t + 0.7??_{t???1}
  146. X=arima.sim(n = 2400, list(ma = .7),sd = 1)
  147. plot(acf(X),lwd=2, main = "Acf MA(1)")
  148. plot(pacf(X),lwd=2, main = "Pacf MA(1)")