Loading the dataset and converting variables to factor level vars.
dataSet <- read.csv(file.path(tempDirectory,"dataset1.csv"),
header = TRUE,
na.strings = c("#NULL!",""," "))
nameList <- c("State",
"banConsumptionPublicPlaces",
"licensingSystemForPlacesOfConsumption",
"regulationOnDaysAndHrsOfSale",
"businessHours",
"fixedMinimumSalePrice",
"regulationOnDensityOfOutlets",
"quotaTargetsGivenForRetailSellers",
"minLegalDrinkingAge",
"warningDisplayOrQualityLabelling",
"distributionSystem",
"pointOfSaleAdvertProhibition",
"percapitaAnnualIncome",
"healthIndex",
"percentageOfPopBelowPovertyLine",
"prevOfAlcoholCurrentUse",
"prevOfAlcoholDependence",
"RTAwithDUI")
names(dataSet)<- nameList
dataSet$banConsumptionPublicPlaces <- factor(dataSet$banConsumptionPublicPlaces,
levels = c("1","2"),
labels = c("yes","no"))
dataSet$licensingSystemForPlacesOfConsumption <- factor(dataSet$licensingSystemForPlacesOfConsumption,
levels = c("1","2"),
labels = c("yes","no"))
dataSet$regulationOnDaysAndHrsOfSale <- factor(dataSet$regulationOnDaysAndHrsOfSale,
levels = c("1","2"),
labels = c("yes","no"))
dataSet$regulationOnDensityOfOutlets <- factor(dataSet$regulationOnDensityOfOutlets,
levels = c("1","2"),
labels = c("yes","no"))
dataSet$fixedMinimumSalePrice <- factor(dataSet$fixedMinimumSalePrice,
levels = c("1","2"),
labels = c("yes","no"))
dataSet$quotaTargetsGivenForRetailSellers <- factor(dataSet$quotaTargetsGivenForRetailSellers,
levels = c("1","2"),
labels = c("yes","no"))
dataSet$warningDisplayOrQualityLabelling <- factor(dataSet$warningDisplayOrQualityLabelling,
levels = c("1","2"),
labels = c("yes","no"))
dataSet$pointOfSaleAdvertProhibition <- factor(dataSet$pointOfSaleAdvertProhibition,
levels = c("1","2"),
labels = c("yes","no"))
dataSet$distributionSystem <- factor(dataSet$distributionSystem,
levels = c("1","2","3","4"),
labels = c("Govt.Monopoly",
"Auction",
"Lottery",
"Licensing.System"))Here is the summary of what was missing:
sumna <- function(x){
return(sum(is.na(x)))
}
missingReport <- lapply(dataSet,sumna)
missingReport <- as.data.frame(missingReport)
missingReport <- t(missingReport)
kable(missingReport)| State | 0 |
| banConsumptionPublicPlaces | 0 |
| licensingSystemForPlacesOfConsumption | 0 |
| regulationOnDaysAndHrsOfSale | 0 |
| businessHours | 2 |
| fixedMinimumSalePrice | 1 |
| regulationOnDensityOfOutlets | 1 |
| quotaTargetsGivenForRetailSellers | 0 |
| minLegalDrinkingAge | 0 |
| warningDisplayOrQualityLabelling | 0 |
| distributionSystem | 0 |
| pointOfSaleAdvertProhibition | 4 |
| percapitaAnnualIncome | 2 |
| healthIndex | 0 |
| percentageOfPopBelowPovertyLine | 0 |
| prevOfAlcoholCurrentUse | 0 |
| prevOfAlcoholDependence | 0 |
| RTAwithDUI | 0 |
We have imputed these values to get a complete dataset
library(mice)
imputations <- mice(dataSet)##
## iter imp variable
## 1 1 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 1 2 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 1 3 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 1 4 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 1 5 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 2 1 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 2 2 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 2 3 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 2 4 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 2 5 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 3 1 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 3 2 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 3 3 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 3 4 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 3 5 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 4 1 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 4 2 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 4 3 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 4 4 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 4 5 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 5 1 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 5 2 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 5 3 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 5 4 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
## 5 5 businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets pointOfSaleAdvertProhibition percapitaAnnualIncome
dataSetComplete <- complete(imputations)Five point summary for various variables are as follows:
summary(dataSetComplete)## State banConsumptionPublicPlaces
## Length:31 yes:30
## Class :character no : 1
## Mode :character
##
##
##
## licensingSystemForPlacesOfConsumption regulationOnDaysAndHrsOfSale
## yes:31 yes:31
## no : 0 no : 0
##
##
##
##
## businessHours fixedMinimumSalePrice regulationOnDensityOfOutlets
## Min. : 0.00 yes:22 yes:20
## 1st Qu.: 9.50 no : 9 no :11
## Median :12.00
## Mean :11.35
## 3rd Qu.:13.50
## Max. :15.00
## quotaTargetsGivenForRetailSellers minLegalDrinkingAge
## yes:26 Min. :18.00
## no : 5 1st Qu.:21.00
## Median :21.00
## Mean :21.06
## 3rd Qu.:21.00
## Max. :25.00
## warningDisplayOrQualityLabelling distributionSystem
## yes:22 Govt.Monopoly :10
## no : 9 Auction : 4
## Lottery : 3
## Licensing.System:14
##
##
## pointOfSaleAdvertProhibition percapitaAnnualIncome healthIndex
## yes: 7 Min. : 28101 Min. :28.61
## no :24 1st Qu.: 63995 1st Qu.:43.30
## Median : 89217 Median :51.90
## Mean :109367 Mean :50.67
## 3rd Qu.:136064 3rd Qu.:59.70
## Max. :337734 Max. :74.01
## percentageOfPopBelowPovertyLine prevOfAlcoholCurrentUse
## Min. : 0.00 Min. : 0.90
## 1st Qu.: 8.75 1st Qu.: 8.85
## Median :11.30 Median :16.40
## Mean :16.71 Mean :15.83
## 3rd Qu.:25.60 3rd Qu.:21.45
## Max. :39.90 Max. :35.60
## prevOfAlcoholDependence RTAwithDUI
## Min. : 0.150 Min. : 1.0
## 1st Qu.: 1.000 1st Qu.: 20.0
## Median : 2.400 Median : 139.0
## Mean : 3.566 Mean : 378.7
## 3rd Qu.: 4.500 3rd Qu.: 355.0
## Max. :14.200 Max. :3595.0
As we can see there is no variance in licensingSystemForPlacesOfConsumption and regulationOnDaysAndHrsOfSale. These two variables will contribute nothing to clustering thus they must be excluded. We must note that Gujarat is not there in the dataset. We also exclude Bihar and J&K becuase one has prohibition and the other has very low alcohol consumption due to religous prohibition.
dataSetForAnalysis <- dataSetComplete %>%
select(-licensingSystemForPlacesOfConsumption,
-regulationOnDaysAndHrsOfSale) %>%
filter(!State %in% c("Bihar","Jammu and Kashm"))All variables that are on real scales for example percapita income and RTA counts must be scaled. Scaling is substracting mean from each value and dividing the result with SD. This is essential otherwise, these variables which have very high values will have undue inluence in deciding the clusters. We also change minimum legal age to a factor variable as it has only three unique vals.
dataSetForAnalysis$percapitaAnnualIncome <- as.numeric(scale(
dataSetForAnalysis$percapitaAnnualIncome))
dataSetForAnalysis$RTAwithDUI <- as.numeric(
scale(dataSetForAnalysis$RTAwithDUI)
)
dataSetForAnalysis$minLegalDrinkingAge <- factor(dataSetForAnalysis$minLegalDrinkingAge,ordered = TRUE)Since many variables are factors (nominal) we have to use Gower Distance and PAM as chosen methods for clustering. Read about it: https://www.nature.com/articles/s41598-021-83340-8 ### Deciding the number of clusters The number of clusters is decided using Silhoutte width. How many clusters give the minimum sil width?
gowerDist <- daisy(dataSetForAnalysis[,-1],"gower")
gowerMat <- as.matrix(gowerDist)
sil_width <- c(NA)
for(i in 2:8){
pam_fit <- pam(gowerMat, diss = TRUE, k = i)
sil_width[i] <- pam_fit$silinfo$avg.width
}
plot(1:8, sil_width,
xlab = "Number of clusters",
ylab = "Silhouette Width")
lines(1:8, sil_width)
### Making 3 Clusters You will have to read partioning around medioids
(PAM) clustering, to understand this output. By and large, it is not a
very tight cluster. But okay to make some discussion
pam_fit <- pam(gowerMat, diss = TRUE,3)
summary(pam_fit)## Medoids:
## ID
## [1,] "9" "9"
## [2,] "8" "8"
## [3,] "19" "19"
## Clustering vector:
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
## 1 2 1 3 3 2 2 2 1 1 3 2 3 2 3 2 1 1 3 1 3 3 3 3 2 3
## 27 28 29
## 2 3 3
## Objective function:
## build swap
## 0.1875206 0.1875206
##
## Numerical information per cluster:
## size max_diss av_diss diameter separation
## [1,] 7 0.2487170 0.1837766 0.4490381 0.1998061
## [2,] 9 0.3423574 0.2228159 0.6336260 0.1247666
## [3,] 13 0.2771515 0.1651014 0.4248762 0.1247666
##
## Isolated clusters:
## L-clusters: character(0)
## L*-clusters: character(0)
##
## Silhouette plot information:
## cluster neighbor sil_width
## 9 1 3 0.36558546
## 18 1 2 0.32173266
## 1 1 3 0.12651708
## 3 1 3 0.08360225
## 17 1 2 0.06961138
## 20 1 3 0.06687850
## 10 1 2 0.05887164
## 16 2 3 0.23955364
## 6 2 3 0.20254937
## 25 2 3 0.11142574
## 8 2 3 0.10789482
## 14 2 3 0.01103254
## 12 2 1 -0.01012017
## 7 2 3 -0.05497210
## 2 2 3 -0.11171946
## 27 2 3 -0.11387714
## 19 3 2 0.43577741
## 26 3 2 0.38669280
## 22 3 2 0.31686935
## 5 3 2 0.29305491
## 15 3 2 0.25933482
## 21 3 1 0.25913293
## 4 3 2 0.25804097
## 28 3 2 0.23838620
## 13 3 1 0.22794094
## 29 3 2 0.19853783
## 11 3 2 0.16272891
## 24 3 1 0.15587402
## 23 3 2 0.03958781
## Average silhouette width per cluster:
## [1] 0.15611414 0.04241858 0.24861222
## Average silhouette width of total data set:
## [1] 0.162294
##
## Available components:
## [1] "medoids" "id.med" "clustering" "objective" "isolation"
## [6] "clusinfo" "silinfo" "diss" "call"
plot(pam_fit)
### Examining the clusters
tsne_obj <- Rtsne(gowerMat, is_distance = TRUE,perplexity = 1)
tsne_data <- tsne_obj$Y %>%
data.frame() %>%
setNames(c("X", "Y")) %>%
mutate(cluster = factor(pam_fit$clustering))
ggplot(aes(x = X, y = Y), data = tsne_data) +
geom_point(aes(color = cluster))
The second cluster is the most homogenous. Full comparison of clusters
is as follows. Medioids are states that typify a cluster. Now we have to
see the pattern and make a discussion around it.
dataSetForAnalysis$cluster <- pam_fit$clustering
giveProp <- function(x){
y <- mean(x=="yes",na.rm=TRUE)
y <- ceiling(y*100)
return(y)
}
tab1 <- dataSetForAnalysis %>%
group_by(cluster)%>%
summarise(across(c(2,4,5,6,8,10),giveProp))
tab2 <- dataSetForAnalysis %>%
group_by(cluster)%>%
summarise(across(where(is.numeric),mean))
tableSummary <- left_join(tab1,tab2)
tab3 <- dataSetForAnalysis %>%
group_by(cluster)%>%
summarise("NumOfStates"= n(),
"States"= paste0(State,collapse = ", "))
tableSummary <- left_join(tableSummary,tab3)
tableSummary$medioids <- dataSetForAnalysis$State[pam_fit$id.med]
tableSummary <- t(tableSummary)
library(kableExtra)
kable(tableSummary,digits = 2)| cluster | 1 | 2 | 3 |
| banConsumptionPublicPlaces | 100 | 89 | 100 |
| fixedMinimumSalePrice | 15 | 89 | 85 |
| regulationOnDensityOfOutlets | 43 | 34 | 100 |
| quotaTargetsGivenForRetailSellers | 72 | 67 | 100 |
| warningDisplayOrQualityLabelling | 15 | 89 | 85 |
| pointOfSaleAdvertProhibition | 15 | 34 | 16 |
| businessHours | 11.28571 | 11.38889 | 12.11538 |
| percapitaAnnualIncome | -0.39953899 | -0.05431532 | 0.25273929 |
| healthIndex | 56.53000 | 53.00000 | 46.42846 |
| percentageOfPopBelowPovertyLine | 19.00000 | 22.70000 | 13.10923 |
| prevOfAlcoholCurrentUse | 13.75714 | 16.08889 | 18.86923 |
| prevOfAlcoholDependence | 2.685714 | 4.177778 | 4.130769 |
| RTAwithDUI | -0.30045972 | 0.31021858 | -0.05298071 |
| NumOfStates | 7 | 9 | 13 |
| States | Kerala, Maharashtra, West bengal, Meghalaya, Delhi, Assam, Arunachal Prade | Andra pradesh, Karnataka, Tamil nadu, Telangana, Chattisgarh, Chandigarh, Jharkand, Madhya Pradesh, Odisha | Punjab, Himachalpradesh, Haryana, Sikkim, Goa, Puducherry, Andaman and Nic, Tripura, Rajasthan, Uttarakhand, Daman and Diu, Dadra and Nagar, Uttar Pradesh |
| medioids | West bengal | Telangana | Puducherry |
dataSetForAnalysis %>% group_by(cluster,distributionSystem)%>% summarise("N"= n())