options(repos = c(CRAN = "https://cran.r-project.org/"))
INSTALLING REQUIRED LIBRARIES
library(xts)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(ggplot2)
library(dplyr)
##
## ######################### 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: 'dplyr'
## The following objects are masked from 'package:xts':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
install.packages("forecast")
## also installing the dependency 'RcppArmadillo'
##
## There is a binary version available but the source version is later:
## binary source needs_compilation
## RcppArmadillo 0.12.6.4.0 0.12.6.6.0 TRUE
##
##
## The downloaded binary packages are in
## /var/folders/1t/lvl69_w12vj1sz_yxkxrvt7w0000gn/T//RtmprH7sXy/downloaded_packages
## installing the source package 'RcppArmadillo'
## Warning in install.packages("forecast"): installation of package
## 'RcppArmadillo' had non-zero exit status
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
IMPORTING DOWNLOADED TIME SERIES DATA
Obesity_eating <- read.csv('/Users/ankit/Downloads/Obesity_eating_time_series.csv')
CREATING A XTS OBJECT
xts_data <- xts(Obesity_eating[, c("Obesity")], order.by = as.Date(Obesity_eating$Date))
FITTING A LINEAR REGRESSION MODEL
# Fit a linear regression model
lm_model <- lm(Obesity ~ time(Date), data = Obesity_eating )
summary(lm_model)
##
## Call:
## lm(formula = Obesity ~ time(Date), data = Obesity_eating)
##
## Residuals:
## Min 1Q Median 3Q Max
## -239.06 -58.60 -21.43 73.48 176.32
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1220.2524 48.1667 25.334 4.17e-16 ***
## time(Date) -0.3649 3.8360 -0.095 0.925
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 106.4 on 19 degrees of freedom
## Multiple R-squared: 0.0004761, Adjusted R-squared: -0.05213
## F-statistic: 0.009051 on 1 and 19 DF, p-value: 0.9252
INTERPRETATION: the linear model does not provide strong evidence of a meaningful relationship between Obesity and time(Date). The estimated slope is not statistically different from zero, and the model does not explain much of the variability in the response variable. It’s important to interpret these results in the context of your specific data and research question. If there is no theoretical or practical reason to include time as a predictor, it might be worthwhile to reconsider the model or explore other factors.
CHECKING FOR MULTIPLE TRENDS and Plotting ACF and PACF
# Install and load necessary packages
install.packages("strucchange")
##
## The downloaded binary packages are in
## /var/folders/1t/lvl69_w12vj1sz_yxkxrvt7w0000gn/T//RtmprH7sXy/downloaded_packages
library(strucchange)
## Loading required package: sandwich
# Converting 'Date' to Date format
Obesity_eating$Date <- as.Date(Obesity_eating$Date)
Obesity_eating_ts <- ts(Obesity_eating$Obesity, start = start(Obesity_eating$Date), frequency = 1)
lm_model <- lm(Obesity ~ time(Date), data = Obesity_eating)
# Plotting ACF and PACF
ggtsdisplay(Obesity_eating_ts, acf = TRUE, pacf = TRUE)
## Warning in ggplot2::geom_segment(lineend = "butt", ...): Ignoring unknown
## parameters: `acf` and `pacf`