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

View File

@@ -1,5 +1,5 @@
```{r}
setwd('/Users/arthurdanjou/Workspace/studies/M1/General Linear Models/TP1-bis')
setwd("/Users/arthurdanjou/Workspace/studies/M1/General Linear Models/TP1-bis")
library(tidyverse)
options(scipen = 999, digits = 5)
@@ -56,8 +56,8 @@ summary(model)
coef(model)
```
```{r}
data <- data %>%
mutate(yhat = beta0 + beta1 * poids) %>%
data <- data |>
mutate(yhat = beta0 + beta1 * poids) |>
mutate(residuals = cholesterol - yhat)
data
@@ -71,8 +71,8 @@ ggplot(data, aes(x = poids, y = cholesterol)) +
```{r}
mean(data[, "cholesterol"])
mean(data[, "yhat"])
mean(data[, "residuals"]) %>% round(10)
cov(data[, "residuals"], data[, "poids"]) %>% round(10)
mean(data[, "residuals"]) |> round(10)
cov(data[, "residuals"], data[, "poids"]) |> round(10)
(RSS <- sum((data[, "residuals"])^2))
(TSS <- sum((y - mean(y))^2))
TSS - beta1 * Sxy
@@ -117,10 +117,10 @@ t <- qt(0.975, dof)
sigma_hat <- sigma(model)
n <- nrow(data)
data <- data %>%
data <- data |>
mutate(error = t *
sigma_hat *
sqrt(1 / n + (poids - mean(poids))^2 / RSS)) %>%
sqrt(1 / n + (poids - mean(poids))^2 / RSS)) |>
mutate(conf.low = yhat - error, conf.high = yhat + error, error = NULL)
ggplot(data, aes(x = poids, y = cholesterol)) +

View File

@@ -1,5 +1,5 @@
```{r}
setwd('/Users/arthurdanjou/Workspace/studies/M1/General Linear Models/TP2-bis')
setwd("/Users/arthurdanjou/Workspace/studies/M1/General Linear Models/TP2-bis")
library(tidyverse)
library(GGally)
@@ -10,9 +10,9 @@ library(qqplotr)
options(scipen = 999, digits = 5)
```
```{r}
data <- read.csv('data02.csv', sep = ',', header = TRUE, dec = ".")
data %>%
mutate(type = factor(type, levels = c("maths", "english", "final"), labels = c("maths", "english", "final"))) %>%
data <- read.csv("data02.csv", sep = ",", header = TRUE, dec = ".")
data |>
mutate(type = factor(type, levels = c("maths", "english", "final"), labels = c("maths", "english", "final"))) |>
ggplot(aes(x = note)) +
facet_wrap(vars(type), scales = "free_x") +
geom_histogram(binwidth = 4, color = "black", fill = "grey80") +
@@ -21,8 +21,8 @@ data %>%
```
```{r}
data_wide <- pivot_wider(data, names_from = type, values_from = note)
data_wide %>%
select(-id) %>%
data_wide |>
select(-id) |>
ggpairs() + theme_bw(14)
```
```{r}
@@ -67,12 +67,12 @@ linearHypothesis(model, "maths - english = 0")
# Submodel testing
```{r}
data_predict <- predict(model, newdata = expand.grid(maths = seq(70, 90, 2), english = c(75, 85)), interval = "confidence") %>%
as_tibble() %>%
data_predict <- predict(model, newdata = expand.grid(maths = seq(70, 90, 2), english = c(75, 85)), interval = "confidence") |>
as_tibble() |>
bind_cols(expand.grid(maths = seq(70, 90, 2), english = c(75, 85)))
data_predict %>%
mutate(english = as.factor(english)) %>%
data_predict |>
mutate(english = as.factor(english)) |>
ggplot(aes(x = maths, y = fit, color = english, fill = english, label = round(fit, 1))) +
geom_ribbon(aes(ymin = lwr, ymax = upr), alpha = 0.2, show.legend = FALSE) +
geom_point(size = 2) +

View File

@@ -1,5 +1,5 @@
```{r}
setwd('/Users/arthurdanjou/Workspace/studies/M1/General Linear Models/TP2')
setwd("/Users/arthurdanjou/Workspace/studies/M1/General Linear Models/TP2")
```
# Question 1 : Import dataset and check variables
@@ -9,8 +9,8 @@ library(dplyr)
cepages <- read.csv("Cepages B TP2.csv", header = TRUE, sep = ";", dec = ",")
cepages$Couleur <- as.factor(cepages$Couleur)
cepages$Origine <- as.factor(cepages$Origine)
cepages <- cepages %>% mutate(across(where(is.character), as.numeric))
cepages <- cepages %>% mutate(across(where(is.integer), as.numeric))
cepages <- cepages |> mutate(across(where(is.character), as.numeric))
cepages <- cepages |> mutate(across(where(is.integer), as.numeric))
paged_table(cepages)
```
@@ -39,7 +39,7 @@ tapply(cepages$pH, list(cepages$Couleur, cepages$Origine), mean)
library(ggplot2)
ggplot(cepages, aes(x = AcTot, y = pH, color = Couleur)) +
geom_point(col = 'red', size = 0.5) +
geom_point(col = "red", size = 0.5) +
geom_smooth(method = "lm", se = F)
ggplot(cepages, aes(y = pH, x = AcTot, colour = Couleur, fill = Couleur)) +
@@ -50,8 +50,8 @@ ggplot(cepages, aes(y = pH, x = AcTot, colour = Couleur, fill = Couleur)) +
```{r}
ggplot(cepages, aes(x = AcTot, y = pH, color = Origine)) +
geom_smooth(method = 'lm', se = F) +
geom_point(col = 'red', size = 0.5)
geom_smooth(method = "lm", se = F) +
geom_point(col = "red", size = 0.5)
ggplot(cepages, aes(y = pH, x = AcTot, colour = Origine, fill = Origine)) +
geom_boxplot(alpha = 0.5, outlier.alpha = 0)

View File

@@ -1,5 +1,5 @@
```{r}
setwd('/Users/arthurdanjou/Workspace/studies/M1/General Linear Models/TP3')
setwd("/Users/arthurdanjou/Workspace/studies/M1/General Linear Models/TP3")
```
# Question 1 : Import dataset and check variables
@@ -9,8 +9,8 @@ library(dplyr)
ozone <- read.table("ozone.txt", header = TRUE, sep = " ", dec = ".")
ozone$vent <- as.factor(ozone$vent)
ozone$temps <- as.factor(ozone$temps)
ozone <- ozone %>% mutate(across(where(is.character), as.numeric))
ozone <- ozone %>% mutate(across(where(is.integer), as.numeric))
ozone <- ozone |> mutate(across(where(is.character), as.numeric))
ozone <- ozone |> mutate(across(where(is.integer), as.numeric))
paged_table(ozone)
```
@@ -25,8 +25,8 @@ summary(model_T12)
library(ggplot2)
ggplot(ozone, aes(x = T12, y = maxO3)) +
geom_smooth(method = 'lm', se = T) +
geom_point(col = 'red', size = 0.5) +
geom_smooth(method = "lm", se = T) +
geom_point(col = "red", size = 0.5) +
labs(title = "maxO3 ~ T12") +
theme_minimal()
```
@@ -130,5 +130,4 @@ new_obs <- list(
maxO3v = 85
)
predict(model_backward, new_obs, interval = "confidence")
```

View File

@@ -1,5 +1,5 @@
```{r}
setwd('/Users/arthurdanjou/Workspace/studies/M1/General Linear Models/TP4')
setwd("/Users/arthurdanjou/Workspace/studies/M1/General Linear Models/TP4")
set.seed(0911)
library(ggplot2)
@@ -22,19 +22,19 @@ library(lmtest) # LRtest
library(survey) # Wald test
library(vcdExtra) # deviance test
library(rsample) # for data splitting
library(rsample) # for data splitting
library(glmnet)
library(nnet) # multinom, glm
library(caret)
library(ROCR)
#library(PRROC) autre package pour courbe roc et courbe pr
# library(PRROC) autre package pour courbe roc et courbe pr
library(ISLR) # dataset for statistical learning
ggplot2::theme_set(ggplot2::theme_light())# Set the graphical theme
ggplot2::theme_set(ggplot2::theme_light()) # Set the graphical theme
```
```{r}
car <- read.table('car_income.txt', header = TRUE, sep = ';')
car %>% rmarkdown::paged_table()
car <- read.table("car_income.txt", header = TRUE, sep = ";")
car |> rmarkdown::paged_table()
summary(car)
```
@@ -44,7 +44,7 @@ summary(model_purchase)
```
```{r}
p1 <- car %>%
p1 <- car |>
ggplot(aes(y = purchase, x = income + age)) +
geom_point(alpha = .15) +
geom_smooth(method = "lm") +
@@ -53,7 +53,7 @@ p1 <- car %>%
ylab("Probability of Purchase")
p2 <- car %>%
p2 <- car |>
ggplot(aes(y = purchase, x = income + age)) +
geom_point(alpha = .15) +
geom_smooth(method = "glm", method.args = list(family = "binomial")) +
@@ -66,9 +66,9 @@ ggplotly(p2)
```
```{r}
car <- car %>%
car <- car |>
mutate(old = ifelse(car$age > 3, 1, 0))
car <- car %>%
car <- car |>
mutate(rich = ifelse(car$income > 40, 1, 0))
model_old <- glm(purchase ~ age + income + rich + old, data = car, family = "binomial")
summary(model_old)
@@ -90,5 +90,5 @@ pima.te$pred <- as.factor(pima.te$pred)
pima.te$type <- as.factor(pima.te$type)
# Confusion matrix
confusionMatrix(data = pima.te$type, reference = pima.te$pred, positive = 'Yes')
confusionMatrix(data = pima.te$type, reference = pima.te$pred, positive = "Yes")
```