Assignment 4

Background

Here is a function that generates data from an AR(1) model starting with the first value set to 0

generate_ar1 <- function(n = 100, c = 0, phi, sigma = 1) {
  # Generate errors
  error <- rnorm(n, mean = 0, sd = sigma)
  # Set up vector for the response with initial values set to 0
  y <- rep(0, n)
  # Generate remaining observations
  for(i in seq(2, length = n-1)) {
    y[i] <- c + phi * y[i-1] + error[i]
  }
  return(y)
}

Here n is the number of observations to simulate, c is the constant, phi is the AR coefficient, and sigma is the standard deviation of the noise. The following example shows the function being used to generate 50 observations

library(fpp3)
tsibble(time = 1:50, y = generate_ar1(n=50, c=1, phi=0.8), index = time) |>
  autoplot(y)

Instructions

  1. Modify the generate_ar1 function to generate data from an ARIMA(p,d,q) model with parameters to be specified by the user. The first line of your function definition should be

    generate_arima <- function(n = 100, d = 0, c = 0, phi = NULL, theta = NULL, sigma = 1)

    Here phi and theta are vectors of AR and MA coefficients. Your function should return a numeric vector of length n.

    For example generate_arima(n = 50, d = 1, c = 2, theta = c(0.3, -0.6)) should return 50 observations generated from the ARIMA(0,1,2) model y_t = y_{t-1} + 2 + 0.3\varepsilon_{t-1} - 0.6\varepsilon_{t-2} + \varepsilon_t where \varepsilon \sim N(0,1).

  2. The noise should be generated using the rnorm() function.

  3. Your function should check stationarity and invertibility conditions and return an error if either condition is not satisfied. You can use the stop() function to generate an error. The model will be stationary if the following expression returns TRUE:

    !any(abs(polyroot(c(1,-phi))) <= 1)

    The MA parameters will be invertible if the following expression returns TRUE:

    !any(abs(polyroot(c(1,theta))) <= 1)
  4. The above function sets the first value of every series to 0. Your function should fix this problem by generating more observations than required and then discarding the first few observations. You will need to consider how many observations to discard, to prevent the returned series from being affected by the initial values.

  5. You may find the diffinv() function useful.

Please submit your solution as a .R file.




Due: 3 May 2024
  Submit