library(MASS)
mu <- c(0,0)
S <- matrix(c(1,.5,.5,1),2,2)
N <- 500
datum <- mvrnorm(N,mu,S)
e <- c(rbind(datum[,1], datum[,2]))
x <- 1*rnorm(1000)
y <- 1 + x +e
mod1 <- lm(y~x)
mod1 <- lm(y~x)
durbinWatsonTest(mod1)
> durbinWatsonTest(mod1)
lag Autocorrelation D-W Statistic p-value
1 0.2655003 1.467445 0
Alternative hypothesis: rho != 0
j <- acf(e)
j
[COLOR=#000000] 0 1 2 3 4 5 6 7 8 9 10 11 1.000 0.198 -0.014 0.014 -0.031 -0.005 -0.046 -0.055 -0.015 0.030 -0.005 -0.049 12 13 14 15 16 17 18 19 20 21 22 23 -0.029 -0.020 -0.067 0.000 0.014 -0.029 0.013 -0.013 0.014 0.024 -0.047 0.007 24 25 26 27 28 29 30 -0.006 0.042 0.031 0.008 0.012 0.032 0.066
[/COLOR]
n=10000
x= matrix(ncol= 5, nrow= n)
x1= rnorm(n)
x[ , 1]= x1
x[ , 2]= c(0, x1[ 1:(n-1)])
x[ , 3]= c(rep(0, 2), x1[1:(n-2)])
x[ , 4]= c(rep(0, 3), x1[1:(n-3)])
x[ , 5]= c(rep(0, 4), x1[1:(n-4)])
head(x)
covm= as.matrix(read.table( text= "
1.00 0.50 0.35 0.15 0.10
0.50 1.00 0.25 0.12 0.05
0.35 0.25 1.00 0.08 0.03
0.15 0.12 0.08 1.00 0.01
0.10 0.05 0.03 0.01 1.00
", stringsAsFactors= F
))
x= x %*% chol(covm)
cor(x)
acf(x[ , 1], plot= F)
[/FONT][/COLOR][COLOR=#000000][FONT=Times New Roman][COLOR=#000000][FONT=Times New Roman]
n=10000
x= matrix(ncol= 5, nrow= n)
x1= rnorm(n)
x[ , 1]= x1
x[ , 2]= c(0, x1[ 1:(n-1)])
x[ , 3]= c(rep(0, 2), x1[1:(n-2)])
x[ , 4]= c(rep(0, 3), x1[1:(n-3)])
x[ , 5]= c(rep(0, 4), x1[1:(n-4)])
colnames(x)= c("x1", "x2", "x3", "x4", "x5")
head(x)
covm= as.matrix(read.table( text= "
1.00 0.50 0.35 0.15 0.00
0.50 1.00 0.00 0.00 0.00
0.35 0.00 1.00 0.00 0.00
0.15 0.00 0.00 1.00 0.00
0.00 0.00 0.00 0.00 1.00
", stringsAsFactors= F
))
x= x %*% chol(covm)
colnames(x)= c("x1", "x2", "x3", "x4", "x5")
head(x)
cor(x)
acf(x[ , 1], lag.max= 5, plot= F)
acf(x[ , 2], lag.max= 5, plot= F)
acf(x[ , 3], lag.max= 5, plot= F)
acf(x[ , 4], lag.max= 5, plot= F)
acf(x[ , 5], lag.max= 5, plot= F)
n= 1000
p= 0.5
x_tm0= rnorm(n)
x_tm1= c(0, x_tm0[1:(n-1)])
# x_tm1
b= rbinom(n, 1, p)
# b_tm1
x1= b* x_tm1+ (1-b)* x_tm0
acf(x1, lag.max= 5, plot= F)
x2= p* x_tm1+ (1-p)* x_tm0
acf(x1, lag.max= 5, plot= F)