'일반화 선형회귀모형 구분'에 해당되는 글 1건

  1. 2018.12.16 [R] 로지스틱 회귀분석을 통한 유방암 예측(분류) (1/4): WDBC Data 소개 4

이번 포스팅부터 4번에 나누어서 로지스틱 회귀분석을 통한 유방암 예측(분류)를 소개하겠습니다. 


보통 통계학 수업 듣다보면 모델링 및 해석에 집중하고 탐색적 데이터 분석이나 데이터 전처리는 언급이 없거나 건너뛰는 경우가 많은데요, 이번 포스팅에서는 가급적이면 실제 데이터 분석을 하는데 있어서 거치게 되는 데이터 로딩, 탐색적 데이터 분석, 전처리, 시각화 등의 절차까지 모두 포함하여 상세하게 설명하려고 노력하겠습니다. 



[로지스틱 회귀분석을 통한 유방암 예측(분류) 포스팅 순서]

  1. WDBC(Wisconsin Diagnostic Breast Cancer) dataset 소개 및 분석 목적과 방향 설정
  2. 탐색적 데이터 분석 및 전처리
  3. 1차 변수 선택 및 목표변수와 설명변수 간 관계 분석
  4. 로지스틱 회귀모형 적합 및 모델 평가, 해석



  (1) WDBC(Wisconsin Diagnostic Breast Cancer) Dataset 소개


이번 분석에 사용할 데이터는 미국 중북부 위스콘신 주 남부 Madson에 있는 Wisconsin 대학병원 Dr.William H. Wolberg 교수님, 컴퓨터 공학과 W.Nick Street 교수님, Olvi L.Mangasarian 교수님이 1995년에 공개한 자료입니다. 

(for more information: https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.names)


가는 주사바늘을 사용하여 낭종(주모니 혹은 내용물이나 멍울)의 세포나 조직을 떼어내서 조직검사한 이미지를 디지털로 변환하여 측정한 30개의 설명변수와, 환자 ID, 진단 결과(악성 M=malignant, 양성 B=benign) 의 변수를 포함하여, 총 32개의 변수로 구성되어 있고, 569명의 환자에 대해 조사한 데이터셋입니다. 


데이터셋이 공개되어 있는 주소는 https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data 입니다. 


* image source: https://pathology.jhu.edu/breast/my-results/types-of-breast-cancer/



외부 공개된 데이터셋을 data.table 패키지의 fread() 함수를 사용해서 불러온 후 R의 DataFrame 객체로 만들어보겠습니다. 



> rm(list=ls()) # clear all

> options(scipen=30)

> # data loading: WDBC (Wisconsin Diagnostic Breast Cancer)

> library(data.table)

data.table 1.11.8  Latest news: r-datatable.com

> library(curl)

> url <- c("https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data")

> wdbc <- data.frame(fread(url))

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current

                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0  0  121k    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0100  121k  100  121k    0     0  81060      0  0:00:01  0:00:01 --:--:-- 81060

> str(wdbc)

'data.frame': 569 obs. of  32 variables:

 $ V1 : int  842302 842517 84300903 84348301 84358402 843786 844359 84458202 844981 84501001 ...

 $ V2 : chr  "M" "M" "M" "M" ...

 $ V3 : num  18 20.6 19.7 11.4 20.3 ...

 $ V4 : num  10.4 17.8 21.2 20.4 14.3 ...

 $ V5 : num  122.8 132.9 130 77.6 135.1 ...

 $ V6 : num  1001 1326 1203 386 1297 ...

 $ V7 : num  0.1184 0.0847 0.1096 0.1425 0.1003 ...

 $ V8 : num  0.2776 0.0786 0.1599 0.2839 0.1328 ...

 $ V9 : num  0.3001 0.0869 0.1974 0.2414 0.198 ...

 $ V10: num  0.1471 0.0702 0.1279 0.1052 0.1043 ...

 $ V11: num  0.242 0.181 0.207 0.26 0.181 ...

 $ V12: num  0.0787 0.0567 0.06 0.0974 0.0588 ...

 $ V13: num  1.095 0.543 0.746 0.496 0.757 ...

 $ V14: num  0.905 0.734 0.787 1.156 0.781 ...

 $ V15: num  8.59 3.4 4.58 3.44 5.44 ...

 $ V16: num  153.4 74.1 94 27.2 94.4 ...

 $ V17: num  0.0064 0.00522 0.00615 0.00911 0.01149 ...

 $ V18: num  0.049 0.0131 0.0401 0.0746 0.0246 ...

 $ V19: num  0.0537 0.0186 0.0383 0.0566 0.0569 ...

 $ V20: num  0.0159 0.0134 0.0206 0.0187 0.0188 ...

 $ V21: num  0.03 0.0139 0.0225 0.0596 0.0176 ...

 $ V22: num  0.00619 0.00353 0.00457 0.00921 0.00511 ...

 $ V23: num  25.4 25 23.6 14.9 22.5 ...

 $ V24: num  17.3 23.4 25.5 26.5 16.7 ...

 $ V25: num  184.6 158.8 152.5 98.9 152.2 ...

 $ V26: num  2019 1956 1709 568 1575 ...

 $ V27: num  0.162 0.124 0.144 0.21 0.137 ...

 $ V28: num  0.666 0.187 0.424 0.866 0.205 ...

 $ V29: num  0.712 0.242 0.45 0.687 0.4 ...

 $ V30: num  0.265 0.186 0.243 0.258 0.163 ...

 $ V31: num  0.46 0.275 0.361 0.664 0.236 ...

 $ V32: num  0.1189 0.089 0.0876 0.173 0.0768 ...




V1 은 환자 ID, V2는 진단 결과(악성 M, 양성 B), V3 ~ V32 까지 30개의 설명변수입니다. 설명변수로는 radius, texture, perimeter, area, smoothness, compactness, concavity, concave points, symmetry, fractal dimension 의 10개 지표에 대해서 mean(평균), se(standard error), worst(가장 나쁜 혹은 큰 측정치) 를 각각 측정(즉 10개 지표 * 3개 측정 기준 = 30개 설명변수)한 자료 입니다. 


(data info: https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.names)




colnames() 함수로 변수명을 변경해보겠습니다. 


 

> # column names

> colnames(wdbc) <- c("id", "diagnosis", "radius_mean", "texture_mean", 

+                     "perimeter_mean", "area_mean", "smoothness_mean", "compactness_mean",

+                     "concavity_mean", "concave.points_mean", "symmetry_mean", "fractal_dimension_mean", 

+                     "radius_se", "texture_se", "perimeter_se", "area_se", 

+                     "smoothness_se", "compactness_se", "concavity_se", "concave.points_se", 

+                     "symmetry_se", "fractal_dimension_se", "radius_worst", "texture_worst", 

+                     "perimeter_worst", "area_worst", "smoothness_worst", "compactness_worst", 

+                     "concavity_worst", "concave.points_worst", "symmetry_worst", "fractal_dimension_worst")



str() 함수로 데이터셋 구조와 변수에 대해서 살펴보니 목표변수 diagnosis는 M(악성)과 B(양성) 의 두 개 범주(category)를 가진 범주형 데이터이며, 30개 설명변수는 모두 숫자형(numeric), 연속형 변수입니다. 각 설명변수별로 측정 단위가 다르므로 (길이, 면적 등) 나중에 표준화를 해서 분석하겠습니다. 



str(wdbc)

'data.frame': 569 obs. of  32 variables:

 $ id                     : int  842302 842517 84300903 84348301 84358402 843786 844359 84458202 844981 84501001 ...

 $ diagnosis              : chr  "M" "M" "M" "M" ...

 $ radius_mean            : num  18 20.6 19.7 11.4 20.3 ...

 $ texture_mean           : num  10.4 17.8 21.2 20.4 14.3 ...

 $ perimeter_mean         : num  122.8 132.9 130 77.6 135.1 ...

 $ area_mean              : num  1001 1326 1203 386 1297 ...

 $ smoothness_mean        : num  0.1184 0.0847 0.1096 0.1425 0.1003 ...

 $ compactness_mean       : num  0.2776 0.0786 0.1599 0.2839 0.1328 ...

 $ concavity_mean         : num  0.3001 0.0869 0.1974 0.2414 0.198 ...

 $ concave.points_mean    : num  0.1471 0.0702 0.1279 0.1052 0.1043 ...

 $ symmetry_mean          : num  0.242 0.181 0.207 0.26 0.181 ...

 $ fractal_dimension_mean : num  0.0787 0.0567 0.06 0.0974 0.0588 ...

 $ radius_se              : num  1.095 0.543 0.746 0.496 0.757 ...

 $ texture_se             : num  0.905 0.734 0.787 1.156 0.781 ...

 $ perimeter_se           : num  8.59 3.4 4.58 3.44 5.44 ...

 $ area_se                : num  153.4 74.1 94 27.2 94.4 ...

 $ smoothness_se          : num  0.0064 0.00522 0.00615 0.00911 0.01149 ...

 $ compactness_se         : num  0.049 0.0131 0.0401 0.0746 0.0246 ...

 $ concavity_se           : num  0.0537 0.0186 0.0383 0.0566 0.0569 ...

 $ concave.points_se      : num  0.0159 0.0134 0.0206 0.0187 0.0188 ...

 $ symmetry_se            : num  0.03 0.0139 0.0225 0.0596 0.0176 ...

 $ fractal_dimension_se   : num  0.00619 0.00353 0.00457 0.00921 0.00511 ...

 $ radius_worst           : num  25.4 25 23.6 14.9 22.5 ...

 $ texture_worst          : num  17.3 23.4 25.5 26.5 16.7 ...

 $ perimeter_worst        : num  184.6 158.8 152.5 98.9 152.2 ...

 $ area_worst             : num  2019 1956 1709 568 1575 ...

 $ smoothness_worst       : num  0.162 0.124 0.144 0.21 0.137 ...

 $ compactness_worst      : num  0.666 0.187 0.424 0.866 0.205 ...

 $ concavity_worst        : num  0.712 0.242 0.45 0.687 0.4 ...

 $ concave.points_worst   : num  0.265 0.186 0.243 0.258 0.163 ...

 $ symmetry_worst         : num  0.46 0.275 0.361 0.664 0.236 ...

 $ fractal_dimension_worst: num  0.1189 0.089 0.0876 0.173 0.0768 ...


> head(wdbc, 2)

      id diagnosis radius_mean texture_mean perimeter_mean area_mean smoothness_mean compactness_mean

1 842302         M       17.99        10.38          122.8      1001         0.11840          0.27760

2 842517         M       20.57        17.77          132.9      1326         0.08474          0.07864

  concavity_mean concave.points_mean symmetry_mean fractal_dimension_mean radius_se texture_se

1         0.3001             0.14710        0.2419                0.07871    1.0950     0.9053

2         0.0869             0.07017        0.1812                0.05667    0.5435     0.7339

  perimeter_se area_se smoothness_se compactness_se concavity_se concave.points_se symmetry_se

1        8.589  153.40      0.006399        0.04904      0.05373           0.01587     0.03003

2        3.398   74.08      0.005225        0.01308      0.01860           0.01340     0.01389

  fractal_dimension_se radius_worst texture_worst perimeter_worst area_worst smoothness_worst

1             0.006193        25.38         17.33           184.6       2019           0.1622

2             0.003532        24.99         23.41           158.8       1956           0.1238

  compactness_worst concavity_worst concave.points_worst symmetry_worst fractal_dimension_worst

1            0.6656          0.7119               0.2654         0.4601                 0.11890

2            0.1866          0.2416               0.1860         0.2750                 0.08902

 




  (2) 분석 목적 및 분석 방향 설정


WDBC 데이터셋의 diagnosis 진단 변수는 목표변수(반응변수, 종속변수, Output 변수)로서 2개의 class (악성 Malignant, 양성 Benign)를 가진 범주형 자료로서, 30개의 연속형 설명변수를 사용해서 2개의 class (악성, 양성 여부)를 분류(classification) 하는 문제입니다. 




이진 분류에 사용할 수 있는 분석 알고리즘은 무척 많습니다. 가령, KNN, Naive Bayes, Logistic Regression, SVM, Decision Tree, Random Forest, GBM, Deep Neural Network 등 많습니다. 


다만, 이번 데이터가 의료 데이터인지라 의사 선생님이 진단 결과에 대한 해석과 이해가 용이한 분석 모델이 활용도 측면에서 좋을 것으로 예상하여 로지스틱 회귀모형(Logistic Regression)을 선택하였습니다. 즉, 30개의 설명변수를 사용하여 유방암 악성(Malignant)에 속할 0~1 사이의 확률값을 계산하여 진단하는 분류/예측 모델을 만들어보겠습니다. (당연히 다른 분석 알고리즘도 얼마든지 사용 가능합니다 ^^)


참고로 일반화 선형회귀모형(GLM: Generalized Linear Model)은 반응변수가 정규분포를 따르는 연속형변수가 아닐 경우 & 고정효과 만을 다루는 경우에 사용하는데요, 특히 이항변수를 반응변수로 가지며 Logit link를 사용하는 GLM 이 바로 로지스틱 회귀모형이 되겠습니다. 

(참고: https://en.wikipedia.org/wiki/Generalized_linear_model)


[ 일반화 선형회귀모형 (GLM: Generalized Linear Model) 구분 ]





다음번 포스팅에서는 WDBC 데이터에 대한 탐색적 데이터 분석과 데이터 전처리에 대해서 다루겠습니다. 


많은 도움이 되었기를 바랍니다. 



728x90
반응형
Posted by Rfriend
,