library("tidyverse")
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.1 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library("ggplot2")
library("sf")
## Linking to GEOS 3.9.1, GDAL 3.3.2, PROJ 7.2.1; sf_use_s2() is TRUE
library("osmdata")
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library("rmarkdown")
library("lubridate")
library("ggmap")
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library("mapview")
library("hrbrthemes")
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
## Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
## if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
library("RColorBrewer")
library("readr")
library("leaflet")
library("XML")
## Warning: package 'XML' was built under R version 4.2.1
library("osmextract")
## Warning: package 'osmextract' was built under R version 4.2.1
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright.
## Check the package website, https://docs.ropensci.org/osmextract/, for more details.
library("forcats")
library("spatialreg")
## Warning: package 'spatialreg' was built under R version 4.2.2
## Loading required package: spData
## Warning: package 'spData' was built under R version 4.2.2
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
## Loading required package: Matrix
## Warning: package 'Matrix' was built under R version 4.2.1
##
## Attaching package: 'Matrix'
##
## The following objects are masked from 'package:tidyr':
##
## expand, pack, unpack
library("spdep")
## Warning: package 'spdep' was built under R version 4.2.2
## Loading required package: sp
##
## Attaching package: 'spdep'
##
## The following objects are masked from 'package:spatialreg':
##
## get.ClusterOption, get.coresOption, get.mcOption,
## get.VerboseOption, get.ZeroPolicyOption, set.ClusterOption,
## set.coresOption, set.mcOption, set.VerboseOption,
## set.ZeroPolicyOption
library("rgdal")
## Warning: package 'rgdal' was built under R version 4.2.1
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
##
## rgdal: version: 1.5-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.3, released 2022/04/22
## Path to GDAL shared files: C:/Users/sixto/AppData/Local/R/win-library/4.2/rgdal/gdal
## GDAL binary built with GEOS: TRUE
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/sixto/AppData/Local/R/win-library/4.2/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.5-0
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
We download the dataset for use.
propiedades = read_sf("C:/Users/sixto/OneDrive/Documentos/Di_tella/FIU_ii/Tesis_2/tesis2/propiedades.geojson")
propiedades = filter(propiedades, rooms <10, rooms !="", surface_covered !="")
ggplot() +
geom_sf(data = propiedades, aes(color = pm2)) +
scale_size_continuous(range = c(1, 10)) +
labs(title = "PM2 Distribution Map") +
theme_minimal() + scale_color_viridis_c()
ggplot() +
geom_sf(data = propiedades, aes(color = rooms)) +
labs(title = "Rooms distribution") +
theme_minimal() + scale_color_viridis_c()
library("e1071")
# Select the columns for clustering
selected_columns <- c("rooms", "bedrooms", "bathrooms", "surface_total", "surface_covered", "pm2",
"casa_rosada", "Escuelas", "bancos", "Universidades", "trenes",
"subte_estaciones", "avenidas", "estaciones_eco_bici", "hospitales",
"polideportivos", "plazas", "parques", "plazoletas", "comisarias",
"Escuelas_entorno", "bancos_entorno", "Universidades_entorno",
"trenes_entorno", "subte_estaciones_entorno", "avenidas_entorno",
"estaciones_eco_bici_entorno", "hospitales_entorno", "polideportivos_entorno",
"plazas_entorno", "parques_entorno", "plazoletas_entorno", "comisarias_entorno",
"delitos_entorno")
# Subset the data with selected columns
subset_data <- propiedades[selected_columns]
subset_data_1 = st_drop_geometry(subset_data)
# Perform FCM clustering
k <- 5 # Number of clusters
m <- 5 # Fuzziness parameter
fcm_model <- cmeans(na.omit(subset_data_1), centers = k, m = m, iter.max = 100)
# Cluster assignments
cluster_assignments <- fcm_model$cluster
# Degrees of membership
degrees_of_membership <- fcm_model$membership
# You can further analyze and visualize the clusters as needed
library(ggplot2)
# Create a data frame with cluster assignments
clustered_data <- data.frame(subset_data_1, Cluster = cluster_assignments)
# Scatter plot of two numeric variables, colored by cluster
ggplot(clustered_data, aes(x = rooms, y = bathrooms, color = factor(Cluster))) +
geom_point() +
labs(title = "Cluster Scatter Plot", x = "Rooms", y = "Bathrooms") +
theme_minimal()
# Histogram of a variable by cluster
ggplot(clustered_data, aes(x = surface_total, fill = factor(Cluster))) +
geom_histogram(binwidth = 1, position = "identity", alpha = 0.7) +
labs(title = "Cluster Distributions of Surface Total",
x = "Surface Total",
y = "Frequency") +
theme_minimal()
library(ggplot2)
# Create a data frame with cluster assignments
clustered_data <- data.frame(subset_data_1, Cluster = cluster_assignments)
# Scatter plot of "delitos_entorno" vs "pm2" colored by cluster
ggplot(clustered_data, aes(x = delitos_entorno, y = pm2, color = factor(Cluster))) +
geom_point() +
labs(title = "Clustering Relationship between Delitos Entorno and PM2",
x = "Delitos Entorno",
y = "PM2") +
theme_minimal()
library(ggplot2)
# Create a data frame with cluster assignments
clustered_data <- data.frame(pm2 = subset_data_1$pm2, Cluster = cluster_assignments)
# Boxplot of "pm2" values by cluster
ggplot(clustered_data, aes(x = factor(Cluster), y = pm2, fill = factor(Cluster))) +
geom_boxplot() +
labs(title = "Cluster Distribution of PM2 Values",
x = "Cluster",
y = "PM2") +
theme_minimal()
# Add the cluster assignments to the original propiedades dataset
propiedades$Cluster <- cluster_assignments
ggplot() +
geom_sf(data = propiedades, aes(color = factor(Cluster))) +
scale_color_viridis_d()
labs(title = "Cluster Map",
fill = "Cluster") +
theme_minimal()
## NULL
# Assuming degrees_of_membership is a matrix with each column representing a cluster's membership values
num_clusters <- ncol(degrees_of_membership)
# Add degree of membership columns to propiedades for each cluster
for (cluster in 1:num_clusters) {
propiedades[paste("Degree_of_Membership_Cluster", cluster)] <- degrees_of_membership[, cluster]
}
# Now, the propiedades dataset contains individual columns for degree of membership for each cluster
ggplot() +
geom_sf(data = propiedades, aes(color = `Degree_of_Membership_Cluster 1`)) +
scale_color_viridis_c() +
labs(title = "Degree of Membership (Cluster 1) Map") +
theme_minimal()
ggplot() +
geom_sf(data = propiedades, aes(color = `Degree_of_Membership_Cluster 2`)) +
scale_color_viridis_c() +
labs(title = "Degree of Membership (Cluster 1) Map") +
theme_minimal()
ggplot() +
geom_sf(data = propiedades, aes(color = `Degree_of_Membership_Cluster 3`)) +
scale_color_viridis_c() +
labs(title = "Degree of Membership (Cluster 1) Map") +
theme_minimal()
ggplot() +
geom_sf(data = propiedades, aes(color = `Degree_of_Membership_Cluster 4`)) +
scale_color_viridis_c() +
labs(title = "Degree of Membership (Cluster 1) Map") +
theme_minimal()
ggplot() +
geom_sf(data = propiedades, aes(color = `Degree_of_Membership_Cluster 5`)) +
scale_color_viridis_c() +
labs(title = "Degree of Membership (Cluster 1) Map") +
theme_minimal()