-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyelp_business_logistic_regression_R.R
172 lines (127 loc) · 4.68 KB
/
yelp_business_logistic_regression_R.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
### Yelp business logistic regression
### Results:
# Model 2 chosen for the paper due to highest accuracy: 0.347
data.path <- 'C:\\Users\\Elaine\\Documents\\Desk R\\YelpR\\';
data.file <- paste(data.path,'yelp_business_clean_version6.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) )
### LOGISTIC REGRESSION
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
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)
#use caret to get performance metrics
cm <- confusionMatrix(ctable)
print(cm)
# extract F1 score for all classes
f1pca = cm[["byClass"]][ , "F1"] #for multiclass classification problems
#print(mean(f1pca))
#extract precision
precisionpca = cm[["byClass"]][ , "Precision"]
#print(precisionpca)
#print(mean(precisionpca))
#extract RECALL
recallpca = cm[["byClass"]][ , "Recall"]
#print(recallpca)
#print(mean(recallpca))
logisticresults = data.frame(mean_accuracy, f1pca, precisionpca, recallpca )
cat("\n\nLogistic Model Results:\n")
print(round(logisticresults, digits = 3))
write_csv(logisticresults, "~/Desk R/YelpR/logisticmod1.csv")
### LOGISTIC MODEL 2
# Split the data into training and test set
set.seed(1123)
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)
#use caret to get performance metrics
cm <- confusionMatrix(ctable)
print(cm)
#get csv of confusion Matrix table
write.table(ctable, file = "~/Desk R/YelpR/logisticctable2.csv", sep="," )
# extract F1 score for all classes
f1pca = cm[["byClass"]][ , "F1"] #for multiclass classification problems
#print(mean(f1pca))
#extract precision
precisionpca = cm[["byClass"]][ , "Precision"]
#print(precisionpca)
#print(mean(precisionpca))
#extract RECALL
recallpca = cm[["byClass"]][ , "Recall"]
#print(recallpca)
#print(mean(recallpca))
logisticresults = data.frame(mean_accuracy, f1pca, precisionpca, recallpca )
cat("\n\nLogistic Model Results:\n")
print(round(logisticresults, digits = 3))
write_csv(logisticresults, "~/Desk R/YelpR/logisticmod2.csv")
### LOGISTIC MODEL 3
# Split the data into training and test set
set.seed(5523)
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)
#use caret to get performance metrics
cm <- confusionMatrix(ctable)
print(cm)
# extract F1 score for all classes
f1pca = cm[["byClass"]][ , "F1"] #for multiclass classification problems
#print(mean(f1pca))
#extract precision
precisionpca = cm[["byClass"]][ , "Precision"]
#print(precisionpca)
#print(mean(precisionpca))
#extract RECALL
recallpca = cm[["byClass"]][ , "Recall"]
#print(recallpca)
#print(mean(recallpca))
logisticresults = data.frame(mean_accuracy, f1pca, precisionpca, recallpca )
cat("\n\nLogistic Model Results:\n")
print(round(logisticresults, digits = 3))
write_csv(logisticresults, "~/Desk R/YelpR/logisticmod3.csv")