** Problem Statement **

This is a retail project where our challenge is to predict whether a retail store should get opened or not based on certain factors such as sales, population,area etc. We have been given two datasets store_train.csv and store_test.csv .We need to use data store_train to build predictive model for response variable ‘store’. store_test data contains all other factors except ‘store’, we need to predict that using the model that we will develop. We will be submitting our predicted values in terms of probability scores. This is a typical classification problem & we will use random forest for model building.

Let’s start Setting our working directory first.

setwd("D:/Edvancer/R Tutorials/R Projects Codes/P2-Retail")

Next we need to import both train & test data sets.

s_train=read.csv("store_train.csv",stringsAsFactors = F)
s_test=read.csv("store_test.csv",stringsAsFactors = F)

Let us load data wrangling library dplyr so as to glimpse our data.

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
glimpse(s_train)
## Observations: 3,338
## Variables: 17
## $ Id             <dbl> 2300919770, 5000129575, 2501308470, 603599999, ...
## $ sales0         <int> 848, 925, 924, 924, 1017, 1494, 691, 918, 931, ...
## $ sales1         <int> 588, 717, 616, 646, 730, 1071, 476, 663, 628, 4...
## $ sales2         <int> 666, 780, 739, 683, 735, 1196, 541, 774, 775, 4...
## $ sales3         <int> 1116, 1283, 1154, 1292, 1208, 1861, 861, 1189, ...
## $ sales4         <int> 1133, 1550, 1314, 1297, 1326, 2023, 923, 1477, ...
## $ country        <int> 9, 1, 13, 35, 27, 9, 103, 183, 89, 57, 3, 109, ...
## $ State          <int> 23, 50, 25, 6, 50, 25, 26, 37, 12, 5, 53, 28, 3...
## $ CouSub         <int> 19770, 29575, 8470, 99999, 60100, 37995, 99999,...
## $ countyname     <chr> "Hancock County", "Addison County", "Hampden Co...
## $ storecode      <chr> "NCNTY23009N23009", "NCNTY50001N50001", "METRO4...
## $ Areaname       <chr> "Hancock County, ME", "Addison County, VT", "Sp...
## $ countytownname <chr> "Eastbrook town", "Granville town", "Brimfield ...
## $ population     <int> 423, 298, 3609, 34895, 1139, 5136, 67077, 90099...
## $ state_alpha    <chr> "ME", "VT", "MA", "CA", "VT", "MA", "MI", "NC",...
## $ store_Type     <chr> "Supermarket Type1", "Supermarket Type1", "Supe...
## $ store          <int> 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1,...
glimpse(s_test)
## Observations: 1,431
## Variables: 16
## $ Id             <dbl> 101799999, 101999999, 102199999, 103599999, 103...
## $ sales0         <int> 696, 599, 599, 599, 599, 599, 788, 599, 757, 59...
## $ sales1         <int> 511, 481, 423, 459, 481, 460, 628, 484, 450, 43...
## $ sales2         <int> 514, 500, 475, 462, 505, 463, 663, 505, 572, 46...
## $ sales3         <int> 867, 883, 802, 883, 746, 866, 1084, 746, 950, 8...
## $ sales4         <int> 1034, 894, 1061, 886, 801, 961, 1288, 870, 1079...
## $ country        <int> 17, 19, 21, 35, 37, 39, 51, 53, 65, 67, 73, 83,...
## $ State          <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ CouSub         <int> 99999, 99999, 99999, 99999, 99999, 99999, 99999...
## $ countyname     <chr> "Chambers County", "Cherokee County", "Chilton ...
## $ storecode      <chr> "NCNTY01017N01017", "NCNTY01019N01019", "METRO1...
## $ Areaname       <chr> "Chambers County, AL", "Cherokee County, AL", "...
## $ countytownname <chr> "Chambers County", "Cherokee County", "Chilton ...
## $ population     <int> 34215, 25989, 43643, 13228, 11539, 37765, 79303...
## $ state_alpha    <chr> "AL", "AL", "AL", "AL", "AL", "AL", "AL", "AL",...
## $ store_Type     <chr> "Supermarket Type1", "Supermarket Type1", "Supe...

** Understanding our Data **

Each row represnts characteristic of a single planned store.We can see from above that many categorical data has been coded to mask the data.Here is the interpretation for the columns Id : store id numeric sale figures for 5 types : sales0 sales1 sales2 sales3 sales4

country : categorical :: coded values for country

State : categorical :: coded values for State

CouSub : numeric ::subscription values at county level

countyname : Categorical ::county names

storecode : categorical :: store codes

Areaname : categorical :: name of the area , many times it matches with county name

countytownname : categorical :: county town name

population : numeric :: population of the store area

state_alpha : categorical :: short codes for state

store_Type : categorical :: type of store

store : categorical 1/0 : target indicator var 1=opened 0=not opened

** Data Preparation **

We’ll combine our two datasets so that we do not need to prepare data separately for them. And we’ll also avoid problem of dealing with different columns in different datasets. However before combining them, we’ll need to add response column to test because number of columns need to be same for two datasets to stack vertically.We are also going to add an identifier column ‘data’ which will recognize whether it is from train or test.

s_test$store=NA
s_train$data="train"
s_test$data="test"
s=rbind(s_train,s_test)

Let us glimpse our combined data sets s using glimpse & str function.

glimpse(s)
## Observations: 4,769
## Variables: 18
## $ Id             <dbl> 2300919770, 5000129575, 2501308470, 603599999, ...
## $ sales0         <int> 848, 925, 924, 924, 1017, 1494, 691, 918, 931, ...
## $ sales1         <int> 588, 717, 616, 646, 730, 1071, 476, 663, 628, 4...
## $ sales2         <int> 666, 780, 739, 683, 735, 1196, 541, 774, 775, 4...
## $ sales3         <int> 1116, 1283, 1154, 1292, 1208, 1861, 861, 1189, ...
## $ sales4         <int> 1133, 1550, 1314, 1297, 1326, 2023, 923, 1477, ...
## $ country        <int> 9, 1, 13, 35, 27, 9, 103, 183, 89, 57, 3, 109, ...
## $ State          <int> 23, 50, 25, 6, 50, 25, 26, 37, 12, 5, 53, 28, 3...
## $ CouSub         <int> 19770, 29575, 8470, 99999, 60100, 37995, 99999,...
## $ countyname     <chr> "Hancock County", "Addison County", "Hampden Co...
## $ storecode      <chr> "NCNTY23009N23009", "NCNTY50001N50001", "METRO4...
## $ Areaname       <chr> "Hancock County, ME", "Addison County, VT", "Sp...
## $ countytownname <chr> "Eastbrook town", "Granville town", "Brimfield ...
## $ population     <int> 423, 298, 3609, 34895, 1139, 5136, 67077, 90099...
## $ state_alpha    <chr> "ME", "VT", "MA", "CA", "VT", "MA", "MI", "NC",...
## $ store_Type     <chr> "Supermarket Type1", "Supermarket Type1", "Supe...
## $ store          <int> 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1,...
## $ data           <chr> "train", "train", "train", "train", "train", "t...
str(s)
## 'data.frame':    4769 obs. of  18 variables:
##  $ Id            : num  2.30e+09 5.00e+09 2.50e+09 6.04e+08 5.00e+09 ...
##  $ sales0        : int  848 925 924 924 1017 1494 691 918 931 584 ...
##  $ sales1        : int  588 717 616 646 730 1071 476 663 628 455 ...
##  $ sales2        : int  666 780 739 683 735 1196 541 774 775 477 ...
##  $ sales3        : int  1116 1283 1154 1292 1208 1861 861 1189 1228 727 ...
##  $ sales4        : int  1133 1550 1314 1297 1326 2023 923 1477 1502 949 ...
##  $ country       : int  9 1 13 35 27 9 103 183 89 57 ...
##  $ State         : int  23 50 25 6 50 25 26 37 12 5 ...
##  $ CouSub        : int  19770 29575 8470 99999 60100 37995 99999 99999 99999 99999 ...
##  $ countyname    : chr  "Hancock County" "Addison County" "Hampden County" "Lassen County" ...
##  $ storecode     : chr  "NCNTY23009N23009" "NCNTY50001N50001" "METRO44140M44140" "NCNTY06035N06035" ...
##  $ Areaname      : chr  "Hancock County, ME" "Addison County, VT" "Springfield, MA HUD Metro FMR Area" "Lassen County, CA" ...
##  $ countytownname: chr  "Eastbrook town" "Granville town" "Brimfield town" "Lassen County" ...
##  $ population    : int  423 298 3609 34895 1139 5136 67077 900993 73314 22609 ...
##  $ state_alpha   : chr  "ME" "VT" "MA" "CA" ...
##  $ store_Type    : chr  "Supermarket Type1" "Supermarket Type1" "Supermarket Type1" "Supermarket Type3" ...
##  $ store         : int  0 0 1 0 0 0 0 1 1 0 ...
##  $ data          : chr  "train" "train" "train" "train" ...

Many categorical data like ‘country’ & ‘State’ has already been coded to mask the data. We can see the same using frequency table as shown below.

table(s$country)
## 
##   1   3   5   6   7   9  10  11  12  13  14  15  16  17  19  20  21  23 
## 133 219 163   1 168 213   2 165   1 131   1 145   1 165 144   2 121 100 
##  25  27  28  29  30  31  33  35  36  37  39  41  43  45  47  49  50  51 
## 104 152   2  90   1  70  41  40   1  40  39  39  39  39  38  37   1  37 
##  53  54  55  56  57  59  60  61  63  65  67  68  69  70  71  73  75  77 
##  37   1  36   1  37  36   1  36  35  35  35   1  35   1  35  34  34  34 
##  78  79  81  83  85  86  87  89  90  91  93  95  97  99 100 101 103 105 
##   1  33  33  33  33   1  33  32   1  32  31  31  31  31   1  31  31  32 
## 107 109 110 111 113 115 117 119 121 122 123 125 127 129 130 131 133 135 
##  30  30   1  29  28  28  27  27  27   1  26  26  25  23   1  23  23  21 
## 137 139 141 143 145 147 149 150 151 153 155 157 159 161 163 164 165 167 
##  21  20  20  19  19  19  19   1  17  18  16  16  16  16  16   1  15  14 
## 169 170 171 173 175 177 179 180 181 183 185 186 187 188 189 191 193 195 
##  14   1  14  14  13  12  12   1  12  12  12   1  10   1   9   8   8  10 
## 197 198 199 201 203 205 207 209 211 213 215 217 219 220 221 223 225 227 
##   9   1   8   6   5   5   5   5   4   4   4   4   4   1   4   4   4   4 
## 229 230 231 233 235 237 239 240 241 243 245 247 249 251 253 255 257 259 
##   4   1   3   3   3   3   3   1   2   2   2   2   2   2   2   2   2   2 
## 261 263 265 267 269 270 271 273 275 277 279 281 282 283 285 287 289 290 
##   3   2   2   2   2   1   2   2   3   2   2   2   1   2   2   2   2   1 
## 291 293 295 297 299 301 303 305 307 309 311 313 315 317 319 321 323 325 
##   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   1   1 
## 327 329 331 333 335 337 339 341 343 345 347 349 351 353 355 357 359 361 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 363 365 367 369 371 373 375 377 379 381 383 385 387 389 391 393 395 397 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 399 401 403 405 407 409 411 413 415 417 419 421 423 425 427 429 431 433 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 435 437 439 441 443 445 447 449 451 453 455 457 459 461 463 465 467 469 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 471 473 475 477 479 481 483 485 487 489 491 493 495 497 499 501 503 505 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 507 510 515 520 530 540 550 560 570 580 590 595 600 610 620 630 640 650 
##   1   4   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 660 670 678 680 683 685 690 700 710 720 730 735 740 750 760 770 775 790 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
## 800 810 820 830 840 
##   1   1   1   1   1
table(s$State)
## 
##   1   2   4   5   6   8   9  10  11  12  13  15  16  17  18  19  20  21 
##  67  29  15  75  58  64 169   3   1  67 159   5  44 102  92  99 105 120 
##  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39 
##  64 535  25 351  83  87  82 116  56  93  17 259  21  33  62 100  53  88 
##  40  41  42  44  45  46  47  48  49  50  51  53  54  55  56  66  72  78 
##  77  36  67  39  46  66  95 254  29 255 135  39  55  72  23   1  78   3

Since we will be using random forest we need to convert data type of response (which is store in this case) to factor type using function as.factor. This is how randomforest differentiates from regression & classification.If we need to build a regression model then response variable should be kept numeric else factor for classification.

s$store=as.factor(s$store)

Let us see if store column has been changed to factor type or not using glimpse function again from dplyr package.

glimpse(s)
## Observations: 4,769
## Variables: 18
## $ Id             <dbl> 2300919770, 5000129575, 2501308470, 603599999, ...
## $ sales0         <int> 848, 925, 924, 924, 1017, 1494, 691, 918, 931, ...
## $ sales1         <int> 588, 717, 616, 646, 730, 1071, 476, 663, 628, 4...
## $ sales2         <int> 666, 780, 739, 683, 735, 1196, 541, 774, 775, 4...
## $ sales3         <int> 1116, 1283, 1154, 1292, 1208, 1861, 861, 1189, ...
## $ sales4         <int> 1133, 1550, 1314, 1297, 1326, 2023, 923, 1477, ...
## $ country        <int> 9, 1, 13, 35, 27, 9, 103, 183, 89, 57, 3, 109, ...
## $ State          <int> 23, 50, 25, 6, 50, 25, 26, 37, 12, 5, 53, 28, 3...
## $ CouSub         <int> 19770, 29575, 8470, 99999, 60100, 37995, 99999,...
## $ countyname     <chr> "Hancock County", "Addison County", "Hampden Co...
## $ storecode      <chr> "NCNTY23009N23009", "NCNTY50001N50001", "METRO4...
## $ Areaname       <chr> "Hancock County, ME", "Addison County, VT", "Sp...
## $ countytownname <chr> "Eastbrook town", "Granville town", "Brimfield ...
## $ population     <int> 423, 298, 3609, 34895, 1139, 5136, 67077, 90099...
## $ state_alpha    <chr> "ME", "VT", "MA", "CA", "VT", "MA", "MI", "NC",...
## $ store_Type     <chr> "Supermarket Type1", "Supermarket Type1", "Supe...
## $ store          <fct> 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1,...
## $ data           <chr> "train", "train", "train", "train", "train", "t...

Next we will convert all categorical variables to dummies. We will write a function which will take care of that instead of converting them one by one.

** Writing a Dummy Creation Function **

CreateDummies=function(data,var,freq_cutoff=0){
t=table(data[,var])
t=t[t>freq_cutoff]
t=sort(t)
categories=names(t)[-1]
for( cat in categories){
name=paste(var,cat,sep="_")
name=gsub(" ","",name)
name=gsub("-","_",name)
name=gsub("\\?","Q",name)
name=gsub("<","LT_",name)
name=gsub("\\+","",name)
name=gsub("\\/","_",name)
name=gsub(">","GT_",name)
name=gsub("=","EQ_",name)
name=gsub(",","",name)
data[,name]=as.numeric(data[,var]==cat)
}
data[,var]=NULL
return(data)
}

Let me explain the function ‘CreateDummies’ we just created

t=table(data[,var]) this bit creates a frequency table for the given categorical column. t here is now simply a table which contains names as categories of the categorical variable and their frequency in the data.

t=t[t>freq_cutoff] this line of code removes those categories from the table which have frequencies below the frequency cutoff. ( this is a subjective choice)

‘t=sort(t)’ this line simple sorts the remaining table in ascending order

categories=names(t)[-1] since we sorted the table in ascending manner in the previous line, first category here has least count. In this line of code we are taking out the category names except the first one ( which has least count), thus making n-1 dummies from the remaining categories.

name=paste(var,cat,sep=“_“) all the dummy vars that we intend to make, need to have some name. this line of code creates that name by concatenating variable name with category name with an _.

name=gsub(" “,”“,name) subsequent lines like these using gsub are essentially cleaning up the name. Since we dont have any control over what the categories can be, we are removing special characters and spaces in the code in an automated fashion.

data[,name]=as.numeric(data[,var]==cat) once we have a cleaned up name, this line creates the dummy var for that particular category.

data[,var]=NULL once we are done creating dummies for the variable using for loop. Variable is removed from the data in this line.

Let us have a look at our categorical variables by writing following lines of codes

names(s)[sapply(s,function(x) is.character(x))]
## [1] "countyname"     "storecode"      "Areaname"       "countytownname"
## [5] "state_alpha"    "store_Type"     "data"

Now we will check for High-Cardinality in the categorical variables i.e we will check for variables with many distinct values. We will discard those variables from our modelling. Because including these attributes by standard dummy encoding increases the dimensionality of the data to such an extent that either the classification technique is unable to process them or if one would use some regularized linear technique that is able to cope with huge dimensions, it leads to a model with thousands or even millions of features, thereby losing the often required comprehensibility aspect.

length(unique(s$countyname))
## [1] 1962
length(unique(s$storecode))
## [1] 2572
length(unique(s$Areaname))
## [1] 2572
length(unique(s$countytownname))
## [1] 3176
length(unique(s$state_alpha))
## [1] 54
length(unique(s$store_Type))
## [1] 4

We will ignore columns or variables like countyname,storecode,Areaname,countytownname for their High-Cardinality. Further we will ignore data column for obvious reason.

s=s %>% select(-countyname,-storecode,-Areaname,-countytownname)

Above codes will discard those four variables & we are left with 14 variables now. Next Let us make dummies for the rest of columns - state_alpha & store_Type.

cat_cols=c("state_alpha","store_Type")

for(cat in cat_cols){
 s=CreateDummies(s,cat,100) 
}

This will increase our columns to 26 as we can glimpse the same as shown below.

glimpse(s)
## Observations: 4,769
## Variables: 26
## $ Id                          <dbl> 2300919770, 5000129575, 2501308470...
## $ sales0                      <int> 848, 925, 924, 924, 1017, 1494, 69...
## $ sales1                      <int> 588, 717, 616, 646, 730, 1071, 476...
## $ sales2                      <int> 666, 780, 739, 683, 735, 1196, 541...
## $ sales3                      <int> 1116, 1283, 1154, 1292, 1208, 1861...
## $ sales4                      <int> 1133, 1550, 1314, 1297, 1326, 2023...
## $ country                     <int> 9, 1, 13, 35, 27, 9, 103, 183, 89,...
## $ State                       <int> 23, 50, 25, 6, 50, 25, 26, 37, 12,...
## $ CouSub                      <int> 19770, 29575, 8470, 99999, 60100, ...
## $ population                  <int> 423, 298, 3609, 34895, 1139, 5136,...
## $ store                       <fct> 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1...
## $ data                        <chr> "train", "train", "train", "train"...
## $ state_alpha_KS              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ state_alpha_MO              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ state_alpha_KY              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ state_alpha_VA              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ state_alpha_GA              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ state_alpha_CT              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ state_alpha_TX              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ state_alpha_VT              <dbl> 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0...
## $ state_alpha_NH              <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ state_alpha_MA              <dbl> 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0...
## $ state_alpha_ME              <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ store_Type_SupermarketType3 <dbl> 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0...
## $ store_Type_GroceryStore     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0...
## $ store_Type_SupermarketType1 <dbl> 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1...

Let us see if there is any missing values in our data. We will use lapply function which will give output in list format as shown below.

lapply(s,function(x) sum(is.na(x)))
## $Id
## [1] 0
## 
## $sales0
## [1] 0
## 
## $sales1
## [1] 0
## 
## $sales2
## [1] 0
## 
## $sales3
## [1] 0
## 
## $sales4
## [1] 0
## 
## $country
## [1] 1
## 
## $State
## [1] 0
## 
## $CouSub
## [1] 0
## 
## $population
## [1] 2
## 
## $store
## [1] 1431
## 
## $data
## [1] 0
## 
## $state_alpha_KS
## [1] 0
## 
## $state_alpha_MO
## [1] 0
## 
## $state_alpha_KY
## [1] 0
## 
## $state_alpha_VA
## [1] 0
## 
## $state_alpha_GA
## [1] 0
## 
## $state_alpha_CT
## [1] 0
## 
## $state_alpha_TX
## [1] 0
## 
## $state_alpha_VT
## [1] 0
## 
## $state_alpha_NH
## [1] 0
## 
## $state_alpha_MA
## [1] 0
## 
## $state_alpha_ME
## [1] 0
## 
## $store_Type_SupermarketType3
## [1] 0
## 
## $store_Type_GroceryStore
## [1] 0
## 
## $store_Type_SupermarketType1
## [1] 0

From above we can see that We do have missing values in columns like country, population & store. Next we impute those missing values with the mean of train data as shown below.

for(col in names(s)){
if(sum(is.na(s[,col]))>0 & !(col %in% c("data","store"))){
s[is.na(s[,col]),col]=mean(s[s$data=='train',col],na.rm=T)
}
}

We can always cross check if those NAs has been replaced with mean or not by using lapply function again.

lapply(s,function(x) sum(is.na(x)))
## $Id
## [1] 0
## 
## $sales0
## [1] 0
## 
## $sales1
## [1] 0
## 
## $sales2
## [1] 0
## 
## $sales3
## [1] 0
## 
## $sales4
## [1] 0
## 
## $country
## [1] 0
## 
## $State
## [1] 0
## 
## $CouSub
## [1] 0
## 
## $population
## [1] 0
## 
## $store
## [1] 1431
## 
## $data
## [1] 0
## 
## $state_alpha_KS
## [1] 0
## 
## $state_alpha_MO
## [1] 0
## 
## $state_alpha_KY
## [1] 0
## 
## $state_alpha_VA
## [1] 0
## 
## $state_alpha_GA
## [1] 0
## 
## $state_alpha_CT
## [1] 0
## 
## $state_alpha_TX
## [1] 0
## 
## $state_alpha_VT
## [1] 0
## 
## $state_alpha_NH
## [1] 0
## 
## $state_alpha_MA
## [1] 0
## 
## $state_alpha_ME
## [1] 0
## 
## $store_Type_SupermarketType3
## [1] 0
## 
## $store_Type_GroceryStore
## [1] 0
## 
## $store_Type_SupermarketType1
## [1] 0

Now we are done with data preparation , lets separate the data next.

s_train=s %>% filter(data=="train") %>% select(-data)
s_test=s %>% filter(data=="test") %>% select(-data,-store)

Next we will break our train data into 2 parts. We will build model on one part & check its performance on the other.

set.seed(2)
s=sample(1:nrow(s_train),0.8*nrow(s_train))
s_train1=s_train[s,]
s_train2=s_train[-s,]

** Model Building **

Let us load the package randomForest first

library(randomForest)
## randomForest 4.6-14
## Type rfNews() to see new features/changes/bug fixes.
## 
## Attaching package: 'randomForest'
## The following object is masked from 'package:dplyr':
## 
##     combine

Next we will build our model with 5 variables randomly subsetted at each node i.e mtry & let just say we want to grow 100 such trees.

model_rf=randomForest(store~.-Id,data=s_train1,mtry=5,ntree=100)

Let us see what does this model_rf represent

model_rf
## 
## Call:
##  randomForest(formula = store ~ . - Id, data = s_train1, mtry = 5,      ntree = 100) 
##                Type of random forest: classification
##                      Number of trees: 100
## No. of variables tried at each split: 5
## 
##         OOB estimate of  error rate: 23.41%
## Confusion matrix:
##      0   1 class.error
## 0 1273 243   0.1602902
## 1  382 772   0.3310225

It is clear from above that OOB estimate of error has come out to be 23.22 % which is decent. Also from the confusion matrix we can conclude that our model has correctly predicted for 1277 stores as ‘not opened’ & for 773 stores correctly predicted as ‘opened’. The diagonal values 381 & 239 are giving justification as to why our OOB error is around 23%.

** Model Validation **

Lets see performance of this model on the validation data s_train2 that we kept aside.

val.score=predict(model_rf,newdata=s_train2,type='response')

Again we need to check the accuracy using confusionMatrix from caret package. What we will get is an accuracy of 78% which seems to be a fair model.

library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:randomForest':
## 
##     margin
confusionMatrix(val.score,s_train2$store)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction   0   1
##          0 311  91
##          1  48 218
##                                           
##                Accuracy : 0.7919          
##                  95% CI : (0.7591, 0.8221)
##     No Information Rate : 0.5374          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.5774          
##                                           
##  Mcnemar's Test P-Value : 0.0003675       
##                                           
##             Sensitivity : 0.8663          
##             Specificity : 0.7055          
##          Pos Pred Value : 0.7736          
##          Neg Pred Value : 0.8195          
##              Prevalence : 0.5374          
##          Detection Rate : 0.4656          
##    Detection Prevalence : 0.6018          
##       Balanced Accuracy : 0.7859          
##                                           
##        'Positive' Class : 0               
## 

Now let us calculate probability score for our validation data set s_train2.

val.prob_score=predict(model_rf,newdata=s_train2,type='prob')

In order to check the performance of our model let us calculate its auc score. For that we need to first import a package named ‘pROC’.

library(pROC)
## Type 'citation("pROC")' for a citation.
## 
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
auc_score=auc(roc(s_train2$store,val.prob_score[,1]))
auc_score
## Area under the curve: 0.8236

From above it is clear that the auc score or the tentative score performance of our model is going to be around 0.82 which is decent enough.

We can also plot our auc

plot(roc(s_train2$store,val.prob_score[,1]))

Next we will build the random forest model on the entire training data set ‘s_train’ & predict the same on test data set ‘s_test’

model_rf_final=randomForest(store~.-Id,data=s_train,mtry=5,ntree=100)
model_rf_final
## 
## Call:
##  randomForest(formula = store ~ . - Id, data = s_train, mtry = 5,      ntree = 100) 
##                Type of random forest: classification
##                      Number of trees: 100
## No. of variables tried at each split: 5
## 
##         OOB estimate of  error rate: 22.47%
## Confusion matrix:
##      0    1 class.error
## 0 1583  292   0.1557333
## 1  458 1005   0.3130554

We will now use this model to predict probability score for test data .

test.score=predict(model_rf_final,newdata = s_test,type='prob')[,1]

To see what does my test.score contains

test.score
##    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15 
## 0.72 0.94 0.66 0.94 0.82 0.78 0.32 0.82 0.86 0.85 0.32 0.42 0.88 0.19 0.70 
##   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30 
## 0.61 0.86 0.75 0.52 0.61 0.53 0.58 0.75 0.34 0.63 0.40 0.34 0.28 0.90 0.71 
##   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45 
## 0.96 0.58 0.88 0.71 0.72 0.78 0.79 0.37 0.93 0.59 0.93 0.67 0.99 0.96 0.86 
##   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60 
## 0.91 0.87 0.15 0.66 0.68 0.38 0.08 0.08 0.56 0.35 0.24 0.73 0.41 0.89 0.28 
##   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75 
## 0.61 0.82 0.80 0.70 0.37 0.87 0.38 0.77 0.65 0.91 0.71 0.28 0.78 0.14 0.04 
##   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90 
## 0.01 0.03 0.01 0.11 0.06 0.13 0.05 0.13 0.25 0.06 0.05 0.34 0.45 0.56 0.71 
##   91   92   93   94   95   96   97   98   99  100  101  102  103  104  105 
## 0.80 0.68 0.64 0.30 0.11 0.19 0.49 0.23 0.16 0.05 0.66 0.14 0.42 0.11 0.19 
##  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120 
## 0.01 0.17 0.22 0.07 0.15 0.22 0.09 0.09 0.72 0.76 0.78 0.27 0.29 0.84 0.05 
##  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135 
## 0.34 0.92 0.56 0.25 0.24 0.38 0.40 0.17 0.77 0.93 0.17 0.73 0.13 0.05 0.28 
##  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150 
## 0.44 0.66 0.85 0.73 0.52 0.83 0.58 0.34 0.17 0.79 0.71 0.08 0.09 0.83 0.31 
##  151  152  153  154  155  156  157  158  159  160  161  162  163  164  165 
## 0.59 0.23 0.90 0.69 0.47 0.89 0.79 0.08 0.44 0.80 0.86 0.08 0.92 0.22 0.74 
##  166  167  168  169  170  171  172  173  174  175  176  177  178  179  180 
## 0.90 0.91 0.98 0.11 0.91 0.78 0.10 0.69 0.71 0.47 0.67 0.70 0.92 0.97 0.79 
##  181  182  183  184  185  186  187  188  189  190  191  192  193  194  195 
## 0.37 0.94 0.48 0.81 0.87 0.89 0.92 0.55 0.83 0.21 0.89 0.48 0.78 0.37 0.80 
##  196  197  198  199  200  201  202  203  204  205  206  207  208  209  210 
## 0.73 0.39 0.91 0.64 0.88 0.36 0.76 0.90 0.30 0.17 0.03 0.78 0.49 0.76 0.42 
##  211  212  213  214  215  216  217  218  219  220  221  222  223  224  225 
## 0.18 0.22 0.86 0.87 0.54 0.81 0.82 0.73 0.50 0.49 0.80 0.74 0.51 0.67 0.18 
##  226  227  228  229  230  231  232  233  234  235  236  237  238  239  240 
## 0.41 0.24 0.05 0.81 0.79 0.08 0.60 0.14 0.27 0.48 0.47 0.74 0.66 0.66 0.76 
##  241  242  243  244  245  246  247  248  249  250  251  252  253  254  255 
## 0.14 0.62 0.73 0.90 0.18 0.88 0.34 0.75 0.89 0.51 0.31 0.84 0.89 0.66 0.97 
##  256  257  258  259  260  261  262  263  264  265  266  267  268  269  270 
## 0.90 0.74 0.91 0.75 0.83 0.79 0.25 0.85 0.77 0.95 0.67 0.74 0.82 0.69 0.78 
##  271  272  273  274  275  276  277  278  279  280  281  282  283  284  285 
## 0.70 0.72 0.72 0.54 0.56 0.93 0.85 0.25 0.21 0.83 0.65 0.71 0.75 0.80 0.83 
##  286  287  288  289  290  291  292  293  294  295  296  297  298  299  300 
## 0.39 0.69 0.51 0.24 0.85 0.76 0.93 0.57 0.87 0.65 0.88 0.77 0.24 0.51 0.89 
##  301  302  303  304  305  306  307  308  309  310  311  312  313  314  315 
## 0.52 0.23 0.80 0.29 0.09 0.45 0.91 0.19 0.35 0.87 0.78 0.78 0.89 0.36 0.88 
##  316  317  318  319  320  321  322  323  324  325  326  327  328  329  330 
## 0.69 0.83 0.04 0.66 0.13 0.65 0.88 0.19 0.63 0.79 0.46 0.08 0.82 0.08 0.20 
##  331  332  333  334  335  336  337  338  339  340  341  342  343  344  345 
## 0.89 0.18 0.15 0.41 0.64 0.20 0.67 0.22 0.53 0.81 0.45 0.11 0.19 0.21 0.84 
##  346  347  348  349  350  351  352  353  354  355  356  357  358  359  360 
## 0.91 0.98 0.96 0.95 0.87 0.99 0.97 0.91 0.95 0.97 0.92 0.92 0.96 0.90 0.97 
##  361  362  363  364  365  366  367  368  369  370  371  372  373  374  375 
## 0.96 0.83 0.15 0.01 0.02 0.03 0.00 0.78 0.74 0.88 0.84 0.91 0.97 0.92 0.84 
##  376  377  378  379  380  381  382  383  384  385  386  387  388  389  390 
## 0.97 0.92 0.89 0.89 0.73 0.94 0.88 0.81 0.92 0.88 0.83 0.63 0.51 0.93 0.86 
##  391  392  393  394  395  396  397  398  399  400  401  402  403  404  405 
## 0.84 0.88 0.96 0.98 0.93 0.90 0.92 0.99 0.67 0.57 0.94 0.99 0.88 0.97 0.95 
##  406  407  408  409  410  411  412  413  414  415  416  417  418  419  420 
## 0.97 0.86 0.91 0.85 0.98 0.91 0.98 0.96 0.80 0.97 0.96 0.10 0.04 0.04 0.05 
##  421  422  423  424  425  426  427  428  429  430  431  432  433  434  435 
## 0.14 0.09 0.04 0.14 0.15 0.11 0.14 0.02 0.09 0.06 0.12 0.10 0.13 0.08 0.10 
##  436  437  438  439  440  441  442  443  444  445  446  447  448  449  450 
## 0.02 0.79 0.77 0.87 0.95 0.97 0.96 0.99 0.41 0.40 0.55 0.86 0.98 0.98 0.88 
##  451  452  453  454  455  456  457  458  459  460  461  462  463  464  465 
## 0.81 0.91 0.92 0.85 0.88 0.92 0.98 0.97 1.00 0.97 1.00 0.90 0.95 0.84 0.87 
##  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480 
## 0.71 0.70 0.95 0.95 0.93 0.91 0.97 0.98 0.97 0.99 0.96 0.94 0.07 0.14 0.09 
##  481  482  483  484  485  486  487  488  489  490  491  492  493  494  495 
## 0.11 0.16 0.68 0.06 0.58 0.13 0.02 0.12 0.64 0.17 0.22 0.02 0.12 0.17 0.10 
##  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510 
## 0.07 0.00 0.03 0.03 0.02 0.51 0.67 0.71 0.05 0.10 0.48 0.27 0.09 0.17 0.36 
##  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525 
## 0.04 0.25 0.02 0.04 0.09 0.20 0.05 0.00 0.06 0.08 0.24 0.12 0.04 0.13 0.00 
##  526  527  528  529  530  531  532  533  534  535  536  537  538  539  540 
## 0.08 0.25 0.02 0.27 0.27 0.16 0.01 0.00 0.04 0.02 0.10 0.00 0.01 0.02 0.03 
##  541  542  543  544  545  546  547  548  549  550  551  552  553  554  555 
## 0.00 0.04 0.00 0.02 0.15 0.15 0.15 0.31 0.15 0.20 0.13 0.14 0.16 0.03 0.02 
##  556  557  558  559  560  561  562  563  564  565  566  567  568  569  570 
## 0.24 0.07 0.26 0.13 0.06 0.26 0.06 0.39 0.06 0.22 0.57 0.77 0.73 0.58 0.79 
##  571  572  573  574  575  576  577  578  579  580  581  582  583  584  585 
## 0.76 0.89 0.91 0.49 0.91 0.73 0.86 0.83 0.60 0.07 0.23 0.73 0.40 0.68 0.43 
##  586  587  588  589  590  591  592  593  594  595  596  597  598  599  600 
## 0.92 0.95 0.78 0.49 0.93 0.90 0.90 0.68 0.70 0.81 0.72 0.29 0.88 0.15 0.88 
##  601  602  603  604  605  606  607  608  609  610  611  612  613  614  615 
## 0.87 0.10 0.79 0.92 0.76 0.89 0.40 0.84 0.90 0.82 0.75 0.81 0.88 0.86 0.71 
##  616  617  618  619  620  621  622  623  624  625  626  627  628  629  630 
## 0.54 0.71 0.69 0.85 0.83 0.31 0.77 0.90 0.67 0.69 0.77 0.75 0.89 0.83 0.69 
##  631  632  633  634  635  636  637  638  639  640  641  642  643  644  645 
## 0.30 0.79 0.53 0.75 0.87 0.66 0.92 0.88 0.77 0.03 0.41 0.75 0.77 0.75 0.99 
##  646  647  648  649  650  651  652  653  654  655  656  657  658  659  660 
## 0.91 0.78 0.84 0.76 0.60 0.77 0.84 0.72 0.75 0.74 0.56 0.98 0.58 0.84 0.65 
##  661  662  663  664  665  666  667  668  669  670  671  672  673  674  675 
## 0.87 0.81 0.57 0.59 0.97 0.87 0.95 0.85 0.86 0.80 0.42 0.87 0.76 0.86 0.53 
##  676  677  678  679  680  681  682  683  684  685  686  687  688  689  690 
## 0.73 0.24 0.97 0.98 0.86 0.85 0.59 0.99 0.95 0.70 0.77 0.98 0.86 0.17 1.00 
##  691  692  693  694  695  696  697  698  699  700  701  702  703  704  705 
## 0.90 0.44 0.69 0.56 0.57 0.78 0.91 0.67 0.89 0.85 0.82 0.99 0.97 0.99 0.97 
##  706  707  708  709  710  711  712  713  714  715  716  717  718  719  720 
## 0.87 0.61 0.98 0.96 0.99 0.69 0.93 0.60 0.86 0.93 0.97 0.66 0.94 0.98 0.92 
##  721  722  723  724  725  726  727  728  729  730  731  732  733  734  735 
## 0.93 0.98 0.99 0.97 0.71 1.00 0.78 0.89 0.99 1.00 0.92 0.85 0.90 0.07 0.19 
##  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750 
## 0.08 0.19 0.24 0.13 0.13 0.64 0.92 0.89 0.92 0.16 0.25 0.06 0.20 0.14 0.09 
##  751  752  753  754  755  756  757  758  759  760  761  762  763  764  765 
## 0.10 0.09 0.42 0.13 0.32 0.19 0.05 0.07 0.04 0.95 0.84 0.96 0.96 0.97 0.97 
##  766  767  768  769  770  771  772  773  774  775  776  777  778  779  780 
## 0.96 0.96 0.92 0.15 0.40 0.02 0.07 0.16 0.12 0.06 0.14 0.13 0.10 0.08 0.31 
##  781  782  783  784  785  786  787  788  789  790  791  792  793  794  795 
## 0.83 0.73 0.85 0.74 0.75 0.48 0.87 0.85 0.52 0.37 0.70 0.51 0.90 0.20 0.08 
##  796  797  798  799  800  801  802  803  804  805  806  807  808  809  810 
## 0.05 0.19 0.22 0.41 0.44 0.17 0.86 0.77 0.81 0.58 0.55 0.53 0.43 0.40 0.58 
##  811  812  813  814  815  816  817  818  819  820  821  822  823  824  825 
## 0.81 0.68 0.64 0.10 0.77 0.34 0.82 0.19 0.57 0.82 0.49 0.49 0.80 0.74 0.97 
##  826  827  828  829  830  831  832  833  834  835  836  837  838  839  840 
## 0.79 0.96 0.73 0.82 0.87 0.44 0.69 0.93 0.42 0.86 0.91 0.88 0.96 0.69 0.29 
##  841  842  843  844  845  846  847  848  849  850  851  852  853  854  855 
## 0.36 0.62 0.40 0.64 0.25 0.76 0.38 0.69 0.28 0.13 0.59 0.39 0.50 0.79 0.68 
##  856  857  858  859  860  861  862  863  864  865  866  867  868  869  870 
## 0.18 0.19 0.88 0.21 0.29 0.76 0.83 0.71 0.80 0.80 0.70 0.82 0.98 0.18 0.40 
##  871  872  873  874  875  876  877  878  879  880  881  882  883  884  885 
## 0.68 0.89 0.13 0.83 0.77 0.61 0.53 0.75 0.78 0.87 0.42 0.31 0.48 0.86 0.10 
##  886  887  888  889  890  891  892  893  894  895  896  897  898  899  900 
## 0.42 0.55 0.40 0.54 0.63 0.39 0.25 0.29 0.45 0.90 0.79 0.44 0.75 0.55 0.38 
##  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915 
## 0.39 0.84 0.56 0.33 0.35 0.82 0.04 0.45 0.96 0.68 0.21 0.56 0.10 0.22 0.14 
##  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930 
## 0.09 0.41 0.26 0.33 0.68 0.80 0.20 0.63 0.79 0.78 0.56 0.30 0.40 0.59 0.68 
##  931  932  933  934  935  936  937  938  939  940  941  942  943  944  945 
## 0.72 0.69 0.72 0.98 0.72 0.78 0.58 0.95 0.67 0.96 0.84 0.87 0.21 0.83 0.83 
##  946  947  948  949  950  951  952  953  954  955  956  957  958  959  960 
## 0.82 0.81 0.55 0.73 0.80 0.69 0.85 0.26 0.59 0.25 0.32 0.77 0.55 0.48 0.65 
##  961  962  963  964  965  966  967  968  969  970  971  972  973  974  975 
## 0.89 0.70 0.77 0.62 0.79 0.83 0.63 0.21 0.82 0.91 0.61 0.59 0.45 0.43 0.26 
##  976  977  978  979  980  981  982  983  984  985  986  987  988  989  990 
## 0.73 0.69 0.26 0.55 0.46 0.33 0.58 0.30 0.35 0.18 0.50 0.63 0.79 0.26 0.80 
##  991  992  993  994  995  996  997  998  999 1000 1001 1002 1003 1004 1005 
## 0.49 0.57 0.52 0.87 0.91 0.93 0.78 0.72 0.73 0.57 0.04 0.70 0.30 0.72 0.81 
## 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 
## 0.49 0.66 0.06 0.93 0.86 0.82 0.37 0.52 0.69 0.91 0.84 0.86 0.19 0.90 0.76 
## 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 
## 0.80 0.80 0.11 0.70 0.93 0.09 0.63 0.68 0.18 0.56 0.68 0.69 0.84 0.90 0.66 
## 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 
## 0.05 0.70 0.86 0.76 0.07 0.65 0.79 0.24 0.70 0.51 0.67 0.77 0.73 0.92 0.83 
## 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 
## 0.79 0.57 0.29 0.41 0.76 0.55 0.75 0.72 0.86 0.83 0.73 0.80 0.89 0.97 0.98 
## 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 
## 0.97 0.97 0.99 0.99 0.99 0.96 0.93 1.00 0.98 0.99 0.26 0.22 0.15 0.24 0.34 
## 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 
## 0.98 0.98 1.00 1.00 0.52 0.18 0.55 0.17 0.13 0.08 0.10 0.88 0.78 0.72 0.90 
## 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 
## 0.91 0.89 0.85 0.89 0.99 0.97 0.99 0.98 0.83 0.99 1.00 0.90 0.91 0.94 0.96 
## 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 
## 0.90 0.83 0.99 0.82 0.78 0.92 0.92 0.70 0.60 0.95 0.82 0.54 0.65 0.64 0.46 
## 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 
## 0.16 0.75 0.32 0.02 0.77 0.79 0.29 0.82 0.19 0.58 0.11 0.12 0.15 0.03 0.41 
## 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 
## 0.93 0.23 0.34 0.81 0.14 0.21 0.56 0.50 0.76 0.15 0.66 0.42 0.55 0.38 0.58 
## 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 
## 0.35 0.61 0.66 0.34 0.66 0.79 0.77 0.91 0.22 0.43 0.80 0.83 0.71 0.55 0.90 
## 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 
## 0.86 0.81 0.33 0.65 0.82 0.88 0.45 0.57 0.82 0.35 0.83 0.92 0.56 0.29 0.76 
## 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 
## 0.78 0.40 0.32 0.83 0.83 0.66 0.62 0.51 0.90 0.68 0.78 0.17 0.04 0.03 0.08 
## 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 
## 0.10 0.03 0.21 0.73 0.01 0.12 0.07 0.03 0.15 0.16 0.06 0.03 0.26 0.31 0.09 
## 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 
## 0.08 0.10 0.00 0.45 0.76 0.90 0.91 0.84 0.23 0.64 0.06 0.26 0.85 0.71 0.34 
## 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 
## 0.17 0.40 0.78 0.94 0.75 0.09 0.33 0.31 0.05 0.96 0.98 0.87 0.91 0.50 0.97 
## 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 
## 0.94 0.34 0.89 0.64 0.94 0.94 0.43 0.21 0.74 0.46 0.10 0.93 0.80 0.47 0.61 
## 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 
## 0.07 0.86 0.91 0.87 0.11 0.91 0.73 1.00 0.04 0.00 0.23 0.78 0.28 0.84 0.75 
## 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 
## 0.01 0.88 0.65 0.98 0.78 0.80 0.86 0.76 0.88 0.97 0.61 0.24 0.94 1.00 0.98 
## 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 
## 0.55 0.94 0.15 0.25 0.84 0.73 0.48 0.78 0.73 0.49 0.66 0.91 0.21 0.79 0.90 
## 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 
## 0.71 0.12 0.15 0.15 0.99 0.67 0.07 0.40 0.14 0.15 0.45 0.13 0.97 0.19 0.92 
## 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 
## 0.87 0.10 0.04 0.92 0.93 0.45 0.60 0.98 0.76 0.11 0.78 0.15 0.14 0.95 0.78 
## 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 
## 0.84 0.10 0.90 0.03 0.80 0.94 0.40 0.03 0.00 0.76 0.85 0.89 0.81 0.85 0.44 
## 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 
## 0.20 0.68 0.04 0.02 0.64 0.61 0.55 0.82 0.89 0.15 0.72 0.85 0.98 0.89 0.91 
## 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 
## 0.01 0.82 0.81 0.54 0.84 0.63 0.59 0.57 0.96 0.90 0.40 0.10 0.63 0.06 0.58 
## 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 
## 0.99 0.12 0.59 0.76 0.10 0.01 0.65 0.08 0.98 0.28 0.63 0.83 0.36 0.82 0.13 
## 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 
## 0.17 0.86 0.73 0.97 0.75 0.19 0.77 0.59 0.13 0.07 0.06 0.63 0.98 0.87 0.22 
## 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 
## 0.21 0.92 0.98 0.78 0.31 0.05 0.99 0.09 0.54 0.96 0.18 0.30 0.45 0.96 0.90 
## 1426 1427 1428 1429 1430 1431 
## 0.73 0.84 0.63 0.96 0.96 1.00

** Variable Importance **

We will run below codes to find out the importance of variable. Higher the mean decrease ginni for any variable better is the variable for prediction. So population is the most important variable.

d=importance(model_rf_final)
d=as.data.frame(d)
d$VariableNames=rownames(d)
d %>% arrange(desc(MeanDecreaseGini))
##    MeanDecreaseGini               VariableNames
## 1        258.626715                  population
## 2        167.133394                      sales4
## 3        163.245043                      sales0
## 4        161.830217                      sales3
## 5        147.395128                      sales2
## 6        137.757549                      sales1
## 7        123.945385                     country
## 8        110.103799                       State
## 9         66.276194                      CouSub
## 10        26.698305              state_alpha_MA
## 11        24.843971 store_Type_SupermarketType1
## 12        20.072403              state_alpha_VT
## 13        15.871697     store_Type_GroceryStore
## 14        14.412103 store_Type_SupermarketType3
## 15         7.264191              state_alpha_NH
## 16         7.187766              state_alpha_ME
## 17         5.459227              state_alpha_GA
## 18         5.017612              state_alpha_TX
## 19         4.584571              state_alpha_VA
## 20         4.377301              state_alpha_KS
## 21         4.030374              state_alpha_MO
## 22         3.389849              state_alpha_KY
## 23         2.836885              state_alpha_CT

Upon plotting we get a plot like this.

varImpPlot(model_rf_final)