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
=which(Caravan$Purchase=="Yes")
indexyes=which(Caravan$Purchase=="No")
indexno=c(sample(indexyes,length(indexyes)/2),sample(indexno,length(indexno)/2))
train
=Caravan$Purchase[train]
y=as.matrix(Caravan[train,-86])
Xtrain=Caravan$Purchase[-train]
ytest=Caravan[-train,-86]
Xtest
#Forward
=glm(Purchase~.,family="binomial",data=Caravan,subset=train) mod
Warning: glm.fit: des probabilités ont été ajustées numériquement à 0 ou 1
Show the code
=glm(Purchase~1,family="binomial",data=Caravan,subset=train)
mod0=step(mod0,scope=formula(mod),direction="both",k=log(n/2),trace=0)#par BIC tmp
Error: objet 'n' introuvable
Show the code
$formula tmp
Error: objet 'tmp' introuvable
Show the code
=eval(tmp$call) modforw
Error: objet 'tmp' introuvable
Show the code
=predict(modforw,Xtest,type="response") pred
Error: objet 'modforw' introuvable
Show the code
library(ROCR)
= prediction(pred, ytest) pr
Error: objet 'pred' introuvable
Show the code
= performance(pr, measure = "tpr", x.measure = "fpr") roc
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
=performance(pr,measure="auc")@y.values aucfwd
Error: objet 'pr' introuvable
Show the code
#Lasso
=cv.glmnet(Xtrain,y,family="binomial",type.measure="auc",nfolds=10) cvglmnet
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
=predict(cvglmnet,as.matrix(Xtest),s=cvglmnet$lambda.min,type="response") predlasso
Error: objet 'cvglmnet' introuvable
Show the code
= prediction(predlasso, ytest) prlasso
Error: objet 'predlasso' introuvable
Show the code
= performance(prlasso, measure = "tpr", x.measure = "fpr") roclasso
Error: objet 'prlasso' introuvable
Show the code
=performance(prlasso,measure="auc")@y.values auclasso
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
=coef(cvglmnet,s=cvglmnet$lambda.min) w
Error: objet 'cvglmnet' introuvable
Show the code
=which(w[-1]!=0) index
Error: objet 'w' introuvable
Show the code
=glm(Purchase~.,data=Caravan[train,c(index,86)],family="binomial") fit
Error in eval(mf, parent.frame()): objet 'index' introuvable
Show the code
=predict(fit,Xtest,type="response") predgauss
Error: objet 'fit' introuvable
Show the code
#Adaptive Lasso
=cv.glmnet(Xtrain,y,family="binomial",type.measure="auc",penalty.factor=1/abs(w[-1])) cvglm
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
=predict(cvglm,as.matrix(Xtest),s=cvglm$lambda.min,type="response") predalasso
Error: objet 'cvglm' introuvable
Show the code
#+Gauss pour finir
=coef(cvglm,s=cvglm$lambda.min) wal
Error: objet 'cvglm' introuvable
Show the code
=which(wal[-1]!=0) index
Error: objet 'wal' introuvable
Show the code
=glm(Purchase~.,data=Caravan[train,c(index,86)],family="binomial") fit
Error in eval(mf, parent.frame()): objet 'index' introuvable
Show the code
=predict(fit,Xtest,type="response") predalgauss
Error: objet 'fit' introuvable
Show the code
#Logistic Ridge
=cv.glmnet(Xtrain,y,family="binomial",type.measure="auc",alpha=0) cvglmnet
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
=predict(cvglmnet,as.matrix(Xtest),s=cvglmnet$lambda.min,type="response") predridge
Error: objet 'cvglmnet' introuvable
Show the code
#Logistic Elastic Net (pour alpha=0.5)
=cv.glmnet(Xtrain,y,family="binomial",type.measure="auc",alpha=1/2) cvglmnet
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
=predict(cvglmnet,as.matrix(Xtest),s=cvglmnet$lambda.min,type="response") preden
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)
= prediction(pred, ytest) pr
Error: objet 'pred' introuvable
Show the code
= performance(pr, measure = "tpr", x.measure = "fpr") roc
Error: objet 'pr' introuvable
Show the code
= prediction(predlasso, ytest) prlasso
Error: objet 'predlasso' introuvable
Show the code
= performance(prlasso, measure = "tpr", x.measure = "fpr") roclasso
Error: objet 'prlasso' introuvable
Show the code
= prediction(predgauss, ytest) prgauss
Error: objet 'predgauss' introuvable
Show the code
= performance(prgauss, measure = "tpr", x.measure = "fpr") rocgauss
Error: objet 'prgauss' introuvable
Show the code
= prediction(predalasso, ytest) pralasso
Error: objet 'predalasso' introuvable
Show the code
= performance(pralasso, measure = "tpr", x.measure = "fpr") rocalasso
Error: objet 'pralasso' introuvable
Show the code
= prediction(predalgauss, ytest) pralgauss
Error: objet 'predalgauss' introuvable
Show the code
= performance(pralgauss, measure = "tpr", x.measure = "fpr") rocalgauss
Error: objet 'pralgauss' introuvable
Show the code
= prediction(predridge, ytest) prridge
Error: objet 'predridge' introuvable
Show the code
= performance(prridge, measure = "tpr", x.measure = "fpr") rocridge
Error: objet 'prridge' introuvable
Show the code
= prediction(preden, ytest) pren
Error: objet 'preden' introuvable
Show the code
= performance(pren, measure = "tpr", x.measure = "fpr") rocen
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