본문 바로가기
카테고리 없음

정답

by 정보장교 2018. 6. 15.
반응형

###9번### 

##규제화의 분류

#로지스틱 회귀 문제에 적용 

library(MASS)

 

biopsy$ID = NULL

biopsy

names(biopsy) = c("thick", "u.size", "u.shape", "adhsn", "s.size", "nucl", "chrom", "n.nuc", "mit", "class")

biopsy

#699 obs of 10 variables

 

biopsy.v2 <-na.omit(biopsy)

#결측값이 들어있는 행 전체를 데이터 셋에서 제거

#683 obs of 10 variables

 

biopsy

 

set.seed(123)

#R에서 시드를 설정하는 명령

ind <- sample(2, nrow(biopsy.v2),replace = TRUE, prob = c(0.7,03))

#무작위로 샘플을 추출

#sample 함수 상세 포맷  https://www.rdocumentation.org/packages/base/versions/3.5.0/topics/sample

 

train <- biopsy.v2[ind==1,]

test <- biopsy.v2[ind==2,]

#train 데이터와 test 데이터 설정 

 

x <- as.matrix(train[, 1:9])

y <- train[ , 10]

 

set.seed(3)

fitCV <- cv.glmnet(x, y, family = "binomial", type.measure = "auc", nfolds =5)

#glmnet 함수를 이용해서 family 는 이항 , measure auc=곡선 아래 면적, 5겹 설정

#cv.glmnet 상세 포맷 https://www.rdocumentation.org/packages/glmnet/versions/2.0-16/topics/cv.glmnet

 

plot(fitCV)

#AUC = 가능한 모든 분류 임계값을 고려하는 평가 측정항목

 

#AUC 에 대한 상세 설명  https://developers.google.com/machine-learning/crash-course/classification/roc-and-auc?hl=ko

 

fitCV$lambda.1se

coef(fitCV , s = "lambda.1se")

#Coefficients의 약자 coef를 이용한 회수 계수 추출

#format 가이드 https://www.rdocumentation.org/packages/VGAM/versions/1.0-5/topics/Coef

 

 

library(InformationValue)

predCV <- predict(fitCV, newx = as.matrix(test[,1:9], s = "lambda.1se", type = "response"))

actuals <- ifelse(test$class == "malignant", 1, 0)

misClassError(actuals, predCV)

plotROC(actuals, predCV)

##해석 

#ROC curve(Receiver Operating Characteristic curve) :FPR과 TPR을 각각 x,y축으로 놓은 그래프 

#TPR : True Positive Rate (=민감도, true accept rate)

#1인 케이스에 대해 1로 예측한 비율.(예 암환자를 진찰해서 암이라고 진단 함)

#FPR :  False Positive Rate (=1-특이도, false accept rate)

#0인 케이스에 대해 1로 잘못 예측한 비율.(예 암환자가 아닌데 암이라고 진단 함)

#lambda.1se를 이용한 ROC  도출

#ROC.jpg 참고 

 

 

predCV.min <- predict(fitCV, newx = as.matrix(test[, 1:9]), s = "lambda.min" , type = "response")

misClassError(actuals, predCV.min)

plotROC(actuals, predCV)

#lambda.min를 이용한 ROC  도출

반응형