clear
close all
clc

%% Wine quality data

% First download the dataset from UCI Maching Learning Respository
% and save it in the MATLAB working directory

type('winequality.names')

RedWines = readmatrix('winequality-red.csv');
WhiteWines = readmatrix('winequality-white.csv');

AllWines = [RedWines; WhiteWines];

X = AllWines(:, 1:end-1); % matrix of 11 attributes
y = AllWines(:, end); % wine quality

numRedWines = size(RedWines,1);
numWhiteWines = size(WhiteWines,1);
numWines = numRedWines + numWhiteWines;

baseWineTypes = {'Red wine', 'White wine'};

WineType = categorical([ones(1,numRedWines), repmat(2,[1 numWhiteWines])], ...
                       [1 2], baseWineTypes);
%WineType = categorical([repmat(baseWineTypes(1), 1, numRedWines), ...
%                        repmat(baseWineTypes(2), 1, numWhiteWines)]);

save wine-quality X y WineType

%% visualization of the wine quality data 

figure; 
b = bar([numRedWines, numWhiteWines], 0.6);
xticklabels(baseWineTypes)
set(gca, 'fontsize', 16)
xlabel('Wine type', 'fontsize', 18)
ylabel('counts', 'fontsize', 18)
grid on
title('Number of wines per type', 'fontsize', 20)
% put numbers on top of each bar
xtips = b.XEndPoints;
ytips = b.YEndPoints;
labels = string(b.YData);
text(xtips,ytips,labels,'Color','red','fontsize',14, ...
    'HorizontalAlignment','center',...
    'VerticalAlignment','bottom')
ylim([0, 5500])

% discretize the output variable y
tabulate(y)

WineQuality = discretize(y, [0 5.5 7.5 10], ...
    'categorical', {'Low quality', 'Medium quality', 'High quality'});

tabulate(WineQuality)

countcats(WineQuality)

% make a quick histogram plot to see the distribution of y
figure; 
subplot(1,2,1)
histogram(y,'BinMethod', 'integers') 
set(gca, 'fontsize',14)
subplot(1,2,2); 
histogram(WineQuality) % or bar(countcats(WineQuality))
set(gca, 'fontsize',14)

% 11th attribute of the data
alcohol = X(:, end);

figure; 
histogram(alcohol)
histogram(alcohol,10)
set(gca, 'fontsize',16)
xlabel('alcohol value')
ylabel('count')
grid on

figure; 
%
subplot(1,2,1); 
boxplot(alcohol,WineType); 
set(gca, 'fontsize', 16); 
ylabel('alcohol')
%
subplot(1,2,2); 
boxplot(alcohol,WineQuality);
set(gca, 'fontsize', 16); 
ylabel('alcohol')

%% dot plots
figure; 
%
subplot(1,2,1); 
wineTypeIndex = ones(1, numWines); 
wineTypeIndex(WineType=='White wine') = 2;
gscatter(wineTypeIndex,alcohol,WineType,'rc', '.o', 12); 
set(gca, 'fontsize', 16); 
xticks(1:2)
xticklabels(baseWineTypes)
xlabel('Wine type', 'fontsize', 18)
ylabel('alcohol', 'fontsize', 18)
%
subplot(1,2,2); 
gscatter(y,alcohol,WineQuality,[],[],12); 
set(gca, 'fontsize', 16); 
xlabel('Wine quality (0 to 10 scale)')
ylabel('alcohol')

% two categorical variables
figure; 
gscatter(y, alcohol, {WineQuality, WineType},[],[],12)
set(gca, 'fontsize', 16)
xlabel('Wine quality (0 to 10 scale)', 'fontsize', 18)
ylabel('alcohol', 'fontsize', 18)
grid on

%% grouped / stacked bar plot
freqTable = crosstab(WineType, WineQuality);

figure; 
bar(freqTable); 
xticklabels(baseWineTypes)
b = bar(categorical(baseWineTypes), freqTable); % grouped bar plot
%b = bar(categorical(baseWineTypes), freqTable, 0.6, 'stacked');
legend(b, categories(WineQuality), 'fontsize', 18);
set(gca, 'fontsize', 16)
xlabel('Wine Type', 'fontsize', 18)
ylabel('counts')
grid on
title('Bar plot for frequency table', 'fontsize', 20)
