library(readxl)
todo <- read_excel("Datos Rentas Prob y Est.xlsx", sheet = "Datos")
cali <- read_excel("Datos Rentas Prob y Est.xlsx", sheet = "Cali")
bogota <- read_excel("Datos Rentas Prob y Est.xlsx", sheet = "Bogotá D.C.")
medellin <- read_excel("Datos Rentas Prob y Est.xlsx", sheet = "Medellín")
header=TRUE

# Indicadores de tendencia central todos los datos (rent_price)
media_todo <- mean(todo$rent_price)
mediana_todo <- median(todo$rent_price)
moda_todo <- table(todo$rent_price)
moda_todo <- as.numeric(names(moda_todo)[which.max(moda_todo)])

# Indicadores de dispersión todos los datos  (rent_price)
rango_todo <- range(todo$rent_price)
desviacion_estandar_todo <- sd(todo$rent_price)
varianza_todo <- var(todo$rent_price)

# Indicadores de posición todos p´polos datos  (rent_price)
cuartiles_todo <- quantile(todo$rent_price, c(0.25, 0.5, 0.75))
percentiles_todo <- quantile(todo$rent_price, c(0.25, 0.5, 0.75, 0.90, 0.95, 0.99))

# Verificar valores faltantes
colSums(is.na(todo))
##   rent_price         area   build_area        rooms        baths      garages 
##            0            0            0            0            0            0 
##         city         zone neighborhood 
##            0            0            0
colSums(is.na(cali))
##   rent_price         area   build_area        rooms        baths      garages 
##            0            0            0            0            0            0 
##         city         zone neighborhood 
##            0            0            0
colSums(is.na(bogota))
##   rent_price         area   build_area        rooms        baths      garages 
##            0            0            0            0            0            0 
##         city         zone neighborhood 
##            0            0            0
colSums(is.na(medellin))
##   rent_price         area   build_area        rooms        baths      garages 
##            0            0            0            0            0            0 
##         city         zone neighborhood 
##            0            0            0
# Imputación de valores faltantes para variables numéricas
numeric_vars <- c("rent_price", "area", "build_area", "rooms", "baths", "garages")
for (var in numeric_vars) {
  todo[[var]] <- ifelse(is.na(todo[[var]]), 
                                           mean(todo[[var]], na.rm = TRUE), 
                                           todo[[var]])
}
numeric_vars <- c("rent_price", "area", "build_area", "rooms", "baths", "garages")
for (var in numeric_vars) {
  cali[[var]] <- ifelse(is.na(cali[[var]]), 
                                           mean(cali[[var]], na.rm = TRUE), 
                                           cali[[var]])
}
numeric_vars <- c("rent_price", "area", "build_area", "rooms", "baths", "garages")
for (var in numeric_vars) {
  bogota[[var]] <- ifelse(is.na(bogota[[var]]), 
                                           mean(bogota[[var]], na.rm = TRUE), 
                                           bogota[[var]])
numeric_vars <- c("rent_price", "area", "build_area", "rooms", "baths", "garages")
for (var in numeric_vars) {
  medellin[[var]] <- ifelse(is.na(medellin[[var]]), 
                                           mean(medellin[[var]], na.rm = TRUE), 
                                           medellin[[var]])
}
}
# Imputación de valores faltantes para variables categóricas
categorical_vars <- c("city", "zone", "neighborhood")
for (var in categorical_vars) {
  todo[[var]] <- ifelse(is.na(todo[[var]]), 
                                           names(which.max(table(todo[[var]]))), 
                                           todo[[var]])
}
categorical_vars <- c("city", "zone", "neighborhood")
for (var in categorical_vars) {
  cali[[var]] <- ifelse(is.na(cali[[var]]), 
                                           names(which.max(table(cali[[var]]))), 
                                           cali[[var]])
}
categorical_vars <- c("city", "zone", "neighborhood")
for (var in categorical_vars) {
  bogota[[var]] <- ifelse(is.na(bogota[[var]]), 
                                           names(which.max(table(bogota[[var]]))), 
                                           bogota[[var]])
}
categorical_vars <- c("city", "zone", "neighborhood")
for (var in categorical_vars) {
  medellin[[var]] <- ifelse(is.na(medellin[[var]]), 
                                           names(which.max(table(medellin[[var]]))), 
                                           medellin[[var]])
}

# Mostrar resumen después del preprocesamiento
summary(todo)
##    rent_price          area            build_area            rooms      
##  Min.   : 111111   Length:300         Length:300         Min.   :1.000  
##  1st Qu.:1000000   Class :character   Class :character   1st Qu.:1.000  
##  Median :1250000   Mode  :character   Mode  :character   Median :2.000  
##  Mean   :2375569                                         Mean   :2.273  
##  3rd Qu.:3700000                                         3rd Qu.:3.000  
##  Max.   :8000000                                         Max.   :5.000  
##      baths          garages           city               zone          
##  Min.   :0.000   Min.   :0.0000   Length:300         Length:300        
##  1st Qu.:1.000   1st Qu.:0.0000   Class :character   Class :character  
##  Median :2.000   Median :1.0000   Mode  :character   Mode  :character  
##  Mean   :1.957   Mean   :0.7233                                        
##  3rd Qu.:2.000   3rd Qu.:1.0000                                        
##  Max.   :5.000   Max.   :4.0000                                        
##  neighborhood      
##  Length:300        
##  Class :character  
##  Mode  :character  
##                    
##                    
## 
summary(cali)
##    rent_price          area            build_area            rooms     
##  Min.   : 269000   Length:100         Length:100         Min.   :1.00  
##  1st Qu.: 772500   Class :character   Class :character   1st Qu.:1.00  
##  Median :1100000   Mode  :character   Mode  :character   Median :2.00  
##  Mean   :1677580                                         Mean   :2.22  
##  3rd Qu.:2350000                                         3rd Qu.:3.00  
##  Max.   :5000000                                         Max.   :5.00  
##      baths         garages         city               zone          
##  Min.   :1.00   Min.   :0.00   Length:100         Length:100        
##  1st Qu.:1.00   1st Qu.:0.00   Class :character   Class :character  
##  Median :2.00   Median :0.00   Mode  :character   Mode  :character  
##  Mean   :1.95   Mean   :0.59                                        
##  3rd Qu.:2.00   3rd Qu.:1.00                                        
##  Max.   :5.00   Max.   :2.00                                        
##  neighborhood      
##  Length:100        
##  Class :character  
##  Mode  :character  
##                    
##                    
## 
summary(bogota)
##    rent_price          area            build_area            rooms    
##  Min.   : 500000   Length:100         Length:100         Min.   :1.0  
##  1st Qu.: 800000   Class :character   Class :character   1st Qu.:1.0  
##  Median :1100000   Mode  :character   Mode  :character   Median :2.0  
##  Mean   :1928816                                         Mean   :1.9  
##  3rd Qu.:1250000                                         3rd Qu.:3.0  
##  Max.   :7000000                                         Max.   :3.0  
##      baths         garages         city               zone          
##  Min.   :1.00   Min.   :0.00   Length:100         Length:100        
##  1st Qu.:1.00   1st Qu.:0.00   Class :character   Class :character  
##  Median :1.00   Median :0.00   Mode  :character   Mode  :character  
##  Mean   :1.64   Mean   :0.67                                        
##  3rd Qu.:2.00   3rd Qu.:1.00                                        
##  Max.   :5.00   Max.   :4.00                                        
##  neighborhood      
##  Length:100        
##  Class :character  
##  Mode  :character  
##                    
##                    
## 
summary(medellin)
##    rent_price          area            build_area            rooms    
##  Min.   : 111111   Length:100         Length:100         Min.   :1.0  
##  1st Qu.:1550000   Class :character   Class :character   1st Qu.:2.0  
##  Median :3600000   Mode  :character   Mode  :character   Median :3.0  
##  Mean   :3520311                                         Mean   :2.7  
##  3rd Qu.:6025000                                         3rd Qu.:3.0  
##  Max.   :8000000                                         Max.   :5.0  
##      baths         garages         city               zone          
##  Min.   :0.00   Min.   :0.00   Length:100         Length:100        
##  1st Qu.:1.00   1st Qu.:0.00   Class :character   Class :character  
##  Median :2.00   Median :1.00   Mode  :character   Mode  :character  
##  Mean   :2.28   Mean   :0.91                                        
##  3rd Qu.:3.00   3rd Qu.:2.00                                        
##  Max.   :5.00   Max.   :3.00                                        
##  neighborhood      
##  Length:100        
##  Class :character  
##  Mode  :character  
##                    
##                    
## 
install.packages("ggplot2")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
# Gráficas
library(ggplot2)

# Histograma rent_price
c <- ggplot(data = todo, mapping = aes(rent_price))
c + geom_histogram(aes(y = ..density..), col = "blue", bins = 10) +
  theme_classic() +
  labs(title = "Histograma de la variable rent_price") + 
  geom_density(kernel = "gaussian", col = "red")
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Diagrama de Cajas para rent_price por Ciudad
ggplot(data = todo, mapping = aes(x = city, y = rent_price)) + 
  geom_boxplot(color = "blue") +
  labs(title = "Diagrama de cajas rent_price por Ciudad")

# Diagrama de Cajas para build_area por Ciudad
ggplot(data = todo, mapping = aes(x = city, y = build_area)) + 
  geom_boxplot(color = "green") +
  labs(title = "Diagrama de cajas build_area por Ciudad")

# Diagrama de Dispersión entre area y rent_price
ggplot(data = todo, mapping = aes(x = area, y = rent_price)) +
  geom_point(color = "blue") +
  geom_smooth(method = lm, color = "red") +
  labs(title = "Diagrama de dispersión entre area y rent_price")
## `geom_smooth()` using formula = 'y ~ x'

# Otros gráficos relacionados con las variables mencionadas se pueden agregar de manera similar.

# Puedes repetir estos patrones para los conjuntos de datos cali, bogota y medellin.