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

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) Modify the generate_ar1 function to generate data from any ARMA(p,q) model with parameters to be specified by the user. The first line of your function definition should be generate_arma <- function(n =100, 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_arma(n =50, c =2, phi = c(0.4,-0.6)) should return 50 observations generated from the model where \epsi is iid N(0,1). The noise should be generated using the rnorm() function. 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) Please submit your solution as a .R file. Here is a function that generates data from an A R(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 =\theta , sd = sigma ) # Set up vector for the response with initial values set to \theta y<-rep(\theta , n) # Generate remaining observations for (i in seq (2, length =n-1)){ y[i]<-c+ phi * y[i-1]+error[i]} return(y)} library (fpp 3) tsibble(time =1: 50, y= generate_ar1(n=50, c=1, phi=0.8), index = time)|> autoplot (y) generate_arma <- function (n=100, 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_arma (n=50, c=2, phi =c(0.4,-0.6)) should return 50 observations generated from the model
y_t=2+0.4 y_t-1-0.6 y_t-2+\epsi _t
where \epsi is iid N(0,1) The noise should be generated using the rnorm() function. ! 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) Please submit your solution as a .R file.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!