getwd()
## [1] "C:/Users/Nicolas Bertuleit/Documents"
#read.csv("C:/Users/Nicolas Bertuleit/Documents/housing.csv")
housing_data <-read.csv("C:/Users/Nicolas Bertuleit/Documents/housing.csv")
#We assigned a name to our dataset.
housing_data <- subset(housing_data, select = -c(url, region_url,image_url,description))
#we deleted some columns that we didnt need for our analysis.
write.csv(housing_data, "Housing_data2.csv", row.names = FALSE)
#re-write the dataset withouth the deleted columns
housing_data2 <-read.csv("C:/Users/Nicolas Bertuleit/Documents/housing_data2.csv")
#View(housing_data2)
str(housing_data2)
## 'data.frame': 384977 obs. of 18 variables:
## $ id : num 7.05e+09 7.05e+09 7.04e+09 7.05e+09 7.05e+09 ...
## $ region : chr "reno / tahoe" "reno / tahoe" "reno / tahoe" "reno / tahoe" ...
## $ price : num 1148 1200 1813 1095 289 ...
## $ type : chr "apartment" "condo" "apartment" "apartment" ...
## $ sqfeet : int 1078 1001 1683 708 250 720 661 708 1053 930 ...
## $ beds : int 3 2 2 1 0 1 1 1 2 2 ...
## $ baths : num 2 2 2 1 1 1 1 1 2 2 ...
## $ cats_allowed : int 1 0 1 1 1 1 1 1 1 0 ...
## $ dogs_allowed : int 1 0 1 1 1 1 1 1 1 0 ...
## $ smoking_allowed : int 0 0 1 1 1 1 1 1 0 0 ...
## $ wheelchair_access : int 0 0 0 0 1 0 0 0 0 0 ...
## $ electric_vehicle_charge: int 0 0 0 0 0 0 0 0 0 0 ...
## $ comes_furnished : int 0 0 0 0 1 0 0 0 0 0 ...
## $ laundry_options : chr "w/d in unit" "w/d hookups" "w/d in unit" "w/d in unit" ...
## $ parking_options : chr "carport" "carport" "attached garage" "carport" ...
## $ lat : num 39.5 39.5 39.6 39.4 39.5 ...
## $ long : num -120 -120 -120 -120 -120 ...
## $ state : chr "ca" "ca" "ca" "ca" ...
#structure of the data set(we ran it a few times, already modified)
summary(housing_data2)
## id region price type
## Min. :7.004e+09 Length:384977 Min. :0.000e+00 Length:384977
## 1st Qu.:7.036e+09 Class :character 1st Qu.:8.050e+02 Class :character
## Median :7.043e+09 Mode :character Median :1.036e+03 Mode :character
## Mean :7.041e+09 Mean :8.826e+03
## 3rd Qu.:7.048e+09 3rd Qu.:1.395e+03
## Max. :7.051e+09 Max. :2.768e+09
##
## sqfeet beds baths cats_allowed
## Min. : 0 Min. : 0.000 Min. : 0.000 Min. :0.0000
## 1st Qu.: 750 1st Qu.: 1.000 1st Qu.: 1.000 1st Qu.:0.0000
## Median : 949 Median : 2.000 Median : 1.000 Median :1.0000
## Mean : 1060 Mean : 1.905 Mean : 1.481 Mean :0.7269
## 3rd Qu.: 1150 3rd Qu.: 2.000 3rd Qu.: 2.000 3rd Qu.:1.0000
## Max. :8388607 Max. :1100.000 Max. :75.000 Max. :1.0000
##
## dogs_allowed smoking_allowed wheelchair_access electric_vehicle_charge
## Min. :0.0000 Min. :0.0000 Min. :0.00000 Min. :0.00000
## 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.00000
## Median :1.0000 Median :1.0000 Median :0.00000 Median :0.00000
## Mean :0.7079 Mean :0.7318 Mean :0.08211 Mean :0.01287
## 3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.:0.00000 3rd Qu.:0.00000
## Max. :1.0000 Max. :1.0000 Max. :1.00000 Max. :1.00000
##
## comes_furnished laundry_options parking_options lat
## Min. :0.00000 Length:384977 Length:384977 Min. :-43.53
## 1st Qu.:0.00000 Class :character Class :character 1st Qu.: 33.45
## Median :0.00000 Mode :character Mode :character Median : 37.65
## Mean :0.04813 Mean : 37.23
## 3rd Qu.:0.00000 3rd Qu.: 41.14
## Max. :1.00000 Max. :102.04
## NA's :1918
## long state
## Min. :-163.89 Length:384977
## 1st Qu.:-100.78 Class :character
## Median : -87.75 Mode :character
## Mean : -92.70
## 3rd Qu.: -81.18
## Max. : 172.63
## NA's :1918
housing_data2$dogs_allowed <- as.logical(housing_data2$dogs_allowed)
#We converted this variable into from boolean to logical
housing_data2$cats_allowed <- as.logical(housing_data2$cats_allowed)
#We converted this variable into from boolean to logical
housing_data2$smoking_allowed <- as.logical(housing_data2$smoking_allowed)
#We converted this variable into from boolean to logical
housing_data2$wheelchair_access <- as.logical(housing_data2$wheelchair_access)
#We converted this variable into from boolean to logical
housing_data2$comes_furnished <- as.logical(housing_data2$comes_furnished)
#We converted this variable into from boolean to logical
housing_data2$electric_vehicle_charge <- as.logical(housing_data2$electric_vehicle_charge)
summary(housing_data2)
## id region price type
## Min. :7.004e+09 Length:384977 Min. :0.000e+00 Length:384977
## 1st Qu.:7.036e+09 Class :character 1st Qu.:8.050e+02 Class :character
## Median :7.043e+09 Mode :character Median :1.036e+03 Mode :character
## Mean :7.041e+09 Mean :8.826e+03
## 3rd Qu.:7.048e+09 3rd Qu.:1.395e+03
## Max. :7.051e+09 Max. :2.768e+09
##
## sqfeet beds baths cats_allowed
## Min. : 0 Min. : 0.000 Min. : 0.000 Mode :logical
## 1st Qu.: 750 1st Qu.: 1.000 1st Qu.: 1.000 FALSE:105141
## Median : 949 Median : 2.000 Median : 1.000 TRUE :279836
## Mean : 1060 Mean : 1.905 Mean : 1.481
## 3rd Qu.: 1150 3rd Qu.: 2.000 3rd Qu.: 2.000
## Max. :8388607 Max. :1100.000 Max. :75.000
##
## dogs_allowed smoking_allowed wheelchair_access electric_vehicle_charge
## Mode :logical Mode :logical Mode :logical Mode :logical
## FALSE:112445 FALSE:103262 FALSE:353366 FALSE:380022
## TRUE :272532 TRUE :281715 TRUE :31611 TRUE :4955
##
##
##
##
## comes_furnished laundry_options parking_options lat
## Mode :logical Length:384977 Length:384977 Min. :-43.53
## FALSE:366449 Class :character Class :character 1st Qu.: 33.45
## TRUE :18528 Mode :character Mode :character Median : 37.65
## Mean : 37.23
## 3rd Qu.: 41.14
## Max. :102.04
## NA's :1918
## long state
## Min. :-163.89 Length:384977
## 1st Qu.:-100.78 Class :character
## Median : -87.75 Mode :character
## Mean : -92.70
## 3rd Qu.: -81.18
## Max. : 172.63
## NA's :1918
summary(housing_data2$price)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000e+00 8.050e+02 1.036e+03 8.826e+03 1.395e+03 2.768e+09
#to understand the values of price. The dataset included values extremely big compared to the mean and median of the rest of the dataset.
range(housing_data2$price)
## [1] 0 2768307249
quantile(housing_data2$price)
## 0% 25% 50% 75% 100%
## 0 805 1036 1395 2768307249
housing_data2 <- subset(housing_data2, price <= 20000)
#To clean the data set, we eliminated the rows that included prices above $20.000
quantile(housing_data2$price)
## 0% 25% 50% 75% 100%
## 0 805 1035 1395 20000
range(housing_data2$price)
## [1] 0 20000
housing_data2 <- subset(housing_data2, nchar(state) == 2)
##To clean the data set, we eliminated the rows that included states with more than 2 characters.
summary(housing_data2)
## id region price type
## Min. :7.004e+09 Length:384807 Min. : 0 Length:384807
## 1st Qu.:7.036e+09 Class :character 1st Qu.: 805 Class :character
## Median :7.043e+09 Mode :character Median : 1035 Mode :character
## Mean :7.041e+09 Mean : 1183
## 3rd Qu.:7.048e+09 3rd Qu.: 1395
## Max. :7.051e+09 Max. :20000
##
## sqfeet beds baths cats_allowed
## Min. : 0 Min. : 0.000 Min. : 0.000 Mode :logical
## 1st Qu.: 750 1st Qu.: 1.000 1st Qu.: 1.000 FALSE:105083
## Median : 949 Median : 2.000 Median : 1.000 TRUE :279724
## Mean : 1060 Mean : 1.905 Mean : 1.481
## 3rd Qu.: 1150 3rd Qu.: 2.000 3rd Qu.: 2.000
## Max. :8388607 Max. :1100.000 Max. :75.000
##
## dogs_allowed smoking_allowed wheelchair_access electric_vehicle_charge
## Mode :logical Mode :logical Mode :logical Mode :logical
## FALSE:112388 FALSE:103241 FALSE:353203 FALSE:379853
## TRUE :272419 TRUE :281566 TRUE :31604 TRUE :4954
##
##
##
##
## comes_furnished laundry_options parking_options lat
## Mode :logical Length:384807 Length:384807 Min. :-43.53
## FALSE:366288 Class :character Class :character 1st Qu.: 33.45
## TRUE :18519 Mode :character Mode :character Median : 37.65
## Mean : 37.23
## 3rd Qu.: 41.14
## Max. :102.04
## NA's :1917
## long state
## Min. :-163.89 Length:384807
## 1st Qu.:-100.78 Class :character
## Median : -87.76 Mode :character
## Mean : -92.70
## 3rd Qu.: -81.18
## Max. : 172.63
## NA's :1917
hist(housing_data2$price)

#to visualize the dsitribution of prices in our data set.
boxplot(housing_data2$price)

housing_data <- na.omit(housing_data)
Q1 <- quantile(housing_data$price_per_sqft, 0.25)
Q3 <- quantile(housing_data$price_per_sqft, 0.75)
IQR <- Q3 - Q1
lower_bound <- Q1 - 1.5 * IQR
upper_bound <- Q3 + 1.5 * IQR
housing_data <- housing_data[housing_data$price_per_sqft >= lower_bound & housing_data$price_per_sqft <= upper_bound, ]
price_per_state <- aggregate(housing_data2$price, by = list(housing_data2$state), FUN = mean)
#Creating a variable of price per state for further analysis.
colnames(price_per_state) <- c("state", "average_Price")
price_per_state <- price_per_state[order(price_per_state
$average_Price), ]
barplot(price_per_state$average_Price, names.arg = price_per_state$state,
xlab = "state", ylab = "average Price", main = "average price by state",
col = "blue", las = 2)

#Based on the analysis, we can conclude that the most expensive state to rent a property is Hawaii.
housing_data2$price_per_sqft <- ifelse(housing_data2$sqfeet == 0, NA, housing_data2$price / housing_data2$sqfeet)
#for further analysis, we create a variable of price per state per square feet.
price_per_sqft2 <- aggregate(housing_data2$price_per_sqft, by = list(housing_data2$state), FUN = mean, na.rm = TRUE)
colnames(price_per_sqft2) <- c("state", "average_price_per_sqft")
price_per_sqft2 <- price_per_sqft2[order(price_per_sqft2$average_price_per_sqft), ]
barplot(price_per_sqft2$average_price_per_sqft, names.arg = price_per_sqft2$state,
xlab = "State", ylab = "Average Price per sqft", main = "Average Price per sqft by State",
col = "green", las = 2)

#based on the price per sqft, the most expensive state for rent is OH.
sum(is.na(housing_data2$sqfeet))
## [1] 0
#there are 3882 missing values in our dataset.
plot(housing_data2$price, housing_data2$sqfeet)

cor(housing_data2$price,housing_data2$sqfeet)
## [1] 0.01011266
table(housing_data2$state)
##
## ak al ar az ca co ct dc de fl ga hi ia
## 2169 8196 3149 6753 33074 11306 3764 2502 2048 31917 13840 1840 7486
## id il in ks ky la ma md me mi mn mo ms
## 4466 9705 6408 7910 5419 7302 4925 7451 420 14512 7468 2157 4972
## mt nc nd ne nh nj nm nv ny oh ok or pa
## 1339 18592 3428 2697 1761 5709 2915 2846 9985 12884 5787 8066 9910
## ri sc sd tn tx ut va vt wa wi wv wy
## 1889 9921 1782 11515 31130 5194 11060 527 7059 6646 819 187
cor(housing_data2[c("beds", "price", "baths", "sqfeet")])
## beds price baths sqfeet
## beds 1.00000000 0.05444964 0.41647863 0.00383261
## price 0.05444964 1.00000000 0.26300858 0.01011266
## baths 0.41647863 0.26300858 1.00000000 0.01116428
## sqfeet 0.00383261 0.01011266 0.01116428 1.00000000
cor(housing_data2[c("beds", "price", "baths", "sqfeet","dogs_allowed")])
## beds price baths sqfeet dogs_allowed
## beds 1.00000000 0.05444964 0.41647863 0.003832610 -0.013477904
## price 0.05444964 1.00000000 0.26300858 0.010112660 -0.026558491
## baths 0.41647863 0.26300858 1.00000000 0.011164277 0.017808290
## sqfeet 0.00383261 0.01011266 0.01116428 1.000000000 -0.002752907
## dogs_allowed -0.01347790 -0.02655849 0.01780829 -0.002752907 1.000000000
#install.packages("ggplot2")
library(ggplot2)
our.data <- data.frame(sqfeet = housing_data2$sqfeet, price_per_sqft = housing_data2$price_per_sqft)
gg <- ggplot(our.data, aes(sqfeet, price_per_sqft))
gg <- gg + geom_point()
gg <- gg + geom_smooth(method = "lm", se=F, color="red")
gg <- gg + ylim(0, 6)
gg <- gg + xlim(500, 1500)
gg <- gg + theme_bw()
print(gg)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 40619 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 40619 rows containing missing values (`geom_point()`).

#the bigger the apartment, the lower the price per square feet.
#housing_data_filtered$pets_allowed <- ifelse(housing_data_filtered$cats_allowed == 1 | #housing_data_filtered$dogs_allowed == 1, 1, 0)
#we are going to clean the data type
property_type <- table(housing_data$type)
to_delete <- names(property_type[property_type < 100])
housing_data_filtered <- subset(housing_data2, !(type %in% to_delete))
nrow(housing_data_filtered)
## [1] 384807
#View(housing_data_filtered)
#install.packages("caret")
#library(caret)
regiones <- c("ak", "az", "ca", "co", "hi", "id", "mt", "nv", "nm", "or", "ut", "wa", "wy") # West
regiones <- c(regiones, "il", "in", "ia", "ks", "mi", "mn", "mo", "ne", "nd", "oh", "sd", "wi") # Midwest
regiones <- c(regiones, "al", "ar", "de", "fl", "ga", "ky", "la", "md", "ms", "nc", "ok", "sc", "tn", "tx", "va", "wv") # South
regiones <- c(regiones, "ct", "me", "ma", "nh", "nj", "ny", "pa", "ri", "vt","dc") # North
asignar_region <- function(estado) {
for (i in 1:length(regiones)) {
if (tolower(estado) %in% regiones[i]) {
return(ifelse(i <= 13, "West", ifelse(i <= 26, "Midwest", ifelse(i <= 41, "South", "North"))))
}
}
return("Desconocido") # Por si acaso
}
housing_data_filtered$region <- sapply(housing_data_filtered$state, asignar_region)
table(housing_data_filtered$region)
##
## Midwest North South West
## 91279 41392 164922 87214
#View(housing_data_filtered)
#set.seed(123)
#train_index <- createDataPartition(housing_data_filtered$price, p = 0.8, list = FALSE)
#train_data <- housing_data_filtered[train_index, ]
#test_data <- housing_data_filtered[-train_index, ]
#model <- lm(price ~ sqfeet + beds + baths + dogs_allowed + cats_allowed+ smoking_allowed + wheelchair_access + electric_vehicle_charge + comes_furnished + region , data = train_data)
#predictions <- predict(model, newdata = test_data)
#RMSE <- sqrt(mean((test_data$price - predictions)^2,na.rm = TRUE))
#print(paste("Root Mean Squared Error (RMSE):", RMSE))
#summary(model)
#set.seed(123)
#train_index2 <- createDataPartition(housing_data_filtered$price, p = 0.8, list = FALSE)
#train_data2 <- housing_data_filtered[train_index2, ]
#test_data2 <- housing_data_filtered[-train_index2, ]
#model2 <- lm(price ~ sqfeet + beds + baths + region , data = train_data2)
#predictions2 <- predict(model, newdata = test_data2)
#RMSE <- sqrt(mean((test_data2$price - predictions2)^2,na.rm = TRUE))
#print(paste("Root Mean Squared Error (RMSE):", RMSE))
region <- "West"
sqfeet <- 1500
beds <- 3
baths <- 2
nuevo_data <- data.frame(region = region, sqfeet = sqfeet, beds = beds, baths = baths)
#prediccion_precio3 <- predict(model2, newdata = nuevo_data)
#print(paste("The predicted price for a property in", region, "with", sqfeet, "square feet,", beds, "beds and", baths, "bads:", prediccion_precio3))
#install.packages("h2o")
#library(h2o)
#h2o.init()
set.seed(0) # Set seed for reproducibility
n_samples_train <- 1000 # Size of the training dataset
n_samples_test <- 200 # Size of the testing dataset
mean_price_per_sqft <- mean(housing_data_filtered$price_per_sqft)
sd_price_per_sqft <- sd(housing_data_filtered$price_per_sqft)
price_per_square_feet <- rnorm(n_samples_train, mean = mean_price_per_sqft, sd = sd_price_per_sqft)
## Warning in rnorm(n_samples_train, mean = mean_price_per_sqft, sd =
## sd_price_per_sqft): NAs produced
region <- sample(c("West", "Midwest", "South", "North"), n_samples_train, replace = TRUE)
beds <- sample(1:5, n_samples_train, replace = TRUE)
baths <- sample(1:3, n_samples_train, replace = TRUE)
train_df <- data.frame(price_per_square_feet, region, beds, baths)
#train_h2o <- as.h2o(train_df)
#if (!requireNamespace("rpart", quietly = TRUE)) {
#install.packages("rpart")
#}
#library(rpart)
#modelo_arbol <- rpart(price ~ ., data = housing_data_filtered)
#print(modelo_arbol)
#predicciones <- predict(modelo_arbol, newdata = housing_data_filtered)
#head(predicciones)
#modelo <- lm(price_per_sqft ~ type, data = housing_data_filtered)
#anova_resultado <- anova(modelo)
#print(anova_resultado)
#modelo_regresion <- lm(price_per_sqft ~ type + beds + baths + region, data = housing_data_filtered)
#summary(modelo_regresion)
#install.packages("rpart")
#library(rpart)
#modelo_arbol <- rpart(price_per_sqft ~ type + beds + baths + region, data = housing_data_filtered)
#plot(modelo_arbol)
#text(modelo_arbol)
#install.packages("randomForest")
#library(randomForest)
#housing_data_filtered <- na.omit(housing_data_filtered)
#set.seed(123) # Para reproducibilidad
#modelo_bosque <- randomForest(price_per_sqft ~ type + beds + baths + region, data = housing_data_filtered)
#varImpPlot(modelo_bosque)
#Type of property has a greater influence in the price per square feet, followed by beds and region.
#table(housing_data_filtered$type)
#tabla_contingencia <- table(housing_data_filtered$type, housing_data_filtered$region)
#prueba_chi <- chisq.test(tabla_contingencia)
#print(prueba_chi)
#We rejetc the null hipothesis, and we can conclude that there is a strong relationship between Type and region.
#correlaciones <- cor(housing_data_filtered[c("price_per_sqft", "beds", "baths")])
#print(correlaciones)
#install.packages("rpart")
#library(rpart)
#modelo_arbol <- rpart(price ~ region + type + beds + baths, data = housing_data_filtered)
#plot(modelo_arbol)
#text(modelo_arbol)