Assignment 2 2022
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Assignment 2
YOUR NAME
2022
Question 1
1. Write a R function that generates (simulates) n observations from the sequence
yt = a + 6yt − 1 + εt . t = 1. 2. ===. n
starting with y0 = 0, where εt is a discrete random variable that takes the value 1 with prob .5 and the
value -1 with prob .5.
# write your code here
2. Write another function that does the same problem, but this time under the assumption that εt is N(0,1).
# write your code here
3. Use these two functions to generate 500 observations on each process. Assume that a = =5 and 6 = =7.
# write your code here
set.seed(1)
4. Use the ggplot2 package to display the two time series you have generated on the same plot.
# write your code here
5. Use the acf function in R to calculate and plot the first 10 autocorrelations of each series.
# write your code here
Question 2
Suppose that in the population, y , the variable of interest, follows a N(u,72 ) distribution, where u is the mean and 72 is the variance. Suppose that you have n iid observations on y , yi , where i goes from 1 to n.
1. Write down the model of yi as a regression.
2. Under the assumption that u = 2 and 72 = 4, write down code for generating n = 200 iid observations from the population. Store the generated data in a data.frame called datdf.
# write your code here
set.seed(11)
3. Now use the ucminf function in the ucminf package to minimize the cost function (negative of the log-likelihood) over 9 = (u. 7). In calculating the log-likelihod assume that the density of yi is the normal density (R function dnorm). Then use the numDeriv package to calculate the inverse of the hessian matrix at the cost-minimizing value of 9 .
# write your code here
4. Now use the MCMCregressg function to estimate the N(u,72 ) model on the data you just generated. Based on the output, what are the posterior mean and sd of u and 72 ?
# write your code here
2022-03-12