99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀R語言線性回歸診斷
R語言線性回歸診斷
2018-01-15
收藏

R語言線性回歸診斷

回歸診斷主要內(nèi)容
(1).誤差項是否滿足獨(dú)立性,等方差性與正態(tài)
(2).選擇線性模型是否合適
(3).是否存在異常樣本
(4).回歸分析是否對某個樣本的依賴過重,也就是模型是否具有穩(wěn)定性
(5).自變量之間是否存在高度相關(guān),是否有多重共線性現(xiàn)象存在
通過了t檢驗(yàn)與F檢驗(yàn),但是做為回歸方程還是有問題

#舉例說明,利用anscombe數(shù)據(jù)
## 調(diào)取數(shù)據(jù)集
data(anscombe)
## 分別調(diào)取四組數(shù)據(jù)做回歸并輸出回歸系數(shù)等值
ff <- y ~ x
for(i in 1:4) {
   ff[2:3] <- lapply(paste(c("y","x"), i, sep=""), as.name)
   assign(paste("lm.",i,sep=""), lmi<-lm(ff, data=anscombe))
}
GetCoef<-function(n) summary(get(n))$coef
lapply(objects(pat="lm\\.[1-4]$"), GetCoef)
[[1]]
             Estimate Std. Error  t value    Pr(>|t|)
(Intercept) 3.0000909  1.1247468 2.667348 0.025734051
x1          0.5000909  0.1179055 4.241455 0.002169629

[[2]]
            Estimate Std. Error  t value    Pr(>|t|)
(Intercept) 3.000909  1.1253024 2.666758 0.025758941
x2          0.500000  0.1179637 4.238590 0.002178816

[[3]]
             Estimate Std. Error  t value    Pr(>|t|)
(Intercept) 3.0024545  1.1244812 2.670080 0.025619109
x3          0.4997273  0.1178777 4.239372 0.002176305

[[4]]
             Estimate Std. Error  t value    Pr(>|t|)
(Intercept) 3.0017273  1.1239211 2.670763 0.025590425
x4          0.4999091  0.1178189 4.243028 0.002164602

從計算結(jié)果可以知道,Estimate, Std. Error,  t value,    Pr(>|t|)這幾個值完全不同,并且通過檢驗(yàn),進(jìn)一步發(fā)現(xiàn)R^2,F值,p值完全相同,方差完全相同。事實(shí)上這四組數(shù)據(jù)完全不同,全部用線性回歸不合適。
## 繪圖
op <- par(mfrow=c(2,2), mar=.1+c(4,4,1,1), oma=c(0,0,2,0))
for(i in 1:4) {
    ff[2:3] <- lapply(paste(c("y","x"), i, sep=""), as.name)
    plot(ff, data =anscombe, col="red", pch=21,
         bg="orange", cex=1.2, xlim=c(3,19), ylim=c(3,13))
    abline(get(paste("lm.",i,sep="")), col="blue")
}
mtext("Anscombe's 4 Regression data sets",
       outer = TRUE, cex=1.5)
par(op)

第1組數(shù)據(jù)適用于線性回歸模型,第二組使用二次模型更加合理,第三組的一個點(diǎn)偏離于整體數(shù)據(jù)構(gòu)成的回歸直線,應(yīng)該去掉。第四級做回歸是不合理的,回歸系只依賴一個點(diǎn)。在得到回歸方程得到各種檢驗(yàn)后,還要做相關(guān)的回歸診斷。
殘差檢驗(yàn)
殘差的檢驗(yàn)是檢驗(yàn)?zāi)P偷恼`差是否滿足正態(tài)性和方差齊性,最簡單直觀的方法是畫出殘差圖。觀察殘差分布情況,作出散點(diǎn)圖
#20-60歲血壓與年齡分析
## (1) 回歸
rt<-read.table("d:/R-TT/book1/1_R/chap06/blood.dat", header=TRUE)
lm.sol<-lm(Y~X, data=rt); lm.sol
summary(lm.sol)

Call:
lm(formula = Y ~ X, data = rt)

Residuals:
     Min       1Q   Median       3Q      Max
-16.4786  -5.7877  -0.0784   5.6117  19.7813

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 56.15693    3.99367  14.061  < 2e-16 ***
X            0.58003    0.09695   5.983 2.05e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 8.146 on 52 degrees of freedom
Multiple R-squared:  0.4077,    Adjusted R-squared:  0.3963
F-statistic: 35.79 on 1 and 52 DF,  p-value: 2.05e-07

## (2) 殘差圖
pre<-fitted.values(lm.sol)
 #fitted value 配適值;擬合值
res<-residuals(lm.sol)
 #計算回歸模型的殘差
rst<-rstandard(lm.sol)
 #計算回歸模型標(biāo)準(zhǔn)化殘差
par(mai=c(0.9, 0.9, 0.2, 0.1))
plot(pre, res, xlab="Fitted Values", ylab="Residuals")
savePlot("resid-1", type="eps")

plot(pre, rst, xlab="Fitted Values",
     ylab="Standardized Residuals")
savePlot("resid-2", type="eps")

殘差
標(biāo)準(zhǔn)差


## (3) 對殘差作回歸,利用殘差絕對值與自變量(x)作回歸,其程序如下:
rt$res<-res
lm.res<-lm(abs(res)~X, data=rt); lm.res
summary(lm.res)
Call:
lm(formula = abs(res) ~ X, data = rt)

Residuals:
    Min      1Q  Median      3Q     Max
-9.7639 -2.7882 -0.1587  3.0757 10.0350

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -1.54948    2.18692  -0.709  0.48179    
X            0.19817    0.05309   3.733  0.00047 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 4.461 on 52 degrees of freedom
Multiple R-squared:  0.2113,    Adjusted R-squared:  0.1962
F-statistic: 13.93 on 1 and 52 DF,  p-value: 0.0004705
si= -1.5495 + 0.1982x

## (4) 計算殘差的標(biāo)準(zhǔn)差,利用方差(標(biāo)準(zhǔn)差的平方)的倒數(shù)作為樣本點(diǎn)的權(quán)重,這樣可以減少非齊性方差帶來的影響
s<-lm.res$coefficients[1]+lm.res$coefficients[2]*rt$X
lm.weg<-lm(Y~X, data=rt, weights=1/s^2); lm.weg
summary(lm.weg)
Call:
lm(formula = Y ~ X, data = rt, weights = 1/s^2)

Weighted Residuals:
    Min      1Q  Median      3Q     Max
-2.0230 -0.9939 -0.0327  0.9250  2.2008

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 55.56577    2.52092  22.042  < 2e-16 ***
X            0.59634    0.07924   7.526 7.19e-10 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.213 on 52 degrees of freedom
Multiple R-squared:  0.5214,    Adjusted R-squared:  0.5122
F-statistic: 56.64 on 1 and 52 DF,  p-value: 7.187e-10
修正后的回歸方程:Y = 55.5658 + 0.5963x

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }