This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
Loan <- read.csv("C:/Users/vj/Desktop/Loan.csv")
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggthemes)
library(ggrepel)
library(ggplot2)
library(forecast)
## Warning: package 'forecast' was built under R version 4.3.2
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
# time series toolkits
library(xts)
## Warning: package 'xts' was built under R version 4.3.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.2
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'xts'
##
## The following objects are masked from 'package:dplyr':
##
## first, last
library(tsibble)
## Warning: package 'tsibble' was built under R version 4.3.2
##
## Attaching package: 'tsibble'
##
## The following object is masked from 'package:zoo':
##
## index
##
## The following object is masked from 'package:lubridate':
##
## interval
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, union
Loan$date_column <- seq(as.Date("2002-01-01"), by = "days", length.out = nrow(Loan))
# Print the result
view(Loan)
# filter only pertinent time series data,
Loan_new <- Loan |>
select(date_column,rate_of_interest) |>
distinct()
# look at the data
View(Loan_new)
# create a tsibble
Loan_ts <- as_tsibble(Loan_new, index=date_column) |>
fill_gaps()
View(Loan_ts)
# an "xts" object separate from the original
Loan_xts <- xts(x = Loan_ts$rate_of_interest,
order.by = Loan_ts$date_column)
Loan_xts <- setNames(Loan_xts, "Interest_rate")
View(Loan_xts)
Loan_xts %>%
ggplot(mapping = aes(x = Index, y = Interest_rate)) +
geom_line() +
labs(title = "Rate_of _interest Over Time",
subtitle = "Overall trends of ROI") +
theme_hc()
The plot shows the overall trajectory of interest rates over the full time period represented in the data.While this gives us a high-level overview, it may be difficult to parse some of the finer trends and patterns from just the full series. To better understand specific phases, we could focus in on subsets of the data.
Loan_ts |>
filter_index("2016-01" ~ "2016-12") |>
ggplot(mapping = aes(x = date_column, y = rate_of_interest)) +
geom_line() +
geom_smooth(method = 'lm', color = 'blue', se=FALSE) +
labs(title = "Rate of interest trend in year 2016") +
theme_hc()
## `geom_smooth()` using formula = 'y ~ x'
Time series data for the year 2016 and then create a plot showing the trend of the “rate_of_interest” variable for that specific year.The plot indicates a very slight upward trend in the rate of interest over the year 2016. The blue linear model trend line, fitted to the data, suggests a subtle increase in the ‘rate_of_interest’ variable over time. However, it’s important to note that the trend appears to be quite modest, and other factors may contribute to fluctuations in the observed values.
Loan_ts |>
index_by(year = floor_date(date_column, 'halfyear')) |>
summarise(avg_ROI = mean(rate_of_interest, na.rm = TRUE)) |>
ggplot(mapping = aes(x = year, y = avg_ROI)) +
geom_line() +
geom_smooth(span = 0.3, color = 'blue', se=FALSE, ) +
labs(title = "Average ROI Over Time",
subtitle = "(by half year)") +
scale_x_date(breaks = "1 year", labels = \(x) year(x)) +
theme_hc()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
The plot illustrates the average rate of interest over time, with data aggregated into half-yearly periods. The blue line, representing a smoothed trend, suggests a subtle upward trajectory in the average rate of interest. This implies that, on average, the rate of interest tends to increase slightly during each half-year period.
Loan_ts |>
filter_index("2016" ~ "2017") |>
drop_na() |>
ggplot(mapping = aes(x = date_column, y = rate_of_interest)) +
geom_point(size=1, shape='O') +
geom_smooth(span=0.2, color = 'blue', se=FALSE) +
labs(title = "Rate of interest change in year 2016 -2017") +
theme_hc()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
The plot visualizes the trend in the rate of interest for the years 2016 and 2017. Each point on the plot represents an observation, and the blue line indicates a smoothed trend.
From the plot, it appears that there is a general trend in the rate of interest during this period. The blue smoothed line suggests the presence of a pattern or direction in the data. Additionally, the scatter points around the line provide an indication of the variability in the rate of interest.
Loan_ts|>
index_by(month = floor_date(date_column, 'quarter')) |>
summarise(avg_sum= mean(rate_of_interest, na.rm = TRUE)) |>
ggplot(mapping = aes(x = month, y = avg_sum)) + geom_line(color="blue",size=1.5) +geom_smooth(span = 0.3, color = 'red', se=FALSE, size=1.5) +labs(title = 'Average ROI over the period', subtitle = "by quarter year",y="Average ROI",x="Year") + scale_x_date(breaks = "1 year", labels = \(x) year(x))+theme_bw()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
The plot illustrates the average rate of interest over time, with data aggregated into quarterly periods. The blue line represents the observed trend, and the red smoothed line provides a clearer picture of the underlying pattern.
From the plot, it appears that there is a fluctuating trend in the average rate of interest. The smoothed red line helps to highlight the general direction of the trend, while the blue line connects the actual quarterly observations.
# Plot ACF and PACF to illustrate seasonality
library(forecast)
Loan_ts <- na.omit(Loan_ts)
my_acf <- acf(Loan_ts$rate_of_interest, lag.max = 30)
my_pacf <- pacf(Loan_ts$rate_of_interest, lag.max = 30)
# Plot ACF
autoplot(my_acf) +
labs(title = "Autocorrelation Function (ACF)",
x = "Lag",
y = "ACF")
# Plot PACF
autoplot(my_pacf) +
labs(title = "Partial Autocorrelation Function (PACF)",
x = "Lag",
y = "PACF")
ACF Interpretation: There is a negative correlation between the two variables. This means that as the date increases, the rate of interest tends to decrease. This could be due to a number of factors, such as economic growth, inflation, or monetary policy. The negative correlation is strongest at lag 1. This means that the rate of interest is most negatively correlated with the rate of interest of the previous period. This suggests that the rate of interest is a non-stationary time series, meaning that its mean and variance change over time.
PACF Interpretation: The Partial Autocorrelation Function (PACF) plot for time series between date and rate of interest shows that there is a significant negative correlation between the rate of interest and the rate of interest of the previous period (lag 1). This indicates that the rate of interest is a non-stationary time series, meaning that its mean and variance change over time. The negative correlation gradually weakens as the lag increases, suggesting that the rate of interest is also influenced by long-term trends and other factors.