-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analyze_HousingStatisticsDataset.R
293 lines (235 loc) · 11 KB
/
Analyze_HousingStatisticsDataset.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
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
# Load Dataset
accomodation <- read.csv("Data/HousingStatsByTown.csv")
######################################################################
# ENRICH DATA (for analysis)
######################################################################
# Add some variables to reflect the proportion of households for all variables tracked
accomodation$PC_ICTOwnsPC <- accomodation$ICT_OwnsPC / accomodation$AllHouseholds
accomodation$PC_ICTNoPC <- accomodation$ICT_DoesNotOwnPC / accomodation$AllHouseholds
accomodation$PC_ICTBroadband <- accomodation$ICT_BroadBand / accomodation$AllHouseholds
accomodation$PC_ICTNonBBInternet <- accomodation$ICT_InternetNonBroadband / accomodation$AllHouseholds
accomodation$PC_ICTNoInternet <- accomodation$ICT_NoInternet / accomodation$AllHouseholds
accomodation$PC_HeatingCentral <- accomodation$Heating_HasCentral / accomodation$AllHouseholds
accomodation$PC_HeatingNoCentral <- accomodation$Heating_NoCentral / accomodation$AllHouseholds
accomodation$PC_SeweragePublic <- accomodation$Sewerage_Public / accomodation$AllHouseholds
accomodation$PC_SewerageIndSeptic <- accomodation$Sewerage_IndSeptic / accomodation$AllHouseholds
accomodation$PC_SewerageIndNonSeptic <- accomodation$Sewerage_IndNonSeptic / accomodation$AllHouseholds
accomodation$PC_SewerageOther <- accomodation$Sewerage_Other / accomodation$AllHouseholds
accomodation$PC_SewerageNone <- accomodation$Sewerage_None / accomodation$AllHouseholds
accomodation$PC_WaterPublicMains <- accomodation$Water_PublicMains / accomodation$AllHouseholds
accomodation$PC_WaterPublicGroup <- accomodation$Water_PublicGroupScheme / accomodation$AllHouseholds
accomodation$PC_WaterPrivateGroup <- accomodation$Water_PrivateGroupScheme / accomodation$AllHouseholds
accomodation$PC_WaterOtherPrivate <- accomodation$Water_OtherPrivateSource / accomodation$AllHouseholds
accomodation$PC_WaterNoPiped <- accomodation$Water_NoPipedWater / accomodation$AllHouseholds
accomodation$PC_ICTInternet <- accomodation$PC_ICTBroadband + accomodation$PC_ICTNonBBInternet
accomodation$TownSize[accomodation$TotalPopulation < 2000] <- "< 2K"
accomodation$TownSize[accomodation$TotalPopulation >= 2000 &
accomodation$TotalPopulation < 5000] <- "2K-5K"
accomodation$TownSize[accomodation$TotalPopulation >= 5000 &
accomodation$TotalPopulation < 20000] <- "5K-20K"
accomodation$TownSize[accomodation$TotalPopulation > 20000] <- "> 20K"
accomodation$PeoplePerHouse <- accomodation$TotalPopulation / accomodation$AllHouseholds
accomodation$RentIncomeRatio <- accomodation$Avg_Rent / (accomodation$Household_Income / 12)
accomodation$TownSize <- factor(accomodation$TownSize,
levels = c("< 2K", "2K-5K", "5K-20K", "> 20K"))
######################################################################
# DESCRIPTIVE STATISTICS
######################################################################
data_numeric_variables <- sapply(accomodation, is.numeric)
numerical_data <- accomodation[, data_numeric_variables]
numerical_summary <- do.call(cbind, lapply(numerical_data, summary))
numerical_summary
alt_numerical_summary <- do.call(rbind, lapply(numerical_data, summary))
alt_numerical_summary
######################################################################
# REFORMAT DATASET
######################################################################
# Remove Area-local variables, and percentages with low variance
accomodation <- accomodation[, c("Towns.by.Size", "County", "TownSize", "PersonsPerSqKm",
"TotalPopulation", "AllHouseholds", "Household_Income",
"Avg_Rent", "PC_ICTOwnsPC", "PC_ICTInternet",
"PC_HeatingNoCentral", "PC_SeweragePublic",
"PC_SewerageIndSeptic", "RentIncomeRatio", "PeoplePerHouse")]
######################################################################
# DESCRIPTIVE STATISTICS (cotd)
######################################################################
# Largest Rent
accomodation[which.max(accomodation$Avg_Rent),]
# Largest Population
accomodation[which.max(accomodation$TotalPopulation),]
# Highest Income
accomodation[which.max(accomodation$Household_Income),]
# Densest Town
accomodation[which.max(accomodation$PersonsPerSqKm),]
# Lowest Rent
accomodation[which.min(accomodation$Avg_Rent),]
# Lowest Population
accomodation[which.min(accomodation$TotalPopulation),]
# Lowest Income
accomodation[which.min(accomodation$Household_Income),]
# Lowest Density
accomodation[which.min(accomodation$PersonsPerSqKm),]
#install.packages("ggplot2")
#install.packages("viridis")
library(ggplot2)
library(viridis)
# Plot average rent per area, color by population
plot <- ggplot(accomodation, aes(x = TownSize, y = Avg_Rent, color = Household_Income))
plot <- plot + geom_point() + scale_color_viridis()
print((plot))
# Plot average rent per area, color by density
plot <- ggplot(accomodation, aes(x = TownSize, y = Avg_Rent, color = PersonsPerSqKm))
plot <- plot + geom_point() + scale_color_viridis()
print((plot))
# Plot average rent per area, color by density
plot <- ggplot(accomodation, aes(x = TownSize, y = Avg_Rent, color = RentIncomeRatio))
plot <- plot + geom_point() + scale_color_viridis()
print((plot))
# Plot average rent per area, color by overall population
plot <- ggplot(accomodation, aes(x = TownSize, y = Avg_Rent, color = TotalPopulation))
plot <- plot + geom_point() + scale_color_viridis()
print((plot))
# Plot average rent per area, color by people per house
plot <- ggplot(accomodation, aes(x = TownSize, y = Avg_Rent, color = PeoplePerHouse))
plot <- plot + geom_point() + scale_color_viridis()
print((plot))
# Investigate correlation between variables
physical_variables_of_interest <- c("PersonsPerSqKm", "TotalPopulation", "PC_HeatingNoCentral",
"PC_SeweragePublic", "PC_SewerageIndSeptic", "PC_ICTOwnsPC",
"PC_ICTInternet", "Household_Income")
pairs(accomodation[physical_variables_of_interest])
financial_variables_of_interest <- c("PersonsPerSqKm", "TotalPopulation", "AllHouseholds", "PeoplePerHouse",
"Household_Income", "Avg_Rent", "RentIncomeRatio")
pairs(accomodation[financial_variables_of_interest])
colors <- rainbow(length(unique(accomodation$TownSize)))
ggplot(accomodation, aes(x=Avg_Rent, y=Household_Income, color=TownSize)) +
geom_point()
ggplot(accomodation, aes(x=Avg_Rent, y=PersonsPerSqKm, color=TownSize)) +
geom_point()
ggplot(accomodation, aes(x=TownSize, y=Household_Income, color=TownSize)) +
geom_point()
ggplot(accomodation, aes(x=PeoplePerHouse, y=RentIncomeRatio, color=TownSize)) +
geom_point()
ggplot(accomodation, aes(x=PeoplePerHouse, y=Avg_Rent, color=TownSize)) +
geom_point()
######################################################################
# Principal Component Analysis
######################################################################
data_numeric_variables <- sapply(accomodation, is.numeric)
# Remove undesired columns from PCA dataset
accomodation_adjusted <- accomodation[, data_numeric_variables]
pca <- prcomp(accomodation_adjusted, center = TRUE, scale. = TRUE)
summary(pca)
# Inspect PCA details
str(pca)
library("factoextra")
eig_values <- get_eigenvalue(pca)
eig_values
library("FactoMineR")
pca2 <- PCA(accomodation_adjusted, graph = FALSE)
print(pca2)
pca2_eig_values <- get_eigenvalue(pca2)
pca2_eig_values
fviz_eig(pca, addlabels = TRUE, ylim = c(0, 50))
pca_for_variables <- get_pca_var(pca)
pca_for_variables
# Correlation plot - visualize proporion of contribution to the variance
library("corrplot")
corrplot(pca_for_variables$cos2, is.corr = FALSE)
fviz_pca_var(pca, col.var = "black")
# Cos2 - quality of representation
fviz_cos2(pca, choice = "var", axes = 1:3)
# Plot quality of representation
fviz_pca_var(pca, col.var = "cos2",
gradient.cols = c("red", "Blue", "Green"),
repel = TRUE # Avoid text overlapping
)
head(pca_for_variables$contrib, 20)
# most contributing variables
fviz_pca_var(pca, col.var = "contrib",
gradient.cols = c("red", "Blue", "Green"),
)
library(factoextra)
fviz_contrib(pca, choice = "var", axes = 1, top = 20)
# Contributions of variables to PC2
fviz_contrib(pca, choice = "var", axes = 2, top = 20)
# Contributions of variables to PC3
fviz_contrib(pca, choice = "var", axes = 3, top = 20)
# Contribution to PC1 - PC5
fviz_contrib(pca, choice = "var", axes = 1:3, top = 20)
fviz_pca_ind(pca,
axes = c(1, 2),
geom.ind = "point",
col.ind = accomodation$TownSize, # colour by town size
addEllipses = TRUE,
legend.title = "Town Size"
)
biplot <- fviz_pca_ind(pca, geom = "point", col.ind = accomodation$Avg_Rent)
ggpubr::ggpar(biplot,
title = "Principal Component Analysis",
subtitle = "Average Rental in Ireland",
caption = "Source: RTB, Census",
xlab = "PC 1", ylab = "PC 2",
legend.title = "Average Rent", legend.position = "top",
ggtheme = theme_gray())
##############################################################################
# Basic statistical analysis of variables
##############################################################################
hist(accomodation$PersonsPerSqKm)
hist(accomodation$TotalPopulation)
hist(accomodation$Household_Income)
hist(accomodation$Avg_Rent)
hist(accomodation$PC_ICTOwnsPC)
hist(accomodation$PC_ICTInternet)
hist(accomodation$PC_HeatingNoCentral)
hist(accomodation$PC_SeweragePublic)
hist(accomodation$PC_SewerageIndSeptic)
hist(accomodation$PC_WaterPublicMains)
hist(accomodation$RentIncomeRatio)
hist(accomodation$PeoplePerHouse)
ggdensity(accomodation$PeoplePerHouse,
main = "Density plot of people per house",
xlab = "People per house")
library("ggpubr")
ggboxplot(accomodation, x = "TownSize", y = "PersonsPerSqKm",
color = "TownSize", palette = colors,
ylab = "People per km2", xlab = "Town Size")
ggboxplot(accomodation, x = "TownSize", y = "Avg_Rent",
color = "TownSize", palette = colors,
ylab = "Average Rent", xlab = "Area")
ggboxplot(accomodation, x = "TownSize", y = "Household_Income",
color = "TownSize", palette = colors,
ylab = "Household Income", xlab = "Area")
ggboxplot(accomodation, x = "TownSize", y = "PeoplePerHouse",
color = "TownSize", palette = colors,
ylab = "People per house", xlab = "Area")
ggboxplot(accomodation, x = "TownSize", y = "RentIncomeRatio",
color = "TownSize", palette = colors,
ylab = "Rent to Income Ratio", xlab = "Area")
write.csv(accomodation, "Data/HousingStatsProcessed.csv")
#Cork C
#Clare CE
#Cavan CN
#Carlow CW
#Dublin D
#Donegal DL
#Galway G
#Kildare KE
#Kilkenny KK
#Kerry KY
#Limerick L
#Longford LD
#Louth LH
#Leitrim LM
#Laois LS
#Meath MH
#Monaghan MN
#Mayo MO
#Offaly OY
#Roscommon RN
#Sligo SO
#Tipperary T
#Waterford W
#Westmeath WH
#Wexford WX
#Wicklow WW