回归诊断的r语言代码

来源:互联网 发布:如何查询别人淘宝id 编辑:程序博客网 时间:2024/05/29 21:33
shuju<-data.frame(x=c(825,215,1070,550,480,920,1350,325,670,1215),y=c(3.5,1,4,2,1,3,4.5,1.5,3,5))
shuju.reg<-lm(y~x)


#方差齐性检验
#1.残差图
shuju.res<-residuals(shuju.reg)
par(mfrow=c(1,1))    #设置画图为1*1的格式
plot(shuju.res~x)     #按照书上作以X为横坐标的残差图
#2.Spearman等级相关系数
cor.test(x,abs(shuju.res),method="spearman")
#3.ncvTest
library(car)
ncvTest(shuju.reg)
#4.spreadLevelPlot
library(car)
spreadLevelPlot(shuju.reg)
#5.gvlma综合诊断
#install.packages("gvlma")
library(gvlma)
gvlma(shuju.reg)


#自相关性
#图形诊断自相关性
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)






#异常值与强影响点
#残差
shuju.stdres<-rstudent(shuju.reg);shuju.stdres     
#中心化杠杆值
ch<-hatvalues(shuju.reg)-1/10;ch
#Cook距离
shuju.cooks<-cooks.distance(shuju.reg);shuju.cooks
#找异常值
isoutlier<-abs(shuju.stdres3)>3;isoutlier
isch<-ch>2*1/10;isch
iscooks<-shuju.cooks>1;iscooks
isch*iscooks


#正态性检验
plot(shuju.reg,2)   #qq plot
qqPlot(shuju.reg)   #这个qq plot含有更多的信息
0 0
原创粉丝点击