檢測多重共線
> library(car)
> vif(fit)
Population Illiteracy? ? Income? ? ? Frost
? 1.245282? 2.165848? 1.345822? 2.082547
> sqrt(vif(fit))>2 #problem?
Population Illiteracy? ? Income? ? ? Frost
? ? FALSE? ? ? FALSE? ? ? FALSE? ? ? FALSE
> #多重共線性可用統(tǒng)計量VIF進(jìn)行檢測。VIF的平方根表示變量回歸參數(shù)的置信區(qū)間的膨脹為與模型無關(guān)的預(yù)測變量的程度。
離群點
> outlierTest(fit)
? ? ? rstudent unadjusted p-value Bonferonni p
Nevada 3.542929? ? ? ? 0.00095088? ? 0.047544
> #若不顯著,則說明數(shù)據(jù)集中么有離群點,若顯著,則你必須刪除該離群點,然后再檢驗是否還有其他離群點的存在。
> #高杠桿值的觀測點可通過帽子統(tǒng)計量(hat statistics)判斷。對于一個給定的數(shù)據(jù)集,帽子均值為P/N,其中P是模型估計的參數(shù)數(shù)目(包含截距項),n是樣本量。一般來說,若觀測點的帽子值大于帽子均值的2或3倍,就可以認(rèn)為高杠桿點。
高杠桿點
> hat.plot<- function(fit){
+ p<- length(coefficients(fit))
+ n<- length(fitted(fit))
+ plot(hatvalues(fit), main="Index Plot Hat Value")
+ abline(h=c(2,3)*p/n, col="red", Ity=2)
+ identity(1:n, hatvalues(fit), names(hatvalues(fit)))
+ }
> hat.plot(fit)
強影響點
> cutoff <- 4/(nrow(states)-length(fit$coeffficients)-2)
> plot(fit, which=4, cook.levels=cutoff)
> abline(h=cutoff, Ity=2, col="red")
> avPlots(fit, ask=FALSE, id.method="identify")
> #avplot()函數(shù)可提供變量添加圖
> influencePlot(fit, id.method="identify", main="Influence Plot", sub="Circle size is propertional to Cook's distance")
? ? ? ? ? ? ? ? StudRes? ? ? ? Hat? ? ? CookD
Alaska? ? ? ? 1.7536917 0.43247319 0.448050997
California? -0.2761492 0.34087628 0.008052956
Nevada? ? ? ? 3.5429286 0.09508977 0.209915743
Rhode Island -2.0001631 0.04562377 0.035858963
> #利用influence()函數(shù),可以將離群點、杠桿值和強影響點的信息整合到一幅圖形中。
變量變換
> summary(powerTransform(states$Murder))
bcPower Transformation to Normality
? ? ? ? ? ? ? Est Power Rounded Pwr Wald Lwr Bnd Wald Upr Bnd
states$Murder? ? 0.6055? ? ? ? ? 1? ? ? 0.0884? ? ? 1.1227
Likelihood ratio test that transformation parameter is equal to 0
(log transformation)
? ? ? ? ? ? ? ? ? ? ? ? ? LRT df? ? pval
LR test, lambda = (0) 5.665991? 1 0.017297
Likelihood ratio test that no transformation is needed
? ? ? ? ? ? ? ? ? ? ? ? ? LRT df? ? pval
LR test, lambda = (1) 2.122763? 1 0.14512
There were 14 warnings (use warnings() to see them)
> #powerTransform()函數(shù)通過lambda的最大似然值來估計正態(tài)化變量X^λ。
> #Murder^0.6來正態(tài)化變量Murder。由于0.6很接近0.5,你可以嘗試用平方根變化來提高模型正態(tài)性的符合程度。
> #在本例中λ=1的假設(shè)接受原假設(shè),(p=0.145),因此沒有強有力的證據(jù)表明本例需要變量變化。
> boxTidwell(Murder~ Population+Illiteracy, data=states)
? ? ? ? ? MLE of lambda Score Statistic (z) Pr(>|z|)
Population? ? ? 0.86939? ? ? ? ? ? -0.3228? 0.7468
Illiteracy? ? ? 1.35812? ? ? ? ? ? ? 0.6194? 0.5357
iterations =? 19
> #boxTidwell()函數(shù)通過獲得預(yù)測變量冪數(shù)的最大似然估計值來改善線性關(guān)系。上述例子結(jié)果表明,
> #使用Population^0.87和Illiteracy^1.36能夠大大改善線性關(guān)系,但是對Population(p=0.75)和Illiteracy(p=0.54)的計分檢驗又表明變量并不需要變化。
> #響應(yīng)變量變換還能改善異方差性(誤差方差非恒定)