# Q3,4,5
library(readxl)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
mort <- read_excel(file.choose())
# plot 
ggplot(mort, aes(x= Year, y= Interest_Rate))+
  geom_line()+
  geom_point()+
  xlab("Year")+
  ylab("Interest Rate")+
  ggtitle("Mortgage intrest rate")

# Graph show a decrease trend that increases after 2020
# Develope an equation
model <- lm(Interest_Rate~ Period, data = mort )
summary(model)
## 
## Call:
## lm(formula = Interest_Rate ~ Period, data = mort)
## 
## 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
# y= 6.70 +-0.13t or 6.70 - 0.13*Period
# forecast the average interest rate for period 25 (i.e., 2024)
forecast_period_25 <- predict(model, newdata = data.frame(Period= 25))
forecast_period_25 # 3.47
##        1 
## 3.472942