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

熱線電話:13121318867

登錄
首頁精彩閱讀R語言中離群值的識別、描述、繪制與移除
R語言中離群值的識別、描述、繪制與移除
2017-02-26
收藏

R語言中離群值的識別、描述、繪制與移除

統(tǒng)計(jì)學(xué)中離群值被定義為離開大部分觀測較遠(yuǎn)的樣本點(diǎn),多數(shù)是由于測量誤差而產(chǎn)生。因此,數(shù)據(jù)分析中離群值的識別和移除(如有必要)是很重要的一個(gè)步驟。

鑒定離群值的方法有很多種,包括基于標(biāo)準(zhǔn)差的方法和基于四分位距的Tukey法。本文我將使用不依賴余數(shù)據(jù)分布類型的Tukey法做演示,該方法的另一個(gè)優(yōu)勢是無需考慮數(shù)據(jù)均值和方差,而這兩個(gè)統(tǒng)計(jì)量恰恰很容易被極端值(離群值)影響。

腳本

我寫了一個(gè)腳本來識別、描述、繪制并移除離群值。我先利用箱線圖來鑒定樣本點(diǎn)是否為離群值。在R中可以使用boxplot.stats()$out命令來繪制這個(gè)圖,該圖能利用Tukey法將游離在1.5倍四分位距外的樣本點(diǎn)單獨(dú)繪制出來。在描述數(shù)據(jù)時(shí),我喜歡匯報(bào)數(shù)據(jù)中離群值的百分比和其均值,同時(shí)我也會分別計(jì)算包含和移除離群值后數(shù)據(jù)的均值。而在各種數(shù)據(jù)圖中,箱線圖和直方圖值最能體現(xiàn)離群值的存在感,因此在下面的腳本中,我將繪制包含和移除離群值的這兩類圖形。最后,在Selva的幫助下,我添加了一個(gè)簡單問題(是/否)來詢問是否要保留離群值。如果選擇是,那么離群值將被NA取代。

腳本如下:

ooutlierKD <- function(dt, var) {
     var_name <- eval(substitute(var),eval(dt))
     tot <- sum(!is.na(var_name))
     na1 <- sum(is.na(var_name))
     m1 <- mean(var_name, na.rm = T)
     par(mfrow=c(2, 2), oma=c(0,0,3,0))
     boxplot(var_name, main="With outliers")
     hist(var_name, main="With outliers", xlab=NA, ylab=NA)
     outlier <- boxplot.stats(var_name)$out
     mo <- mean(outlier)
     var_name <- ifelse(var_name %in% outlier, NA, var_name)
     boxplot(var_name, main="Without outliers")
     hist(var_name, main="Without outliers", xlab=NA, ylab=NA)
     title("Outlier Check", outer=TRUE)
     na2 <- sum(is.na(var_name))
     cat("Outliers identified:", na2 - na1, "\n")
     cat("Propotion (%) of outliers:", round((na2 - na1) / tot*100, 1), "\n")
     cat("Mean of the outliers:", round(mo, 2), "\n")
     m2 <- mean(var_name, na.rm = T)
     cat("Mean without removing outliers:", round(m1, 2), "\n")
     cat("Mean if we remove outliers:", round(m2, 2), "\n")
     response <- readline(prompt="Do you want to remove outliers and to replace with NA? [yes/no]: ")
     if(response == "y" | response == "yes"){
          dt[as.character(substitute(var))] <- invisible(var_name)
          assign(as.character(as.list(match.call())$dt), dt, envir = .GlobalEnv)
          cat("Outliers successfully removed", "\n")
          return(invisible(dt))
     } else{
          cat("Nothing changed", "\n")
          return(invisible(var_name))
     }
}

你可以直接運(yùn)行這些代碼,只要把其中的數(shù)據(jù)集和變量的名稱替換下。

source("http://goo.gl/UUyEzD")
outlierKD(dat, variable)

一個(gè)例子:

Outliers identified: 58
Propotion (%) of outliers: 3.8
Mean of the outliers: 108.1
Mean without removing outliers: 53.79
Mean if we remove outliers: 52.82
Do you want to remove outliers and to replace with NA? [yes/no]: y
Outliers successfully removed

繪制的圖形的例子:

我知道這個(gè)腳本還可以添加不少功能和細(xì)節(jié),很多地方還能優(yōu)化

數(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)的第一個(gè)參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(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){ //倒計(jì)時(shí)完成 $(".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); }