-
Notifications
You must be signed in to change notification settings - Fork 1
/
Final_Code_Khadka.Rmd
1803 lines (1454 loc) · 57 KB
/
Final_Code_Khadka.Rmd
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "A Survey on Machine Learning Techniques for Airbnb Price Prediction"
author: "Kevil Khadka"
date: "4/10/2020"
output:
pdf_document: default
html_document: default
---
## Loading libraries
```{r setup, include=FALSE, warning=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(dplyr)
library(caret)
library(MASS)
library(Sleuth3)
library(ISLR)
library(mice)
library(Hmisc)
library(GGally)
library(leaps)
library(caTools)
library(ggthemes)
library(ggExtra)
library(glmnet)
library(corrplot)
library(leaflet)
library(kableExtra)
library(RColorBrewer)
library(plotly)
library(plotrix)
library(readr)
library(ggmap)
library(ggpubr)
# Decision Tree
library(rpart)
library(rpart.plot)
library(randomForest)
library(partykit)
# Naive Bayes
library(e1071)
library(klaR)
# kNN
library(class)
# Text Analaysis
library(tidytext)
library(wordcloud)
# Sentiment Analysis
library(textdata)
library(reshape2)
# File Location
setwd("/System/Volumes/Data/University of Evansville/SPRING 2020/STAT 493/Airbnb_final project/RCode/Final_Coding_Khadka_files/Datasets")
```
## Importing dataset
```{r, warning=FALSE, echo=FALSE}
# Los Angeles
set.seed(123)
losangeles <- read_csv("/System/Volumes/Data/University of Evansville/SPRING 2020/STAT 493/Airbnb_final project/RCode/Final_Coding_Khadka_files/Datasets/LA_listingsDec18.csv")
summary(losangeles)
```
## Data Exploration
```{r, warning=FALSE, echo=FALSE}
airbnb_LA <- dplyr::select(losangeles, -id, -last_review)
airbnb_LA$neighbourhood_group <- as_factor(airbnb_LA$neighbourhood_group)
airbnb_LA$neighbourhood <- as_factor(airbnb_LA$neighbourhood)
airbnb_LA$room_type <- as_factor(airbnb_LA$room_type)
summary(airbnb_LA)
```
# Missing Data
```{r, warning=FALSE, echo=FALSE}
contents(airbnb_LA)
missing_airbnb <- airbnb_LA %>% summarise_all(~(sum(is.na(.))/n()))
missing_airbnb <- gather(missing_airbnb, key = "variables", value = "percent_missing")
missing_airbnb <- missing_airbnb[missing_airbnb$percent_missing > 0.0, ]
th <- theme_fivethirtyeight() + theme(axis.title = element_text(), axis.title.x = element_text())
ggplot(missing_airbnb, aes(x = reorder(variables, percent_missing), y = percent_missing)) +
geom_bar(stat = "identity", fill = "red", aes(color = I('white')), size = 0.3) +
xlab('variables') +
coord_flip() +
th +
ggtitle("Missing Data") +
xlab("Column name") +
ylab("Percentage missing") +
annotate("text", x = 1.5, y = 0.1, label = "name and host_name has less than 0.001\n percentage missing", color = "slateblue", size = 5)
```
# Data cleaning
```{r, warning=FALSE, echo=FALSE}
# Set NA to value '0'
airbnb_LA$reviews_per_month[is.na(airbnb_LA$reviews_per_month)] <- 0
airbnb_LA$calculated_host_listings_count[is.na(airbnb_LA$calculated_host_listings_count)] <- 0
airbnb_LA$name[is.na(airbnb_LA$name)] <- "No.name"
airbnb_LA$host_name[is.na(airbnb_LA$host_name)] <- "No.name"
# summary(airbnb_LA)
```
## Part 1: Data Visualization
# Leaflet map
```{r}
pal <- colorFactor(palette = c("orange", "red", "green", "purple", "blue"), domain = airbnb_LA$neighbourhood_group)
leaflet(data = airbnb_LA) %>%
addProviderTiles(providers$CartoDB.DarkMatterNoLabels) %>%
addCircleMarkers(~longitude, ~latitude, color = ~pal(neighbourhood_group),
weight = 1, radius=1, fillOpacity = 0.1, opacity = 0.1,
label = paste("Name:", airbnb_LA$name)) %>%
addLegend("bottomright", pal = pal, values = ~neighbourhood_group,
title = "Neighbourhood groups",opacity = 1)
```
# Looking Correlation
```{r, warning=FALSE}
airbnb.LA.1 <- airbnb_LA %>%
select("price", "number_of_reviews", "minimum_nights", "availability_365", "neighbourhood_group", "room_type")
ggpairs(airbnb.LA.1, progress = FALSE)
```
From following ggpairs graph, we cannot see much correlation between those variables.
# Looking our response variable: "price"
```{r, warning=FALSE}
# Do not need the listing which has price value 0
airbnb_LA <- airbnb_LA %>%
filter(price > 0) %>%
filter(reviews_per_month > 0) %>%
filter(number_of_reviews > 0) %>%
filter(calculated_host_listings_count > 0)
# Histogram and Density
ggplot(airbnb_LA, aes(price)) +
geom_histogram(bins = 30, aes(y = ..density..), fill = "red") +
geom_density(alpha = 0.2, fill = "red") +
ggtitle("Distribution of price - Airbnb LA",
subtitle = "The distribution is very skewed") +
theme(axis.title = element_text(), axis.title.x = element_text()) +
geom_vline(xintercept = round(mean(airbnb_LA$price), 3), size = 2, linetype = 3)
price_list <- airbnb_LA %>%
select(price) %>%
dplyr::group_by(price) %>%
dplyr::summarise(n_count = n()) %>%
dplyr::mutate(n_count_per = n_count * 100 / sum(n_count)) %>%
arrange(desc(n_count)) %>%
top_n(10)
print(price_list)
# Log Transform of "price"
airbnb_LA <- airbnb_LA %>%
mutate(log_price = log(price))
ggplot(airbnb_LA, aes(log_price)) +
geom_histogram(bins = 30, aes(y = ..density..), fill = "red") +
th +
geom_density(alpha = 0.2, fill = "red") +
ggtitle("Distribution of price - Airbnb LA",
subtitle = "The distribution is normal") +
theme(axis.title = element_text(), axis.title.x = element_text()) +
geom_vline(xintercept = round(mean(airbnb_LA$log_price), 2), size = 2, linetype = 3)
```
NOTE: 0.1 unit change in log(x) is equivalent to 10% increase in X.
```{r, warning=FALSE}
# Log_Price vs Minimum Nights
rew1 <- ggplot(airbnb_LA, aes(minimum_nights, log_price)) +
th +
theme(axis.title = element_text(), axis.title.x = element_text()) +
geom_point(aes(color = room_type), alpha = 1) +
geom_smooth(method = "lm") +
xlab("Minimum nights") +
ylab("Log Price") +
ggtitle("Relationship between price vs minimum nights")
nightstay_list <- airbnb_LA %>%
select(minimum_nights) %>%
dplyr::group_by(minimum_nights) %>%
dplyr::summarise(n_count = n()) %>%
dplyr::mutate(n_count_per = n_count * 100 / sum(n_count)) %>%
arrange(desc(minimum_nights))
print(nightstay_list)
fit4 <- lm(log_price ~ minimum_nights, data = airbnb_LA)
summary(fit4)
airbnb_LA.2 <- airbnb_LA %>% filter(minimum_nights <= 31)
# summary(airbnb_LA.2)
fit4.1 <- lm(log_price ~ minimum_nights, data = airbnb_LA.2)
fit4.2 <- lm(log_price ~ sqrt(minimum_nights), data = airbnb_LA)
summary(fit4)
summary(fit4.1)
summary(fit4.2)
hist(fit4.1$residuals)
hist(fit4.2$residuals)
# Square root Transform of "minimum_nights"
airbnb_LA <- airbnb_LA.2 %>%
mutate(sqrt.min.nights = sqrt(minimum_nights))
# summary(airbnb_LA)
# Log_Price vs sqrt.Minimum Nights
rew <- ggplot(airbnb_LA, aes(sqrt.min.nights, log_price)) +
th +
theme(axis.title = element_text(), axis.title.x = element_text()) +
geom_point(aes(color = room_type), alpha = 1) +
geom_smooth(method = "lm") +
xlab("Sqrt of Minimum nights") +
ylab("Log Price") +
ggtitle("Relationship between price vs minimum nights")
ggarrange(rew1, rew, nrow = 2, ncol = 1)
```
```{r}
# Price vs Host_listings_count
ggplot(airbnb_LA, aes(calculated_host_listings_count, log_price)) +
th +
geom_point(alpha = 0.5, aes(color = neighbourhood_group)) +
geom_smooth(method = "lm") +
xlab("calculated_host_listings_count") +
ylab("Log Price") +
ggtitle("Relationship between price and calculated_host_listings_count")
# Need to filter calculated_host_listings_count
hostlist_list <- airbnb_LA %>%
select(calculated_host_listings_count) %>%
dplyr::group_by(calculated_host_listings_count) %>%
dplyr::summarise(n_count = n()) %>%
dplyr::mutate(n_count_per = n_count * 100 / sum(n_count)) %>%
arrange(desc(calculated_host_listings_count))
print(hostlist_list)
# View host_listing more than 152
airbnb_LA %>% filter(calculated_host_listings_count == 152)
```
Host Name: Oranj Palm & Catalina Island
Listed Airbnb Location: Other Cities and Unincorporated Areas
so, we can delete the data because it does not belong to Airbnb.
```{r}
fit7 <- lm(log_price ~ calculated_host_listings_count , data = airbnb_LA)
summary(fit7)
hist(fit7$residuals)
# Remove the list from the data
airbnb_LA <- airbnb_LA %>%
filter(calculated_host_listings_count != 152)
# summary(airbnb_LA)
fit7.1 <- lm(log_price ~ calculated_host_listings_count , data = airbnb_LA)
summary(fit7.1)
hist(fit7.1$residuals)
```
```{r}
# Price vs Availability
ggplot(airbnb_LA, aes(availability_365, log_price)) +
th +
geom_point(alpha = 0.5, aes(color = neighbourhood_group)) +
geom_smooth(method = "lm") +
xlab("Availability during year") +
ylab("Log Price") +
ggtitle("Relationship between price and availability")
available_list <- airbnb_LA %>%
select(availability_365) %>%
dplyr::group_by(availability_365) %>%
dplyr::summarise(n_count = n()) %>%
dplyr::mutate(n_count_per = n_count * 100 / sum(n_count)) %>%
arrange(desc(n_count))
print(available_list)
airbnb_LA %>% filter(availability_365 == 0)
# most busiest airbnb hosts seem fully reserved earlier
# Location: mostly City of LA
airbnb_LA <- airbnb_LA %>%
filter(availability_365>0)
summary(airbnb_LA)
fit8 <- lm(log_price ~ availability_365 , data = airbnb_LA)
summary(fit8)
hist(fit8$residuals)
ggplot(airbnb_LA, aes(availability_365, log_price)) +
th +
geom_point(alpha = 0.5, aes(color = neighbourhood_group)) +
geom_smooth(method = "lm") +
xlab("Availability during year") +
ylab("Log Price") +
ggtitle("Relationship between price and availability")
```
```{r, warning=FALSE, echo=FALSE}
# Split 'price' into 3 parts (Low, Medium, High)
data_temp <- sort.int(airbnb_LA$price, decreasing = FALSE)
group_1 <- data_temp[round(length(data_temp)/3, digits = 0)]
group_2 <- data_temp[2*round(length(data_temp)/3, digits = 0)]
airbnb_LA$price_group[airbnb_LA$price <= group_1] <- "Low"
airbnb_LA$price_group[airbnb_LA$price > group_1 & airbnb_LA$price <= group_2] <- "Medium"
airbnb_LA$price_group[airbnb_LA$price > group_2] <- "High"
airbnb_LA$price_group <- factor(airbnb_LA$price_group, levels = c("Low", "Medium", "High"))
ggplot(subset(airbnb_LA, price < 1000), aes(price)) +
geom_density(aes(fill = factor(`price_group`)), alpha = 0.5) +
ggtitle("Listing population density by Price")
# Split the number of review into 5 parts (LR,MR,HR)
airbnb_LA <- airbnb_LA %>%
mutate(num.review.level = ifelse(number_of_reviews <= 10, "LR",
ifelse(number_of_reviews <= 70, "MR", "HR")))
airbnb_LA$num.review.level <- factor(airbnb_LA$num.review.level, levels = c("LR", "MR", "HR"))
ggplot(subset(airbnb_LA, number_of_reviews < 500), aes(number_of_reviews)) +
geom_density(aes(fill = factor(`num.review.level`)), alpha = 0.5) +
ggtitle("Listing population density by Number of Reviews")
# Split the review_per_month into 4 parts (LRM, MRM, HRM)
airbnb_LA <- airbnb_LA %>%
mutate(review.per.month = ifelse(reviews_per_month <= 0.4, "LRM",
ifelse(reviews_per_month <= 3, "MRM", "HRM")))
airbnb_LA$review.per.month <- factor(airbnb_LA$review.per.month, levels = c("LRM", "MRM", "HRM"))
ggplot(subset(airbnb_LA, reviews_per_month < 500), aes(reviews_per_month)) +
geom_density(aes(fill = factor(`review.per.month`)), alpha = 0.5) +
ggtitle("Listing population density by Reviews per month")
summary(airbnb_LA)
```
```{r, warning=FALSE}
levels(airbnb_LA$price_group)
levels(airbnb_LA$review.per.month)
levels(airbnb_LA$num.review.level)
levels(airbnb_LA$room_type)
levels(airbnb_LA$neighbourhood_group)
```
```{r, warning=FALSE}
airbnb_LA$neighbourhood_group <- factor(airbnb_LA$neighbourhood_group, levels = c("City of Los Angeles","Other Cities","Unincorporated Areas"))
# Log_Price vs Neighbourhood Group (BOX PLOT)
ggplot(airbnb_LA, aes(x = neighbourhood_group, y = log_price)) +
geom_boxplot(aes(fill = factor(`neighbourhood_group`))) +
th +
xlab("Neighbourhood Group") +
ylab("Log Price") +
ggtitle("Boxplots of price by Neighbourhood Group-Airbnb_LA") +
geom_hline(yintercept = mean(airbnb_LA$log_price), color = "purple", linetype = 2)
# BAR PLOT
ggplot(airbnb_LA, aes(neighbourhood_group)) +
geom_bar(aes(fill = price_group)) +
xlab("Neighbourhood Group") +
ylab("Count") +
ggtitle("Distribution of Neighbourhood")
neighbour_list <- airbnb_LA %>%
select(neighbourhood_group) %>%
dplyr::group_by(neighbourhood_group) %>%
dplyr::summarise(n_count = n()) %>%
dplyr::mutate(n_count_per = n_count * 100 / sum(n_count)) %>%
arrange(desc(n_count))
print(neighbour_list)
fit1 <- lm(log_price ~ neighbourhood_group, data = airbnb_LA)
summary(fit1)
hist(fit1$residuals)
```
```{r, warning=FALSE}
# Log_Price vs Room type (BOX PLOT)
ggplot(airbnb_LA, aes(x = room_type, y = log_price)) +
geom_boxplot(aes(fill = factor(`room_type`))) +
th +
xlab("Room type") +
ylab("Log Price") +
ggtitle("Boxplots of price by room type - Airbnb_LA",
subtitle = "Entire home/apt has the highest avg price") +
geom_hline(yintercept = mean(airbnb_LA$log_price), color = "purple", linetype = 2)
# BAR PLOT
ggplot(airbnb_LA, aes(room_type)) +
geom_bar(aes(fill = price_group)) +
xlab("Room Type") +
ylab("Count") +
ggtitle("Distribution of Room Type")
roomtype_list <- airbnb_LA %>%
select(room_type) %>%
dplyr::group_by(room_type) %>%
dplyr::summarise(n_count = n()) %>%
dplyr::mutate(n_count_per = n_count * 100 / sum(n_count)) %>%
arrange(desc(n_count))
print(roomtype_list)
fit3 <- lm(log_price ~ room_type, data = airbnb_LA)
summary(fit3)
hist(fit3$residuals)
```
```{r}
# Price vs Number of Reviews
ggplot(airbnb_LA, aes(number_of_reviews, log_price)) +
th +
theme(axis.title = element_text(), axis.title.x = element_text()) +
geom_point(aes(color = factor(`price_group`)), alpha = 1) +
geom_smooth(method = "lm")+
xlab("Number of reviews") +
ylab("Log Price") +
ggtitle("Relationship between number of reviews vs price")
# Bar plot
review1 <- ggplot(airbnb_LA, aes(num.review.level)) +
geom_bar(aes(fill = factor(`room_type`))) +
xlab("Number of Review Level") +
ylab("Count") +
ggtitle("Distribution of Review")
review1
fit5 <- lm(log_price ~ num.review.level, data = airbnb_LA)
summary(fit5)
hist(fit5$residuals)
```
```{r}
# Price VS Reviews_per_month
ggplot(airbnb_LA, aes(reviews_per_month, log_price)) +
th +
geom_point(alpha = 0.5, aes(color = neighbourhood_group)) +
geom_smooth(method = "lm") +
xlab("reviews_per_month") +
ylab("Price") +
ggtitle("Relationship between price and reviews_per_month")
reviewmonth_list <- airbnb_LA %>%
select(reviews_per_month) %>%
dplyr::group_by(reviews_per_month) %>%
dplyr::summarise(n_count = n()) %>%
dplyr::mutate(n_count_per = n_count * 100 / sum(n_count)) %>%
arrange(desc(n_count))
print(reviewmonth_list)
# Bar plot
review<- ggplot(airbnb_LA, aes(review.per.month)) +
geom_bar(aes(fill = factor(`room_type`))) +
xlab("Number of Reviews per month Listing was Active") +
ylab("Count") +
ggtitle("Distribution of Review Per Month")
fit6 <- lm(log_price ~ review.per.month, data = airbnb_LA)
summary(fit6)
hist(fit6$residuals)
ggarrange(review1, review, nrow = 2, ncol = 1)
```
# Finalizing all variables
```{r}
airbnb_LA <- airbnb_LA %>%
mutate(host.list.count = calculated_host_listings_count)
# summary(airbnb_LA)
LA.Dataset <- dplyr::select(airbnb_LA, -name, -host_id, -host_name, -neighbourhood, -latitude, -longitude, -price, -minimum_nights, -number_of_reviews, -reviews_per_month, -calculated_host_listings_count)
summary(LA.Dataset)
#View(LA.Dataset)
```
## Part 2: Machine Learning
# Split the dataset into train/test (70:30)
```{r}
LA.Dataset.1 <- dplyr::select(LA.Dataset, -price_group)
# summary(LA.Dataset.1)
# Split into train/test data (70:30)
LA.Dataset.1 <- mutate(LA.Dataset.1, id = row_number())
la.train <- sample_frac(LA.Dataset.1, .7)
la.test <- anti_join(LA.Dataset.1, la.train, by = 'id')
la.train <- dplyr::select(la.train, -id)
la.test <- dplyr::select(la.test, -id)
```
# Forward Backward Elimination Method
```{r}
## ON TRAIN DATASET
model_base.train <- lm(log_price ~ 1, data = la.train)
model_full.train <- lm(log_price ~ ., data = la.train)
# Forward selection
stepAIC(model_base.train, scope = list(upper = model_full.train, lower = model_base.train), direction = "forward", trace = FALSE)$anova
# Backward elimination
stepAIC(model_full.train, direction = "backward", trace = FALSE)$anova
# Mixed Selection starting with base model
stepAIC(model_base.train, scope = list(upper = model_full.train, lower = model_base.train), direction = "both", trace = FALSE)$anova
# Mixed Selection starting with full model
stepAIC(model_full.train, scope = list(upper = model_full.train, lower = model_base.train), direction = "both", trace = FALSE)$anova
```
Final Model of Train Set from stepAIC:
log_price ~ neighbourhood_group + room_type + availability_365 + sqrt.min.nights + num.review.level + review.per.month
# Mutliple Linear Regression
```{r}
set.seed(432)
reduced_model.train <- lm(log_price ~ neighbourhood_group + room_type + availability_365 + num.review.level + review.per.month + sqrt.min.nights + host.list.count,
data = la.train)
summary(reduced_model.train)
print(sprintf("Multiple R-Squared of Reduced Model of Train Set = %0.4f", (summary(reduced_model.train)$r.squared)))
print(sprintf("Adjusted R-Squared of Reduced Model of Train Set = %0.4f", (summary(reduced_model.train)$adj.r.squared)))
print(sprintf("MSE of Reduced Model of Train Set = %0.4f", anova(reduced_model.train)['Residuals', 'Mean Sq']))
# Testing accuracy
lr.pred.test <- predict(reduced_model.train, la.test)
mlr.test.mse <- mean((lr.pred.test - la.test$log_price)^2)
mlr.test.mse
```
"Multiple R-Squared of Reduced Model of Train Set = 0.4477"
"Adjusted R-Squared of Reduced Model of Train Set = 0.4474"
"MSE of Reduced Model of Train Set = 0.3330"
Test Error Rate: 33.87%
# Decision Tree
Variables needed for decision tree
- neighbourhood_group
- room_type
- availability_365
- price_group
- sqrt.min.nights
- num.review.level
- review.per.month
- host.list.count
```{r, warning=FALSE}
LA.Dataset.2 <- dplyr::select(LA.Dataset, -log_price)
# Split into train/test data
set.seed(54321)
LA.Dataset.2 <- mutate(LA.Dataset.2, id = row_number())
la.train.DT <- sample_frac(LA.Dataset.2, .7)
la.test.DT <- anti_join(LA.Dataset.2, la.train.DT, by = 'id')
la.train.DT <- dplyr::select(la.train.DT, -id)
la.test.DT <- dplyr::select(la.test.DT, -id)
# With train data set
fit.DT <- rpart(price_group ~ neighbourhood_group + room_type + sqrt.min.nights + num.review.level + availability_365 + review.per.month,
data = la.train.DT)
summary(fit.DT)
rpart.plot(fit.DT, type = 4, extra = "auto", nn = TRUE)
# party package better plotting
rparty.tree <- as.party(fit.DT)
rparty.tree
plot(rparty.tree)
# Test Accuracy
DT.pred.test <- predict(fit.DT, newdata = la.test.DT, type = 'class')
# Computing Overall Error
dt.test.mse <- mean(DT.pred.test != la.test.DT$price_group)
dt.test.mse
# TEST Error rate: 37.52 %
DT_accuracy <- confusionMatrix(DT.pred.test, la.test.DT$price_group)
DT_accuracy
# Accuracy : 0.6247
```
# Naive Bayes
```{r, warning=FALSE}
## Using caret package
set.seed(2132)
la.naive.model <- train(x = la.train.DT[-5],
y = la.train.DT$price_group,
'nb',
trControl = trainControl(method = 'cv', number = 10))
la.naive.model
# Prediction
la.naive.test <- predict(la.naive.model$finalModel, la.test.DT)
# Computing Overall Error
nb.test.mse <- mean(la.naive.test$class != la.test.DT$price_group)
nb.test.mse
# TEST ERROR RATE: 36.63 %
NB_accuracy <- confusionMatrix(la.naive.test$class, la.test.DT$price_group)
NB_accuracy
# Accuracy : 0.6336
```
# Random Forest
```{r}
set.seed(543)
rf_airbnb <- randomForest(x = la.train.DT[-5],
y = la.train.DT$price_group,
ntree = 500,
random_state = 0)
rf_airbnb
# OOB estimate of error rate: 35.8 %
plot(rf_airbnb)
varImpPlot(rf_airbnb)
# Generate predictions
# Test Accuracy
rf.pred.test <- predict(rf_airbnb, newdata = la.test.DT)
rf.test.mse <- mean(rf.pred.test != la.test.DT$price_group)
rf.test.mse
# Test Error Rate: 35.40 %
rf_accuracy <- confusionMatrix(rf.pred.test, la.test.DT$price_group)
rf_accuracy
# Accuracy : 0.6459
```
Note: Bagging is simply a special case of a random forest with m = p.
# Bagging
```{r}
set.seed(193)
bag.la <- randomForest(price_group ~ neighbourhood_group + room_type + sqrt.min.nights + num.review.level + availability_365 + review.per.month + host.list.count,
data = la.train.DT,
mtry = 7,
importance = TRUE)
bag.la
# OOB estimate of error rate: 40.03%
varImpPlot(bag.la)
# Prediction
yhat.bag <- predict(bag.la, newdata = la.test.DT)
bag.test.mse_6 <- mean(yhat.bag != la.test.DT$price_group)
bag.test.mse_6
# Test MSE of Bagging: 40.17113
plot(yhat.bag, la.test.DT$price_group)
bagging_accuracy_6 <- confusionMatrix(yhat.bag, la.test.DT$price_group)
bagging_accuracy_6
# Accuracy : 0.5983
## Using 5 predictors
set.seed(5433)
bag.la.5 <- randomForest(price_group ~ neighbourhood_group + room_type + sqrt.min.nights + num.review.level + availability_365 + review.per.month + host.list.count,
data = la.train.DT,
mtry = 5,
importance = TRUE)
bag.la.5
# OOB estimate of error rate: 39.04%
varImpPlot(bag.la.5)
# Prediction
yhat.bag.5 <- predict(bag.la.5, newdata = la.test.DT)
bag.test.mse.5 <- mean(yhat.bag.5 != la.test.DT$price_group)
bag.test.mse.5
# Test MSE of Bagging: 38.73002
plot(yhat.bag.5, la.test.DT$price_group)
bagging_accurcy_5 <- confusionMatrix(yhat.bag.5, la.test.DT$price_group)
bagging_accurcy_5
# Accuracy : 0.6127
## Using sqrt(p)
set.seed(734)
bag.la.sqrt <- randomForest(price_group ~ neighbourhood_group + room_type + sqrt.min.nights + num.review.level + availability_365 + review.per.month,
data = la.train.DT,
mtry = sqrt(7),
importance = TRUE)
bag.la.sqrt
# OOB estimate of error rate: 37.42%
# Prediction
yhat.bag.sqrt <- predict(bag.la.sqrt, newdata = la.test.DT)
bag.test.mse.sq <- mean(yhat.bag.sqrt != la.test.DT$price_group)
bag.test.mse.sq
# Test MSE of Bagging: 36.88%
plot(yhat.bag.sqrt, la.test.DT$price_group)
# Test Accuracy
bagging_accurcy_sqrt <- confusionMatrix(yhat.bag.sqrt, la.test.DT$price_group)
bagging_accurcy_sqrt
# Accuracy : 0.6312
#Using the `importance()` function, we can view the importance of each variable:
importance(bag.la.sqrt)
varImpPlot(bag.la.sqrt)
```
From varImpPlot() function ,we find the important variables: room_type and availability_365
# LDA
```{r}
set.seed(9545)
model_lda <- lda(price_group ~ neighbourhood_group + room_type + sqrt.min.nights + num.review.level + availability_365 + review.per.month + host.list.count,
data = la.train.DT)
model_lda
# Testing accuracy
lda.pred.test <- predict(model_lda, la.test.DT)
lda_accuracy <- confusionMatrix(lda.pred.test$class, la.test.DT$price_group)
lda_accuracy
# Accuracy : 0.6273
lda.test.mse <- mean(lda.pred.test$class != la.test.DT$price_group)
lda.test.mse
# 37.26638
```
# QDA
```{r}
model_qda <- qda(price_group ~ neighbourhood_group + room_type + sqrt.min.nights + num.review.level + availability_365+ review.per.month + host.list.count,
data = la.train.DT)
model_qda
# Testing accuracy
qda.pred.test <- predict(model_qda, la.test.DT)
qda_accuracy <- confusionMatrix(qda.pred.test$class, la.test.DT$price_group)
qda_accuracy
# Accuracy : 0.5159
qda.test.mse <- mean(qda.pred.test$class != la.test.DT$price_group)
qda.test.mse
# 0.4841252
```
# kNN
```{r}
LA.Dataset.3 <- dplyr::select(LA.Dataset, -log_price)
# summary(LA.Dataset.3)
# Split into train/test data (70:30)
LA.Dataset.3 <- mutate(LA.Dataset.3, id = row_number())
la.train.1 <- sample_frac(LA.Dataset.3, .7)
la.test.1 <- anti_join(LA.Dataset.3, la.train.1, by = 'id')
la.train.1 <- dplyr::select(la.train.1, -id)
la.test.1 <- dplyr::select(la.test.1, -id)
# summary(la.test.1)
la.train.knn <- data.frame(as.numeric(la.train.1$neighbourhood_group),
as.numeric(la.train.1$room_type),
as.numeric(la.train.1$availability_365),
as.numeric(la.train.1$sqrt.min.nights),
as.numeric(la.train.1$num.review.level),
as.numeric(la.train.1$review.per.month),
as.numeric(la.train.1$host.list.count))
la.test.knn <- data.frame(as.numeric(la.test.1$neighbourhood_group),
as.numeric(la.test.1$room_type),
as.numeric(la.test.1$availability_365),
as.numeric(la.test.1$sqrt.min.nights),
as.numeric(la.test.1$num.review.level),
as.numeric(la.test.1$review.per.month),
as.numeric(la.test.1$host.list.count))
set.seed(1212)
# prediction k = 8
knn_predict <- knn(la.train.knn, la.test.knn, la.train.1$price_group, k = 8, prob = TRUE)
knn_Accuracy <- confusionMatrix(la.test.1$price_group, knn_predict)
knn_Accuracy
# 0.5383
knn.test.mse <- mean(knn_predict != la.test.1$price_group)
knn.test.mse
# Test error rate of KNN: 46.17%
```
# Accuracies Performance Comparison
```{r}
DT_Accuracy <- DT_accuracy$overall[1]
NB_Accuracy <- NB_accuracy$overall[1]
RF_Accuracy <- rf_accuracy$overall[1]
Bagging_Accuracy <- bagging_accurcy_sqrt$overall[1]
LDA_Accuracy <- lda_accuracy$overall[1]
QDA_Accuracy <- qda_accuracy$overall[1]
kNN_Accuracy <- knn_Accuracy$overall[1]
Accuracy_rate <- data.frame(Model = c("Decision Tree", "Naive Bayes", "Random Forest", "Bagging", "Linear Discriminant Analysis", "Quadratic Discriminant Analysis", "k-nearest Neighbour"),
Accuracy = c(DT_Accuracy, NB_Accuracy, RF_Accuracy, Bagging_Accuracy, LDA_Accuracy, QDA_Accuracy, kNN_Accuracy))
Accuracy_rate$Model <- factor(Accuracy_rate$Model,
levels = c("Quadratic Discriminant Analysis", "k-nearest Neighbour", "Decision Tree","Linear Discriminant Analysis", "Bagging", "Naive Bayes", "Random Forest"))
ggplot(data = Accuracy_rate, aes(x = Model, y = Accuracy, fill= Model)) +
geom_bar(stat = 'identity') +
theme_bw() +
ggtitle('Accuracies of Models') +
xlab("Models") +
ylab("Accuracy") +
coord_flip()
```
# Comparing Test MSE
```{r}
MLR_mse <- mlr.test.mse
DT_mse <- dt.test.mse
NB_mse <- nb.test.mse
RF_mse <- rf.test.mse
Bagging_mse <- bag.test.mse.sq
LDA_mse <- lda.test.mse
QDA_mse <- qda.test.mse
kNN_mse <- knn.test.mse
Test_MSE <- data.frame(Model = c("Decision Tree", "Naive Bayes", "Random Forest", "Bagging", "Linear Discriminant Analysis", "Quadratic Discriminant Analysis", "k-nearest Neighbour", "Multiple Linear Regression"),
test.mse.rate = c(DT_mse, NB_mse, RF_mse, Bagging_mse, LDA_mse, QDA_mse, kNN_mse, MLR_mse))
Test_MSE$Model <- factor(Test_MSE$Model,
levels = c("Quadratic Discriminant Analysis","k-nearest Neighbour","Linear Discriminant Analysis","Decision Tree","Bagging","Naive Bayes","Random Forest","Multiple Linear Regression"))
ggplot(data = Test_MSE, aes(x = Model, y = test.mse.rate, fill= Model)) +
geom_bar(stat = 'identity') +
theme_bw() +
ggtitle('Test MSE of Models') +
xlab("Models") +
ylab("Test MSE rate") +
coord_flip()
```
# Part 3:NLP: Text Analysis
# Unigram Model
# Word Cloud - Los Angeles City
```{r, warning=FALSE}
library(readr)
reviewsDec2018 <- read_csv("/System/Volumes/Data/University of Evansville/SPRING 2020/STAT 493/Airbnb_final project/RCode/Final_Coding_Khadka_files/Datasets/LA_reviewsDec18.csv")
summary(reviewsDec2018)
LA.review <- reviewsDec2018[sample(1:nrow(reviewsDec2018), 1000, replace = FALSE),]
summary(LA.review)
LA_text <- LA.review %>%
select(listing_id, comments)
# str(LA_text)
LA_word <- unnest_tokens(LA_text, word, comments)
LA_word <- LA_word %>%
anti_join(stop_words)
LA_word <- LA_word %>%
filter(word != "apartment", word != "location", word != "stay", word != "host", str_detect(word, "[a-z]"))
LA_word_count <- LA_word %>%
count(word, sort = TRUE)
LA_word_count %>%
with(wordcloud(word, n, max.words = 100, random.order = FALSE, colors = brewer.pal(8, "Dark2")))
```
The most used words in Los Angeles are "clean", "nice", "house", "la" and "comfortable".
# word cloud - Chicago City
```{r, warning=FALSE}
Dec_2018reviews <- read_csv("/System/Volumes/Data/University of Evansville/SPRING 2020/STAT 493/Airbnb_final project/RCode/Final_Coding_Khadka_files/Datasets/Chicago_reviewsDec18.csv")
Chicago.review <- Dec_2018reviews[sample(1:nrow(Dec_2018reviews), 1000, replace = FALSE),]
Chicago_text <- Chicago.review %>%
select(listing_id, comments)
# str(Chicago_text)
Chicago_word <- unnest_tokens(Chicago_text, word, comments)
Chicago_word <- Chicago_word %>%
anti_join(stop_words)
Chicago_word <- Chicago_word %>%
filter(word != "apartment", word != "location", word != "stay", word != "host", str_detect(word, "[a-z]"))
Chicago_word_count <- Chicago_word %>%
count(word, sort = TRUE)
chicago.1 <- Chicago_word_count %>%
with(wordcloud(word, n, max.words = 100, random.order = FALSE, colors = brewer.pal(8, "Dark2")))
```
The most used words in Chicago are "clean", "chicago", "nice", "comfortable", "easy".
# word cloud - New York City
```{r, warning=FALSE}
reviews <- read_csv("/System/Volumes/Data/University of Evansville/SPRING 2020/STAT 493/Airbnb_final project/RCode/Final_Coding_Khadka_files/Datasets/nyc_reviewsDec18.csv")
nyc.review <- reviews[sample(1:nrow(reviews), 1000, replace = FALSE),]
nyc_text <- nyc.review %>%
select(listing_id, comments)
nyc_word <- unnest_tokens(nyc_text, word, comments)
nyc_word <- nyc_word %>%
anti_join(stop_words)
nyc_word <- nyc_word %>%
filter(word != "apartment", word != "location", word != "stay", word != "host", str_detect(word, "[a-z]"))
nyc_word_count <- nyc_word %>%
count(word, sort = TRUE)
nyc_word_count %>%
with(wordcloud(word, n, max.words = 100, random.order = FALSE, colors = brewer.pal(8, "Dark2")))
```
The most used words in New York City are "clean", "subway", "nice", "recommmend".
# Word Cloud - Boston
```{r, warning=FALSE}
reviews <- read_csv("/System/Volumes/Data/University of Evansville/SPRING 2020/STAT 493/Airbnb_final project/RCode/Final_Coding_Khadka_files/Datasets/Boston_reviewsDec18.csv")
boston.review <- reviews[sample(1:nrow(reviews), 1000, replace = FALSE),]
boston_text <- boston.review %>%
select(listing_id, comments)
boston_word <- unnest_tokens(boston_text, word, comments)
boston_word <- boston_word %>%
anti_join(stop_words)
boston_word <- boston_word %>%
filter(word != "apartment", word != "location", word != "stay", word != "host", str_detect(word, "[a-z]"))
boston_word_count <- boston_word %>%
count(word, sort = TRUE)
boston.1 <- boston_word_count %>%
with(wordcloud(word, n, max.words = 100, random.order = FALSE, colors = brewer.pal(8, "Dark2")))
```
The most used words in Boston City are "boston", "clean", "nice", "comfortable", "easy", and "recommend".
# Word Cloud - London
```{r, warning=FALSE}
reviews_1_ <- read_csv("/System/Volumes/Data/University of Evansville/SPRING 2020/STAT 493/Airbnb_final project/RCode/Final_Coding_Khadka_files/Datasets/London_reviewsDec18.csv")
london.review <- reviews_1_[sample(1:nrow(reviews_1_), 1000, replace = FALSE),]
london_text <- london.review %>%
select(listing_id, comments)
london_word <- unnest_tokens(london_text, word, comments)
london_word <- london_word %>%
anti_join(stop_words)
london_word <- london_word %>%
filter(word != "apartment", word != "location", word != "stay", word != "host", str_detect(word, "[a-z]"))
london_word_count <- london_word %>%
count(word, sort = TRUE)
london_word_count %>%
with(wordcloud(word, n, max.words = 100, random.order = FALSE, colors = brewer.pal(8, "Dark2")))
```
The most used words in London are "london", "clean", "nice", "flat", "recommmend", "lovely", "comfortable".
# word cloud - Greater Manchester
```{r, warning=FALSE}
reviews <- read_csv("/System/Volumes/Data/University of Evansville/SPRING 2020/STAT 493/Airbnb_final project/RCode/Final_Coding_Khadka_files/Datasets/Manchester_reviewsDec18.csv")
manchester.review <- reviews[sample(1:nrow(reviews), 1000, replace = FALSE),]
manchester_text <- manchester.review %>%
select(listing_id, comments)
# str(manchester_text)
manchester_word <- unnest_tokens(manchester_text, word, comments)
manchester_word <- manchester_word %>%
anti_join(stop_words)
manchester_word <- manchester_word %>%
filter(word != "apartment", word != "location", word != "stay", word != "host", str_detect(word, "[a-z]"))