##Uncomment it the "glmnet" package is never previously installed
##install.packages("glmnet");
library(glmnet);
tmp<-read.csv("winequality.csv", header=TRUE, sep=";");
n<-nrow(tmp);
K<-ncol(tmp)-1;
x<-matrix(0,n,K);
for(i in 1:K) {x[,i]<-tmp[,i];}
y<-tmp[,K+1];
##To split the data into training and test set
idx<-sample(1:n,floor(n/2),replace=FALSE);
xtr<-x[idx,]; ytr<-y[idx];
xts<-x[-idx,]; yts<-y[-idx];
##Fit the multiple logistic regression model
myglm4<-glmnet(xtr,ytr,family="multinomial");
##Apply the trained model to the test set
mypred4<-predict(myglm4,newx=xts,type="response",s=0.01);
posteriprob<-mypred4[,,1];
yhat<-matrix(1,nrow(xts),1);
for(i in 1:nrow(xts))
{
yhat[i]<-which.max(posteriprob[i,]);
}
acc<-sum(yhat+2==yts)/nrow(xts);
cat("Accuracy on the test set is", acc, "\n");