[R] AR(1) model simulation based on 2 variables
Neapyanith Chea
ne@py@n|thche@ @end|ng |rom gm@||@com
Sun May 23 18:27:17 CEST 2021
Hello there,
I am currently perform a simulation on an AR(1) model with variable changes
to sample size and population parameter (denoted as φ).
For example, assume that we are simulating an AR(1) model with sample
size ∈ {10,100} and φ∈{0.1,0.9} and repeat it 100 times.
This can be done with the help of a looping function (crudely made) below.
library(readr)
library(MASS)
library(dynlm)
set.seed(2000)
reps=100
nv <- c(10,100)
phi.hat<- matrix(nrow=reps, ncol=length(nv))
#Looping 100 repeated samples @phi=0.9
for (i in 1:length(nv)){
n=nv[i]
for (j in 1:reps){
Yi=V=ts(rnorm(n, mean=0, sd=1),start=1, end=n, frequency=1)
Y=0+0.9*Yi[-1]+V
eq1=dynlm(Y~L(Y,1))
phi.hat[j,i]=eq1$coefficients[2]
}
}
#Looping 100 repeated samples @ phi=0.1
for (i in 1:length(nv)){
n=nv[i]
for (j in 1:reps){
Yi=V=ts(rnorm(n, mean=0, sd=1),start=1, end=n, frequency=1)
Y=0+0.1*Yi[-1]+V
eq1=dynlm(Y~L(Y,1))
phi.hat[j,i]=eq1$coefficients[2]
}
}
Having done this, I have received a relatively similar sample coefficient
to population parameter (i.e., mean( phi_hat) ≈ phi). However, for
phi=0.9, the value for mean(phi_hat) is not close to phi. I was wondering
why this is the case. Thank you for reading this!
Regards,
Yanith
[[alternative HTML version deleted]]
More information about the R-help
mailing list