##The data for this analysis includes monthly contract values.
# Create the data frame
month <- 1:12
contract <- c(240, 352, 230, 260, 280, 322, 220, 310, 240, 310, 240, 230)
df <- data.frame(month, contract)
knitr::opts_chunk$set(echo = TRUE)
df$avg_sale3 <- c(
NA, NA, NA,
(df$contract[1] + df$contract[2] + df$contract[3]) / 3,
(df$contract[2] + df$contract[3] + df$contract[4]) / 3,
(df$contract[3] + df$contract[4] + df$contract[5]) / 3,
(df$contract[4] + df$contract[5] + df$contract[6]) / 3,
(df$contract[5] + df$contract[6] + df$contract[7]) / 3,
(df$contract[6] + df$contract[7] + df$contract[8]) / 3,
(df$contract[7] + df$contract[8] + df$contract[9]) / 3,
(df$contract[8] + df$contract[9] + df$contract[10]) / 3,
(df$contract[9] + df$contract[10] + df$contract[11]) / 3
)
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
df <- df %>%
mutate(
square_error = ifelse(is.na(avg_sale3), NA, (contract - avg_sale3)^2)
)
mse <- mean(df$square_error, na.rm = TRUE)
mse
## [1] 2040.444
#Exponential Smoothing ##Calculate the exponential smoothing values and MSE.
knitr::opts_chunk$set(echo = TRUE)
alpha <- 0.2
exp_smooth <- rep(NA, length(df$contract))
exp_smooth[1] <- df$contract[1]
for (i in 2:length(df$contract)) {
exp_smooth[i] <- alpha * df$contract[i-1] + (1 - alpha) * exp_smooth[i-1]
}
mse_exp_smooth <- mean((df$contract[2:12] - exp_smooth[2:12])^2)
mse_exp_smooth
## [1] 2593.762
knitr::opts_chunk$set(echo = TRUE)
library(readxl)
## Warning: package 'readxl' was built under R version 4.2.3
# Load the mortgage data
df <- read_excel("C:\\Users\\dotua\\Downloads\\Mortgage.xlsx")
# Fit a linear regression model
model <- lm(Interest_Rate ~ Period, data = df)
summary(model)
##
## Call:
## lm(formula = Interest_Rate ~ Period, data = df)
##
## 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