data <- read.csv("D:/2024-2025/FA24/Stats/mergedfile.csv")
Task: Select a column in your dataset that encodes time (e.g., Date), and convert it to a proper date format in R.
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
data$Date <- as.Date(data$Date, format = "%Y-%m-%d")
Task: Example filtering for a specific stock (e.g., Boeing):
boeing_data <- data[data$Symbol == "BA", ]
Task: Create a tsibble object with the time and response variable, and generate time series plots for different time windows.
library(tsibble)
## Registered S3 method overwritten by 'tsibble':
## method from
## as_tibble.grouped_df dplyr
##
## Attaching package: 'tsibble'
## The following object is masked from 'package:lubridate':
##
## interval
## The following objects are masked from 'package:base':
##
## intersect, setdiff, union
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
# Example Data
data <- data.frame(
Date = seq(as.Date("2020-01-01"), as.Date("2020-12-31"), by = "day"),
Symbol = "BA",
Adj.Close = runif(366, 100, 200)
)
# Ensure no issues
data <- data %>%
arrange(Date) %>%
distinct(Date, .keep_all = TRUE)
# Convert to tsibble
ts_data <- as_tsibble(data, index = Date)
library(ggplot2)
ggplot(ts_data, aes(x = Date, y = Adj.Close)) +
geom_line() +
ggtitle("Adjusted Close Price Over Time") +
xlab("Date") +
ylab("Adjusted Close Price")
Task: Fit a linear regression model to identify trends
# Convert Date to numeric for regression
data$Date_numeric <- as.numeric(data$Date)
# Fit a linear regression model
lm_trend <- lm(Adj.Close ~ Date, data = ts_data)
summary(lm_trend)
##
## Call:
## lm(formula = Adj.Close ~ Date, data = ts_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -53.168 -21.998 -1.241 20.708 50.103
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 433.55485 249.50056 1.738 0.0831 .
## Date -0.01531 0.01353 -1.132 0.2584
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 27.34 on 364 degrees of freedom
## Multiple R-squared: 0.003508, Adjusted R-squared: 0.0007703
## F-statistic: 1.281 on 1 and 364 DF, p-value: 0.2584
# Add trend line to plot
ggplot(ts_data, aes(x = Date, y = Adj.Close)) +
geom_line() +
geom_smooth(method = "lm", color = "blue") +
ggtitle("Trend in Adjusted Close Prices") +
xlab("Date") +
ylab("Adjusted Close Price")
## `geom_smooth()` using formula = 'y ~ x'
Task: Apply smoothing techniques (e.g., moving averages) to identify seasonal patterns
library(fable)
## Warning: package 'fable' was built under R version 4.4.2
## Loading required package: fabletools
## Warning: package 'fabletools' was built under R version 4.4.2
smoothed <- ts_data %>%
model(ETS(Adj.Close ~ season("A")))
library(forecast)
## Warning: package 'forecast' was built under R version 4.4.2
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
Acf(data$Adj.Close)
Pacf(data$Adj.Close)