Below is a step‐by‐step workflow—complete with annotated R code snippets—that you can adapt for your time series analysis. It walks you through:
To itemize the steps use * followed by a space as shown below. This will itemize the steps using bullet points.
For ordered list, use numbers for example:
Tutorial renders words in italics, or single Tutorial.
This is used to comment texts within RMarkdown ### Bold face Sample or Sample renders word in bold face.
The first thing we want to do is to load all necessary libraries.
# install.packages(c("forecast","tseries","lubridate","ggplot2"))
library(forecast)
library(tseries)
library(lubridate)
library(ggplot2)
Next, let’s import the data set. You cn use Ctrl + Alt + I to create another cell for chunk of R codes.
#import data
setwd("C:\\Users\\LENOVO\\Desktop\\My Stuff") #set work directory just we do in RScript
df <- read.csv("Zamfara_STATE.csv")
Wonna check few heads?
head(df)
cases_zam <- df$Confirmed.uncomplicated.Malaria[1:108]
plot(cases_zam, type = "l", col = "red", ylab = "Malaria cases", xlab = "Time (in months)", lwd = 3)
#creates date sequence
date <- seq(as.Date("2015/1/1"), as.Date("2023/12/1"), by = "month")
#create new dataframe
data <- data.frame(cases_zam, date, df$Temperature[1:108], df$Rainfall[1:108])
start_year <- year(min(data$date))
start_month <- month(min(data$date))
y_ts <- ts(data$cases_zam, start = c(start_year, start_month), frequency = 12)
x1_ts <- ts(data$df.Temperature.1.108., start = c(start_year, start_month), frequency = 12)
x2_ts <- ts(data$df.Rainfall.1.108., start = c(start_year, start_month), frequency = 12)
autoplot(y_ts) + ggtitle("Monthly malaria cases")
ggAcf(y_ts) + ggtitle("ACF of Incidence")
ggPacf(y_ts) + ggtitle("PACF of incidence")
ggseasonplot(y_ts, year.labels = TRUE) + ggtitle("Seasonal Plot of Incidence")
Look for:
Test unit-root:
adf.test(y_ts)
##
## Augmented Dickey-Fuller Test
##
## data: y_ts
## Dickey-Fuller = -3.9584, Lag order = 4, p-value = 0.01384
## alternative hypothesis: stationary
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.