Refactor code for improved readability and consistency across R Markdown files

- Updated comments and code formatting in `3-td_ggplot2 - enonce.Rmd` for clarity.
- Enhanced code structure in `4-td_graphiques - enonce.Rmd` by organizing options and library calls.
- Replaced pipe operator `%>%` with `|>` in `Code_Lec3.Rmd` for consistency with modern R syntax.
- Cleaned up commented-out code and ensured consistent spacing in ggplot calls.
This commit is contained in:
2025-11-06 09:26:58 +01:00
parent 8f5f2b417c
commit 03bf0a4db2
10 changed files with 764 additions and 588 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -297,7 +297,7 @@ On présente ci-dessous un aperçu des données.
fold <- getwd()
# Load data
# load(paste0(fold, "/M2/Data Visualisation/tp1", "/data/datafreMPTL.RData")) # VSCode # nolint
# load(paste0(fold, "/M2/Data Visualisation/tp1", "/data/datafreMPTL.RData")) # VSCode
load(paste0(fold, "/data/datafreMPTL.RData")) # RStudio
paged_table(dat, options = list(rows.print = 15))
```
@@ -505,7 +505,7 @@ df_plot <- dat |>
p3 <- ggplot(df_plot, aes(x = DrivAge, y = freq)) +
geom_point() +
geom_smooth() +
labs(x = "Age du conducteur", y = "Frequence") +
labs(x = "Age du conducteur", y = "Frequence") +
theme_bw()
p3
```
@@ -642,12 +642,16 @@ plot_pairwise_disc <- function(df, var1, var2) {
df |>
group_by(varx, vary) |>
summarize(exp = sum(Exposure),
nb_claims = sum(ClaimNb),
freq = sum(ClaimNb) / sum(Exposure), .groups = "drop") |>
summarize(
exp = sum(Exposure),
nb_claims = sum(ClaimNb),
freq = sum(ClaimNb) / sum(Exposure), .groups = "drop"
) |>
ggplot(aes(x = varx, y = freq, colour = vary, group = vary), alpha = 0.3) +
geom_point() + geom_line() + theme_bw() +
labs(x = var1, y = "Frequence", colour = var2)
geom_point() +
geom_line() +
theme_bw() +
labs(x = var1, y = "Frequence", colour = var2)
}
```

View File

@@ -23,8 +23,13 @@ editor_options:
```{r setup, include=FALSE}
## Global options
knitr::opts_chunk$set(cache = FALSE, warning = FALSE, message = FALSE, fig.retina = 2)
options(encoding = 'UTF-8')
knitr::opts_chunk$set(
cache = FALSE,
warning = FALSE,
message = FALSE,
fig.retina = 2
)
options(encoding = "UTF-8")
```
@@ -33,11 +38,11 @@ options(encoding = 'UTF-8')
library(lattice)
library(grid)
library(ggplot2)
require(gridExtra)
require(gridExtra)
library(locfit)
library(scales)
library(formattable)
library(RColorBrewer)
library(RColorBrewer)
library(plotly)
library(dplyr)
library(tidyr)
@@ -88,7 +93,7 @@ de vie par pays sur la période 1952-1990. Les observations ont lieu tous les 5
Dans un premier temps, il faut installer le package et le charger.
```{r}
# install.packages("gapminder")
# install.packages("gapminder") #nolint
library(gapminder)
```
@@ -140,7 +145,7 @@ pouvez observer entre `gdpPercap` et `lifeExp`.
:::
```{r}
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp)) +
geom_point()
```
@@ -158,7 +163,7 @@ visualisations permettant de comparer des distributions.
```{r}
ggplot(data = gapminder, aes(x = lifeExp)) +
geom_density()
geom_density()
```
@@ -171,16 +176,16 @@ Il faut au préalable récupérer un fond de carte (ici de l'année 2016). Nous
les données `gapminder` de 2007.
```{r}
library(giscoR)
library(giscoR)
library(sf)
world <- gisco_countries
world <- subset(world, NAME_ENGL != "Antarctica") # Remove Antartica
# Merge data
world_df <- gapminder %>%
world_df <- gapminder |>
filter(year == "2007")
world_df <- world %>%
world_df <- world |>
left_join(world_df, by = c("NAME_ENGL" = "country"))
ggplot(world_df) +
@@ -231,7 +236,7 @@ accidents <- read_csv("data/accidentsVelo.csv",
date = col_date(format = "%Y-%m-%d")))
# few ajustements
accidents <- accidents %>%
accidents <- accidents |>
mutate(mois = factor(mois),
jour = factor(jour),
dep = factor(dep),
@@ -247,8 +252,8 @@ correct <- paste0("0", str_sub(correct, 1, 1), ":",
accidents$hrmn[issue] <- correct
# Extract hour
accidents <- accidents %>%
mutate(hour = paste(date, hrmn, sep = " ")) %>%
accidents <- accidents |>
mutate(hour = paste(date, hrmn, sep = " ")) |>
mutate(hour = strptime(hour, "%Y-%m-%d %H:%M")$hour)
# mapping table for french departments
@@ -327,8 +332,8 @@ library(mapview)
library(sf)
## Remove NA
df_map_dyn <- accidents %>%
filter(???) %>%
df_map_dyn <- accidents |>
filter(???) |>
na.omit()
# Make map and print it
@@ -354,27 +359,27 @@ Voici un premier code à trou pour vous aider.
```{r, eval = F}
# get french map - level nuts2
fr <- gisco_get_nuts(resolution = "20", country = ???, nuts_level = ???) %>%
fr <- gisco_get_nuts(resolution = "20", country = ???, nuts_level = ???) |>
mutate(res = "20M")
# Remove white-space to avoid errors.
library(stringr)
departements_francais <- departements_francais %>%
departements_francais <- departements_francais |>
mutate(dep_name = str_trim(dep_name))
fr <- fr %>%
fr <- fr |>
mutate(NUTS_NAME = str_trim(NUTS_NAME))
# Merge and remove departements outside metropolitan France
fr_map <- fr %>%
left_join(???) %>%
fr_map <- fr |>
left_join(???) |>
filter(! dep %in% c("971", ???) )
# count the number of accidents
df_acc <- ???
# merge statistics with the map
map_acc <- fr_map %>%
map_acc <- fr_map |>
left_join(df_acc, by = c("dep" = "dep"))
# map with all accidents

View File

@@ -194,11 +194,11 @@ linear.mod$results
```{r}
Ytrain <- cookie.train$sugars
dfc_train <- data.frame(ytrain = Ytrain, linear.mod = fitted(linear.mod))
dfc_train %>% rmarkdown::paged_table()
dfc_train |> rmarkdown::paged_table()
```
```{r}
dfc_train %>%
dfc_train |>
ggplot(aes(x = ytrain, y = linear.mod)) +
geom_point(size = 2, color = "#983399") +
geom_smooth(method = "lm", color = "#389900") +
@@ -211,9 +211,9 @@ dfc_train %>%
Ytest <- cookie.test$sugars
dfc_test <- data.frame(ytest = Ytest)
dfc_test$linear.mod <- predict(linear.mod, newdata = cookie.test)
# dfc_test%>%rmarkdown::paged_table()
# dfc_test|>rmarkdown::paged_table()
dfc_test %>%
dfc_test |>
ggplot(aes(x = ytest, y = linear.mod)) +
geom_point(size = 2, color = "#983399") +
geom_smooth(method = "lm", color = "#389900") +
@@ -244,7 +244,7 @@ ggplotly(ggplot(Lasso))
```
```{r}
Lasso$results %>% rmarkdown::paged_table()
Lasso$results |> rmarkdown::paged_table()
```
```{r}
@@ -271,8 +271,8 @@ coef_lasso <- data.frame(
Variable = rownames(as.matrix(coef(Lasso$finalModel, Lasso$bestTune$lambda))),
Coefficient = as.matrix(coef(Lasso$finalModel, Lasso$bestTune$lambda))[, 1]
)
coef_lasso %>%
subset(Coefficient != 0) %>%
coef_lasso |>
subset(Coefficient != 0) |>
rmarkdown::paged_table()
```
@@ -298,7 +298,7 @@ ggplotly(ggplot(ridge))
```
```{r}
ridge$results %>% rmarkdown::paged_table()
ridge$results |> rmarkdown::paged_table()
```
```{r}
@@ -320,7 +320,7 @@ vip(ridge, num_features = 15)
```
```{r}
data.frame(as.matrix(coef(ridge$finalModel, ridge$bestTune$lambda))) %>%
data.frame(as.matrix(coef(ridge$finalModel, ridge$bestTune$lambda))) |>
rmarkdown::paged_table()
```
@@ -346,7 +346,7 @@ ggplotly(ggplot(ElNet))
```
```{r}
ElNet$results %>% rmarkdown::paged_table()
ElNet$results |> rmarkdown::paged_table()
```
```{r}
@@ -372,8 +372,8 @@ coef_elnet <- data.frame(
Variable = rownames(as.matrix(coef(ElNet$finalModel, ElNet$bestTune$lambda))),
Coefficient = as.matrix(coef(ElNet$finalModel, ElNet$bestTune$lambda))[, 1]
)
coef_elnet %>%
subset(Coefficient != 0) %>%
coef_elnet |>
subset(Coefficient != 0) |>
rmarkdown::paged_table()
```
@@ -396,7 +396,7 @@ ggplotly(ggplot(pls_mod))
```
```{r}
pls_mod$results %>% rmarkdown::paged_table()
pls_mod$results |> rmarkdown::paged_table()
```
```{r}
@@ -412,7 +412,7 @@ vip(pls_mod, num_features = 20)
```
```{r}
data.frame(Coefficients = as.matrix(coef(pls_mod$finalModel))) %>%
data.frame(Coefficients = as.matrix(coef(pls_mod$finalModel))) |>
rmarkdown::paged_table()
```
@@ -435,7 +435,7 @@ dTrain$ridge <- fitted(ridge)
dTrain$ElNet <- fitted(ElNet)
dTrain$pls <- fitted(pls_mod)
melt.dTrain <- melt(dTrain, id = "yTrain", variable.name = "model")
melt.dTrain %>% ggplot() +
melt.dTrain |> ggplot() +
aes(x = yTrain, y = value) +
geom_smooth(method = "lm") +
geom_point(size = 1, colour = "#983399") +
@@ -446,11 +446,11 @@ melt.dTrain %>% ggplot() +
```
```{r}
dTrain %>% rmarkdown::paged_table()
dTrain |> rmarkdown::paged_table()
```
```{r}
melt.dTrain %>% rmarkdown::paged_table()
melt.dTrain |> rmarkdown::paged_table()
```
### On the test set
@@ -463,10 +463,10 @@ dTest$Lasso <- predict(Lasso, newdata = cookie.test)
dTest$ridge <- predict(ridge, newdata = cookie.test)
dTest$ElNet <- predict(ElNet, newdata = cookie.test)
dTest$pls <- predict(pls_mod, newdata = cookie.test)
# dTest%>% rmarkdown::paged_table()
# dTest|> rmarkdown::paged_table()
melt.dTest <- melt(dTest, id = "yTest", variable.name = "model")
# melt.dTest%>% rmarkdown::paged_table()
melt.dTest %>% ggplot() +
# melt.dTest|> rmarkdown::paged_table()
melt.dTest |> ggplot() +
aes(x = yTest, y = value) +
geom_smooth(method = "lm") +
geom_point(size = 1, colour = "#983399") +
@@ -491,8 +491,8 @@ RMSE <- rbind.data.frame(
)
names(RMSE) <- c("Train", "Test")
row.names(RMSE) <- c("Linear", "Lasso", "Ridge", "ElNet", "PLS")
RMSE %>%
kableExtra::kbl() %>%
RMSE |>
kableExtra::kbl() |>
kableExtra::kable_styling()
```