# Train the Random Forest Regression model
rf_model <- randomForest(y_train ~ ., data = x_train)
# Make predictions on the test set
predictions <- predict(rf_model, newdata = x_test)
# Calculate evaluation metrics
rmse_rf <- sqrt(mean((predictions - y_test)^2))
r_squared_rf <- cor(predictions, y_test)^2
results_rf <- data.frame(Actual = y_test, Predicted = predictions)
# Plot using ggplot
ggplot(results_rf, aes(x = Actual, y = Predicted)) +
geom_point(alpha = 0.6, color = "lightblue") +
geom_abline(intercept = 0, slope = 1, color = "red", linetype = "dashed") +
ggtitle("Actual vs. Predicted Prices (Random Forest)") +
xlab("Actual Prices") +
ylab("Predicted Prices") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5),
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold")
)

The Random Forest Model exhibits superior performance compared to the Simple Linear Regression Model, as indicated by the closer alignment of the scatter plot points to the diagonal red dashed line, which represents perfect prediction (slope = 1). This proximity suggests that our Random Forest model’s predictions are more accurate, particularly for houses with lower to moderate prices.
However, as the price of houses increases beyond $2,000,000, the model’s accuracy diminishes, evident from the widening spread of data points away from the ideal line. This discrepancy indicates that our model struggles to accurately predict the prices of higher-value houses, potentially due to the complexity of factors influencing their pricing beyond what our model captures.

The Random Forest Feature Importance Plot displays the relative significance of different features in predicting house prices. Features with higher importance contribute more to the model’s predictive accuracy, while those with lower importance have less impact. This visualization helps identify which features are most influential in determining house prices.
The Random Forest Feature Importance Plot reveals that sqft_living and grade are the two most influential features in predicting house price. Interestingly, location-related features such as lat, long, and zipcode also rank among the top 10 most important features, underscoring the significant role of a property’s location in determining its value.