在對某一統(tǒng)計(jì)數(shù)據(jù)進(jìn)行深入分析之前凭涂,首先要對數(shù)據(jù)的多項(xiàng)數(shù)據(jù)特征進(jìn)行描述断傲,這些統(tǒng)計(jì)特征包括這養(yǎng)幾個方面
集中趨勢
離散趨勢
偏度
峰度
使用R對這四個方面的特征進(jìn)行探索败徊,所用的數(shù)據(jù)集為R自帶數(shù)據(jù)iris數(shù)據(jù)集
1. 集中趨勢的描述
集中趨勢的描述包括辟拷,均數(shù)撞羽、中位數(shù)、百分位數(shù)
data("iris")
head(iris)
apply(iris[,1:4],2,mean)
apply(iris[,1:4],2, median)
展示結(jié)果
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
> apply(iris[,1:4],2,mean)
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.843333 3.057333 3.758000 1.199333
> apply(iris[,1:4],2, median)
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.80 3.00 4.35 1.30
2. 離散趨勢
描述離散趨勢的統(tǒng)計(jì)指標(biāo)有衫冻,方差诀紊,標(biāo)準(zhǔn)差,中位數(shù)絕對偏差隅俘,變異系數(shù)邻奠,四分位數(shù),極差
# 離散程度
# 主要方差为居、標(biāo)準(zhǔn)差碌宴、中位數(shù)絕對偏差、變異系數(shù)蒙畴、四分位數(shù)贰镣、極差
var(iris$Sepal.Length) #方差
sd(iris$Sepal.Length) #標(biāo)準(zhǔn)差
mad(iris$Sepal.Length) #中位數(shù)絕對偏差
range(iris$Sepal.Length) # 極差
quantile(iris$Sepal.Length) #四分位數(shù)
apply(iris[,1:4],2,quantile)
apply(iris[,1:4],2,sd) / apply(iris[1:4],2,mean) #變異系數(shù)
結(jié)果展示如下
> var(iris$Sepal.Length)
[1] 0.6856935
> sd(iris$Sepal.Length)
[1] 0.8280661
> mad(iris$Sepal.Length)
[1] 1.03782
> range(iris$Sepal.Length)
[1] 4.3 7.9
> quantile(iris$Sepal.Length)
0% 25% 50% 75% 100%
4.3 5.1 5.8 6.4 7.9
> apply(iris[,1:4],2,quantile)
Sepal.Length Sepal.Width Petal.Length Petal.Width
0% 4.3 2.0 1.00 0.1
25% 5.1 2.8 1.60 0.3
50% 5.8 3.0 4.35 1.3
75% 6.4 3.3 5.10 1.8
100% 7.9 4.4 6.90 2.5
> apply(iris[,1:4],2,sd) / apply(iris[1:4],2,mean)
Sepal.Length Sepal.Width Petal.Length Petal.Width
0.1417113 0.1425642 0.4697441 0.6355511
3. 偏度
偏度也稱為偏態(tài)系數(shù),用于衡量分布不對稱程度或偏斜指標(biāo).
正態(tài)分布是一種無偏分布膳凝,其偏度等于0碑隆。相對于正態(tài)分布,右偏分布也稱正偏分布蹬音,其偏度大于0干跛;左偏分布,其偏度小于0 祟绊。
library(moments)
apply(iris[,1:4],2,skewness)
結(jié)果展示如下
> apply(iris[,1:4],2,skewness)
Sepal.Length Sepal.Width Petal.Length Petal.Width
0.3117531 0.3157671 -0.2721277 -0.1019342
當(dāng)然是否正態(tài)分布,還需要進(jìn)行假設(shè)檢驗(yàn)哥捕,見之前所寫的統(tǒng)計(jì)方法選擇
4. 峰度
峰度又稱為峰度系數(shù)牧抽,用于衡量數(shù)據(jù)尾部分散度的指標(biāo),直觀來看遥赚,峰度反應(yīng)了峰部的尖度扬舒。當(dāng)數(shù)據(jù)為正態(tài)分布,峰度近似等于3凫佛。與正態(tài)分布相比較讲坎,峰度大于3孕惜,數(shù)據(jù)有較多遠(yuǎn)離平均值的極端數(shù)據(jù),即數(shù)據(jù)有平峰厚尾性晨炕。反之衫画,峰度小于3,數(shù)據(jù)分布具有尖峰細(xì)尾性瓮栗。
library(moments)
apply(iris[,1:4], 2, FUN = kurtosis)
結(jié)果如下
> apply(iris[,1:4], 2, FUN = kurtosis)
Sepal.Length Sepal.Width Petal.Length Petal.Width
2.426432 3.180976 1.604464 1.663933
結(jié)果解釋同上
5. 可視化偏度和峰度
同樣使用上面的數(shù)據(jù)
library(ggplot2)
library(tidyr)
irislong <- gather(iris[,1:4], key = 'varname', value = 'value')
# 可視化數(shù)據(jù)分布
ggplot(irislong,aes(colours = varname, fill = varname, linetype = varname, alpha = 0.5)) +
theme_bw()+
geom_density(aes(value),bw = 0.5, alpha = 0.4)
從圖中可以看出和計(jì)算結(jié)果呼應(yīng)
6. 綜合分析
其實(shí)針對上面提及到的四個方面的內(nèi)容削罩,已經(jīng)有package將這些都做好
使用summary對數(shù)據(jù)進(jìn)行分析
> summary(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa :50
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 versicolor:50
Median :5.800 Median :3.000 Median :4.350 Median :1.300 virginica :50
Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
可以看出,summary把每一列的數(shù)據(jù)的四分位數(shù)和平均數(shù)费奸,中位數(shù)都進(jìn)行了計(jì)算
使用Hmisc package進(jìn)行分析
> describe(iris$Sepal.Length)
iris$Sepal.Length
n missing distinct Info Mean Gmd .05 .10 .25 .50
150 0 35 0.998 5.843 0.9462 4.600 4.800 5.100 5.800
.75 .90 .95
6.400 6.900 7.255
lowest : 4.3 4.4 4.5 4.6 4.7, highest: 7.3 7.4 7.6 7.7 7.9
可以看出弥激,包括了觀測數(shù)量,缺失值愿阐,基本描述參數(shù)微服,最大/最小的5個數(shù)據(jù)。當(dāng)然也可以對幾組數(shù)據(jù)同時使用
> describe(iris[,1:4])
iris[, 1:4]
4 Variables 150 Observations
------------------------------------------------------------------------------------------------
Sepal.Length
n missing distinct Info Mean Gmd .05 .10 .25 .50
150 0 35 0.998 5.843 0.9462 4.600 4.800 5.100 5.800
.75 .90 .95
6.400 6.900 7.255
lowest : 4.3 4.4 4.5 4.6 4.7, highest: 7.3 7.4 7.6 7.7 7.9
------------------------------------------------------------------------------------------------
Sepal.Width
n missing distinct Info Mean Gmd .05 .10 .25 .50
150 0 23 0.992 3.057 0.4872 2.345 2.500 2.800 3.000
.75 .90 .95
3.300 3.610 3.800
lowest : 2.0 2.2 2.3 2.4 2.5, highest: 3.9 4.0 4.1 4.2 4.4
------------------------------------------------------------------------------------------------
Petal.Length
n missing distinct Info Mean Gmd .05 .10 .25 .50
150 0 43 0.998 3.758 1.979 1.30 1.40 1.60 4.35
.75 .90 .95
5.10 5.80 6.10
lowest : 1.0 1.1 1.2 1.3 1.4, highest: 6.3 6.4 6.6 6.7 6.9
------------------------------------------------------------------------------------------------
Petal.Width
n missing distinct Info Mean Gmd .05 .10 .25 .50
150 0 22 0.99 1.199 0.8676 0.2 0.2 0.3 1.3
.75 .90 .95
1.8 2.2 2.3
lowest : 0.1 0.2 0.3 0.4 0.5, highest: 2.1 2.2 2.3 2.4 2.5
------------------------------------------------------------------------------------------------
psych package進(jìn)行分析
這個包和上面的類似缨历,同樣使用的是describe這個函數(shù)以蕴。
> psych::describe(iris[,1:4])
vars n mean sd median trimmed mad min max range skew kurtosis se
Sepal.Length 1 150 5.84 0.83 5.80 5.81 1.04 4.3 7.9 3.6 0.31 -0.61 0.07
Sepal.Width 2 150 3.06 0.44 3.00 3.04 0.44 2.0 4.4 2.4 0.31 0.14 0.04
Petal.Length 3 150 3.76 1.77 4.35 3.76 1.85 1.0 6.9 5.9 -0.27 -1.42 0.14
Petal.Width 4 150 1.20 0.76 1.30 1.18 1.04 0.1 2.5 2.4 -0.10 -1.36 0.06
使用pastecs的stat.desc函數(shù)進(jìn)行分析
這個包和上面的類似,使用的是stat.desc這個函數(shù)戈二,但是這個包可以對峰度舒裤、偏度、正態(tài)分布進(jìn)行檢驗(yàn)觉吭。
> pastecs::stat.desc(iris[,1:4],basic = T,desc = T,norm = T,p = T)
Sepal.Length Sepal.Width Petal.Length Petal.Width
nbr.val 150.00000000 150.00000000 1.500000e+02 1.500000e+02
nbr.null 0.00000000 0.00000000 0.000000e+00 0.000000e+00
nbr.na 0.00000000 0.00000000 0.000000e+00 0.000000e+00
min 4.30000000 2.00000000 1.000000e+00 1.000000e-01
max 7.90000000 4.40000000 6.900000e+00 2.500000e+00
range 3.60000000 2.40000000 5.900000e+00 2.400000e+00
sum 876.50000000 458.60000000 5.637000e+02 1.799000e+02
median 5.80000000 3.00000000 4.350000e+00 1.300000e+00
mean 5.84333333 3.05733333 3.758000e+00 1.199333e+00
SE.mean 0.06761132 0.03558833 1.441360e-01 6.223645e-02
CI.mean.TRUE Inf Inf Inf Inf
var 0.68569351 0.18997942 3.116278e+00 5.810063e-01
std.dev 0.82806613 0.43586628 1.765298e+00 7.622377e-01
coef.var 0.14171126 0.14256420 4.697441e-01 6.355511e-01
skewness 0.30864073 0.31261470 -2.694109e-01 -1.009166e-01
skew.2SE 0.77924478 0.78927812 -6.801988e-01 -2.547904e-01
kurtosis -0.60581253 0.13870468 -1.416857e+00 -1.358179e+00
kurt.2SE -0.76961200 0.17620762 -1.799947e+00 -1.725403e+00
normtest.W 0.97609027 0.98491787 8.762681e-01 9.018349e-01
normtest.p 0.01018116 0.10115427 7.412263e-10 1.680465e-08
包括了可信區(qū)間這些數(shù)據(jù)腾供,相當(dāng)全面了。