마지막 주차에 알아볼 데이터는 보험료 청구 관련 데이터입니다.
이번 과제에서는 요약과 더불어 간단한 모델링도 진행해봅니다.
우선 보험구 청구 데이터는 아래와 같이 [나이, 성별, 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)
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()
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주차 강의 후기를 마칩니다.
'데이터 사이언스 > 패스트캠퍼스' 카테고리의 다른 글
패스트 캠퍼스 - 컴퓨터 공학_Computer Architecture (0) | 2020.02.24 |
---|---|
[패스트캠퍼스 수강후기] 데이터 분석 입문 올인원 패키지 _ 수강후기 (0) | 2019.11.17 |
[패스트캠퍼스 학습일지] 데이터분석 올인원 패키지 _ 9주차 : 혼자 해보는 데이터 분석_Movies, Highway (0) | 2019.11.10 |
[패스트캠퍼스 학습일지] 데이터분석 올인원 패키지 _ 8주차 : 고등학교 수학으로 이해하는 통계와 데이터 분석 (0) | 2019.11.02 |
[패스트캠퍼스 학습일지] 데이터분석 올인원 패키지 _ 7주차 : 중학교 수학으로 이해하는 통계와 데이터 분석 (0) | 2019.10.22 |