다변량 데이터 읽기

R로 배우는 다변량 확률 분포

Surajit Ray

Professor, University of Glasgow

강의 주제

  • 다변량 데이터 읽기 및 분석
  • 플로팅 기법 탐색
  • 주요 통계 분포 활용

    • 가우시안 분포 및 T 분포
  • 고차원 데이터 기법

    • 주성분 분석(PCA)
R로 배우는 다변량 확률 분포

다변량 데이터 구조

  • 행과 열로 구성된 직사각형 구조
    • 행: 관측값
    • 열: 변수
  • 포함 여부가 다를 수 있음:
    • 행 이름 또는 번호
    • 열 헤더
  • 결측값 가능
R로 배우는 다변량 확률 분포

다변량 데이터 예시

Iris 데이터 (Cambridge University 웹사이트 출처)

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"

$$

꽃받침 길이 및 너비 열 접근

iris_raw[, 1:2] 
iris[, c('Sepal.Length', 'Sepal.Width')]
R로 배우는 다변량 확률 분포

데이터 유형 변환

마지막 변수 Species를 팩터로 변환

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로 배우는 다변량 확률 분포

팩터 레이블 지정

1, 2, 3을 setosa, versicolor, virginica로 재코딩

  • 팩터 레이블 지정
  • 첫 번째 변수를 팩터로 변환
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...