-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yelp_business_logistic_stlouis.R
196 lines (134 loc) · 5.56 KB
/
Yelp_business_logistic_stlouis.R
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
### Yelp Logistic Regression St. Louis Business data
### Status: 3 folds of logistic regression are complete, need to calculate
# weighted avg. metrics to determine the best results to include
data.path <- 'C:\\Users\\Elaine\\Documents\\Desk R\\YelpR\\';
data.file <- paste(data.path,'df_stlouis.csv',sep='');
df = read.csv(data.file,header=TRUE);
options(max.print = 100000)
# drop rows to match linear regression 1, keep stars in the df
df2 = subset(df, select = -c(business_id,name,address,city,state,postal_code,latitude,
longitude,attributes,categories,BusinessParking,market,
Alcohol_None, BYOBCorkage_yes_free, NoiseLevel_average,
NoiseLevel_quiet, Smoking_yes, WiFi_no, Nightlife,
Bars, Sandwiches) )
library(nnet) #for multinomial logistic regression
library(tidyverse)
library(caret)
### LOGISTIC MODEL 1
# Split the data into training and test set
set.seed(123)
training.samples <- df2$stars %>%
createDataPartition(p = 0.8, list = FALSE)
train.data <- df2[training.samples, ]
test.data <- df2[-training.samples, ]
# Fit the model
logregmodel <- nnet::multinom(stars ~., data = train.data)
# Summarize the model
summary(logregmodel)
# Make predictions
options(dplyr.print_max = 1e9)
#check pvalue of logistic regression result
library(broom)
tidy(logregmodel, conf.int= TRUE)
# all pvalues <0.05
# Make predictions
predicted.classes <- logregmodel %>% predict(test.data)
head(predicted.classes)
# Model accuracy
mean_accuracy <- mean(predicted.classes == test.data$stars)
# Building classification table
ctable <- table(test.data$stars, predicted.classes)
#get csv of confusion Matrix table
write.table(ctable, file = "~/Desk R/YelpR/stllogisticctable1.csv", sep="," )
#use caret to get performance metrics
cm <- confusionMatrix(ctable)
print(cm)
# extract F1 score for all classes
f1pca = cm[["byClass"]][ , "F1"]
print(f1pca)
#extract precision
precisionpca = cm[["byClass"]][ , "Precision"]
#extract RECALL
recallpca = cm[["byClass"]][ , "Recall"]
logisticresults = data.frame(mean_accuracy, f1pca, precisionpca, recallpca )
write_csv(logisticresults, "~/Desk R/YelpR/stllogisticmod1.csv")
# Variable Importance Model 1
imp <- varImp(logregmodel, scale = FALSE)
logmod1importance <- data.frame(overall = imp$Overall,
names = rownames(imp))
logmod1importance[order(logmod1importance$overall,decreasing = T),]
write_csv(logmod1importance, "~/Desk R/YelpR/logisticmod1import.csv")
### ST LOUIS LOGISTIC MODEL 2
set.seed(608)
training.samples <- df2$stars %>%
createDataPartition(p = 0.8, list = FALSE)
train.data <- df2[training.samples, ]
test.data <- df2[-training.samples, ]
# Fit the model
logregmodel <- nnet::multinom(stars ~., data = train.data)
# Summarize the model
summary(logregmodel)
# Make predictions
predicted.classes <- logregmodel %>% predict(test.data)
head(predicted.classes)
# Model accuracy
mean_accuracy <- mean(predicted.classes == test.data$stars)
# Building classification table
ctable <- table(test.data$stars, predicted.classes)
#get csv of confusion Matrix table
write.table(ctable, file = "~/Desk R/YelpR/stllogisticctable2.csv", sep="," )
#use caret to get performance metrics
cm <- confusionMatrix(ctable)
print(cm)
# extract F1 score for all classes
f1pca = cm[["byClass"]][ , "F1"]
print(f1pca)
#extract precision
precisionpca = cm[["byClass"]][ , "Precision"]
#extract RECALL
recallpca = cm[["byClass"]][ , "Recall"]
logisticresults = data.frame(mean_accuracy, f1pca, precisionpca, recallpca )
write_csv(logisticresults, "~/Desk R/YelpR/stllogisticmod2.csv")
# Variable Importance Model 2
imp <- varImp(logregmodel, scale = FALSE)
logmodimportance <- data.frame(overall = imp$Overall,
names = rownames(imp))
logmodimportance[order(logmodimportance$overall,decreasing = T),]
write_csv(logmodimportance, "~/Desk R/YelpR/stlouislogisticmod2import.csv")
### ST LOUIS LOGISTIC MODEL 3
set.seed(1987)
training.samples <- df2$stars %>%
createDataPartition(p = 0.8, list = FALSE)
train.data <- df2[training.samples, ]
test.data <- df2[-training.samples, ]
# Fit the model
logregmodel <- nnet::multinom(stars ~., data = train.data)
# Summarize the model
summary(logregmodel)
# Make predictions
predicted.classes <- logregmodel %>% predict(test.data)
head(predicted.classes)
# Model accuracy
mean_accuracy <- mean(predicted.classes == test.data$stars)
# Building classification table
ctable <- table(test.data$stars, predicted.classes)
#get csv of confusion Matrix table
write.table(ctable, file = "~/Desk R/YelpR/stllogisticctable3.csv", sep="," )
#use caret to get performance metrics
cm <- confusionMatrix(ctable)
print(cm)
# extract F1 score for all classes
f1pca = cm[["byClass"]][ , "F1"]
print(f1pca)
#extract precision
precisionpca = cm[["byClass"]][ , "Precision"]
#extract RECALL
recallpca = cm[["byClass"]][ , "Recall"]
logisticresults = data.frame(mean_accuracy, f1pca, precisionpca, recallpca )
write_csv(logisticresults, "~/Desk R/YelpR/stllogisticmod3.csv")
# Variable Importance Model 3
imp <- varImp(logregmodel, scale = FALSE)
logmodimportance <- data.frame(overall = imp$Overall,
names = rownames(imp))
logmodimportance[order(logmodimportance$overall,decreasing = T),]
write_csv(logmodimportance, "~/Desk R/YelpR/stlouislogisticmod3import.csv")