In [19]:
# Loading Libraries
In [20]:
install.packages(c("dplyr", "tidyr", "ggplot2", "plotly", "corrplot", 
                   "caret", "randomForest", "e1071", "nnet", "Metrics", "pROC"))

# For deep learning:
install.packages("keras")
library(keras)
install_keras()  # Ensures TensorFlow backend is installed
Installing packages into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)

Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)

Virtual environment 'r-tensorflow' removed.
Using Python: /usr/bin/python3.10
Creating virtual environment 'r-tensorflow' ... 
+ /usr/bin/python3.10 -m venv /root/.virtualenvs/r-tensorflow

Done!
Installing packages: pip, wheel, setuptools
+ /root/.virtualenvs/r-tensorflow/bin/python -m pip install --upgrade pip wheel setuptools

Virtual environment 'r-tensorflow' successfully created.
Using virtual environment 'r-tensorflow' ...
+ /root/.virtualenvs/r-tensorflow/bin/python -m pip install --upgrade --no-user 'tensorflow==2.15.*' tensorflow-hub tensorflow-datasets scipy requests Pillow h5py pandas pydot

Installation complete.

In [92]:
library(dplyr)
data <- read.csv("/kaggle/input/houseprice/miami-housing.csv") # Make changes based on current directory
summary(data)
    LATITUDE       LONGITUDE         PARCELNO            SALE_PRC      
 Min.   :25.43   Min.   :-80.54   Min.   :1.020e+11   Min.   :  72000  
 1st Qu.:25.62   1st Qu.:-80.40   1st Qu.:1.079e+12   1st Qu.: 235000  
 Median :25.73   Median :-80.34   Median :3.040e+12   Median : 310000  
 Mean   :25.73   Mean   :-80.33   Mean   :2.356e+12   Mean   : 399942  
 3rd Qu.:25.85   3rd Qu.:-80.26   3rd Qu.:3.060e+12   3rd Qu.: 428000  
 Max.   :25.97   Max.   :-80.12   Max.   :3.660e+12   Max.   :2650000  
   LND_SQFOOT     TOT_LVG_AREA  SPEC_FEAT_VAL      RAIL_DIST      
 Min.   : 1248   Min.   : 854   Min.   :     0   Min.   :   10.5  
 1st Qu.: 5400   1st Qu.:1470   1st Qu.:   810   1st Qu.: 3299.4  
 Median : 7500   Median :1878   Median :  2766   Median : 7106.3  
 Mean   : 8621   Mean   :2058   Mean   :  9562   Mean   : 8348.5  
 3rd Qu.: 9126   3rd Qu.:2471   3rd Qu.: 12352   3rd Qu.:12102.6  
 Max.   :57064   Max.   :6287   Max.   :175020   Max.   :29621.5  
   OCEAN_DIST        WATER_DIST      CNTR_DIST        SUBCNTR_DI    
 Min.   :  236.1   Min.   :    0   Min.   :  3826   Min.   :  1463  
 1st Qu.:18079.3   1st Qu.: 2676   1st Qu.: 42823   1st Qu.: 23996  
 Median :28541.8   Median : 6923   Median : 65852   Median : 41110  
 Mean   :31691.0   Mean   :11960   Mean   : 68490   Mean   : 41115  
 3rd Qu.:44310.7   3rd Qu.:19200   3rd Qu.: 89358   3rd Qu.: 53949  
 Max.   :75744.9   Max.   :50400   Max.   :159976   Max.   :110554  
    HWY_DIST            age          avno60plus        month_sold    
 Min.   :   90.2   Min.   : 0.00   Min.   :0.00000   Min.   : 1.000  
 1st Qu.: 2998.1   1st Qu.:14.00   1st Qu.:0.00000   1st Qu.: 4.000  
 Median : 6159.8   Median :26.00   Median :0.00000   Median : 7.000  
 Mean   : 7723.8   Mean   :30.67   Mean   :0.01493   Mean   : 6.656  
 3rd Qu.:10854.2   3rd Qu.:46.00   3rd Qu.:0.00000   3rd Qu.: 9.000  
 Max.   :48167.3   Max.   :96.00   Max.   :1.00000   Max.   :12.000  
 structure_quality
 Min.   :1.000    
 1st Qu.:2.000    
 Median :4.000    
 Mean   :3.514    
 3rd Qu.:4.000    
 Max.   :5.000    
In [93]:
colSums(is.na(data))
LATITUDE
0
LONGITUDE
0
PARCELNO
0
SALE_PRC
0
LND_SQFOOT
0
TOT_LVG_AREA
0
SPEC_FEAT_VAL
0
RAIL_DIST
0
OCEAN_DIST
0
WATER_DIST
0
CNTR_DIST
0
SUBCNTR_DI
0
HWY_DIST
0
age
0
avno60plus
0
month_sold
0
structure_quality
0
In [94]:
cat("\nStructure of Dataset:\n")
str(data)  # Check data types and structure
Structure of Dataset:
'data.frame':	13932 obs. of  17 variables:
 $ LATITUDE         : num  25.9 25.9 25.9 25.9 25.9 ...
 $ LONGITUDE        : num  -80.2 -80.2 -80.2 -80.2 -80.2 ...
 $ PARCELNO         : num  6.22e+11 6.22e+11 6.22e+11 6.22e+11 6.22e+11 ...
 $ SALE_PRC         : num  440000 349000 800000 988000 755000 630000 1020000 850000 250000 1220000 ...
 $ LND_SQFOOT       : int  9375 9375 9375 12450 12800 9900 10387 10272 9375 13803 ...
 $ TOT_LVG_AREA     : int  1753 1715 2276 2058 1684 1531 1753 1663 1493 3077 ...
 $ SPEC_FEAT_VAL    : int  0 0 49206 10033 16681 2978 23116 34933 11668 34580 ...
 $ RAIL_DIST        : num  2816 4359 4413 4585 4063 ...
 $ OCEAN_DIST       : num  12811 10648 10574 10156 10837 ...
 $ WATER_DIST       : num  348 338 297 0 327 ...
 $ CNTR_DIST        : num  42815 43505 43530 43798 43600 ...
 $ SUBCNTR_DI       : num  37742 37340 37329 37423 37551 ...
 $ HWY_DIST         : num  15955 18125 18200 18514 17903 ...
 $ age              : int  67 63 61 63 42 41 63 21 56 63 ...
 $ avno60plus       : int  0 0 0 0 0 0 0 0 0 0 ...
 $ month_sold       : int  8 9 2 9 7 2 2 9 3 11 ...
 $ structure_quality: int  4 4 4 4 4 4 5 4 4 5 ...
In [95]:
cat("\nCheck for missing values:\n")
missing_summary <- sapply(data, function(x) sum(is.na(x)))
print(missing_summary)
Check for missing values:
         LATITUDE         LONGITUDE          PARCELNO          SALE_PRC 
                0                 0                 0                 0 
       LND_SQFOOT      TOT_LVG_AREA     SPEC_FEAT_VAL         RAIL_DIST 
                0                 0                 0                 0 
       OCEAN_DIST        WATER_DIST         CNTR_DIST        SUBCNTR_DI 
                0                 0                 0                 0 
         HWY_DIST               age        avno60plus        month_sold 
                0                 0                 0                 0 
structure_quality 
                0 
In [96]:
cat("\nNumber of unique PARCELNO values:\n")
print(length(unique(data$PARCELNO)))
Number of unique PARCELNO values:
[1] 491
In [97]:
cat("\nCheck for duplicates in PARCELNO:\n")
duplicated_parcel <- data %>% group_by(PARCELNO) %>% summarise(count = n()) %>% filter(count > 1)
print(duplicated_parcel)
Check for duplicates in PARCELNO:
# A tibble: 447 × 2
       PARCELNO count
          <dbl> <int>
 1 131120000000     5
 2 131130000000    20
 3 131131000000     9
 4 131140000000    44
 5 131141000000     2
 6 131220000000     5
 7 131221000000    13
 8 131230000000    36
 9 131240000000    62
10 131250000000    15
# ℹ 437 more rows
In [98]:
duplicates <- data %>% filter(PARCELNO %in% duplicated_parcel$PARCELNO)
head(duplicates)
A data.frame: 6 × 17
LATITUDELONGITUDEPARCELNOSALE_PRCLND_SQFOOTTOT_LVG_AREASPEC_FEAT_VALRAIL_DISTOCEAN_DISTWATER_DISTCNTR_DISTSUBCNTR_DIHWY_DISTageavno60plusmonth_soldstructure_quality
<dbl><dbl><dbl><dbl><int><int><int><dbl><dbl><dbl><dbl><dbl><dbl><int><int><int><int>
125.89103-80.160566.2228e+11440000 93751753 02815.912811.4347.642815.337742.215954.967084
225.89132-80.153976.2228e+11349000 93751715 04359.110648.4337.843504.937340.518125.063094
325.89133-80.153746.2228e+11800000 93752276492064412.910574.1297.143530.437328.718200.561024
425.89176-80.152666.2228e+11988000124502058100334585.010156.5 0.043797.537423.218514.463094
525.89182-80.154646.2228e+11755000128001684166814063.410836.8326.643599.737550.817903.442074
625.89206-80.161356.2228e+11630000 99001531 29782391.413017.0188.943135.138176.215687.241024
In [99]:
str(data)
'data.frame':	13932 obs. of  17 variables:
 $ LATITUDE         : num  25.9 25.9 25.9 25.9 25.9 ...
 $ LONGITUDE        : num  -80.2 -80.2 -80.2 -80.2 -80.2 ...
 $ PARCELNO         : num  6.22e+11 6.22e+11 6.22e+11 6.22e+11 6.22e+11 ...
 $ SALE_PRC         : num  440000 349000 800000 988000 755000 630000 1020000 850000 250000 1220000 ...
 $ LND_SQFOOT       : int  9375 9375 9375 12450 12800 9900 10387 10272 9375 13803 ...
 $ TOT_LVG_AREA     : int  1753 1715 2276 2058 1684 1531 1753 1663 1493 3077 ...
 $ SPEC_FEAT_VAL    : int  0 0 49206 10033 16681 2978 23116 34933 11668 34580 ...
 $ RAIL_DIST        : num  2816 4359 4413 4585 4063 ...
 $ OCEAN_DIST       : num  12811 10648 10574 10156 10837 ...
 $ WATER_DIST       : num  348 338 297 0 327 ...
 $ CNTR_DIST        : num  42815 43505 43530 43798 43600 ...
 $ SUBCNTR_DI       : num  37742 37340 37329 37423 37551 ...
 $ HWY_DIST         : num  15955 18125 18200 18514 17903 ...
 $ age              : int  67 63 61 63 42 41 63 21 56 63 ...
 $ avno60plus       : int  0 0 0 0 0 0 0 0 0 0 ...
 $ month_sold       : int  8 9 2 9 7 2 2 9 3 11 ...
 $ structure_quality: int  4 4 4 4 4 4 5 4 4 5 ...
In [100]:
# Install if necessary
install.packages("dplyr")

# Load the dplyr library
library(dplyr)
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)

In [102]:
cor_matrix <- cor(data[, sapply(data, is.numeric)], use = "complete.obs")
library(corrplot)
corrplot(cor_matrix, method = "circle")
No description has been provided for this image
In [30]:
install.packages("ggplot2")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)

In [103]:
library(ggplot2)
In [104]:
#  Univariate Analysis
# Numerical Variables
ggplot(data, aes(x = SALE_PRC)) + 
  geom_histogram(bins = 30, fill = "blue", color = "black", alpha = 0.7) +
  labs(title = "Distribution of Sale Price", x = "Sale Price ($)", y = "Frequency") +
  theme_minimal()

ggplot(data, aes(x = LND_SQFOOT)) + 
  geom_histogram(bins = 30, fill = "green", color = "black", alpha = 0.7) +
  labs(title = "Distribution of Land Area", x = "Land Area (sq ft)", y = "Frequency") +
  theme_minimal()
No description has been provided for this image
No description has been provided for this image
In [105]:
ggplot(data, aes(x = LND_SQFOOT)) + 
  geom_histogram(bins = 30, fill = "green", color = "black", alpha = 0.7) +
  labs(title = "Distribution of Land Area", x = "Land Area (sq ft)", y = "Frequency") +
  theme_minimal()
No description has been provided for this image
In [106]:
# Categorical Variables
ggplot(data, aes(x = factor(month_sold))) + 
  geom_bar(fill = "orange") +
  labs(title = "Number of Sales by Month", x = "Month", y = "Count") +
  theme_minimal()
No description has been provided for this image
In [107]:
# Scatter Plot for Sale Price vs Floor Area
ggplot(data, aes(x = TOT_LVG_AREA, y = SALE_PRC)) + 
  geom_point(alpha = 0.6, color = "purple") +
  labs(title = "Sale Price vs Total Living Area", x = "Total Living Area (sq ft)", y = "Sale Price ($)") +
  theme_minimal()
No description has been provided for this image
In [108]:
# Relationships with Distance Variables
ggplot(data, aes(x = RAIL_DIST, y = SALE_PRC)) + 
  geom_point(alpha = 0.6, color = "red") +
  labs(title = "Sale Price vs Distance to Rail Line", x = "Rail Distance (ft)", y = "Sale Price ($)") +
  theme_minimal()
No description has been provided for this image
In [109]:
# Insights on Categorical Variables
ggplot(data, aes(x = factor(structure_quality), y = SALE_PRC)) + 
  geom_boxplot(fill = "cyan", color = "black") +
  labs(title = "Sale Price by Structure Quality", x = "Structure Quality", y = "Sale Price ($)") +
  theme_minimal()

ggplot(data, aes(x = factor(avno60plus), y = SALE_PRC)) + 
  geom_boxplot(fill = "pink", color = "black") +
  labs(title = "Sale Price by Airplane Noise Levels", x = "Airplane Noise Level (0 = No, 1 = Yes)", y = "Sale Price ($)") +
  theme_minimal()
No description has been provided for this image
No description has been provided for this image
In [110]:
# Latitude and Longitude Visualization
ggplot(data, aes(x = LONGITUDE, y = LATITUDE, color = SALE_PRC)) +
  geom_point(alpha = 0.7) +
  labs(title = "Geographic Distribution of Properties by Sale Price", x = "Longitude", y = "Latitude") +
  scale_color_gradient(low = "blue", high = "red") + theme_minimal()
No description has been provided for this image
In [111]:
library(caret)
    preproc <- preProcess(data, method = c("center", "scale"))
    data <- predict(preproc, data)
In [114]:
# Load necessary libraries
library(corrplot)

# Select numerical columns only for correlation analysis
numerical_data <- data[, sapply(data, is.numeric)]

#  Calculate the correlation matrix
cor_matrix <- cor(numerical_data, use = "complete.obs")

# Plot the correlation matrix
# This helps to visually identify strong correlations
corrplot(cor_matrix, method = "circle", type = "upper", 
         tl.col = "black", tl.cex = 0.8, number.cex = 0.7, 
         diag = FALSE)
No description has been provided for this image
In [115]:
# Identify and select features with high correlation to SALE_PRC
# Focus on columns with a high absolute correlation to SALE_PRC

cor_sale_prc <- cor_matrix[, "SALE_PRC"]  # Correlations with the target variable
cor_sale_prc_sorted <- sort(cor_sale_prc, decreasing = TRUE)
# View the correlation of each feature with SALE_PRC
print(cor_sale_prc_sorted)
         SALE_PRC      TOT_LVG_AREA     SPEC_FEAT_VAL structure_quality 
     1.0000000000      0.6673009609      0.4974999901      0.3839945707 
       LND_SQFOOT          HWY_DIST         LONGITUDE          LATITUDE 
     0.3630769990      0.2318765566      0.1952742034      0.0477008085 
       month_sold        avno60plus         RAIL_DIST               age 
     0.0003246273     -0.0270255291     -0.0770092480     -0.1234076083 
       WATER_DIST          PARCELNO         CNTR_DIST        OCEAN_DIST 
    -0.1279381020     -0.2040679392     -0.2714252405     -0.2746748563 
       SUBCNTR_DI 
    -0.3700779396 
In [122]:
# Select features for modeling
# Choose features with significant correlation to SALE_PRC 
selected_features <- names(cor_sale_prc_sorted[ cor_sale_prc_sorted > 0])

# Print selected features
print("Selected features for modeling:")
print(selected_features)
[1] "Selected features for modeling:"
[1] "SALE_PRC"          "TOT_LVG_AREA"      "SPEC_FEAT_VAL"    
[4] "structure_quality" "LND_SQFOOT"        "HWY_DIST"         
[7] "LONGITUDE"         "LATITUDE"          "month_sold"       
In [123]:
# Define dependent variable (y) and independent variables (X)
y <- "SALE_PRC"  # Target variable: Sale Price
X <- c("LND_SQFOOT", "TOT_LVG_AREA", "SPEC_FEAT_VAL","HWY_DIST", "SUBCNTR_DI", "structure_quality","LATITUDE","LONGITUDE")  # Independent variables
In [125]:
# Dataset splitting
set.seed(123)  # For reproducibility
trainIndex <- createDataPartition(data[[y]], p = 0.7, list = FALSE)  # 70% training data

# Ensure proper slicing of data
trainData <- data[trainIndex, ]
testData <- data[-trainIndex, ]
In [129]:
#  Preprocess (Scale and Center) the Training Data
preproc <- preProcess(trainData[, X], method = c("center", "scale"))
trainData_scaled <- predict(preproc, trainData)

# Preprocess the Test Data using the same transformation
testData_scaled <- predict(preproc, testData)

# Define the formula for all models
lm_formula <- as.formula(paste(y, "~", paste(X, collapse = "+")))  # Formula for regression

#  Linear Regression Model
lm_model <- lm(lm_formula, data = trainData_scaled)

# Summary of the Linear Regression Model
cat("Linear Regression Model Summary:\n")
print(summary(lm_model))
Linear Regression Model Summary:

Call:
lm(formula = lm_formula, data = trainData_scaled)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.6919 -0.3120 -0.0368  0.2325  5.4385 

Coefficients:
                   Estimate Std. Error t value Pr(>|t|)    
(Intercept)        0.005048   0.005918   0.853   0.3937    
LND_SQFOOT         0.012529   0.006945   1.804   0.0713 .  
TOT_LVG_AREA       0.558415   0.007666  72.841   <2e-16 ***
SPEC_FEAT_VAL      0.129084   0.007157  18.036   <2e-16 ***
HWY_DIST           0.086551   0.006397  13.530   <2e-16 ***
SUBCNTR_DI        -0.149868   0.006904 -21.708   <2e-16 ***
structure_quality  0.258745   0.007282  35.531   <2e-16 ***
LATITUDE          -0.270662   0.009918 -27.291   <2e-16 ***
LONGITUDE          0.422729   0.009864  42.856   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.5845 on 9745 degrees of freedom
Multiple R-squared:  0.6721,	Adjusted R-squared:  0.6718 
F-statistic:  2497 on 8 and 9745 DF,  p-value: < 2.2e-16

In [131]:
# Random Forest Model
set.seed(123)  # Ensure reproducibility
rf_model <- randomForest(lm_formula, data = trainData_scaled, ntree = 100, importance = TRUE)

# Print Random Forest Model results
cat("\nRandom Forest Model Summary:\n")
print(rf_model)

# Variable importance plot for Random Forest
cat("\nVariable Importance from Random Forest:\n")
print(importance(rf_model))
Random Forest Model Summary:

Call:
 randomForest(formula = lm_formula, data = trainData_scaled, ntree = 100,      importance = TRUE) 
               Type of random forest: regression
                     Number of trees: 100
No. of variables tried at each split: 2

          Mean of squared residuals: 0.116294
                    % Var explained: 88.83

Variable Importance from Random Forest:
                   %IncMSE IncNodePurity
LND_SQFOOT        18.23035      727.4472
TOT_LVG_AREA      26.06264     2815.7431
SPEC_FEAT_VAL     17.65398     1315.4594
HWY_DIST          16.91759      551.8016
SUBCNTR_DI        23.61842     1343.4118
structure_quality 18.47449     1230.0835
LATITUDE          10.21103      633.8052
LONGITUDE         14.40084     1354.3957
In [132]:
# Support Vector Machine (SVM) Model
svm_model <- svm(lm_formula, data = trainData_scaled)

# Summary of the SVM Model
cat("\nSupport Vector Machine (SVM) Model Summary:\n")
print(summary(svm_model))
Support Vector Machine (SVM) Model Summary:

Call:
svm(formula = lm_formula, data = trainData_scaled)


Parameters:
   SVM-Type:  eps-regression 
 SVM-Kernel:  radial 
       cost:  1 
      gamma:  0.125 
    epsilon:  0.1 


Number of Support Vectors:  4535





In [133]:
# Evaluate the models on the scaled test data

# Linear Regression Evaluation
lm_pred <- predict(lm_model, newdata = testData_scaled)
lm_rmse <- rmse(testData[[y]], lm_pred)
lm_mae <- mae(testData[[y]], lm_pred)
lm_r2 <- 1 - sum((lm_pred - testData[[y]])^2) / sum((mean(testData[[y]]) - testData[[y]])^2)
lm_mape <- mean(abs((testData[[y]] - lm_pred) / testData[[y]])) * 100

cat("\nLinear Regression Metrics:\n")
cat("  RMSE:", lm_rmse, "\n")
cat("  MAE:", lm_mae, "\n")
cat("  R-squared:", lm_r2, "\n")
cat("  MAPE:", lm_mape, "%\n")
Linear Regression Metrics:
  RMSE: 0.5297724 
  MAE: 0.3584271 
  R-squared: 0.6894946 
  MAPE: 1259.377 %
In [134]:
# Random Forest Evaluation
rf_pred <- predict(rf_model, newdata = testData_scaled)
rf_rmse <- rmse(testData[[y]], rf_pred)
rf_mae <- mae(testData[[y]], rf_pred)
rf_r2 <- 1 - sum((rf_pred - testData[[y]])^2) / sum((mean(testData[[y]]) - testData[[y]])^2)
rf_mape <- mean(abs((testData[[y]] - rf_pred) / testData[[y]])) * 100

cat("\nRandom Forest Metrics:\n")
cat("  RMSE:", rf_rmse, "\n")
cat("  MAE:", rf_mae, "\n")
cat("  R-squared:", rf_r2, "\n")
cat("  MAPE:", rf_mape, "%\n")
Random Forest Metrics:
  RMSE: 0.3022697 
  MAE: 0.1546044 
  R-squared: 0.8989166 
  MAPE: 510.303 %
In [135]:
# SVM Evaluation
svm_pred <- predict(svm_model, newdata = testData_scaled)
svm_rmse <- rmse(testData[[y]], svm_pred)
svm_mae <- mae(testData[[y]], svm_pred)
svm_r2 <- 1 - sum((svm_pred - testData[[y]])^2) / sum((mean(testData[[y]]) - testData[[y]])^2)
svm_mape <- mean(abs((testData[[y]] - svm_pred) / testData[[y]])) * 100

cat("\nSupport Vector Machine (SVM) Metrics:\n")
cat("  RMSE:", svm_rmse, "\n")
cat("  MAE:", svm_mae, "\n")
cat("  R-squared:", svm_r2, "\n")
cat("  MAPE:", svm_mape, "%\n")
Support Vector Machine (SVM) Metrics:
  RMSE: 0.3366777 
  MAE: 0.1735161 
  R-squared: 0.8745937 
  MAPE: 511.8469 %
In [162]:
# Model Comparison (Choose the Best Model)
cat("\nComparison of Models:\n")
cat("Best model is the one with the lowest RMSE and highest R-squared.\n")
cat("Linear Regression RMSE:", lm_rmse, "R-squared:", lm_r2, "\n")
cat("Random Forest RMSE:", rf_rmse, "R-squared:", rf_r2, "\n")
cat("SVM RMSE:", svm_rmse, "R-squared:", svm_r2, "\n")

# Make the final decision on which model to choose
if (rf_rmse == min(c(lm_rmse, rf_rmse, svm_rmse))) {
  cat("\nRandom Forest is the best model based on RMSE.\n")
} else if (svm_rmse == min(c(lm_rmse, rf_rmse, svm_rmse))) {
  cat("\nSupport Vector Machine is the best model based on RMSE.\n")
} else {
  cat("\nLinear Regression is the best model based on RMSE.\n")
}
Comparison of Models:
Best model is the one with the lowest RMSE and highest R-squared.
Linear Regression RMSE: 0.5297724 R-squared: 0.6894946 
Random Forest RMSE: 0.3022697 R-squared: 0.8989166 
SVM RMSE: 0.3366777 R-squared: 0.8745937 

Random Forest is the best model based on RMSE.
In [163]:
# Save the Random Forest Model to a file
save(rf_model, file = "random_forest_model.RData")

# Load the Random Forest Model from the file
load("random_forest_model.RData")
In [170]:
#user input array (ensure it has the same structure as training data)
user_input <- data.frame(
  LND_SQFOOT = c(10000),        # For square footage
  TOT_LVG_AREA = c(2000),       # Total living area
  SPEC_FEAT_VAL = c(0),         # Special features
  HWY_DIST = c(500),            # Distance to highway
  SUBCNTR_DI = c(2),            # Distance to subcenter
  structure_quality = c(4),     # Structure quality (1 to 5 scale)
  LATITUDE = c(25.8910),        # Latitude
  LONGITUDE = c(-80.1606)       # Longitude
)

# Use the same preprocessing to scale the user input (using pre-trained preproc object)
user_input_scaled <- predict(preproc, user_input)

# Make prediction using the saved Random Forest model
load("random_forest_model.RData")  # Load the trained model

# Make the prediction with the scaled user input
user_prediction <- predict(rf_model, newdata = user_input_scaled)
In [171]:
user_prediction
1: 1.96672379965129