 Write a function named tp420 in R that plots a given vector of data as a time series. Your code should take as input a vector x of data and output a plot of x versus time t.Time t should range from 1 to the length of the vector x.
 Write a function named fd420 that performs the“firstdifference”operation on a given vector x. Your code should take as input a vector x of data and output a vector of firstdifferences computed from x. Your function should also plot (using tp420) the returned vector as a time series.
Recall that the ith firstdifference is the difference between the (i+ 1)th andthe ith elements of x. Check to make sure that the returned vector has length thatis onesmallerthanthelengthofx.
 Writeafunctionnamedsacf420thatcalculatesthesampleautocorrelationfunction fromagivenvectorx.Yourcodeshouldtakeasinputavectorxofdataandoutput avectorofsampleautocorrelationscomputedfromx.Yourfunctionshouldalso plot(usingtp420)thereturnedvectorasatimeseries.
Recall that the ilag sample autocorrelation
γˆ(i)
ρˆ(i)=γˆ(0) i=1,2,…,n−1
whereγˆ(j)isthejlagsamplecovarianceandnisthelengthofx.Yourcode
shouldcalculateγˆ(j)explicitly,thatis,youarenotallowedtousethebuiltin
functioninR.Checktomakesurethatallreturnedcorrelationsarelessthan1in absolutevalue.
 Write a function named arpcoeff420 that estimates and returns the parametersfor an AR(p) model. Your code will take as input a vector x of data and a positive integerp,andreturnparametersfittedfromanAR(p)model.
Recall the AR(p)model:
Y_{t}= µ+ φ_{1}Y_{t}_{−}_{1}+ φ_{2}Y_{t}_{−}_{2}+ … + φ_{p}Y_{t}_{−}_{p}+s_{t}. (1) Your code should return a vector containing estimates of the (p +2) parameters
µ, φ_{1}, φ_{2}, . . . ,φ_{p}, σ^{2}.
The parameters φ_{1}, φ_{2}, . . . ,φ_{p}can be estimated by fitting data to the model
Y_{t}=µ+φ_{1}Y_{t}_{−}_{1}+φ_{2}Y_{t}_{−}_{2}+…+φ_{p}Y_{t}_{−}_{p}+s_{t} (2)
treatingitasanordinarylinearregressionmodel.Accordingly,yourcodeshould have the followingsteps.

 Form the “data” matrix D for the linear regression in (2). If the length of the original data vector x is n, then the data matrix D will have n p rows and p + 1 columns. Looking at (2), we see that the first column of D will bea column of 1s. The second column will have the data x_{p}, x_{p}_{+1}, . . . , x_{n}_{−}_{1}. Likewise,thethirdcolumnofDwillhavethedatax_{p}_{−}_{1},x_{p},…,x_{n}_{−}_{2}andthe last (or p + 1th) column of D will have the data x_{1}, x_{2}, . . . ,x_{n}_{−}_{p}.
 FormthedependentvariablevectorYforthelinearregressionin(2).Again looking at (2), we see that Y should be a (n −p)vector containing thedata x_{p}_{+1},x_{p}_{+2},…,x_{n}.
 PerformlinearregressionwithdatamatrixDanddependentvariableY.
Recallthatthecoefficientsofsuchalinearregressionaregivenby (µ,φˆ1,,φˆ2,…,φˆp)^{T}=(D^{T}D)^{−}^{1}D^{T}Y.
(In R, the solution z to the linear equation Az = b can be obtained using “solve(A,b).” Also, in R, the transpose of a matrix M is obtained as t(M ) and the product of two matrices M and N is obtained as M % ∗%N .)
 Check to make sure that the solution you give has p + 1 elements. Now estimate σ^{2}from theresiduals.
Last Updated on March 3, 2019 by Essay Pro