rm(list = ls()) #数据处理 library(openxlsx) library(car) library(lmtest) data = read.xlsx("xiti4.xlsx",sheet = 1) data attach(data)
fm1 = lm(y~x1+x2+x3+x4+x5+x6+x7) #多元回归模型 coef(fm1)
#残差图:残差分析 ei = resid(fm1) X = cbind(1,as.matrix(data[,2:8])) t = ti(ei,X) #外部学生化残差 r = ri(ei,X) #学生化残差 plot(fitted(fm1),t)
上图表明:外扩,方差不齐
#判别强影响点 #cook距离 res = cooks.distance(fm1) res[res>4/(25-7-1)]
15 20 22 23 24 0.7626036 0.4177263 1.0797246 115.3547590 5.9390636
influence.measures(fm1)
inf列显示强影响点
#判别异常点 calcu_F = function(p,r) #p回归参数个数,r学生化残差 { n = length(r) ans = (n-p-2)*(r**2)/(n-p-1-r**2) return(ans) } FF = calcu_F(7,r) f_val = qf(1-0.05,1,17) FF[FF>f_val] #F统计量
15 20 23 24 6.369148 8.245486 27.667296 10.433935
> #判别异常点 > outlierTest(fm1) #Bonferroni p<0.05此点异常 rstudent unadjusted p-value Bonferroni p 23 -5.259971 7.7773e-05 0.0019443