閱讀多變量資料

R 的多變量機率分配

Surajit Ray

Professor, University of Glasgow

課程主題

  • 閱讀並分析多變量資料
  • 探索繪圖技巧
  • 使用常見統計分配

    • Gaussian 與 T 分配
  • 高維資料技巧

    • 主成分分析(PCA)
R 的多變量機率分配

多變量資料的結構

  • 長方形結構:以列與欄組織
    • 列代表觀測值
    • 欄代表變數
  • 可能包含或不包含:
    • 列名稱或編號
    • 欄標題
  • 可能有遺漏值
R 的多變量機率分配

多變量資料範例

Iris 資料,來源:Cambridge University website

5.1  3.5  1.4  0.2  1
4.9  3.0  1.4  0.2  1
4.7  3.2  1.3  0.2  1

新生兒體重資料(含欄標題的 CSV)

"","case","bwt","gestation","parity","age","height","weight","smoke"
"1",1,120,284,0,27,62,100,0
"2",2,113,282,0,33,64,135,0
R 的多變量機率分配

讀取資料

從一個 URL 讀取

iris_url <- "https://mlg.eng.cam.ac.uk/teaching/3f3/1011/iris.data"
iris_raw <- read.table(iris_url,  sep = "", header = FALSE)

本機讀取

iris_raw <- read.table("iris.txt", sep = "", header = FALSE)
R 的多變量機率分配

檢視資料集

head(iris_raw, n = 4)
     V1  V2  V3  V4 V5
1   5.1 3.5 1.4 0.2  1  
2   4.9 3.0 1.4 0.2  1  
3   4.7 3.2 1.3 0.2  1  
4   4.6 3.1 1.5 0.2  1
R 的多變量機率分配

指定欄位名稱

colnames(iris_raw) <- c("Sepal.Length", "Sepal.Width", "Petal.Length","Petal.Width", "Species" )
head(iris_raw)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2       1
2          4.9         3.0          1.4         0.2       1
3          4.7         3.2          1.3         0.2       1
4          4.6         3.1          1.5         0.2       1
5          5.0         3.6          1.4         0.2       1
6          5.4         3.9          1.7         0.4       1
R 的多變量機率分配

存取特定欄位

檢查目前的欄位名稱

names(iris_raw)
"Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"

$$

存取 Sepal length 與 Sepal width 欄位

iris_raw[, 1:2] 
iris[, c('Sepal.Length', 'Sepal.Width')]
R 的多變量機率分配

變更資料型別

將最後一個變數 Species 轉為 factor

iris_raw$species <- as.factor(iris_raw$species)
str(iris_raw)
'data.frame':    150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
R 的多變量機率分配

指定 factor 標籤

123 的物種標籤改編為 setosaversicolorvirginica

  • 指定 factor 標籤
  • 將第一個變數改為 factor
library(car) 
iris_raw$Species <- recode(iris_raw$Species,
                          " 1 ='setosa'; 2 = 'versicolor'; 3 = 'virginica'")
str(iris_raw)
'data.frame':    150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
R 的多變量機率分配

讀取含欄名的 CSV 資料

新生兒體重資料(含欄標題的 CSV)

"","case","bwt","gestation","parity","age","height","weight","smoke"
"1",1,120,284,0,27,62,100,0
"2",2,113,282,0,33,64,135,0
"3",3,128,279,0,28,64,115,1

讀取新生兒體重資料

bwt <- read.csv("birthweight.csv", row.names = 1)
head(bwt, n = 3)
  case bwt gestation parity age height weight smoke
1    1 120       284      0  27     62    100     0
2    2 113       282      0  33     64    135     0
3    3 128       279      0  28     64    115     1
R 的多變量機率分配

一起來練習吧!

R 的多變量機率分配

Preparing Video For Download...