# Optimizing Decision Trees for Housing Price Prediction: The Boston Housing Dataset in R

## Introduction

Predicting housing prices is a classic regression problem in the field of machine learning. Decision trees are among the most accessible and widely used algorithms for this purpose due to their simplicity and interpretability. This article delves into the use of decision trees in R for predicting median housing prices using the `rpart` package and the Boston Housing dataset.

## The Boston Housing Dataset: A Primer

The Boston Housing dataset is a renowned dataset in machine learning, consisting of information collected by the U.S Census Service concerning housing in the area of Boston Mass. It includes data on various aspects such as crime rates, average number of rooms, accessibility to highways, and more. The goal is to predict the median value of owner-occupied homes (medv).

## Decision Trees for Regression: A Brief Overview

Unlike classification trees, regression trees predict a continuous quantity. In the context of the Boston Housing dataset, a regression tree will be built to predict the median value of homes based on the input features.

## Crafting a Decision Tree in R

### 1. Initial Setup

We begin by importing the necessary libraries and dataset:

`````````R
library(rpart)
library(mlbench)

# Load the Boston Housing dataset
data(BostonHousing)
`````````

### 2. Model Training

We train the decision tree model with a minimum split criterion:

`````````R
# Train the decision tree model with a control on minimum split
fit <- rpart(medv~., data=BostonHousing, control=rpart.control(minsplit=5))

# Display the model summary
print(fit)
`````````

### 3. Prediction Phase

The model can now be used to predict housing prices:

`````````R
# Make predictions on the dataset
predictions <- predict(fit, BostonHousing[,1:13])
`````````

### 4. Accuracy Assessment

The mean squared error (MSE) is a common metric for evaluating regression models:

`````````R
# Calculate the Mean Squared Error (MSE) for the predictions
mse <- mean((BostonHousing\$medv - predictions)^2)
print(mse)
`````````

## Conclusion

Decision trees provide a user-friendly approach to predicting housing prices, as demonstrated with the Boston Housing dataset in R. The `rpart` package makes it convenient to implement and tweak decision trees, ensuring models can be tailored to specific datasets for improved accuracy.

## End-to-End Coding Example:

To encapsulate the entire process, here’s the full code:

`````````R
# Predicting Housing Prices with Decision Trees in R

library(rpart)
library(mlbench)

# Load the Boston Housing dataset
data(BostonHousing)

# Train the decision tree model with specified control parameters
fit <- rpart(medv~., data=BostonHousing, control=rpart.control(minsplit=5))

# Print out a summary of the model
print(fit)

# Use the model to make predictions
predictions <- predict(fit, BostonHousing[,1:13])

# Evaluate the model's accuracy using Mean Squared Error
mse <- mean((BostonHousing\$medv - predictions)^2)
print(mse)
`````````

Executing the code above will build a decision tree regression model to predict median house values in the Boston area, providing an estimate of the model’s accuracy through the MSE. It showcases the practical application of decision trees in a real-world regression scenario within the R environment.