** Analyzing Mortgage Interest Rates**

Project Objective

To investigate mortgage rates, identify trends, and forecast future rates using time series and linear modeling

Question 1 & 2: Develop the Model & Asses Predictor Significance

Step 1: Install and load requried libraries

#install.packages("readxl")
#install.packages("ggplot2")
library(readxl) #allows us to import excel files
library(ggplot2) #allows us to make time series plot

Step 2: Import & clean the data

file_path <- "/Users/ericheredia/Downloads/Mortgage.xlsx"
mortgage_data <- read_excel(file_path)
head(mortgage_data)
## # A tibble: 6 × 3
##   Year                Period Interest_Rate
##   <dttm>               <dbl>         <dbl>
## 1 2000-01-01 00:00:00      1          8.05
## 2 2001-01-01 00:00:00      2          6.97
## 3 2002-01-01 00:00:00      3          6.54
## 4 2003-01-01 00:00:00      4          5.83
## 5 2004-01-01 00:00:00      5          5.84
## 6 2005-01-01 00:00:00      6          5.87

Step 3: Summarize the data

head(mortgage_data)
## # A tibble: 6 × 3
##   Year                Period Interest_Rate
##   <dttm>               <dbl>         <dbl>
## 1 2000-01-01 00:00:00      1          8.05
## 2 2001-01-01 00:00:00      2          6.97
## 3 2002-01-01 00:00:00      3          6.54
## 4 2003-01-01 00:00:00      4          5.83
## 5 2004-01-01 00:00:00      5          5.84
## 6 2005-01-01 00:00:00      6          5.87
summary(mortgage_data)
##       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
Data Description: A description of some of the features are presented in the table below.
Variable    |   Definition
------------|--------------
1. Period   | A numeric identifier
2. Interest Rate | The avg. interest rate for the corresponding year
summary(mortgage_data)
Interpretation : The median interest rate is 4.86 for the 30 year fixed rate.

Step 4: Build time series plot

library(ggplot2)
ggplot(mortgage_data, aes(x = Period, y = Interest_Rate)) +
  geom_line() +
  geom_point() +
  labs(title = "30-Year Fixed-Rate Mortgage Interest Rates (Time Series)",
       x = "Period (Years Since 2000)",
       y = "Interest Rate (%)") +
  theme_minimal()

Interpretation: There is a general decline over the past two decades, with significant recent upward movement.

Step 5: Build the linear regression model

model <- lm(Interest_Rate ~ Period, data = mortgage_data)
intercept <- coef(model)[1]
slope <- coef(model)[2]
cat("Trend Equation: Interest Rate = ", round(intercept, 2), " + ", round(slope, 2), " * Period\n")
## Trend Equation: Interest Rate =  6.7  +  -0.13  * Period
Interpretation: There is a general declining trend in mortgage interest rates over the observed time period.

Question 3: Forecast the AVG interest rate for period 25

period_25 <- 25
forecast <- intercept + slope * period_25
cat("Forecasted Interest Rate for Period 25 (2024): ", round(forecast, 2), "%\n")
## Forecasted Interest Rate for Period 25 (2024):  3.47 %
Interpretation:The linear trend equation predicts that the average interest rate for a 30-year fixed mortgage will be approximately 3.47% in 2024.