自相关性的诊断以及修正方法r语言代码

来源:互联网 发布:python setdefault函数 编辑:程序博客网 时间:2024/04/30 19:54
x<-c(127.3,130.0,132.7,129.4,135.0,137.1,141.1,142.8,145.5,145.3,
148.3,146.4,150.2,153.1,157.3,160.7,164.2,165.6,168.7,172.0)
y<-c(20.96,21.40,21.96,21.52,22.39,22.76,23.48,23.66,24.10,24.01,
24.54,24.28,25.00,25.64,26.46,26.98,27.52,27.78,28.24,28.78)
shuju<-data.frame(x,y)
shuju.reg<-lm(y~x)
summary(shuju.reg)  #(1)建立回归方程
shuju.res
#自相关性
#图形诊断自相关性
shuju.res1<-shuju.res[1:length(shuju.res)-1]   
shuju.res2<-shuju.res[2:length(shuju.res)]
plot(shuju.res1,shuju.res2)   #e(t)与e(t-1)作图


#统计量诊断自相关性
library(car)
durbinWatsonTest(shuju.reg)


#迭代法
acf.1<-acf(shuju.res) #计算各阶自相关系数;acf.1;acf.1$acf[[2]]
rhohat <- 1-0.6632531/2;rhohat 
newy<-y[2:length(y)]-rhohat*y[1:length(y)-1]
newx<-x[2:length(y)]-rhohat*x[1:length(y)-1]
new.reg<-lm(newy~newx)
summary(new.reg)
durbinWatsonTest(new.reg) 


#差分法
diffy <- diff(y)
diffx <- diff(x)
diff.reg <- lm(diffy~diffx-1)
summary(diff.reg)
durbinWatsonTest(diff.reg)
2 0
原创粉丝点击