Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[線性回歸] [資料探索] 如何知道資料集右偏或是左偏,並做相關調整 #2

Open
ningchencontact opened this issue Jul 3, 2017 · 8 comments
Assignees

Comments

@ningchencontact
Copy link
Collaborator

ningchencontact commented Jul 3, 2017

之前 Kristen分享要做資料探索,確定資料是右偏或是左偏,若有偏移要做調整。
想要確認此資料集偏移狀況,以及怎麼調整

目前的心得有以下,

  1. 資料的右偏或是左偏是看數值型變數,以此資料集來說只有powerPS要看
    (km不需要看因為從圖形上看起來是離散的形式,細節在下方comment)
> mean(train$powerPS)
[1] 112.1224
> median(train$powerPS)
[1] 105
> sd(train$powerPS)
[1] 158.9318

無偏態的資料參考 wiki1
2.目前看起來powerPS應該有偏移,所以,接下來問題是 要怎麼調整
image

@githubningyuan
Copy link
Collaborator

githubningyuan commented Jul 6, 2017

請問資料左/右偏的意思是?
(拍謝 我的統計基礎不好otz)
如果簡化二手車資料成:有兩個屬性x1和x2,價錢是 y,
那麼:

  1. 左偏的意思是說,以x1做橫軸,y做縱軸,則類似山丘的曲線往左(或右)邊偏移嗎?
    因為有時x1屬性跟y的關係若更像單調增加/減少,例如說x1=故障次數,那會不會
    y=f(x1)本來就不像山丘? 例如線性回歸就是y=ax1 + bx2 + c的模型。
  2. 如果x2屬性是離散變量(例如x2 = a, b, c),甚至沒有大小關係,那左/右偏如何看?因為
    y對x2畫圖,完全可以把橫軸a->b->c的順序改畫成b->a->c。
  3. 也或許我誤會問題的意思了。我理解中,像回歸目標常是"最小化均方差",背後比較像是假設:
    label(真正車價) t = y + e,
    y = f(x1, x2),
    e則用常態分佈做估計: e~N(y, sigma)
    那,左/右偏指的是e對於均值f(x1, x2)的分佈不對稱嗎?

@kristenchan kristenchan self-assigned this Jul 8, 2017
@ningchencontact
Copy link
Collaborator Author

ningchencontact commented Jul 11, 2017

@kristenchan 以這個資料集來說,我是不是只需要對 PowerPS跟kilometer 做右偏或是左偏的檢查,因為,其他的都是factor或是time的型態?
還是,PowerPS也應該要轉成factor型態不做檢查?

data.frame': 5500 obs. of 22 variables:
$ id : int 62048 64904 174025 65713 25121 71210 186135 175003 164013 4361 ...
$ dateCrawled : Factor w/ 5464 levels "2016-03-05T14:08:40Z",..: 510 2377 3181 610 3506 1465 348 2900 379 2879 ...
$ name : Factor w/ 4747 levels "Reserviert__Silberner_skoda_Fabia_Kombi_1.2...Ersatzteilspender",..: 4168 4037 1058 3969 2049 4105 1287 3987 4272 2654 ...
$ seller : Factor w/ 2 levels "","privat": 2 2 2 2 2 2 2 2 2 2 ...
$ offerType : Factor w/ 1 level "Angebot": 1 1 1 1 1 1 1 1 1 1 ...
$ price : int 3764 1510 1287 2198 2730 759 916 1988 6654 624 ...
$ abtest : Factor w/ 2 levels "control","test": 1 1 2 2 1 1 1 1 1 2 ...
$ vehicleType : Factor w/ 8 levels "","bus","cabrio",..: 7 7 6 5 4 3 5 7 6 5 ...
$ yearOfRegistration : int 1984 2002 2004 2006 2001 1995 1996 2002 2007 2000 ...
$ gearbox : Factor w/ 3 levels "","automatik",..: 3 3 3 3 3 3 3 3 2 3 ...
$ powerPS : int 34 179 102 54 143 90 60 75 140 60 ...
$ model : Factor w/ 213 levels "","1_reihe","100",..: 118 105 184 98 52 105 93 105 150 80 ...
$ kilometer : int 125000 150000 150000 125000 150000 150000 100000 150000 150000 125000 ...
$ monthOfRegistration: int 4 8 6 12 5 7 0 3 10 2 ...
$ fuelType : Factor w/ 7 levels "","benzin","cng",..: 2 2 2 2 4 2 2 2 4 2 ...
$ brand : Factor w/ 39 levels "alfa_romeo","audi",..: 38 38 10 38 20 38 11 38 38 24 ...
$ notRepairedDamage : Factor w/ 3 levels "","ja","nein": 3 2 3 3 3 3 3 3 3 2 ...
$ dateCreated : Factor w/ 74 levels "2016-02-14T00:00:00Z",..: 9 22 27 10 29 16 8 25 9 25 ...
$ nrOfPictures : int 0 0 0 0 0 0 0 0 0 0 ...
$ postalCode : int 26215 3048 58099 53604 63450 76870 49626 78166 80937 86492 ...
$ lastSeen : Factor w/ 3101 levels "","2016-03-05T16:17:45Z",..: 61 573 1224 180 1386 501 358 2487 195 1431 ...
$ ad_exist_time : int 1 0 6 3 7 5 10 14 5 12 ...

我判斷PoserPS應該有左偏需要做調整
powerps_price

KM的部分看起來是不是 不應該視為numeric 處理,而應該轉成factor
km_price

@ningchencontact
Copy link
Collaborator Author

@githubningyuan

針對你的問題,我回覆如下

請問資料左/右偏的意思是?
(拍謝 我的統計基礎不好otz)
=> 我講的概念 像是 wiki這一篇

如果簡化二手車資料成:有兩個屬性x1和x2,價錢是 y,
那麼:

左偏的意思是說,以x1做橫軸,y做縱軸,則類似山丘的曲線往左(或右)邊偏移嗎?
因為有時x1屬性跟y的關係若更像單調增加/減少,例如說x1=故障次數,那會不會
y=f(x1)本來就不像山丘? 例如線性回歸就是y=ax1 + bx2 + c的模型。

=> 我的理解是 要丟到線性回歸前 要
a. 先檢查 x1vs y ; x2 vs y 的偏度,
b. 如果 右偏或是左偏需調整
c. 調整後才丟到線性回歸中

如果x2屬性是離散變量(例如x2 = a, b, c),甚至沒有大小關係,那左/右偏如何看?因為
y對x2畫圖,完全可以把橫軸a->b->c的順序改畫成b->a->c。

=>離散的就不用看右偏左偏

也或許我誤會問題的意思了。我理解中,像回歸目標常是"最小化均方差",背後比較像是假設:
label(真正車價) t = y + e,
y = f(x1, x2),
e則用常態分佈做估計: e~N(y, sigma)
那,左/右偏指的是e對於均值f(x1, x2)的分佈不對稱嗎?

PS. @kristenchan 不確定我的解釋是否正確

@FernandoHsu
Copy link

FernandoHsu commented Jul 11, 2017

  1. 關於資料分配的偏度,通常是指平均數和中位數相同為對稱分配,平均數大於中位數為右偏,更精準可以用以下幾種方法可以用來判斷:
    (1)做機率分配圖
    (2)用皮爾森偏態係數

2.因為R裡面lm()指令線性回歸的參數估計是用OLS(最小平方法),此方法容易受到離群值影響斜率的參數估計,所以當資料為偏態時或是厚尾分配時,會改採用其他參數估計方法,如LAD,或分位數回歸。

3.根據高斯馬可夫定理,線性回歸模型一般要滿足下面基本假設,估計出來的參數才能有不偏和有效性。
Y= aX + u, 其中u為隨機誤差
(1)參數線性
(2)隨機抽樣
(3)X變數間無完全共線性
(4)隨機誤差u的條件均數E(u|X)=0
(5)隨機誤差u的條件變異數為常數Var(u|x)=constant


@kristenchan
Copy link
Collaborator

kristenchan commented Jul 11, 2017

@agogomei01
找到一個 簡單的例子 可以表達我之前提到的內容
https://academic.macewan.ca/burok/Stat252/notes/transformations.pdf

KM 看起來確實比較適合轉成factor

@kristenchan 我看得懂你的pdf檔案,不過,以powerPS為例,我不太確定要怎麼用R作出以下這圖型調整
image

@kristenchan
Copy link
Collaborator

kristenchan commented Jul 27, 2017

@agogomei01
若畫機率分配圖 有點困難的話 我會建議畫 boxplot
這個應該相對容易

另外 有一個判斷左右偏比較客觀的方式 就是去看 skewness
image

@kristenchan
Copy link
Collaborator

另外 上面的那圖 其實就是 直方圖
有找到一個小小教學影片 https://www.youtube.com/watch?v=9KZzNi62Tpk 大概在 3:00 開始

@ClayLu
Copy link
Collaborator

ClayLu commented Jul 30, 2017

我之前念統計學有學過下面這張圖,為X固定下y或殘差要遵守常態分配。

要推導到X變數也要符合常態分佈,我就不太確定了。是否除了要能判斷資料集右偏或是左偏外,還要多想一步是否需要做調整? 什麼狀況該做調整? 不知道大家怎麼想呢?

20471976_10154914226261819_1632587722_n


@ClayLu

  • 我對於"X固定下y或殘差要遵守常態分配" 有點不理解,以此資料集來說 好像沒有X固定或不固定的狀況

  • 我做到的功課是數值型都要檢查是否常態分配,以此資料集就是powerPS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants