반응형

마지막 주차에 알아볼 데이터는 보험료 청구 관련 데이터입니다.

이번 과제에서는 요약과 더불어 간단한 모델링도 진행해봅니다.

 

우선 보험구 청구 데이터는 아래와 같이 [나이, 성별, BMI 수치, 자녀 수, 흡연 여부, 지역, 보험료] 라는 항목들로 구성되어 있습니다. 각 항목들에 대해서, 보험료와 어떤 관계를 가지고 있는지 알아볼 예정입니다.

 




  #### 주제 3
  #### 보험료 청구 데이터 요약
    
    ## 개인 신상 및 건강 정보와 보험료 청구금액 데이터
    ## 출처 : kaggle(https://www.kaggle.com/mirichoi0218/insurance)

##0 데이터 불러오기

  # kaggle.com에서 직접 다운로드
    ## URL : https://www.kaggle.com/mirichoi0218/insurance

# 다운로드한 파일을 강의자료의 data 폴더에 넣기

# 기본 사용 라이브러리 임포트
library(dplyr)
library(ggplot2)
library(rpart)
library(rpart.plot)

##1 데이터 불러와서 insurance로 저장하기

insurance = read.csv('data/insurance.csv')

##2 summary( )함수로 데이터 요약하고
##  수치형 변수는 히스토그램, 범주형 변수는 표 만들기
## 히스토그램 = [age, bmi,  charges] 표 = [sex, children, smoker, region]

summary(insurance)
names(insurance)
head(insurance)

ggplot(insurance, aes(x = age)) + geom_histogram(bins = 47)

< 보험 가입자의 연령별 분포 >

ggplot(insurance, aes(x = bmi)) + geom_histogram(bins = 47)

< 보험 가입자들의 bmi 분포 >

ggplot(insurance, aes(x = charges)) + geom_histogram(bins = 47)

< 보험 가입자들의 청구요금 분포 >

table(insurance$sex)
table(insurance$children)
table(insurance$smoker)
table(insurance$region)

##3 bmi와 charges의 산점도 그리고 상관계수 계산하기

ggplot(insurance, aes(x = bmi, y = charges)) + geom_jitter()

< bmi 수치와 보험 청구료의 산점도 >

cor.test(insurance$bmi , insurance$charges)

 


##4 region별 charges의 상자그림 그리고 그룹별 평균 계산하기

insurance%>% group_by(region) %>% summarise(mean = mean(charges))
ggplot(insurance, aes(x = region, y = charges)) + geom_boxplot()


##5 lm( )과 rpart( )를 활용하여 
##  관심변수 charges를 나머지 변수로 설명하는 모형 적합하기 

lm(insurance$charges ~ insurance$age)

ggplot(insurance, aes(x = age, y = charges)) + geom_jitter()

위의 그래프는 [나이 - 보험청구료] 산점도입니다. 관찰해보면 명확히 3개의 그룹으로 나눠져있는 것을 볼 수 있습니다.

 

linear_model = lm(charges ~ age + bmi + sex + children + smoker + region , data = insurance)
summary(linear_model)
rpart_model = rpart(charges ~ age + bmi + sex + children + smoker + region , data = insurance)
summary(rpart_model)

rpart.plot(rpart_model, cex = 1)

 

 

이번주에는 보험료 데이터를 가지고 요약, 시각화, 간단한 모델링을 진행해 보았습니다.
이상 10주차 강의 후기를 마칩니다.

반응형

+ Recent posts