統計分析——描述統計之資料水平描述
一組樣本資料的數值特徵一般來說可以從三個方面來描述:
-
資料的水平(也可以稱之為集中趨勢或位置度量),反映資料的數值大小
-
資料的差異,反映資料間的離散程度
-
資料的分佈形狀,反映資料分佈的偏度和峰度
描述水平的統計量
資料水平是指數值大小,描述資料水平的統計量有平均數
,分位數
,眾數
,同時這幾個統計量也可以用來描述資料的集中趨勢度。
平均數
**簡單平均數(simple mean)**的公式:
加權平均數(weighted mean):如果樣本被分為K組,每組的組中值(組上限與下限的平均數)為m1,m2,…,mk表示各組的頻數用f1,f2,…,fk表示,則樣本平均數的計算公式為:
一般來說,總體的平均數是無從得知的,因為無法得到總體是資料,所以我們常常從樣本的平均數來推測總體的平均數。
R方法
# 在 R中求簡單平均數
load(".\\tongjixue\\example\\ch3\\example3_1.RData") # 30名學生的成績
head(example3_1,5) # 展示前5名學生的成績
mean(example3_1$分數) # 求分數的平均值
# mean(x, trim = 0, na.rm = FALSE, ...)
# x - 向量
# trim - 取值在0~0.5之間,例如trim=0.1,表示計算之前先排序,然後去掉前10%和後10%的資料,最後計算剩餘資料的平均值
# na.rm - 預設為FALSE,當為TRUE時,表示去掉資料中的缺失值。(當資料中有缺失值時無法計算)
分數 |
---|
85 |
55 |
91 |
66 |
79 |
80
# 在 R中求加權平均數
load(".\\tongjixue\\example\\ch3\\example3_2.RData")
example3_2
weighted.mean(example3_2$組中值, example3_2$人數)
# weighted.mean(x, w,...,na.rm=FALSE)
# x - 計算加權平均數的物件,對應公式中的 f
# w - 相應的權數向量,相當於公式中的 m
分組 | 組中值 | 人數 |
---|---|---|
60以下 | 55 | 3 |
60—70 | 65 | 4 |
70—80 | 75 | 4 |
80—90 | 85 | 10 |
90—100 | 95 | 9 |
81
python方法
import numpy as np
import pandas as pd
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all" # jupyter結果多行顯示
data_1 = np.array([[1, 2], [3, 4]]) # 矩陣
data_2 = pd.DataFrame(data_1) # 資料框
data_2
0 | 1 | |
---|---|---|
0 | 1 | 2 |
1 | 3 | 4 |
# 在 python中求簡單平均數
# 利用資料框自帶的方法
data_2.mean()
# data.mean(axis=None, skipna=True)
# axis - 預設為axis=None,即輸出每列的平均值
# skipna:布林值,預設為True,計算結果時排除NA / null值
# 使用 numpy的函式
np.mean(data_1,axis=(1,0))
# np.mean(data, axis=None)
# axis - 預設為axis=None,如果為元組,則計算多軸上的平均值。例如(0,1)計算行和列的所有資料的平均值。
0 2.0
1 3.0
dtype: float64
2.5
# 匯入資料
data_2 = pd.read_csv('.\\tongjixue\\example\\ch3\\example3_2.csv',engine='python')
data_2
分組 | 組中值 | 人數 | |
---|---|---|---|
0 | 60以下 | 55 | 3 |
1 | 60—70 | 65 | 4 |
2 | 70—80 | 75 | 4 |
3 | 80—90 | 85 | 10 |
4 | 90—100 | 95 | 9 |
# 在python中求 加權平均數
np.average(data_2['組中值'],weights=data_2['人數'])
# numpy.average(a, axis=None, weights=None,...)
# a - array_like,計算加權平均數的物件,對應公式中的 f
# weights - array_like,相應的權數向量,相當於公式中的 m
# axis - 預設為axis=None,如果為元組,則計算多軸上的平均值。
81.0
data = np.arange(6).reshape((3,2))
data
np.average(data,axis=1, weights=[1./4, 3./4])
array([[0, 1],
[2, 3],
[4, 5]])
array([0.75, 2.75, 4.75])
因為加權平均數是使用組中值來代表該組資料的,所以同一組資料,簡單平均和加權平均結果不同,除非每組資料在組中值兩側成對稱分佈,故除非資料本來就是分組情況,一般都用簡單平均求平均值。
分位數
分位數代表資料水平的高低,常用的分位數有四分位數
,中位數
,百分位數
中位數
中位數是一組資料排序後位於中間位置的數值,用Me表示
中位數的特點是不受極端值的影響
四分位數
同中位數,將資料排序後位於1/4和3/4位置的資料。
百分位數
同四分位數,利用99個數據點將資料分為100份,百分位數提供了資料在最大值和最小值期間資料點分佈資訊。
R方法
# 利用之前example3.1的學生成績資料
# 中位數
median(example3_1$分數)
# 四分位數
quantile(example3_1$分數,probs = c(0.25,0.75))
# R總計算分位數有9種方法,預設type=7。
#百分位數
quantile(example3_1$分數,probs=c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9))
85
25% 75%
70.5 90
10% 20% 30% 40% 50% 60% 70% 80% 90%
60.4 66.8 74.1 81.6 85 86 89.3 91 92.3
python方法
data_3 = pd.read_csv('.\\tongjixue\\example\\ch3\\example3_1.csv',engine='python')
np.percentile(data_3.分數,(25,50,75))
array([70.5, 85. , 90. ])
求分位數在統計雪上有多種方法,當分位點位於兩個數值中間時有不同的取值的方法,這個以後詳細討論。
眾數
一組資料眾數數顯頻數最多的數值,用 表示,眾數在資料量比較大時才有意義,眾數可能不存在,也可能有2個或者多個。
R中沒有直接求出眾數的內建函式,所以需要自己寫自定義眾數函式
R方法
# 自定義函式
getmode <- function(x){
y <- sort(unique(x)) # 去重數值並排序
tab <- tabulate(match(x,y)) # 比較x與y中的數值,並列出他們在y中的位置,在計算每個位置的頻數放入物件tab中
y[tab==max(tab)] # 找出y中頻數最多的元素
}
getmode(example3_1$分數)
86
python方法
在numpy或者pandas是沒有求眾數的方法的,但是我們可以利用scipy科學計算庫中的mode函式
from scipy.stats import mode
m0 = mode(data_3['分數'])[0][0]
print(m0)
# 或者利用numpy中的bincount()函式,此函式將資料按直方圖統計
count = np.bincount(data_3['分數'])
m0_1 = np.argmax(count)
print(m0_1)
86
86