Description
Régression logistique
Régression logistique Lasso
Sélection automatique
Validation croisée
Author

Clément Poupelin

Published

February 25, 2025

Show the code
####################################
### Ex 10 : Caravan
library(ISLR)
data("Caravan")
dim(Caravan)
[1] 5822   86
Show the code
dim(na.omit(Caravan))
[1] 5822   86
Show the code
indexyes=which(Caravan$Purchase=="Yes")
indexno=which(Caravan$Purchase=="No")
train=c(sample(indexyes,length(indexyes)/2),sample(indexno,length(indexno)/2))

y=Caravan$Purchase[train]
Xtrain=as.matrix(Caravan[train,-86])
ytest=Caravan$Purchase[-train]
Xtest=Caravan[-train,-86]

#Forward
mod=glm(Purchase~.,family="binomial",data=Caravan,subset=train)
Warning: glm.fit: des probabilités ont été ajustées numériquement à 0 ou 1
Show the code
mod0=glm(Purchase~1,family="binomial",data=Caravan,subset=train)
tmp=step(mod0,scope=formula(mod),direction="both",k=log(n/2),trace=0)#par BIC
Error: objet 'n' introuvable
Show the code
tmp$formula
Error: objet 'tmp' introuvable
Show the code
modforw=eval(tmp$call)
Error: objet 'tmp' introuvable
Show the code
pred=predict(modforw,Xtest,type="response")
Error: objet 'modforw' introuvable
Show the code
library(ROCR)
pr = prediction(pred, ytest) 
Error: objet 'pred' introuvable
Show the code
roc = performance(pr, measure = "tpr", x.measure = "fpr") 
Error: objet 'pr' introuvable
Show the code
plot(roc)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'roc' introuvable
Show the code
aucfwd=performance(pr,measure="auc")@y.values
Error: objet 'pr' introuvable
Show the code
#Lasso
cvglmnet=cv.glmnet(Xtrain,y,family="binomial",type.measure="auc",nfolds=10)
Error in cv.glmnet(Xtrain, y, family = "binomial", type.measure = "auc", : impossible de trouver la fonction "cv.glmnet"
Show the code
plot(cvglmnet)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'cvglmnet' introuvable
Show the code
predlasso=predict(cvglmnet,as.matrix(Xtest),s=cvglmnet$lambda.min,type="response")
Error: objet 'cvglmnet' introuvable
Show the code
prlasso = prediction(predlasso, ytest) 
Error: objet 'predlasso' introuvable
Show the code
roclasso = performance(prlasso, measure = "tpr", x.measure = "fpr") 
Error: objet 'prlasso' introuvable
Show the code
auclasso=performance(prlasso,measure="auc")@y.values
Error: objet 'prlasso' introuvable
Show the code
#Comparaison : Lasso semble un peu meilleur, mais sélectionne plus de variables
#(il faudrait recommencer avec plusieurs découpages train/test)
plot(roc)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'roc' introuvable
Show the code
plot(roclasso,add=T,col=2)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'roclasso' introuvable
Show the code
aucfwd
Error: objet 'aucfwd' introuvable
Show the code
auclasso
Error: objet 'auclasso' introuvable
Show the code
coef(modforw)
Error: objet 'modforw' introuvable
Show the code
coef(cvglmnet,s=cvglmnet$lambda.min)
Error: objet 'cvglmnet' introuvable
Show the code
#D'autres méthodes possibles :

#Gauss Lasso
w=coef(cvglmnet,s=cvglmnet$lambda.min)
Error: objet 'cvglmnet' introuvable
Show the code
index=which(w[-1]!=0)
Error: objet 'w' introuvable
Show the code
fit=glm(Purchase~.,data=Caravan[train,c(index,86)],family="binomial")
Error in eval(mf, parent.frame()): objet 'index' introuvable
Show the code
predgauss=predict(fit,Xtest,type="response")
Error: objet 'fit' introuvable
Show the code
#Adaptive Lasso
cvglm=cv.glmnet(Xtrain,y,family="binomial",type.measure="auc",penalty.factor=1/abs(w[-1]))
Error in cv.glmnet(Xtrain, y, family = "binomial", type.measure = "auc", : impossible de trouver la fonction "cv.glmnet"
Show the code
plot(cvglm)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'cvglm' introuvable
Show the code
predalasso=predict(cvglm,as.matrix(Xtest),s=cvglm$lambda.min,type="response")
Error: objet 'cvglm' introuvable
Show the code
#+Gauss pour finir
wal=coef(cvglm,s=cvglm$lambda.min)
Error: objet 'cvglm' introuvable
Show the code
index=which(wal[-1]!=0)
Error: objet 'wal' introuvable
Show the code
fit=glm(Purchase~.,data=Caravan[train,c(index,86)],family="binomial")
Error in eval(mf, parent.frame()): objet 'index' introuvable
Show the code
predalgauss=predict(fit,Xtest,type="response")
Error: objet 'fit' introuvable
Show the code
#Logistic Ridge
cvglmnet=cv.glmnet(Xtrain,y,family="binomial",type.measure="auc",alpha=0)
Error in cv.glmnet(Xtrain, y, family = "binomial", type.measure = "auc", : impossible de trouver la fonction "cv.glmnet"
Show the code
plot(cvglmnet)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'cvglmnet' introuvable
Show the code
predridge=predict(cvglmnet,as.matrix(Xtest),s=cvglmnet$lambda.min,type="response")
Error: objet 'cvglmnet' introuvable
Show the code
#Logistic Elastic Net (pour alpha=0.5)
cvglmnet=cv.glmnet(Xtrain,y,family="binomial",type.measure="auc",alpha=1/2)
Error in cv.glmnet(Xtrain, y, family = "binomial", type.measure = "auc", : impossible de trouver la fonction "cv.glmnet"
Show the code
plot(cvglmnet)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'cvglmnet' introuvable
Show the code
preden=predict(cvglmnet,as.matrix(Xtest),s=cvglmnet$lambda.min,type="response")
Error: objet 'cvglmnet' introuvable
Show the code
#Comparaison finale : les méthodes supplémentaires n'apportent rien d'intéressant 
#(il faudrait recommencer avec plusieurs découpages train/test)
library(ROCR)
pr = prediction(pred, ytest) 
Error: objet 'pred' introuvable
Show the code
roc = performance(pr, measure = "tpr", x.measure = "fpr") 
Error: objet 'pr' introuvable
Show the code
prlasso = prediction(predlasso, ytest) 
Error: objet 'predlasso' introuvable
Show the code
roclasso = performance(prlasso, measure = "tpr", x.measure = "fpr") 
Error: objet 'prlasso' introuvable
Show the code
prgauss = prediction(predgauss, ytest) 
Error: objet 'predgauss' introuvable
Show the code
rocgauss = performance(prgauss, measure = "tpr", x.measure = "fpr")
Error: objet 'prgauss' introuvable
Show the code
pralasso = prediction(predalasso, ytest) 
Error: objet 'predalasso' introuvable
Show the code
rocalasso = performance(pralasso, measure = "tpr", x.measure = "fpr")
Error: objet 'pralasso' introuvable
Show the code
pralgauss = prediction(predalgauss, ytest) 
Error: objet 'predalgauss' introuvable
Show the code
rocalgauss = performance(pralgauss, measure = "tpr", x.measure = "fpr")
Error: objet 'pralgauss' introuvable
Show the code
prridge = prediction(predridge, ytest) 
Error: objet 'predridge' introuvable
Show the code
rocridge = performance(prridge, measure = "tpr", x.measure = "fpr") 
Error: objet 'prridge' introuvable
Show the code
pren = prediction(preden, ytest) 
Error: objet 'preden' introuvable
Show the code
rocen = performance(pren, measure = "tpr", x.measure = "fpr") 
Error: objet 'pren' introuvable
Show the code
plot(roc)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'roc' introuvable
Show the code
plot(roclasso,add=T,col=2)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'roclasso' introuvable
Show the code
plot(rocgauss,add=T,col=3)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'rocgauss' introuvable
Show the code
plot(rocalasso,add=T,col=4)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'rocalasso' introuvable
Show the code
plot(rocalgauss,add=T,col=5)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'rocalgauss' introuvable
Show the code
plot(rocridge,add=T,col=6)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'rocridge' introuvable
Show the code
plot(rocen,add=T,col=7)
Error in h(simpleError(msg, call)): erreur d'�valuation de l'argument 'x' lors de la s�lection d'une m�thode pour la fonction 'plot' : objet 'rocen' introuvable
Show the code
performance(pr,measure="auc")@y.values
Error: objet 'pr' introuvable
Show the code
performance(prlasso,measure="auc")@y.values
Error: objet 'prlasso' introuvable
Show the code
performance(prgauss,measure="auc")@y.values
Error: objet 'prgauss' introuvable
Show the code
performance(pralasso,measure="auc")@y.values
Error: objet 'pralasso' introuvable
Show the code
performance(pralgauss,measure="auc")@y.values
Error: objet 'pralgauss' introuvable
Show the code
performance(prridge,measure="auc")@y.values
Error: objet 'prridge' introuvable
Show the code
performance(pren,measure="auc")@y.values
Error: objet 'pren' introuvable