Browse Source

Cours métaméthodes

master
Maxime Wack 6 years ago
parent
commit
af1f2d463b
24 changed files with 116 additions and 0 deletions
  1. +1
    -0
      05_Metamethods/.~lock.cours.odp#
  2. BIN
      05_Metamethods/all_1.png
  3. BIN
      05_Metamethods/all_2.png
  4. BIN
      05_Metamethods/all_3.png
  5. BIN
      05_Metamethods/all_4.png
  6. BIN
      05_Metamethods/all_5.png
  7. BIN
      05_Metamethods/all_6.png
  8. BIN
      05_Metamethods/all_7.png
  9. BIN
      05_Metamethods/all_8.png
  10. BIN
      05_Metamethods/all_9.png
  11. BIN
      05_Metamethods/cours.odp
  12. BIN
      05_Metamethods/error.png
  13. +115
    -0
      05_Metamethods/figures.R
  14. BIN
      05_Metamethods/rsquared.png
  15. BIN
      05_Metamethods/set.png
  16. BIN
      05_Metamethods/train_1.png
  17. BIN
      05_Metamethods/train_2.png
  18. BIN
      05_Metamethods/train_3.png
  19. BIN
      05_Metamethods/train_4.png
  20. BIN
      05_Metamethods/train_5.png
  21. BIN
      05_Metamethods/train_6.png
  22. BIN
      05_Metamethods/train_7.png
  23. BIN
      05_Metamethods/train_8.png
  24. BIN
      05_Metamethods/train_9.png

+ 1
- 0
05_Metamethods/.~lock.cours.odp# View File

@@ -0,0 +1 @@
,maxx,satamaxx-laptop,02.05.2018 00:03,file:///home/maxx/.config/libreoffice/4;

BIN
05_Metamethods/all_1.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 108KB

BIN
05_Metamethods/all_2.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 114KB

BIN
05_Metamethods/all_3.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 123KB

BIN
05_Metamethods/all_4.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 123KB

BIN
05_Metamethods/all_5.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 124KB

BIN
05_Metamethods/all_6.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 126KB

BIN
05_Metamethods/all_7.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 159KB

BIN
05_Metamethods/all_8.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 164KB

BIN
05_Metamethods/all_9.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 149KB

BIN
05_Metamethods/cours.odp View File


BIN
05_Metamethods/error.png View File

Before After
Width: 2100  |  Height: 2100  |  Size: 61KB

+ 115
- 0
05_Metamethods/figures.R View File

@@ -0,0 +1,115 @@
library(tidyverse)
library(shiny)
library(miniUI)
library(broom)

tibble(x = runif(20, 0, 10),
y = x^2 - 2*x + rnorm(20, 0, 5)) -> df

df %>%
ggplot() +
aes(x = x, y = y) +
geom_point() -> g

df %>%
sample_n(10) -> train

df %>%
anti_join(train) -> test

train %>%
ggplot() +
aes(x = x, y = y) +
geom_point() -> gtrain

gtrain +
geom_point(data = test, aes(color = "red")) +
theme(legend.position = "none") -> g

ggsave(filename = "set.png", plot = g)

powers <- function(df, n)
{
for (i in 1:n)
df[[str_c("x", i)]] <- df$x^i

df
}

LM <- function(df, n)
{
str_c("y ~ ", str_c("x", 1:n, collapse = " + ")) %>% as.formula -> form

df <- powers(df, n)

lm(form, data = df)
}

PREDICT <- function(train, test, n)
{
model <- LM(train, n)

test <- powers(test, n)

mean((test$y - predict(model, test))^2)
}

Fggfun <- function(model)
{
model %>%
pull(estimate) -> est
Vectorize(
function(x)
{
sum(x^(0:(length(est) - 1)) * est)
})
}

1:9 %>% map(~LM(train, .) %>% tidy %>% Fggfun) -> ggfuns

1:9 %>%
map(function(n)
{
LM(train, n) %>%
tidy %>%
Fggfun %>%
stat_function(fun = .)
}) -> regs

1:9 %>%
map(function(n)
{
(gtrain + regs[n]) %>%
ggsave(filename = str_c("train_", n, ".png"))
})

1:9 %>%
map(function(n)
{
(g + regs[n]) %>%
ggsave(filename = str_c("all_", n, ".png"))
})

(
1:9 %>%
map_dbl(~LM(train, .) %>% glance %>% pull(r.squared)) %>%
tibble(x = 1:9, y = .) %>%
ggplot() +
aes(x = x, y = y) +
geom_point() +
geom_line() +
scale_x_continuous(breaks = 0:10)
) %>%
ggsave(filename = "rsquared.png", plot = .)

(
1:9 %>%
map_dbl(~PREDICT(train, test, .)) %>%
tibble(x = 1:9, y = .) %>%
ggplot() +
aes(x = x, y = y) +
geom_point() +
geom_line() +
scale_x_continuous(breaks = 0:10)
) %>%
ggsave(filename = "error.png", plot = .)

BIN
05_Metamethods/rsquared.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 149KB

BIN
05_Metamethods/set.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 58KB

BIN
05_Metamethods/train_1.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 106KB

BIN
05_Metamethods/train_2.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 111KB

BIN
05_Metamethods/train_3.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 114KB

BIN
05_Metamethods/train_4.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 114KB

BIN
05_Metamethods/train_5.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 114KB

BIN
05_Metamethods/train_6.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 117KB

BIN
05_Metamethods/train_7.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 149KB

BIN
05_Metamethods/train_8.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 156KB

BIN
05_Metamethods/train_9.png View File

Before After
Width: 2953  |  Height: 3229  |  Size: 148KB

Loading…
Cancel
Save