Haziq Ibrahim
Haziq Ibrahim

Reputation: 1

Time Series Forecast using Arima in R

I am trying to forecast student behaviour by year. It isn't working, maybe because my data is too small. I'm using Arima; however, the trend line keeps showing a straight line which I'm not sure is right. Might be this because ARIMA shows ARIMA(0,0,0) with non-zero mean.

Image Data

Year - General Students - Numeric

How can I forecast a student's behaviour by year?

Upvotes: 0

Views: 113

Answers (1)

Isaiah
Isaiah

Reputation: 2155

Small data set, but this is one way to go about it. Modelling each student separately (with student as a key), and using the tidyverts approach:

library(dplyr)
library(tidyr)
library(tsibble)
library(feasts)
library(fable)

Data set

df <- structure(list(Year = structure(c(1995, 1996, 1997), class = "numeric"), 
                     Student1 = c(3, 1, 3), Student2 = c(2, 2, 2), Student3 = c(2, 
                                                                                3, 3), Student4 = c(2, 3, 2), Student5 = c(3, 3, 4)), row.names = c(NA, 
                                                                                                                                                3L), class = "data. Frame")

Tidy data

df <- df |> pivot_longer(names_to = "Student", cols = starts_with("Student")) |>
  as_tsibble(index = Year, key = Student)

Visualise

df |> autoplot()

df |>
  filter(Student == "Student1") |>
  gg_tsdisplay(value, plot_type = 'partial')

Fit ARIMA

Stu_fit <- df |>
  model(search = ARIMA(value, stepwise = FALSE))

Check fit

glance(Stu_fit)
Stu_fit$search

Stu_fit |>
  filter(Student == "Student1") |>
  gg_tsresiduals()

Forecast

Stu_fit |>
  forecast(h = 5) |>
  filter(.model == 'search') |>
  autoplot()

Hope this is helps! :-)

Upvotes: 1

Related Questions