** Chapter 16: Mortgage Rates**

Project Objective

To find the estimated mortgage rate for year 2024.

Linear Trend Regression Approach

Step 1: Install and load required libraries

library(readxl)
library(ggplot2)

Step 2: Import the data

mortgage_rates <- read_excel("Mortgage.xlsx")
Data Description: A description of the features are presented in the table below. 
Variable          | Definition 
-------------     | -------------
1. Period         | Year # of data
2. Interest_Rates | Mortgage Rate 

Step 3: Summarize the data

summary(mortgage_rates)
##       Year                         Period      Interest_Rate  
##  Min.   :2000-01-01 00:00:00   Min.   : 1.00   Min.   :2.958  
##  1st Qu.:2005-10-01 18:00:00   1st Qu.: 6.75   1st Qu.:3.966  
##  Median :2011-07-02 12:00:00   Median :12.50   Median :4.863  
##  Mean   :2011-07-02 18:00:00   Mean   :12.50   Mean   :5.084  
##  3rd Qu.:2017-04-02 06:00:00   3rd Qu.:18.25   3rd Qu.:6.105  
##  Max.   :2023-01-01 00:00:00   Max.   :24.00   Max.   :8.053
Interpretation: On average the mortgage rate was around 5.084% over the past 24 years.

Step 4: Construct a time Series Plot

ggplot(mortgage_rates, aes(x=Period, y=Interest_Rate)) +
  geom_line() +
  geom_point() +
  xlab("Period") +
  ylab("Rates") + 
  ggtitle("Time series plot of Mortgage Rates")

Interpretation: We can notice a decreasing pattern with a main increase in the rate towards 2023. 

Step 5: Devolop a linear Trend Equation

model <- lm(Interest_Rate ~ Period, data = mortgage_rates )
summary(model)
## 
## Call:
## lm(formula = Interest_Rate ~ Period, data = mortgage_rates)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.3622 -0.7212 -0.2823  0.5015  3.1847 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.69541    0.43776  15.295 3.32e-13 ***
## Period      -0.12890    0.03064  -4.207 0.000364 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.039 on 22 degrees of freedom
## Multiple R-squared:  0.4459, Adjusted R-squared:  0.4207 
## F-statistic:  17.7 on 1 and 22 DF,  p-value: 0.0003637
Result - estimated linear trend equation: rates= 6.70 - 0.13*period
The R- square is 0.45 (Fits the data Moderatly)
The overall model is significant as the p-value < 0.05

Step 6: Calculate the fitted values from the model

mortgage_rates$predicted_rates <- predict(model)
Interpretation: This is able to show predictions of what the interest rate should look like each year

Calculate the residuals

mortgage_rates$residuals <- mortgage_rates$Interest_Rate - mortgage_rates$predicted_rates
Interpretation: Looking at the difference between the actual rate and the estimated rate 

Forecast the mortgage rates in 2024 (i.e., Period 25)

Forecast_Period_25 <- predict(model, newdata = data.frame(Period = 25))
Forecast_Period_25
##        1 
## 3.472942
Interpretation: The interest rate would be around 3.47 in the year 2024.