Exercice 09 Bonus : régression logistique pénélisée

Régression logistique pénélisée via le github de Laurent Rouvière
regression logistique
regression Ridge
regression Lasso
validation croisée
Author

Clément Poupelin

Published

Invalid Date

Modified

February 21, 2025

Dans cette partie et avant de passer à l’exercice 10, nous allons faire la section 3.3 sur la régression logistique pénélisée du tutoriel de Laurent Rouvière

Show the code
library(dplyr)

Attachement du package : 'dplyr'
Les objets suivants sont masqués depuis 'package:stats':

    filter, lag
Les objets suivants sont masqués depuis 'package:base':

    intersect, setdiff, setequal, union
Show the code
library(glmnet)
Le chargement a nécessité le package : Matrix
Loaded glmnet 4.1-8
Show the code
ad.data <- readr::read_delim(
  "~/Documents/1_Projet/Perso/Statistique_en_grande_dimension/data/internet+advertisements/ad.data",
  delim = ",",
  col_names = FALSE,
  na = c("?"),
  trim_ws = TRUE,
  col_types = readr::cols(X1559 = readr::col_factor())
) |>
  rename(Y = X1559)  
Show the code
summary(ad.data$Y)
   ad. nonad. 
   459   2820 
Show the code
sum(is.na(ad.data))
[1] 2729
Show the code
var.na <- apply(is.na(ad.data),2,any)
names(ad.data)[var.na]
[1] "X1" "X2" "X3" "X4"
Show the code
ind.na <- apply(is.na(ad.data),1,any)
sum(ind.na)
[1] 920
Show the code
ad.data1 <- ad.data[,var.na==FALSE]
dim(ad.data1)
[1] 3279 1555
Show the code
sum(is.na(ad.data1))
[1] 0
Show the code
X.ad <- model.matrix(Y~.,data=ad.data1)[,-1]
Y.ad <- ad.data1$Y
Show the code
set.seed(1234)
lasso.cv <- cv.glmnet(X.ad,Y.ad,family="binomial",alpha=1)
plot(lasso.cv)