ETC1010 Assignment B
Kien Do 36082376
B-1 Sentiment analysis of reviews
1) Table of sentiment_score for each review_id
# A tibble: 166,793 x 2
review_id
sentiment_score
<chr>
<dbl>
1 ---v3SgPiNYCHZa2f64OHA
7
2 --2z5bwg3PPrWnr9wI84cA
5
3 --3OMvHzIVQn2_lVw6Y2eQ
3
4 --3QROksQn3nYqrKI1oNzw
8
5 --4LY9ofH7f6_SJyNShZsA
-1
6 --7XOV5T9yZR5w1DIy_Dog
2
7 --7vh7hCQSEyf6RansfVrQ
4
8 --9cDt8EhiPsDnJbLju4xw
13
9 --AAK3WP7h3v_DIfv80TWw
0
10 --C30v9RQbI1-VvTrx87Qg
6
# i 166,783 more rows
2) Justify the design
Bing Lexicon is an English lexicon that categorises words in a binary fashion, either positive
or negative, which is then used to perform sentiment analysis. dplyr package in R are used
to perform further data analsis .Firstly, the function get_sentiments('bing') is used to
get the Bing lexicon. Next, unnest_tokens function is used to create a new “word” column
that consists of one word used in each review per row. Then, join the data frame from
get_sentiments('bing') to the data frame from unnest_tokens by the “word” column for
a new data frame. This data frame will have the word used in each review and what Bing
Lexicon for that word is (positive or negative). Next, use this dataframe and create a new
column sentiment_value that if the sentiment is positive, give it a 1 or a -1 otherwise.
Finally, group the data by review_id, create the sentiment_score column that is the sum of
1
the sentiment_value of each review and summarise it. The result will be what is produced
above, where each review_id is assigned a sentiment_score.
3) Visualise sentiment_score against review stars rating
Distribution of Sentiment Score per Stars Level
Using Bing Lexicon
1
2
−25 0 25 50
−25 0 25 50
3
4
5
−25 0 25 50
−25 0 25 50
−25 0 25 50
Count
9000
6000
3000
0
Sentiment Score
4) Compute Pearson correlation
cor(sentiment_score_star$sentiment_score,sentiment_score_star$stars, method = 'pearson')
[1] 0.4821291
B-2 Clustering with selected states
1) Filter data out to four selected states ‘FL’, ‘PA’, ‘CA’, ‘NV’
# A tibble: 6 x 22
# Groups:
state [3]
business_id
name city state postal_code latitude longitude stars_business
<chr>
<chr> <chr> <chr> <chr>
<dbl>
<dbl>
<dbl>
2
1 LMXa9Vi82nHJs~ Oish~ Olds~ FL
34677
28.0
-82.7
4.5
2 U_gjhQ9phwKEq~ Chad~ Chad~ PA
19317
39.9
-75.6
3.5
3 AH5JQnN6mIPee~ Tamp~ Tampa FL
33607
28.0
-82.5
4
28.0
-82.2
4 RGW5lgEBMS2vM~ Sunk~ Dover FL
33527
3.5
5 n6sjUe18SUp1d~ Arti~ Reno NV
89523
39.5
-120.
4
6 mTcPIOzFmsUd0~ Heat~ Doyl~ PA
18901
40.3
-75.1
5
# i 14 more variables: review_count_business <dbl>, is_open <dbl>,
#
categories_raw <chr>, accepts_credit_cards <lgl>, good_for_kids <lgl>,
#
by_appointment_only <lgl>, bike_parking <lgl>, restaurants_takeout <lgl>,
#
restaurants_delivery <lgl>, outdoor_seating <lgl>,
#
restaurants_reservations <lgl>, hours_per_week <dbl>,
#
open_on_weekends <lgl>, n_days_open <dbl>
2) Run k-means on latitude and longtitude ( k =4)
long_lat <- business_state[,c('longitude','latitude')]
set.seed(2025)
longlat_cluster <- kmeans(long_lat,4,nstart = 100)
3) Create scatter plot of businesses colored by cluster (facet by state)
Scatter Plot of K−Means Clustering of Business Locations by State
k=4
CA
FL
NV
PA
40
36
1
2
3
32
4
Longitude
3
−80
−90
−100
−110
−120
−80
−90
−100
−110
−120
−80
−90
−100
−110
−120
−80
−90
−100
−110
28
−120
Latitude
Groups
4) Comment on Scatter Plot
• The cluster only aligns with state boundaries half of the times, with the cluster analysis
correctly detecting restaurants in Florida and Pasadena, groups 3 and 4. However, the
analysis incorrectly identifies California and Nevada, with group 1 being split between
the restaurants in California and Nevada. Hypothetically, if a business falls in another
state cluster, one possible reason could be because of the close geographical location;
the cluster might group that business into another state, which is what is observed in
the graph, where part of California’s restaurants are incorrectly grouped as in Nevada,
because of the close geographical location between these 2 states.
• K-means isn’t an acceptable clustering algorithm here, as it cannot distinguish between
California and Nevada. This is due to the way K-means is calculated, where it leads to a
tednency create circle-ish groups, which in this case incorrectly group Nevada and California together. However, if users only want to distinguish between Florida, Pasadena,
and other states, then K-means could be acceptable here since it has correctly grouped
those states.
Scatter−Plot of K−Means Clustering of Business Locations by State
k=5
CA
FL
NV
PA
40
Latitude
Groups
1
36
2
3
32
4
5
−80
−90
−100
−110
−120
−80
−90
−100
−110
−120
−80
−90
−100
−110
−120
−80
−90
−100
−110
−120
28
Longitude
• when k=5, everything else is the same other than it splits Pasadena’s restaurant into 2
groups (Group 2 and Group 4), making the result even more inaccurate since now the
means only correctly group one out of four states. Thus, I would choose k = 4, since in
that scenario the model correctly predict businesses in 2/4 states, instead of 1/4 in k =
5.
4
B-3 Business characteristics and star ratings
1) Creating binary variable restaurant_flag
Rows: 7239 Columns: 22
-- Column specification -------------------------------------------------------Delimiter: ","
chr (6): business_id, name, city, state, postal_code, categories_raw
dbl (7): latitude, longitude, stars_business, review_count_business, is_open...
lgl (9): accepts_credit_cards, good_for_kids, by_appointment_only, bike_park...
i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
[1] 1 0 0 0 0 0 0 0 1 0
Where 1 means the business is in the category Restaurant
2) Fit Linear Reggresion model predicting stars_business
summary(
lm(
stars_business ~
restaurant_flag +
review_count_business +
accepts_credit_cards +
hours_per_week +
open_on_weekends,
data = business_res
)
)
3) Report 𝑅2 and coefficient
Call:
lm(formula = stars_business ~ restaurant_flag + review_count_business +
accepts_credit_cards + hours_per_week + open_on_weekends,
data = business_res)
5
Residuals:
Min
1Q
-3.0372 -0.5534
Median
0.1323
3Q
0.6633
Max
2.2183
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)
4.318e+00 7.114e-02 60.691 < 2e-16 ***
restaurant_flag
-2.271e-01 2.808e-02 -8.088 7.52e-16 ***
review_count_business
6.849e-04 9.952e-05
6.882 6.62e-12 ***
accepts_credit_cardsTRUE -1.308e-01 6.242e-02 -2.095
0.0362 *
hours_per_week
-7.167e-03 4.023e-04 -17.812 < 2e-16 ***
open_on_weekendsTRUE
2.235e-02 4.288e-02
0.521
0.6023
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.9083 on 5183 degrees of freedom
(2050 observations deleted due to missingness)
Multiple R-squared: 0.08245,
Adjusted R-squared: 0.08157
F-statistic: 93.15 on 5 and 5183 DF, p-value: < 2.2e-16
4) Predicting Hypothetical Business
4.318 + 100 * 6.849e-04 - 1.308e-01 - 7.167e-03 * 40
[1] 3.96901
Thus, the model predict this hypothetical business will get ≈ 4/5 stars
5) Comment on Regression model
Based on the model, review_count_business and open_on_weekends are variables that push
ratings up, while restaurant_flag, accepts_credit_cards and hours_per_week decrease the
rating. One limitation of the model many other variables that are significant but aren’t
accounted for, such as the location of the business or whether or not it is open. This is
shown with the small 𝑅2 of the model, being only at 0.08245, which means only 8.245%
of the variation in stars_business is explained through the 5 variables. A linear regression
model might also not be appropriate to use in this context, since it’s intuitive to think that
review_count_business could cause heterosexuality, since at low review count each review
influences lots into the stars_business variable, which could cause high variance of the error
term when review count is low. Thus, further investigation is needed to ensure the accuracy
of the model.
6
B-4 User analytics: Modelling influence
1) Data Preparation
Warning: There was 1 warning in `mutate()`.
i In argument: `tenure_years = `/`(...)`.
Caused by warning:
! 2 failed to parse.
# A tibble: 131,541 x 6
user_id
avg_review_length review_count fans average_stars tenure_years
<chr>
<dbl>
<dbl> <dbl>
<dbl>
<dbl>
1 ---2PmXbF47D~
120
36
3
4.98
12.7
2 --2vR0DIsmQ6~
183
1534
880
4.18
12.6
44
5
3 --38T5f-O0Hw~
1
0
11.8
4 --4AjktZiHow~
32
57
0
4.07
10.1
5 --5t79VnMbzU~
86
8
0
2.88
9.44
6 --6CxpMhbrry~
53
4
0
2.25
12.0
7 --82s4IumnrL~
447
3
0
1
6.52
8 --E0uVPphTOR~
88
6
0
3.67
7.24
9 --F7iEaFFPO1~
22
10
0
1.92
14.2
10 --G3OI-rV7Vz~
87
2
0
5
8.96
# i 131,531 more rows
2) Create Regression
summary(lm(fans ~ review_count + average_stars +avg_review_length +tenure_years, data = user_
Call:
lm(formula = fans ~ review_count + average_stars + avg_review_length +
tenure_years, data = user_summary)
Residuals:
Min
1Q
-1387.0
-1.5
Median
1.4
3Q
Max
3.0 12242.1
Coefficients:
(Intercept)
Estimate Std. Error t value Pr(>|t|)
-1.5707600 0.7360896 -2.134 0.03285 *
7
review_count
0.1363587 0.0006863 198.679 < 2e-16 ***
average_stars
0.4261643 0.1435407
2.969 0.00299 **
avg_review_length 0.0177186 0.0014996 11.816 < 2e-16 ***
-0.4483189 0.0446366 -10.044 < 2e-16 ***
tenure_years
--Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 47.67 on 131534 degrees of freedom
(2 observations deleted due to missingness)
Multiple R-squared: 0.2416,
Adjusted R-squared: 0.2416
F-statistic: 1.048e+04 on 4 and 131534 DF, p-value: < 2.2e-16
3) Commentary
• The meaning of each coefficient mostly align with the expectations, with the coefficient
sign on review_count, average_stars and avg_review_length being positive, meaning if
any of those variables increases fan count will also increases. However, tenure years did
not align with expectations, since it is believable that higher tenure years can lead to
higher fan because this mean they would have more experience in the field.
• All of the variables are statistically significance at 5%, with all of its p-values being
except for average_stars which have a higher p-value but still below the significance
level.
• One limitation to discussed could be that its believe that the relationship of
avg_review_length might not be linear, since at higher review_length it could be
because lots of people are leaving complaints review which could affect fan variable.
Another problem is outlines, since most people won’t have lots of followers, while some
will have a massive amount, leading to the result being skewed and not representative.
4) Three users with the most fan
# A tibble: 3 x 7
user_id avg_review_length review_count fans average_stars yelping_since
<chr>
<dbl>
<dbl> <dbl>
<dbl> <dttm>
1 37cpUo~
292.
1882 12497
4.39 2009-04-25 14:54:26
2 hizGc5~
223
1825 3642
4.23 2009-05-22 20:19:55
3 Hi10sG~
201.
17473 3493
3.77 2009-05-26 11:33:58
# i 1 more variable: tenure_years <dbl>
8
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )