-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.R
More file actions
240 lines (211 loc) · 5.72 KB
/
Copy pathanalysis.R
File metadata and controls
240 lines (211 loc) · 5.72 KB
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
library(mongolite)
# Store your connection string in an .env or R environment variable
connection_string <- Sys.getenv("MONGODB_URI")
airbnb_collection <- mongo(
collection = "listingsAndReviews",
db = "sample_airbnb",
url = connection_string
)
# Test connection
test <- airbnb_collection$find(limit = 5)
print(test)
#Now I wanna pull a focused dataset
library(mongolite)
library(dplyr)
library(stringr)
airbnb_df <- airbnb_collection$find(
query = '{}',
fields = '{
"name": 1,
"summary": 1,
"description": 1,
"space": 1,
"neighborhood_overview": 1,
"notes": 1,
"transit": 1,
"access": 1,
"interaction": 1,
"house_rules": 1,
"property_type": 1,
"room_type": 1,
"bed_type": 1,
"minimum_nights": 1,
"maximum_nights": 1,
"accommodates": 1,
"bedrooms": 1,
"beds": 1,
"bathrooms": 1,
"number_of_reviews": 1,
"price": 1,
"cleaning_fee": 1,
"extra_people": 1,
"address.market": 1,
"address.country": 1,
"review_scores.review_scores_rating": 1,
"_id": 0
}'
)
glimpse(airbnb_df)
#To clean a bit
# First i´m inspecting the nested columns
names(airbnb_df$address)
names(airbnb_df$review_scores)
head(airbnb_df$address)
head(airbnb_df$review_scores)
#Clean Dataframe
library(dplyr)
airbnb_clean <- airbnb_df %>%
mutate(
minimum_nights = as.numeric(minimum_nights),
maximum_nights = as.numeric(maximum_nights),
market = address$market,
country = address$country,
review_score = review_scores$review_scores_rating
) %>%
select(
name, summary, space, description, neighborhood_overview, notes,
transit, access, interaction, house_rules,
property_type, room_type, bed_type,
minimum_nights, maximum_nights,
accommodates, bedrooms, beds, bathrooms,
number_of_reviews, price, cleaning_fee, extra_people,
market, country, review_score
)
glimpse(airbnb_clean)
colSums(is.na(airbnb_clean))
#Viz 1 -Price Distribution
library(ggplot2)
library(scales)
ggplot(airbnb_clean %>% filter(price < 1000), aes(x = price)) +
geom_histogram(bins = 40) +
scale_x_continuous(labels = dollar_format()) +
labs(
title = "Distribution of Airbnb Listing Prices (Filtered)",
x = "Price",
y = "Number of Listings"
) +
theme_minimal()
#Viz 2- Price by Room type
#used median instead of mean because airbnb prices are skewed, the mean is distored by luxury listings
avg_price_room <- airbnb_clean %>%
group_by(room_type) %>%
summarise(median_price = median(price, na.rm = TRUE))
ggplot(avg_price_room, aes(x = reorder(room_type, median_price), y = median_price)) +
geom_col() +
coord_flip() +
scale_y_continuous(labels = dollar_format()) +
labs(
title = "Median Price by Room Type",
x = "Room Type",
y = "Median Price"
) +
theme_minimal()
#Viz 3 -Top Markets
top_markets <- airbnb_clean %>%
count(market, sort = TRUE) %>%
slice_head(n = 10)
ggplot(top_markets, aes(x = reorder(market, n), y = n)) +
geom_col() +
coord_flip() +
labs(
title = "Top Markets by Number of Listings",
x = "Market",
y = "Listings"
) +
theme_minimal()
#Viz 4- Bedroom vs price
avg_price_bedrooms <- airbnb_clean %>%
filter(price < 1000, bedrooms <= 6) %>%
group_by(bedrooms) %>%
summarise(avg_price = mean(price, na.rm = TRUE))
ggplot(avg_price_bedrooms, aes(x = bedrooms, y = avg_price)) +
geom_line() +
geom_point() +
scale_y_continuous(labels = dollar_format()) +
labs(
title = "Average Price by Bedrooms (Filtered)",
x = "Bedrooms",
y = "Average Price"
) +
theme_minimal()
#double checking its clean
airbnb_text <- airbnb_clean %>%
mutate(
full_text = paste(
name, summary, space, description,
neighborhood_overview, notes,
transit, access, interaction, house_rules,
sep = " "
)
)
#tokenize
library(tidytext)
library(tidyr)
library(stringr)
tidy_words <- airbnb_text %>%
select(room_type, market, full_text) %>%
unnest_tokens(word, full_text)
#cleaning words (removes the, and, is..)
data("stop_words")
tidy_words_clean <- tidy_words %>%
anti_join(stop_words, by = "word") %>%
filter(str_detect(word, "[a-z]")) %>%
filter(nchar(word) > 2)
#WORD FREQUENCY 1
tidy_words_clean <- tidy_words %>%
anti_join(stop_words, by = "word") %>%
filter(str_detect(word, "[a-z]")) %>%
filter(nchar(word) > 2) %>%
filter(!word %in% c("apartment", "room", "bed", "bedroom", "house"))
top_words <- tidy_words_clean %>%
count(word, sort = TRUE)
top_words %>%
slice_head(n = 15) %>%
ggplot(aes(x = reorder(word, n), y = n)) +
geom_col() +
coord_flip() +
labs(
title = "Most Frequent Words in Airbnb Listings",
x = "Word",
y = "Frequency"
) +
theme_minimal()
#SENTIMENT ANALYSIS 2
bing_sentiment <- tidy_words_clean %>%
inner_join(get_sentiments("bing"), by = "word")
sentiment_summary <- bing_sentiment %>%
count(sentiment)
ggplot(sentiment_summary, aes(x = sentiment, y = n, fill = sentiment)) +
geom_col() +
labs(
title = "Sentiment in Airbnb Listings",
x = "Sentiment",
y = "Word Count"
) +
theme_minimal()
#sentiment by roomtype
sentiment_by_room <- bing_sentiment %>%
count(room_type, sentiment)
ggplot(sentiment_by_room, aes(x = sentiment, y = n, fill = sentiment)) +
geom_col() +
facet_wrap(~room_type) +
labs(
title = "Sentiment by Room Type"
) +
theme_minimal()
#TF-IDF 3
tfidf_words %>%
arrange(desc(tf_idf)) %>%
group_by(room_type) %>%
slice_head(n = 10) %>%
ggplot(aes(x = reorder(word, tf_idf), y = tf_idf, fill = room_type)) +
geom_col(show.legend = FALSE) +
facet_wrap(~room_type, scales = "free") +
coord_flip() +
labs(
title = "Top Terms by Room Type (TF-IDF)",
x = "Word",
y = "Importance"
) +
theme_minimal() +
theme(strip.text = element_text(size = 10))