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 model starting with the first value set to generatear functionn c phi, sigma # Generate errors error rnormn mean sd sigma # Set up vector for the response with initial values set to y rep n # Generate remaining observations fori in seq length n yi c phi yi errori returny 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 observations libraryfpp tsibbletime : y generatearn c phi index time autoploty Modify the generatear function to generate data from any ARMApq model with parameters to be specified by the user. The first line of your function definition should be generatearma functionn c phi NULL, theta NULL, sigma Here phi and theta are vectors of AR and MA coefficients. Your function should return a numeric vector of length n For example generatearman c phi c should return observations generated from the model where epsi is iid N 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: anyabspolyrootcphi The MA parameters will be invertible if the following expression returns TRUE: anyabspolyrootctheta Please submit your solution as a R file. Here is a function that generates data from an A R model starting with the first value set to generatear function n c phi, sigma # Generate errors error rnormn mean theta sd sigma # Set up vector for the response with initial values set to theta yreptheta n # Generate remaining observations for i in seq length n yic phi yierrori returny library fpp tsibbletime : y generatearn c phi index time autoplot y generatearma function n c phi NULL, theta NULL, sigma Here phi and theta are vectors of AR and MA coefficients. Your function should return a numeric vector of length n For example generatearma n c phi c should return observations generated from the model
yt yt ytepsi t
where epsi is iid N The noise should be generated using the rnorm function. any abs polyroot c phi The MA parameters will be invertible if the following expression returns TRUE: any abs polyroot c theta 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
