library(ggplot2)
library(ISLR)
## Warning: 패키지 'ISLR'는 R 버전 4.2.2에서 작성되었습니다
data(Wage)
library(dplyr)
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
glimpse(Wage)
## Rows: 3,000
## Columns: 11
## $ year       <int> 2006, 2004, 2003, 2003, 2005, 2008, 2009, 2008, 2006, 2004,…
## $ age        <int> 18, 24, 45, 43, 50, 54, 44, 30, 41, 52, 45, 34, 35, 39, 54,…
## $ maritl     <fct> 1. Never Married, 1. Never Married, 2. Married, 2. Married,…
## $ race       <fct> 1. White, 1. White, 1. White, 3. Asian, 1. White, 1. White,…
## $ education  <fct> 1. < HS Grad, 4. College Grad, 3. Some College, 4. College …
## $ region     <fct> 2. Middle Atlantic, 2. Middle Atlantic, 2. Middle Atlantic,…
## $ jobclass   <fct> 1. Industrial, 2. Information, 1. Industrial, 2. Informatio…
## $ health     <fct> 1. <=Good, 2. >=Very Good, 1. <=Good, 2. >=Very Good, 1. <=…
## $ health_ins <fct> 2. No, 2. No, 1. Yes, 1. Yes, 1. Yes, 1. Yes, 1. Yes, 1. Ye…
## $ logwage    <dbl> 4.318063, 4.255273, 4.875061, 5.041393, 4.318063, 4.845098,…
## $ wage       <dbl> 75.04315, 70.47602, 130.98218, 154.68529, 75.04315, 127.115…
dim(Wage)
## [1] 3000   11
colnames(Wage)
##  [1] "year"       "age"        "maritl"     "race"       "education" 
##  [6] "region"     "jobclass"   "health"     "health_ins" "logwage"   
## [11] "wage"
row.names(Wage)
##    [1] "231655" "86582"  "161300" "155159" "11443"  "376662" "450601" "377954"
##    [9] "228963" "81404"  "302778" "305706" "8690"   "153561" "449654" "447660"
##   [17] "160191" "230312" "301585" "153682" "158226" "11141"  "448410" "305116"
##   [25] "233002" "8684"   "229379" "86064"  "378472" "157244" "82694"  "7690"  
##   [33] "377879" "9747"   "233301" "157123" "230823" "80406"  "228851" "153810"
##   [41] "81383"  "303642" "87492"  "8692"   "86929"  "380872" "449480" "305136"
##   [49] "227963" "232863" "8621"   "379668" "84595"  "154634" "450864" "84377" 
##   [57] "234086" "154482" "85916"  "161065" "12003"  "228071" "13479"  "81494" 
##   [65] "159076" "159207" "447501" "153767" "81071"  "376442" "87299"  "228621"
##   [73] "232494" "228400" "451860" "157058" "159583" "233759" "159224" "374859"
##   [81] "11710"  "86298"  "453021" "161431" "305888" "232199" "86568"  "447500"
##   [89] "452506" "450908" "82573"  "159196" "156110" "14148"  "232000" "453486"
##   [97] "156065" "229079" "450905" "10660"  "449456" "374660" "87463"  "9273"  
##  [105] "377517" "231592" "303825" "156310" "303376" "230586" "450109" "379991"
##  [113] "87291"  "228517" "160971" "307464" "449246" "233043" "377184" "8033"  
##  [121] "233687" "447751" "230398" "378429" "447412" "13924"  "87630"  "84600" 
##  [129] "451987" "160246" "307755" "375007" "303430" "379330" "8339"   "83190" 
##  [137] "452580" "302701" "83222"  "159871" "82108"  "229714" "159048" "302868"
##  [145] "10499"  "84127"  "11661"  "86282"  "305327" "81655"  "303953" "374752"
##  [153] "159109" "159285" "159909" "378138" "9786"   "86191"  "154759" "12122" 
##  [161] "306309" "377137" "375944" "304068" "156557" "306779" "161021" "85966" 
##  [169] "453692" "233313" "9681"   "379875" "305250" "84642"  "451306" "228212"
##  [177] "157989" "88028"  "155697" "234523" "233149" "13626"  "234640" "453255"
##  [185] "154195" "232628" "85539"  "377464" "230582" "9573"   "84164"  "11880" 
##  [193] "85545"  "153581" "232176" "450900" "377900" "232371" "12056"  "84397" 
##  [201] "233497" "228458" "230993" "153269" "305585" "302536" "307024" "233435"
##  [209] "12288"  "303912" "14404"  "233441" "377452" "449474" "381113" "156406"
##  [217] "378714" "376309" "451901" "450725" "375024" "159821" "229865" "83880" 
##  [225] "9965"   "157250" "453371" "228243" "307645" "156420" "160039" "233379"
##  [233] "231082" "301920" "80687"  "450566" "86450"  "234600" "307663" "303900"
##  [241] "453852" "230477" "227901" "86826"  "8653"   "305209" "81173"  "451854"
##  [249] "228591" "302140" "153731" "228955" "9621"   "305165" "378023" "82989" 
##  [257] "82023"  "374706" "379611" "452592" "305871" "157388" "448936" "377039"
##  [265] "82375"  "449852" "301195" "228628" "85756"  "160269" "301711" "304988"
##  [273] "156809" "14133"  "306552" "377157" "82901"  "159765" "229546" "9730"  
##  [281] "158570" "83618"  "379620" "449489" "447879" "10076"  "84867"  "14063" 
##  [289] "12822"  "302239" "84949"  "11129"  "450963" "154245" "231844" "87485" 
##  [297] "306574" "154477" "13319"  "452710" "156407" "153297" "13996"  "80386" 
##  [305] "12586"  "234325" "305437" "231481" "83198"  "231026" "303072" "305362"
##  [313] "447780" "87269"  "307318" "302976" "302515" "14041"  "228348" "381272"
##  [321] "87402"  "451721" "8318"   "447992" "377619" "153764" "158574" "374992"
##  [329] "155488" "8969"   "86280"  "451302" "448525" "381232" "304500" "156397"
##  [337] "9066"   "82172"  "375305" "13745"  "87094"  "87118"  "11498"  "87260" 
##  [345] "303688" "304050" "378178" "302298" "157594" "10835"  "81344"  "449687"
##  [353] "7980"   "449444" "10723"  "154638" "85115"  "232201" "452906" "301654"
##  [361] "307056" "161096" "87060"  "12303"  "229331" "159410" "154322" "378567"
##  [369] "450843" "85580"  "303657" "231767" "82755"  "84630"  "154919" "83800" 
##  [377] "375159" "452406" "375122" "87199"  "82912"  "379794" "307692" "7744"  
##  [385] "85617"  "82419"  "158170" "10787"  "86679"  "379950" "380366" "377229"
##  [393] "14381"  "380704" "154336" "303935" "449322" "447585" "154099" "83458" 
##  [401] "228692" "10081"  "86122"  "452287" "82373"  "448894" "81495"  "155436"
##  [409] "231274" "158044" "153953" "450724" "160958" "376843" "229069" "9667"  
##  [417] "450052" "448988" "82603"  "7878"   "14321"  "8396"   "447417" "87860" 
##  [425] "232031" "378256" "155519" "304018" "9841"   "448723" "233955" "8039"  
##  [433] "448678" "306092" "153618" "157194" "306995" "304167" "12086"  "12979" 
##  [441] "451080" "85699"  "81085"  "228178" "12945"  "450263" "301266" "157405"
##  [449] "161083" "8459"   "451331" "305396" "451170" "306067" "303214" "452388"
##  [457] "302942" "11666"  "378911" "82114"  "7969"   "379907" "232187" "379987"
##  [465] "448271" "378868" "11319"  "155212" "160678" "86604"  "227990" "8348"  
##  [473] "156680" "158692" "231182" "306711" "12734"  "229031" "449980" "160714"
##  [481] "302861" "230508" "12537"  "450603" "86119"  "448869" "451086" "154643"
##  [489] "304493" "303813" "376113" "86394"  "301315" "232504" "10753"  "157040"
##  [497] "305092" "12728"  "447357" "159513" "161380" "14457"  "158761" "154582"
##  [505] "158274" "378307" "9863"   "155729" "87848"  "305240" "159441" "80586" 
##  [513] "83515"  "231410" "81995"  "232366" "229698" "449667" "81457"  "83804" 
##  [521] "154652" "159717" "302069" "13267"  "8550"   "449709" "157793" "302529"
##  [529] "157309" "451256" "159735" "85268"  "453542" "158301" "307764" "231800"
##  [537] "12439"  "7412"   "86091"  "159115" "10935"  "447841" "234010" "453198"
##  [545] "376184" "160130" "306716" "304809" "11522"  "306077" "301911" "155698"
##  [553] "450165" "159956" "380945" "301907" "159358" "233565" "301859" "302193"
##  [561] "161261" "451254" "305400" "233567" "380078" "80581"  "87755"  "13685" 
##  [569] "83327"  "159834" "84809"  "87700"  "305809" "380902" "158812" "306821"
##  [577] "231279" "12575"  "232850" "379119" "81696"  "378745" "304502" "80930" 
##  [585] "9157"   "377201" "160496" "306557" "302928" "7880"   "379439" "155174"
##  [593] "80508"  "81575"  "231191" "232830" "85999"  "450673" "447485" "453584"
##  [601] "8016"   "160160" "380311" "11315"  "232256" "80679"  "229791" "450800"
##  [609] "230173" "228889" "7978"   "374363" "380398" "452862" "228899" "232593"
##  [617] "231255" "306356" "158824" "380061" "376529" "304383" "10445"  "229647"
##  [625] "377052" "380800" "450000" "233715" "304660" "233026" "80647"  "87572" 
##  [633] "11809"  "233001" "305553" "379752" "380738" "447577" "87038"  "154076"
##  [641] "374391" "449807" "307460" "449365" "378727" "156087" "12157"  "301568"
##  [649] "10832"  "160170" "453712" "87395"  "305387" "229944" "380031" "231476"
##  [657] "154504" "231749" "377308" "8330"   "154617" "87791"  "230102" "157248"
##  [665] "447647" "231197" "379516" "451555" "158257" "81725"  "305274" "379947"
##  [673] "84795"  "83423"  "301999" "305108" "375810" "153403" "154768" "302561"
##  [681] "153724" "84494"  "83214"  "378589" "307823" "303202" "452445" "159137"
##  [689] "451436" "232410" "307670" "159052" "155165" "13246"  "158241" "379243"
##  [697] "9196"   "450568" "7781"   "10997"  "153426" "80273"  "374547" "85217" 
##  [705] "377128" "158403" "228131" "452170" "154530" "81780"  "378629" "305370"
##  [713] "301646" "12249"  "302635" "8793"   "12032"  "13257"  "156095" "232674"
##  [721] "378952" "304280" "376160" "86736"  "305599" "9903"   "13558"  "378086"
##  [729] "80963"  "376772" "453319" "231608" "87803"  "154604" "304641" "452853"
##  [737] "85681"  "452452" "8297"   "233667" "377125" "380656" "374314" "306069"
##  [745] "306727" "379612" "153785" "157154" "306904" "9761"   "86702"  "158105"
##  [753] "379806" "449268" "83615"  "378890" "230899" "9311"   "7750"   "302750"
##  [761] "231066" "157343" "9952"   "233281" "86503"  "14172"  "14341"  "231725"
##  [769] "377627" "375298" "84172"  "154047" "375008" "86259"  "230737" "378052"
##  [777] "155909" "375457" "155588" "306550" "82560"  "379734" "376576" "158927"
##  [785] "374739" "83417"  "14351"  "159265" "157150" "451548" "302614" "453033"
##  [793] "84994"  "227948" "380920" "231461" "160102" "449353" "448411" "306810"
##  [801] "155681" "10055"  "233682" "157882" "302173" "231216" "9850"   "161403"
##  [809] "377645" "84260"  "155985" "306353" "228023" "156832" "304488" "305737"
##  [817] "303148" "302080" "447857" "233741" "85657"  "379124" "157694" "83479" 
##  [825] "450888" "14252"  "14421"  "301908" "452061" "304906" "302424" "159404"
##  [833] "231750" "305745" "376178" "450343" "160548" "302678" "302085" "159635"
##  [841] "81174"  "82948"  "82251"  "379828" "450063" "9535"   "154111" "305404"
##  [849] "85550"  "447980" "14481"  "85819"  "450651" "82222"  "12508"  "377741"
##  [857] "302139" "153833" "85624"  "230790" "449764" "449202" "13741"  "302523"
##  [865] "81623"  "13718"  "10378"  "161366" "9215"   "157493" "302363" "11991" 
##  [873] "449684" "11238"  "159751" "452407" "233537" "229847" "9864"   "447299"
##  [881] "231895" "230128" "12999"  "160782" "379257" "161228" "232829" "234092"
##  [889] "84421"  "157953" "86497"  "7737"   "13471"  "448820" "85643"  "453480"
##  [897] "85241"  "156206" "84713"  "155666" "83567"  "449541" "448368" "377037"
##  [905] "231115" "305375" "303226" "85015"  "228219" "379558" "160043" "82149" 
##  [913] "156798" "9029"   "157633" "9820"   "448635" "81776"  "86230"  "84040" 
##  [921] "11619"  "87714"  "14549"  "83620"  "378744" "10047"  "11784"  "12453" 
##  [929] "378962" "230457" "447527" "154701" "453719" "153784" "160819" "380247"
##  [937] "81823"  "155141" "156716" "7546"   "374359" "301939" "306511" "452755"
##  [945] "8667"   "450499" "156050" "160046" "87749"  "8363"   "234293" "448089"
##  [953] "453187" "447835" "307486" "82464"  "155315" "380158" "375387" "85776" 
##  [961] "229893" "229973" "13960"  "234361" "301929" "306789" "230376" "380304"
##  [969] "306501" "376743" "230371" "304137" "374823" "451590" "227916" "7993"  
##  [977] "159388" "304858" "230749" "86325"  "154901" "81954"  "451332" "379839"
##  [985] "449978" "233080" "307530" "161327" "158949" "448811" "11623"  "10265" 
##  [993] "379315" "230661" "81295"  "305243" "228627" "13060"  "375312" "156535"
## [1001] "304223" "307620" "85969"  "13360"  "11113"  "305667" "234017" "12837" 
## [1009] "302321" "81373"  "160726" "452148" "229256" "306650" "452276" "302510"
## [1017] "81228"  "228440" "228053" "10471"  "231759" "155837" "302024" "301193"
## [1025] "158905" "453630" "303454" "228501" "304450" "378489" "86991"  "153315"
## [1033] "231642" "234448" "80744"  "374333" "304748" "232443" "451096" "82862" 
## [1041] "380709" "376073" "301216" "233939" "451070" "449595" "158200" "9638"  
## [1049] "157436" "452319" "302474" "230090" "306734" "234336" "8827"   "8301"  
## [1057] "85748"  "376569" "378593" "158180" "82969"  "228806" "380329" "304795"
## [1065] "14455"  "304663" "87070"  "380220" "12400"  "303868" "7845"   "234319"
## [1073] "233042" "233381" "157856" "82387"  "301852" "82525"  "449130" "84343" 
## [1081] "452636" "233211" "233851" "12233"  "12501"  "153789" "87826"  "8875"  
## [1089] "229950" "85730"  "377504" "13650"  "155246" "227999" "157775" "453565"
## [1097] "8927"   "380898" "85375"  "153469" "83341"  "8279"   "307337" "13148" 
## [1105] "84841"  "85515"  "86629"  "231027" "157246" "13588"  "376203" "83057" 
## [1113] "230814" "305515" "307267" "88046"  "302698" "232536" "158406" "452654"
## [1121] "155330" "307313" "159448" "155833" "307280" "7401"   "11845"  "307683"
## [1129] "8589"   "375802" "305144" "453214" "8360"   "377324" "302793" "154537"
## [1137] "232060" "83048"  "376922" "87672"  "234303" "374328" "158629" "231349"
## [1145] "377297" "230086" "81256"  "306063" "378700" "86261"  "450507" "159849"
## [1153] "301682" "448787" "82646"  "81754"  "14268"  "85704"  "233733" "229393"
## [1161] "83536"  "375861" "14368"  "85295"  "231204" "13498"  "302692" "307635"
## [1169] "83924"  "80239"  "379220" "453870" "85521"  "9738"   "157529" "307581"
## [1177] "380007" "453715" "381016" "8356"   "156461" "380325" "156598" "450872"
## [1185] "305335" "449727" "452029" "232822" "158958" "233370" "161344" "233394"
## [1193] "233584" "448801" "302882" "306349" "452162" "231483" "82643"  "305066"
## [1201] "11473"  "86233"  "12787"  "233174" "303344" "307057" "301337" "81731" 
## [1209] "157918" "160058" "159843" "157233" "231489" "452313" "233665" "157818"
## [1217] "376459" "13375"  "154265" "156078" "303231" "159873" "228330" "451634"
## [1225] "159186" "160564" "447672" "452613" "12504"  "302206" "234624" "159256"
## [1233] "14460"  "233287" "452861" "88065"  "7550"   "302313" "82600"  "380306"
## [1241] "13488"  "153493" "227880" "378323" "81976"  "155452" "450962" "11853" 
## [1249] "156191" "87661"  "82514"  "378030" "157381" "86485"  "10930"  "307771"
## [1257] "85202"  "233889" "7496"   "453337" "376076" "160307" "161341" "11390" 
## [1265] "301389" "304303" "87441"  "12175"  "12033"  "82301"  "83227"  "153765"
## [1273] "158059" "375572" "448806" "82280"  "153375" "380151" "230950" "14546" 
## [1281] "450602" "81692"  "158659" "306889" "379968" "10750"  "155530" "86633" 
## [1289] "378686" "10144"  "87286"  "87895"  "375523" "451283" "14519"  "380780"
## [1297] "86239"  "10984"  "231862" "449495" "448715" "448977" "234530" "86190" 
## [1305] "230114" "447347" "155626" "376579" "451819" "155900" "8505"   "234613"
## [1313] "11832"  "11081"  "301246" "375987" "156259" "85655"  "448836" "153902"
## [1321] "305415" "9743"   "230054" "12519"  "227962" "83739"  "157490" "82456" 
## [1329] "159012" "88116"  "376549" "87170"  "11435"  "85129"  "448698" "154709"
## [1337] "379618" "161267" "380753" "153467" "231484" "379211" "156236" "450978"
## [1345] "84773"  "449829" "305946" "12507"  "154311" "154975" "307420" "228741"
## [1353] "376605" "304583" "159573" "375743" "449387" "83565"  "7664"   "157200"
## [1361] "230665" "12813"  "302409" "83416"  "83964"  "13483"  "232794" "447864"
## [1369] "452049" "87163"  "157204" "80604"  "161272" "87121"  "231993" "377133"
## [1377] "449641" "156429" "12311"  "379397" "84236"  "451645" "8988"   "307325"
## [1385] "11919"  "11403"  "88019"  "306940" "155528" "301789" "378048" "8841"  
## [1393] "85602"  "304529" "157296" "155829" "379034" "153627" "229223" "380392"
## [1401] "158726" "377495" "230878" "453110" "85888"  "376196" "228775" "153559"
## [1409] "304567" "83140"  "450085" "378923" "87254"  "9513"   "156960" "80462" 
## [1417] "304074" "306048" "448446" "448106" "86335"  "230002" "447211" "305589"
## [1425] "8755"   "13308"  "448287" "10187"  "80524"  "377969" "153484" "11876" 
## [1433] "229679" "229435" "80558"  "11650"  "82081"  "82799"  "155256" "80446" 
## [1441] "159200" "8197"   "153388" "88113"  "155513" "158884" "230315" "86782" 
## [1449] "306149" "380883" "7639"   "381143" "9181"   "158451" "229117" "233600"
## [1457] "231346" "379646" "228164" "85693"  "452344" "160400" "160533" "307204"
## [1465] "450078" "84607"  "10989"  "155859" "234391" "81772"  "377896" "153951"
## [1473] "231716" "229399" "302102" "81714"  "155433" "87168"  "85320"  "7420"  
## [1481] "451424" "229703" "14417"  "9241"   "377591" "302226" "376645" "157321"
## [1489] "154342" "301885" "84599"  "374558" "301209" "378837" "374851" "153917"
## [1497] "86617"  "374294" "375787" "83031"  "155234" "156931" "84305"  "233131"
## [1505] "451873" "452428" "232403" "158091" "159901" "374894" "376018" "85534" 
## [1513] "378978" "305552" "12671"  "157620" "156543" "234246" "157606" "230929"
## [1521] "87063"  "157575" "8274"   "229913" "301692" "304818" "448786" "229605"
## [1529] "231509" "80950"  "87704"  "158664" "81095"  "302918" "161349" "306415"
## [1537] "10603"  "86007"  "306146" "234661" "84287"  "377509" "377763" "306688"
## [1545] "302888" "377799" "381003" "380799" "83368"  "11548"  "304155" "9144"  
## [1553] "11624"  "153897" "157384" "453498" "229614" "380993" "159758" "10899" 
## [1561] "374347" "374986" "81794"  "303090" "375451" "154262" "379725" "88050" 
## [1569] "451349" "84848"  "306255" "82384"  "378696" "85794"  "450118" "9457"  
## [1577] "230105" "153266" "157526" "234243" "231448" "449095" "303172" "81506" 
## [1585] "80235"  "87943"  "83985"  "375234" "156633" "14384"  "8450"   "160165"
## [1593] "451015" "232095" "154723" "304780" "377397" "303182" "11131"  "7535"  
## [1601] "375336" "451744" "232578" "374914" "307821" "305822" "7495"   "449798"
## [1609] "159599" "448887" "453745" "375738" "229672" "161522" "378510" "160189"
## [1617] "230629" "233946" "86943"  "230497" "377005" "80350"  "81917"  "301556"
## [1625] "8888"   "307676" "7381"   "233790" "303636" "81716"  "158585" "307816"
## [1633] "154833" "450754" "157064" "377858" "380391" "452617" "452685" "154958"
## [1641] "161421" "229694" "7822"   "12679"  "447486" "13835"  "379762" "81141" 
## [1649] "7699"   "85842"  "12364"  "84080"  "451706" "14152"  "85144"  "302797"
## [1657] "81223"  "88062"  "452864" "12144"  "160944" "8105"   "154672" "453271"
## [1665] "155921" "154732" "233690" "86143"  "156740" "12718"  "449744" "448117"
## [1673] "154750" "83654"  "81277"  "379686" "227939" "80897"  "231662" "154839"
## [1681] "305114" "450873" "447174" "377920" "161041" "14541"  "376938" "447645"
## [1689] "80720"  "447722" "155179" "8907"   "160862" "155453" "378199" "12156" 
## [1697] "447458" "232327" "301930" "158517" "12579"  "13995"  "82195"  "306499"
## [1705] "302319" "447816" "449074" "229524" "232133" "85388"  "13298"  "307289"
## [1713] "7589"   "9082"   "305036" "302117" "160497" "450224" "449975" "80475" 
## [1721] "378718" "13370"  "380165" "86118"  "159505" "450919" "80496"  "302736"
## [1729] "81910"  "159140" "8846"   "7415"   "80565"  "452789" "13535"  "156743"
## [1737] "87391"  "450392" "155733" "12600"  "80237"  "306346" "81445"  "231389"
## [1745] "378598" "306310" "380028" "86072"  "11321"  "307301" "447298" "161253"
## [1753] "377359" "453728" "14042"  "304908" "160028" "13005"  "158124" "86567" 
## [1761] "82851"  "305392" "153650" "304093" "233121" "230850" "157822" "451294"
## [1769] "14543"  "159847" "83903"  "378535" "453171" "301347" "453420" "155699"
## [1777] "380739" "9766"   "234383" "232723" "81234"  "229581" "87788"  "452608"
## [1785] "8652"   "451324" "160285" "84767"  "229001" "230839" "11453"  "87015" 
## [1793] "157453" "87789"  "10827"  "450748" "83883"  "230302" "160949" "302490"
## [1801] "452615" "156728" "158466" "8344"   "7436"   "374803" "12491"  "232762"
## [1809] "230998" "84524"  "13464"  "159550" "234248" "234357" "451267" "155944"
## [1817] "228029" "159068" "228443" "376883" "233097" "228418" "80966"  "229478"
## [1825] "374483" "381181" "12955"  "161502" "380218" "156036" "229899" "305122"
## [1833] "450849" "376406" "303218" "156645" "14338"  "14140"  "380621" "447629"
## [1841] "86490"  "302168" "158551" "233702" "378925" "160680" "158298" "450290"
## [1849] "305059" "301937" "160924" "12963"  "10344"  "8757"   "82071"  "155631"
## [1857] "9139"   "10933"  "449746" "379247" "13497"  "14109"  "234415" "234376"
## [1865] "155250" "85134"  "381020" "159971" "9591"   "83666"  "301931" "307750"
## [1873] "453302" "307225" "230293" "154660" "156739" "8789"   "375928" "447995"
## [1881] "447215" "448598" "229234" "447888" "82715"  "82946"  "80449"  "86044" 
## [1889] "157202" "450096" "448535" "450450" "451844" "451460" "11973"  "452739"
## [1897] "230033" "377968" "447810" "9620"   "83273"  "381222" "301976" "381289"
## [1905] "307040" "450227" "302539" "86544"  "86015"  "84882"  "301497" "378528"
## [1913] "7539"   "14035"  "379720" "87842"  "12496"  "453550" "12905"  "307423"
## [1921] "302880" "229722" "13420"  "305048" "81872"  "11889"  "301313" "448972"
## [1929] "304644" "380216" "154881" "85010"  "303653" "374554" "7830"   "14307" 
## [1937] "83763"  "161310" "450610" "8736"   "379881" "161031" "156574" "380839"
## [1945] "380501" "449794" "81288"  "231420" "10519"  "381177" "229249" "453598"
## [1953] "82185"  "453746" "448132" "86463"  "84715"  "155547" "83136"  "8414"  
## [1961] "304459" "378108" "155401" "228504" "376032" "305001" "10568"  "158869"
## [1969] "82571"  "233856" "85706"  "87534"  "158028" "376894" "450739" "453840"
## [1977] "85005"  "12029"  "233756" "228877" "233966" "376565" "375596" "7603"  
## [1985] "376986" "448055" "81116"  "228861" "87034"  "305311" "378766" "13578" 
## [1993] "302454" "160354" "452525" "83954"  "302858" "11897"  "305534" "448547"
## [2001] "154312" "13095"  "155937" "233677" "82261"  "450761" "11935"  "449193"
## [2009] "13380"  "13510"  "303955" "82964"  "155032" "81944"  "228060" "228305"
## [2017] "8471"   "375955" "378357" "228453" "375260" "84747"  "9001"   "13523" 
## [2025] "377490" "85233"  "8658"   "302756" "228793" "85808"  "229152" "449705"
## [2033] "85799"  "155865" "85689"  "450894" "378221" "230406" "376602" "156489"
## [2041] "7568"   "453257" "376581" "379989" "234332" "11046"  "303385" "450697"
## [2049] "156124" "161043" "449651" "233230" "231523" "154395" "304408" "380248"
## [2057] "83466"  "13091"  "84078"  "14266"  "82469"  "450648" "230082" "302568"
## [2065] "452618" "449703" "228508" "453301" "8172"   "380101" "306319" "10780" 
## [2073] "8168"   "234147" "379279" "13027"  "159780" "153749" "87231"  "381130"
## [2081] "83569"  "82380"  "87411"  "80378"  "306390" "233582" "230156" "80860" 
## [2089] "157459" "228496" "453835" "376688" "84683"  "380971" "448578" "376787"
## [2097] "231118" "7373"   "156889" "80853"  "379750" "377519" "11199"  "306981"
## [2105] "232149" "13294"  "229612" "12617"  "451199" "7609"   "231686" "234389"
## [2113] "229901" "153622" "86931"  "9860"   "82359"  "83774"  "379289" "451529"
## [2121] "157319" "10740"  "83212"  "234447" "448296" "229238" "156703" "303382"
## [2129] "83663"  "13264"  "228067" "379994" "374633" "380994" "452521" "301537"
## [2137] "452259" "380756" "156099" "379666" "13066"  "233531" "159976" "155361"
## [2145] "301559" "159911" "378511" "303173" "155313" "231958" "375616" "307319"
## [2153] "12456"  "448765" "301777" "11762"  "379995" "302475" "303761" "7976"  
## [2161] "157964" "9336"   "379954" "230714" "83126"  "82701"  "452203" "154263"
## [2169] "230644" "304966" "156987" "88084"  "160389" "81881"  "83677"  "80890" 
## [2177] "306629" "12188"  "301793" "447716" "10043"  "156286" "376933" "160815"
## [2185] "84588"  "156257" "306463" "304367" "156055" "84965"  "86137"  "7434"  
## [2193] "13145"  "376834" "80861"  "307009" "9407"   "448011" "301160" "87472" 
## [2201] "86258"  "86622"  "234539" "233141" "160720" "380833" "447332" "13074" 
## [2209] "377769" "159957" "14047"  "229085" "156367" "86170"  "231625" "376206"
## [2217] "374441" "229681" "84975"  "80338"  "158704" "86260"  "377704" "450907"
## [2225] "8898"   "86506"  "81695"  "452011" "233422" "232655" "161451" "450319"
## [2233] "305903" "12064"  "448531" "450203" "302905" "377539" "306049" "234286"
## [2241] "82123"  "13073"  "12975"  "9152"   "85858"  "449352" "156000" "374878"
## [2249] "374341" "453736" "83544"  "374646" "14363"  "380303" "11668"  "156595"
## [2257] "303371" "12558"  "154495" "232910" "448457" "156591" "374845" "154639"
## [2265] "376657" "453155" "302655" "234441" "10756"  "157788" "159478" "82408" 
## [2273] "231687" "451735" "14361"  "447846" "87736"  "159033" "452456" "229915"
## [2281] "85977"  "158370" "306799" "86857"  "154144" "155861" "302734" "87460" 
## [2289] "301952" "452190" "302487" "375314" "80513"  "8245"   "450802" "380599"
## [2297] "8760"   "228870" "86333"  "82672"  "303408" "379215" "153433" "452035"
## [2305] "12738"  "10025"  "302428" "230517" "451939" "83710"  "11155"  "10801" 
## [2313] "377849" "155762" "380873" "11350"  "449916" "11955"  "452076" "158386"
## [2321] "8307"   "452919" "304571" "84662"  "157527" "155753" "85476"  "86888" 
## [2329] "11885"  "379300" "84104"  "451062" "379529" "155117" "234168" "228808"
## [2337] "85191"  "159708" "155806" "378375" "155162" "230689" "158165" "448727"
## [2345] "154715" "449784" "82382"  "159218" "81077"  "303201" "375099" "452764"
## [2353] "84935"  "302956" "231714" "377432" "450157" "155744" "12978"  "228951"
## [2361] "156037" "302728" "233642" "228903" "449895" "451041" "376582" "160144"
## [2369] "379821" "161269" "87217"  "160293" "86674"  "87761"  "303104" "229277"
## [2377] "452824" "232086" "233767" "12838"  "159042" "375741" "85607"  "86744" 
## [2385] "11797"  "452631" "374872" "448646" "379131" "231134" "9431"   "379883"
## [2393] "376403" "376258" "14511"  "449447" "231149" "13208"  "376577" "9330"  
## [2401] "380510" "234379" "449359" "449768" "304596" "83073"  "80331"  "307248"
## [2409] "87597"  "161439" "234656" "153663" "11542"  "81869"  "156792" "376283"
## [2417] "452245" "231515" "306859" "82001"  "375236" "86010"  "230943" "228764"
## [2425] "156896" "453724" "379194" "452457" "232552" "450777" "158588" "377411"
## [2433] "302723" "153400" "86089"  "160883" "379090" "302129" "86755"  "377441"
## [2441] "87396"  "307386" "84169"  "449775" "228357" "449987" "303856" "304921"
## [2449] "374988" "229150" "84663"  "379349" "157534" "305469" "231089" "82956" 
## [2457] "379819" "449534" "448935" "375717" "304731" "10772"  "14158"  "85775" 
## [2465] "161082" "377328" "155957" "154985" "87716"  "374629" "232517" "84219" 
## [2473] "380749" "453653" "81828"  "377546" "451614" "160598" "7816"   "157421"
## [2481] "9691"   "304770" "81122"  "233881" "377413" "303918" "229437" "447724"
## [2489] "85017"  "9109"   "84346"  "301863" "452767" "380912" "450623" "7899"  
## [2497] "13782"  "452871" "13456"  "306022" "232102" "375105" "81175"  "375985"
## [2505] "233063" "85647"  "11736"  "153843" "10673"  "84898"  "375389" "374477"
## [2513] "160912" "13605"  "305951" "84566"  "160765" "449036" "13295"  "7863"  
## [2521] "87877"  "86166"  "160895" "450033" "156104" "160718" "81086"  "87767" 
## [2529] "13125"  "231653" "85304"  "375038" "9595"   "87553"  "81392"  "451374"
## [2537] "158047" "81515"  "153519" "380550" "304366" "304377" "9064"   "378073"
## [2545] "158940" "380022" "227841" "12002"  "304602" "450489" "158881" "154684"
## [2553] "9521"   "451147" "448799" "86446"  "378317" "7499"   "154659" "159096"
## [2561] "379777" "161093" "8398"   "86001"  "9947"   "13597"  "8402"   "229336"
## [2569] "9471"   "307463" "303301" "307758" "8704"   "450579" "377548" "379802"
## [2577] "155828" "160072" "158458" "380862" "229278" "158416" "374326" "84906" 
## [2585] "10154"  "304890" "448330" "302440" "8594"   "160473" "452589" "154916"
## [2593] "453566" "453173" "231286" "83337"  "8625"   "304249" "452699" "13542" 
## [2601] "233331" "12638"  "376869" "11100"  "379372" "376009" "304338" "304116"
## [2609] "452574" "230268" "159545" "229535" "161425" "376778" "232700" "306371"
## [2617] "304010" "154423" "377710" "8072"   "306740" "374463" "155676" "378483"
## [2625] "11655"  "159926" "161488" "380370" "154590" "86392"  "12146"  "301904"
## [2633] "375850" "449254" "378197" "12275"  "447285" "229216" "379593" "10380" 
## [2641] "378289" "453516" "8978"   "87747"  "13594"  "448361" "82431"  "227968"
## [2649] "154580" "155201" "228062" "301131" "81508"  "160105" "10455"  "230783"
## [2657] "380368" "8521"   "302231" "154400" "376389" "231469" "155163" "229213"
## [2665] "161447" "11017"  "81252"  "227917" "82223"  "450449" "155027" "301716"
## [2673] "160957" "307162" "153288" "10549"  "448079" "85787"  "232991" "232011"
## [2681] "234060" "83770"  "228316" "451479" "233965" "8562"   "231536" "451622"
## [2689] "451993" "159704" "228435" "11386"  "10964"  "227859" "449407" "233646"
## [2697] "450142" "375066" "377927" "154494" "80376"  "81068"  "233828" "85640" 
## [2705] "10274"  "153933" "10377"  "87797"  "84394"  "232457" "377675" "304688"
## [2713] "233833" "374782" "154678" "303392" "453674" "302464" "230110" "86348" 
## [2721] "449111" "156748" "8312"   "378918" "154871" "302382" "305523" "377010"
## [2729] "157367" "154310" "8265"   "154479" "14202"  "161025" "85939"  "158022"
## [2737] "229664" "448419" "301493" "9698"   "451173" "377766" "379628" "158239"
## [2745] "450366" "10192"  "303306" "230907" "87177"  "85563"  "378239" "232251"
## [2753] "448807" "451298" "234574" "303467" "160372" "80668"  "230140" "87724" 
## [2761] "159738" "13820"  "159737" "158555" "8911"   "230355" "155813" "304171"
## [2769] "154382" "447429" "307480" "301212" "450515" "160071" "231742" "379911"
## [2777] "231151" "7548"   "157269" "378112" "158610" "87409"  "451122" "227949"
## [2785] "451359" "13067"  "374802" "86228"  "379346" "376228" "228763" "85100" 
## [2793] "87786"  "87346"  "10641"  "451408" "159969" "376315" "233368" "155264"
## [2801] "7747"   "302426" "157328" "9454"   "160476" "84722"  "160518" "12284" 
## [2809] "375528" "83300"  "9974"   "11998"  "11771"  "85840"  "83877"  "305966"
## [2817] "7951"   "451372" "83243"  "10311"  "159643" "378452" "153846" "13108" 
## [2825] "11905"  "375715" "82621"  "12425"  "82295"  "379133" "305527" "13255" 
## [2833] "86181"  "157316" "450927" "229450" "85047"  "447600" "450428" "85993" 
## [2841] "378229" "449815" "229998" "448218" "306163" "11664"  "8194"   "14196" 
## [2849] "88022"  "160061" "7840"   "450384" "160299" "233831" "449264" "12477" 
## [2857] "375640" "161055" "82687"  "301552" "375238" "453137" "379538" "451582"
## [2865] "447462" "158645" "87154"  "378986" "160715" "81037"  "82804"  "154694"
## [2873] "452218" "157720" "374624" "153643" "86198"  "84791"  "232289" "374507"
## [2881] "305083" "87712"  "306948" "13141"  "155887" "228654" "304515" "83807" 
## [2889] "229996" "301419" "306410" "452996" "14126"  "158400" "87273"  "231271"
## [2897] "155621" "11861"  "154008" "161216" "447422" "156545" "13641"  "304636"
## [2905] "452513" "81809"  "231777" "9019"   "9487"   "304371" "305970" "155402"
## [2913] "307501" "155886" "229104" "87711"  "374720" "156049" "159113" "449072"
## [2921] "87306"  "229074" "302202" "303505" "11180"  "9111"   "447235" "304337"
## [2929] "159650" "234513" "159500" "12446"  "9781"   "305890" "306575" "153588"
## [2937] "306484" "377256" "379428" "450934" "377314" "234185" "448466" "12792" 
## [2945] "80262"  "82810"  "301634" "306030" "231565" "451279" "307401" "453377"
## [2953] "159201" "84893"  "161391" "161397" "161416" "306010" "306687" "232349"
## [2961] "83445"  "303155" "450065" "231998" "80918"  "304411" "155790" "159561"
## [2969] "377472" "450455" "304184" "154351" "447182" "13962"  "154728" "380298"
## [2977] "230171" "307415" "161305" "451605" "301838" "154752" "8804"   "158531"
## [2985] "379706" "306214" "158084" "305029" "307412" "377739" "451296" "157053"
## [2993] "303357" "233408" "449482" "376816" "302281" "10033"  "14375"  "453557"
View(Wage)

head(Wage);tail(Wage)
##        year age           maritl     race       education             region
## 231655 2006  18 1. Never Married 1. White    1. < HS Grad 2. Middle Atlantic
## 86582  2004  24 1. Never Married 1. White 4. College Grad 2. Middle Atlantic
## 161300 2003  45       2. Married 1. White 3. Some College 2. Middle Atlantic
## 155159 2003  43       2. Married 3. Asian 4. College Grad 2. Middle Atlantic
## 11443  2005  50      4. Divorced 1. White      2. HS Grad 2. Middle Atlantic
## 376662 2008  54       2. Married 1. White 4. College Grad 2. Middle Atlantic
##              jobclass         health health_ins  logwage      wage
## 231655  1. Industrial      1. <=Good      2. No 4.318063  75.04315
## 86582  2. Information 2. >=Very Good      2. No 4.255273  70.47602
## 161300  1. Industrial      1. <=Good     1. Yes 4.875061 130.98218
## 155159 2. Information 2. >=Very Good     1. Yes 5.041393 154.68529
## 11443  2. Information      1. <=Good     1. Yes 4.318063  75.04315
## 376662 2. Information 2. >=Very Good     1. Yes 4.845098 127.11574
##        year age           maritl     race       education             region
## 449482 2009  31       2. Married 1. White 4. College Grad 2. Middle Atlantic
## 376816 2008  44       2. Married 1. White 3. Some College 2. Middle Atlantic
## 302281 2007  30       2. Married 1. White      2. HS Grad 2. Middle Atlantic
## 10033  2005  27       2. Married 2. Black    1. < HS Grad 2. Middle Atlantic
## 14375  2005  27 1. Never Married 1. White 3. Some College 2. Middle Atlantic
## 453557 2009  55     5. Separated 1. White      2. HS Grad 2. Middle Atlantic
##              jobclass         health health_ins  logwage      wage
## 449482 2. Information 2. >=Very Good     1. Yes 4.893207 133.38061
## 376816  1. Industrial 2. >=Very Good     1. Yes 5.041393 154.68529
## 302281  1. Industrial 2. >=Very Good      2. No 4.602060  99.68946
## 10033   1. Industrial      1. <=Good      2. No 4.193125  66.22941
## 14375   1. Industrial 2. >=Very Good     1. Yes 4.477121  87.98103
## 453557  1. Industrial      1. <=Good     1. Yes 4.505150  90.48191
Wage %>% count(education)
##            education   n
## 1       1. < HS Grad 268
## 2         2. HS Grad 971
## 3    3. Some College 650
## 4    4. College Grad 685
## 5 5. Advanced Degree 426
colSums(is.na(Wage))
##       year        age     maritl       race  education     region   jobclass 
##          0          0          0          0          0          0          0 
##     health health_ins    logwage       wage 
##          0          0          0          0
max(Wage$wage)
## [1] 318.3424
min(Wage$wage)
## [1] 20.08554
mean(Wage$wage)
## [1] 111.7036
median(Wage$wage)
## [1] 104.9215
range(Wage$wage)
## [1]  20.08554 318.34243
quantile(Wage$wage)
##        0%       25%       50%       75%      100% 
##  20.08554  85.38394 104.92151 128.68049 318.34243
var(Wage$wage)
## [1] 1741.276
sd(Wage$wage)
## [1] 41.7286
summary(Wage)
##       year           age                     maritl           race     
##  Min.   :2003   Min.   :18.00   1. Never Married: 648   1. White:2480  
##  1st Qu.:2004   1st Qu.:33.75   2. Married      :2074   2. Black: 293  
##  Median :2006   Median :42.00   3. Widowed      :  19   3. Asian: 190  
##  Mean   :2006   Mean   :42.41   4. Divorced     : 204   4. Other:  37  
##  3rd Qu.:2008   3rd Qu.:51.00   5. Separated    :  55                  
##  Max.   :2009   Max.   :80.00                                          
##                                                                        
##               education                     region               jobclass   
##  1. < HS Grad      :268   2. Middle Atlantic   :3000   1. Industrial :1544  
##  2. HS Grad        :971   1. New England       :   0   2. Information:1456  
##  3. Some College   :650   3. East North Central:   0                        
##  4. College Grad   :685   4. West North Central:   0                        
##  5. Advanced Degree:426   5. South Atlantic    :   0                        
##                           6. East South Central:   0                        
##                           (Other)              :   0                        
##             health      health_ins      logwage           wage       
##  1. <=Good     : 858   1. Yes:2083   Min.   :3.000   Min.   : 20.09  
##  2. >=Very Good:2142   2. No : 917   1st Qu.:4.447   1st Qu.: 85.38  
##                                      Median :4.653   Median :104.92  
##                                      Mean   :4.654   Mean   :111.70  
##                                      3rd Qu.:4.857   3rd Qu.:128.68  
##                                      Max.   :5.763   Max.   :318.34  
## 
#Relation and Correlation
cor(Wage%>% select(age,logwage,wage))
##               age   logwage      wage
## age     1.0000000 0.2178893 0.1956372
## logwage 0.2178893 1.0000000 0.9506834
## wage    0.1956372 0.9506834 1.0000000
#Correlation function cant be used to find the relation between the #qualitative variables. So , we use chi square test to test our hypothesis #that is the two variables are independent.we will find the relation #between the jobclass and the education of the workers.

## Hypothesis testing
options(scipen = 99)
chisq.test(table(Wage$education,Wage$race))
## Warning in chisq.test(table(Wage$education, Wage$race)): 카이제곱 approximation
## 은 정확하지 않을수도 있습니다
## 
##  Pearson's Chi-squared test
## 
## data:  table(Wage$education, Wage$race)
## X-squared = 138.03, df = 12, p-value < 0.00000000000000022
chisq.test(table(Wage$maritl,Wage$race))
## Warning in chisq.test(table(Wage$maritl, Wage$race)): 카이제곱 approximation은
## 정확하지 않을수도 있습니다
## 
##  Pearson's Chi-squared test
## 
## data:  table(Wage$maritl, Wage$race)
## X-squared = 61.634, df = 12, p-value = 0.00000001135
chisq.test(table(Wage$health,Wage$maritl))
## 
##  Pearson's Chi-squared test
## 
## data:  table(Wage$health, Wage$maritl)
## X-squared = 12.832, df = 4, p-value = 0.01213
# EDA

g<-ggplot(data=Wage,aes(wage))
g+geom_density()

g1<-ggplot(data=Wage,aes(age))
g1+geom_density()

boxplot(Wage %>% select(age,wage))

glimpse(Wage)
## Rows: 3,000
## Columns: 11
## $ year       <int> 2006, 2004, 2003, 2003, 2005, 2008, 2009, 2008, 2006, 2004,…
## $ age        <int> 18, 24, 45, 43, 50, 54, 44, 30, 41, 52, 45, 34, 35, 39, 54,…
## $ maritl     <fct> 1. Never Married, 1. Never Married, 2. Married, 2. Married,…
## $ race       <fct> 1. White, 1. White, 1. White, 3. Asian, 1. White, 1. White,…
## $ education  <fct> 1. < HS Grad, 4. College Grad, 3. Some College, 4. College …
## $ region     <fct> 2. Middle Atlantic, 2. Middle Atlantic, 2. Middle Atlantic,…
## $ jobclass   <fct> 1. Industrial, 2. Information, 1. Industrial, 2. Informatio…
## $ health     <fct> 1. <=Good, 2. >=Very Good, 1. <=Good, 2. >=Very Good, 1. <=…
## $ health_ins <fct> 2. No, 2. No, 1. Yes, 1. Yes, 1. Yes, 1. Yes, 1. Yes, 1. Ye…
## $ logwage    <dbl> 4.318063, 4.255273, 4.875061, 5.041393, 4.318063, 4.845098,…
## $ wage       <dbl> 75.04315, 70.47602, 130.98218, 154.68529, 75.04315, 127.115…
Wage %>% count(jobclass)
##         jobclass    n
## 1  1. Industrial 1544
## 2 2. Information 1456
Wage %>% count(education)
##            education   n
## 1       1. < HS Grad 268
## 2         2. HS Grad 971
## 3    3. Some College 650
## 4    4. College Grad 685
## 5 5. Advanced Degree 426
table(Wage$region, Wage$race)
##                        
##                         1. White 2. Black 3. Asian 4. Other
##   1. New England               0        0        0        0
##   2. Middle Atlantic        2480      293      190       37
##   3. East North Central        0        0        0        0
##   4. West North Central        0        0        0        0
##   5. South Atlantic            0        0        0        0
##   6. East South Central        0        0        0        0
##   7. West South Central        0        0        0        0
##   8. Mountain                  0        0        0        0
##   9. Pacific                   0        0        0        0
table(Wage$education, Wage$health, Wage$health_ins)
## , ,  = 1. Yes
## 
##                     
##                      1. <=Good 2. >=Very Good
##   1. < HS Grad              48             76
##   2. HS Grad               222            390
##   3. Some College          124            343
##   4. College Grad           98            431
##   5. Advanced Degree        56            295
## 
## , ,  = 2. No
## 
##                     
##                      1. <=Good 2. >=Very Good
##   1. < HS Grad              60             84
##   2. HS Grad               126            233
##   3. Some College           63            120
##   4. College Grad           47            109
##   5. Advanced Degree        14             61