326:一起來(lái)學(xué)習(xí)密度圖

密度圖在工作中遇見(jiàn)的比較少,但是也碰到好幾次讀者詢問(wèn),所以自己查了一些資料,跟大家分享一下。

大家首先要知道圖形其實(shí)是表格數(shù)據(jù)的另一種呈現(xiàn)方式,我們得知道圖形要展示什么數(shù)據(jù),怎么展示,之后就是去實(shí)現(xiàn)了。

所以,我們先學(xué)習(xí)一下密度圖(Density Plot)能傳達(dá)什么信息。密度圖也叫做核密度圖( Kernel Density Plot)。密度圖將數(shù)據(jù)在連續(xù)間隔或時(shí)間段內(nèi)的分布可視化(也就是X軸,但不一定都是代表時(shí)間)。

這個(gè)圖表是直方圖的一種變體,它使用核平滑來(lái)繪制值,通過(guò)平滑噪聲來(lái)實(shí)現(xiàn)更平滑的分布(As opposed to the histogram, the density plot can smooth out the distribution of values and reduce the noise.)。密度圖的峰值有助于顯示值在區(qū)間內(nèi)的集中位置。

密度圖相對(duì)于直方圖的一個(gè)優(yōu)勢(shì)是,它們更善于確定分布形狀,因?yàn)樗鼈儾皇芩褂玫南渥訑?shù)量的影響。僅包含4個(gè)bin的直方圖不會(huì)產(chǎn)生像20個(gè)bin直方圖那樣可區(qū)分的分布形狀。(這說(shuō)的是什么意思我也不懂,第一次聽(tīng)到箱子)

比如下面是一張密度圖,你能從這張圖獲取什么信息?

1)密度圖的峰值位置可以反映數(shù)據(jù)的集中趨勢(shì),比如均值或中位數(shù)

2)密度圖的寬度可以表明數(shù)據(jù)的離散程度,曲線越寬,數(shù)據(jù)的變異性越大。

3)在密度圖的尾部可能會(huì)出現(xiàn)一些小的峰,這可能表明數(shù)據(jù)中存在異常值或次要的群體。

4)如果在同一坐標(biāo)軸上繪制多個(gè)不同組的密度圖,可以直觀地比較它們的分布差異。

5)密度圖的y軸表示概率密度,因此可以通過(guò)圖形了解數(shù)據(jù)在某個(gè)區(qū)間內(nèi)出現(xiàn)的概率。

最著名的密度曲線是鐘形曲線,它代表正態(tài)分布。

是不是有些困惑?沒(méi)事,我用SAShelp的cars數(shù)據(jù)集舉個(gè)例子,針對(duì)horsepower這個(gè)變量的值,先看下基本的統(tǒng)計(jì)量,重點(diǎn)關(guān)注一下中位值和平均值,然后看一下對(duì)應(yīng)的直方圖。

data cars;

set sashelp.cars;

if type in ("Hybrid","SUV","Truck");

keep horsepower Type;

proc sort;by Type;

run;

proc univariate data=cars;

by Type;

var horsepower;

run;

type="Hybrid"


type="Hybrid"



type="SUV"


type="SUV"

看密度圖的峰值,代表均值和中位數(shù)的集中位置,可以看到峰值聚集在230多,跟算出來(lái)的均值是差不多的,這個(gè)解釋了信息1)

type="Truck"


type="Truck"

這是3種類型混合在一起的密度圖,正態(tài)倒是能清晰的看出

通過(guò)比較3個(gè)密度圖的寬度,我們可以看到type="SUV"和type="Truck"曲線更寬,所以type="SUV"和type="Truck"的變異性更大,也可以看到這兩組數(shù)據(jù)的方差和標(biāo)準(zhǔn)差(Variance & Std Deviation)也更大(方差和標(biāo)準(zhǔn)差就是反映數(shù)據(jù)變異程度的),所以通過(guò)這張圖解釋了信息2)

前面說(shuō)過(guò)密度圖能夠看出數(shù)據(jù)的大致分布形狀。

比如下面這樣是左偏態(tài)分布,也就意味著平均值比中位數(shù)小。

這個(gè)是右偏態(tài)分布,也就意味著平均值比中位數(shù)大。

還有一個(gè)就是無(wú)偏態(tài)分布,意味著平均值跟中位數(shù)相等。

上面的數(shù)據(jù)感覺(jué)偏態(tài)不太明顯,即使第3張圖mean>median,知道是右偏態(tài),但是感覺(jué)圖形跟模板差好多,所以我dummy一些數(shù)據(jù)

data cars_1;

set sashelp.cars;

if type in ("Hybrid","SUV","Truck");

if type="Truck" then horsepower=(1 /rand("Beta", 2, 5));

keep horsepower Type;

proc sort;by Type;

run;


這也是一個(gè)右偏態(tài)的數(shù)據(jù),說(shuō)實(shí)話,感覺(jué)跟模板還是不太像,但是右偏態(tài)數(shù)據(jù)在均值左側(cè)的數(shù)量較多,這里的均值是6.26,通過(guò)柱狀圖也可以看出數(shù)據(jù)集中在哪,所以還是滿足右偏態(tài)。

大家可以通過(guò)proc univariate算出來(lái)的值和圖形比較,看下信息1)到5)是否都符合,除了信息3)數(shù)據(jù)我沒(méi)dummy,其他基本符合了。


data cars;

set sashelp.cars;

if type in ("Hybrid","SUV","Truck");

keep horsepower Type;

proc sort;by Type;

run;

proc univariate data=cars;

by Type;

var horsepower;

run;

proc template;

? ? define statgraph densityplot;

? ? ? ? begingraph;

? ? ? ? ? ? layout overlay;

/* histogram horsepower / ;*/

? ? ? ? ? ? ? ? densityplot horsepower /group=type normal() lineattrs=(thickness=2)

? ? ? ? ? ? ? ? ? ? name='density' legendlabel='Type';

? ? ? ? ? ? ? ? discretelegend 'density';

? ? ? ? ? ? endlayout;

? ? ? ? endgraph;

? ? end;

run;

proc sgrender data=cars template=densityplot;

/*by Type;*/

run;

data cars_1;

set sashelp.cars;

if type in ("Hybrid","SUV","Truck");

if type="Truck" then horsepower=(1 /rand("Beta", 2, 5));

keep horsepower Type;

proc sort;by Type;

run;

data cars;

set cars_1 ;

proc sort;by Type;

run;

proc univariate data=cars;

by Type;

var horsepower;

run;

proc template;

? ? define statgraph densityplot;

? ? ? ? begingraph;

? ? ? ? ? ? layout overlay;

histogram horsepower / ;

? ? ? ? ? ? ? ? densityplot horsepower / normal() lineattrs=(thickness=2)

? ? ? ? ? ? ? ? ? ? name='density' legendlabel='Type';

? ? ? ? ? ? ? ? discretelegend 'density';

? ? ? ? ? ? endlayout;

? ? ? ? endgraph;

? ? end;

run;

proc sgrender data=cars template=densityplot;

by Type;

run;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容