如何讀取一筆資料檔案,並透過敘述統計的方法進行初步的處理,以利於更進一步瞭解該筆資料?

以下的例子將透過 R 讀取一個包含 60 位小學生體重資料的 csv 檔案 (1.csv)
  • 首先,確認 1.csv 檔已位於 工作目錄(working directory) 中,接下來透過 read.csv 指令讀取檔案並寫入 x。因為原檔案並無標頭(標題列或變數名稱列),故 header 指令提示等於 F(False)。
x <- read.csv("1.csv", header=F)
  • 印出 x 檢查讀取與寫入是否正確:
x 
##    V1
## 1  23
## 2  35
## 3  48
## 4  72
## 5  54
## 6  62
## 7  43
## 8  45
## 9  51
## 10 33
## 11 38
## 12 41
## 13 54
## 14 58
## 15 40
## 16 42
## 17 35
## 18 33
## 19 38
## 20 67
## 21 77
## 22 69
## 23 88
## 24 45
## 25 47
## 26 63
## 27 45
## 28 41
## 29 51
## 30 52
## 31 40
## 32 44
## 33 51
## 34 55
## 35 50
## 36 56
## 37 29
## 38 34
## 39 37
## 40 39
## 41 52
## 42 48
## 43 43
## 44 50
## 45 51
## 46 52
## 47 77
## 48 60
## 49 50
## 50 42
## 51 33
## 52 51
## 53 55
## 54 46
## 55 34
## 56 45
## 57 51
## 58 45
## 59 55
## 60 48
  • 透過 class 指令詢問 x類別,得到 data.frame (data frame,即 資料框)
class(x) 
## [1] "data.frame"
  • 透過 dim 指令詢問 x維度,得到 60 1 (60 列 1 行)
dim(x) 
## [1] 60  1
  • 注意 x 的屬性是 data frame,故取其第一行所有元素進行計算(中括號內的逗號前後分別代表 ,逗號前空白代表取 所有的列)。將第一行的資料取出,改名為 x1,利於後續分析。
x1 <- x[,1]   # 這個動作,等同於將 60 筆小學生體重資料讀入x1,亦即 x1 <- c(23, 35, ..., 48)
  • 透過 class 指令詢問 x1類別,可得到 integer (整數數值資料 的一種),因此可以開始用 x1 數值資料直接處理後續的計算與繪圖。
class(x1) 
## [1] "integer"

繪製莖葉圖與盒形圖(盒鬚圖)以初步瞭解資料的分布概況
  • 莖葉圖
stem(x1) 
## 
##   The decimal point is 1 digit(s) to the right of the |
## 
##   2 | 39
##   3 | 33344557889
##   4 | 0011223345555567888
##   5 | 0001111112224455568
##   6 | 02379
##   7 | 277
##   8 | 8
  • 莖葉圖(調整 scale ,增加組數)
stem(x1,scale=2) 
## 
##   The decimal point is 1 digit(s) to the right of the |
## 
##   2 | 3
##   2 | 9
##   3 | 33344
##   3 | 557889
##   4 | 001122334
##   4 | 5555567888
##   5 | 00011111122244
##   5 | 55568
##   6 | 023
##   6 | 79
##   7 | 2
##   7 | 77
##   8 | 
##   8 | 8
  • 盒形圖
boxplot(x1) 

  • 盒形圖(以 水平 方式呈現)
boxplot(x1, horizontal=T) 

開始敘述統計量的計算
  • 計算中央位置參數,包含 (1)算術平均數 mean (2)中位數 median (3)眾數。 注意 R 中沒有直接計算眾數的指令,故改採用 table 指令列表,很容易決定出眾數為 51
mean(x1)
## [1] 48.55
median(x1)
## [1] 48
table(x1)
## x1
## 23 29 33 34 35 37 38 39 40 41 42 43 44 45 46 47 48 50 51 52 54 55 56 58 60 
##  1  1  3  2  2  1  2  1  2  2  2  2  1  5  1  1  3  3  6  3  2  3  1  1  1 
## 62 63 67 69 72 77 88 
##  1  1  1  1  1  2  1

mean(x1) = 48.55 略大於 median(x1) = 48,可判斷資料為 右偏 (平均數 偏到 中位數 的右邊)

  • 計算一般的位置參數,亦即任意 分位數 ,例如 (1)\(Q_1, Q_2, Q_3\) (2)\(D_1, D_2, \dots , D_{10}\) (3)\(P_{18}, P_{34}, P_{62}, P_{77}\),均可使用 quantile 指令產生:
quantile(x1, probs=c(0.25,0.50,0.75))    # `probs=` 可以省略
##   25%   50%   75% 
## 40.75 48.00 54.00
quantile(x1, c(0.1,1.0,0.1))           # 也可以寫成 `seq(0.1,1.0,0.1)`
##  10% 100%  10% 
##   34   88   34
quantile(x1, c(0.18,0.34,0.62,0.77))
##   18%   34%   62%   77% 
## 38.00 43.06 51.00 54.43
  • 全距 (Range)
max(x1)-min(x1)
## [1] 65
  • 四分位距 (Inter Qurtile Range, 即 \(Q_3 - Q_1\))
quantile(x1,0.75) - quantile(x1,0.25)
##   75% 
## 13.25

或採用

as.numeric(quantile(x1,0.75) - quantile(x1,0.25))  
## [1] 13.25

或直接採用 IQR 指令

IQR(x1)
## [1] 13.25
  • (樣本)變異數 (\(s^2\)): (Sample) Variance
var(x1)
## [1] 151.0314

如果資料是 母體 資料,記得要將結果乘以 n-1 再除以 n,才是母體變異數的值,亦即 \(SS*\frac{n-1}{n}\)

var(x1)*(length(x1)-1)/length(x1)
## [1] 148.5142
  • (樣本)標準差 (\(s\)):(Sample) Standard Deviation
sd(x1)
## [1] 12.28948
  • (樣本)變異係數 (sample c.v.):(Sample) Coefficient of Variation,亦即 \(\frac{s}{\bar{x}} * 100%\)
sd(x1)/mean(x1)*100
## [1] 25.31304

計算顯示 樣本變異係數 約為 \(25.31304 \%\)

  • 偏態(skewness) 與 峰度(kurtosis) 係數 ($_1 , _2 $) : 可引進 moments 套件做計算
install.packages("moments")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
library(moments)
skewness(x1)
## [1] 0.8292971
kurtosis(x1)
## [1] 4.093509

由結果可知資料之 偏態 為 ‘右偏’ (\(\beta_1 \approx 0.8292971 > 0\)) ,資料之 峰度高狹峰 (\(\beta_2 \approx 4.093509 > 3\))

  • Z分數 (Z score):可利用 with 指令指定原來的 data frame (x) 的(唯一)變數 V1,計算每位學童體重的 Z分數,並將結果增列為原 data frame 新的一行(此處將此新變數名稱設定為 zscore)
x$zscore <- with(x,(V1-mean(V1))/sd(V1))
x
##    V1      zscore
## 1  23 -2.07901366
## 2  35 -1.10256889
## 3  48 -0.04475372
## 4  72  1.90813583
## 5  54  0.44346867
## 6  62  1.09443185
## 7  43 -0.45160571
## 8  45 -0.28886491
## 9  51  0.19935747
## 10 33 -1.26530969
## 11 38 -0.85845770
## 12 41 -0.61434650
## 13 54  0.44346867
## 14 58  0.76895026
## 15 40 -0.69571690
## 16 42 -0.53297611
## 17 35 -1.10256889
## 18 33 -1.26530969
## 19 38 -0.85845770
## 20 67  1.50128384
## 21 77  2.31498782
## 22 69  1.66402464
## 23 88  3.21006219
## 24 45 -0.28886491
## 25 47 -0.12612412
## 26 63  1.17580225
## 27 45 -0.28886491
## 28 41 -0.61434650
## 29 51  0.19935747
## 30 52  0.28072787
## 31 40 -0.69571690
## 32 44 -0.37023531
## 33 51  0.19935747
## 34 55  0.52483907
## 35 50  0.11798708
## 36 56  0.60620946
## 37 29 -1.59079128
## 38 34 -1.18393929
## 39 37 -0.93982809
## 40 39 -0.77708730
## 41 52  0.28072787
## 42 48 -0.04475372
## 43 43 -0.45160571
## 44 50  0.11798708
## 45 51  0.19935747
## 46 52  0.28072787
## 47 77  2.31498782
## 48 60  0.93169106
## 49 50  0.11798708
## 50 42 -0.53297611
## 51 33 -1.26530969
## 52 51  0.19935747
## 53 55  0.52483907
## 54 46 -0.20749451
## 55 34 -1.18393929
## 56 45 -0.28886491
## 57 51  0.19935747
## 58 45 -0.28886491
## 59 55  0.52483907
## 60 48 -0.04475372

Fears are worse than facts.

想像的恐懼往往比實際經歷(事實)更可怕。