library(tidyverse)
library(ggplot2)
library("readxl")
::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE) knitr
Challenge 10
challenge_10
purrr
I’m reusing the data set from Challenge 9 - debt_in_trillions.
<-read_excel("_data/debt_in_trillions.xlsx")
data data
# A tibble: 74 × 8
`Year and Quarter` Mortgage HE Revolvin…¹ Auto …² Credi…³ Stude…⁴ Other Total
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 03:Q1 4.94 0.242 0.641 0.688 0.241 0.478 7.23
2 03:Q2 5.08 0.26 0.622 0.693 0.243 0.486 7.38
3 03:Q3 5.18 0.269 0.684 0.693 0.249 0.477 7.56
4 03:Q4 5.66 0.302 0.704 0.698 0.253 0.449 8.07
5 04:Q1 5.84 0.328 0.72 0.695 0.260 0.446 8.29
6 04:Q2 5.97 0.367 0.743 0.697 0.263 0.423 8.46
7 04:Q3 6.21 0.426 0.751 0.706 0.33 0.41 8.83
8 04:Q4 6.36 0.468 0.728 0.717 0.346 0.423 9.04
9 05:Q1 6.51 0.502 0.725 0.71 0.364 0.394 9.21
10 05:Q2 6.70 0.528 0.774 0.717 0.374 0.402 9.49
# … with 64 more rows, and abbreviated variable names ¹`HE Revolving`,
# ²`Auto Loan`, ³`Credit Card`, ⁴`Student Loan`
I will be using the map function to compute z-score for all the columns in the given data. Below, is a defined z-score function.
<- function(val) {
zscore <- (val - mean(val)) / sd(val)
result return(result)
}
<- map(
result list(data$Mortgage,
$`HE Revolving`,
data$`Auto Loan`,
data$`Credit Card`,
data$`Student Loan`,
data$Other,
data$Total),
data
zscore) result
[[1]]
[1] -2.812097806 -2.695629898 -2.608700951 -2.206127094 -2.054212430
[6] -1.947028196 -1.741943400 -1.615347848 -1.487064354 -1.331773810
[11] -1.154540036 -0.989121847 -0.707235750 -0.433789356 -0.193257805
[16] -0.033747409 0.124919017 0.364606597 0.549436104 0.697974886
[21] 0.810222942 0.843137786 0.860861163 0.829634260 0.726669878
[26] 0.665904012 0.565471540 0.480230535 0.472634802 0.362074686
[31] 0.282741473 0.150238128 0.227883400 0.204252230 0.108039610
[36] -0.005052417 -0.073414016 -0.107172830 -0.207605301 -0.203385450
[41] -0.288626455 -0.365427757 -0.318165417 -0.189881924 -0.091981363
[46] -0.150215318 -0.120676355 -0.087761512 -0.086917541 -0.133335911
[51] -0.011804180 -0.021087854 0.080188588 0.074280796 0.064153152
[56] 0.173869297 0.297932939 0.351947042 0.395833500 0.513145379
[61] 0.561251689 0.611889910 0.730889729 0.717386204 0.818662646
[66] 0.955385843 0.981548924 1.082825366 1.214484741 1.267654873
[71] 1.339392353 1.492994957 1.591739488 1.829739127
[[2]]
[1] -2.21554145 -2.07003010 -1.99727442 -1.73050361 -1.52032054 -1.20504594
[7] -0.72809206 -0.38856557 -0.11371079 0.09647228 0.20156381 0.39557895
[13] 0.53300634 0.59767805 0.70276959 0.71085355 0.71893752 0.83211301
[19] 0.92912058 1.05846401 1.18780743 1.31715086 1.42224239 1.52733392
[25] 1.60008960 1.59200564 1.55158582 1.53865147 1.44730268 1.34625313
[31] 1.27188066 1.22661046 1.00591824 0.87657482 0.99136711 0.89759312
[37] 0.77390847 0.58959409 0.46025066 0.37941102 0.29048742 0.19347985
[43] 0.15306003 0.10455624 0.08030435 0.03988453 -0.03287115 -0.04903907
[49] -0.04903907 -0.13796268 -0.19455043 -0.23497025 -0.25113818 -0.30772593
[55] -0.35622971 -0.34814575 -0.48557314 -0.51790899 -0.55024485 -0.58258070
[61] -0.64725242 -0.67958827 -0.76042791 -0.84126756 -0.88977134 -0.94635909
[67] -0.97061098 -1.01911477 -1.05145062 -1.14037423 -1.24546576 -1.35055729
[73] -1.46373279 -1.56882432
[[3]]
[1] -1.23519967 -1.31616276 -1.05196741 -0.96674310 -0.89856365 -0.80055570
[7] -0.76646597 -0.86447393 -0.87725757 -0.66845802 -0.42982995 -0.59175614
[13] -0.60880100 -0.57471128 -0.46818089 -0.46818089 -0.58323371 -0.52783791
[19] -0.48096454 -0.49374818 -0.52357669 -0.51505426 -0.51931548 -0.59601735
[25] -0.70254774 -0.80055570 -0.81760056 -0.89046734 -0.96376025 -0.97441329
[31] -0.94117580 -0.93691459 -0.95992515 -0.92839216 -0.85424701 -0.83848051
[37] -0.82825360 -0.77072719 -0.69402531 -0.63010708 -0.58323371 -0.49800940
[43] -0.36591172 -0.28920984 -0.23807526 -0.11023879 0.01333645 0.10282198
[49] 0.15821778 0.32014396 0.48633137 0.56729446 0.59712297 0.73348186
[55] 0.86984075 0.96358749 1.00619965 1.10420760 1.20221556 1.23630528
[61] 1.27039501 1.30874594 1.42379876 1.46214970 1.48771699 1.56015766
[67] 1.63685953 1.70503898 1.76895721 1.75617357 1.82861423 1.88827124
[73] 1.92236097 2.06298108
[[4]]
[1] -0.99595580 -0.92327694 -0.92327694 -0.85059807 -0.89420539 -0.86513384
[7] -0.73431189 -0.57441839 -0.67616880 -0.57441839 -0.35638179 -0.29823870
[13] -0.48720375 -0.25463138 -0.03659479 0.15237026 0.10876294 0.57390767
[19] 0.87915890 1.19894591 1.16987436 1.35883941 1.47512559 1.59141177
[25] 1.25708900 0.98090931 0.80648004 0.55937190 0.08550570 -0.17613821
[31] -0.36946399 -0.39126765 -0.87385531 -0.90438043 -0.91891620 -0.76338343
[37] -1.12968491 -1.22852817 -1.19945662 -1.12677776 -1.40295744 -1.28667126
[43] -1.22852817 -1.06863467 -1.41749321 -1.27213549 -1.11224198 -0.82152653
[49] -1.05409889 -0.77791921 -0.61802571 -0.34184602 -0.64709725 -0.39998911
[55] -0.13834520 0.32679953 0.10876294 0.39947840 0.74833695 1.12626704
[61] 0.85008736 1.05358818 1.27162477 1.64955486 1.32976786 1.62048332
[67] 1.80944837 2.47809392 1.98387764 0.87915890 0.73380117 0.90823045
[73] 0.19597758 0.44308571
[[5]]
[1] -1.58568492 -1.58054083 -1.56674530 -1.55715858 -1.54102482 -1.53377632
[7] -1.37688142 -1.34017129 -1.29831706 -1.27306423 -1.26534809 -1.23261293
[13] -1.13253690 -1.12224871 -1.10401056 -1.02240650 -0.96441852 -0.94664801
[19] -0.91274281 -0.86831747 -0.79419573 -0.77759433 -0.72007400 -0.65366841
[25] -0.59872012 -0.56925848 -0.52459838 -0.46193395 -0.37658873 -0.36746966
[31] -0.32888894 -0.25032458 -0.18625721 -0.15773087 -0.11377224 -0.10582227
[37] -0.03554427 -0.01135798 0.08684748 0.11022973 0.15699423 0.17570003
[43] 0.25286146 0.37678739 0.44927236 0.46563994 0.48434574 0.55683072
[49] 0.63165392 0.63399214 0.66438907 0.73219760 0.80000613 0.79532968
[55] 0.84209418 0.91457915 0.99407881 0.99407881 1.02447573 1.07357846
[61] 1.14138699 1.13671054 1.22322486 1.25829824 1.32610677 1.30740097
[67] 1.35416547 1.37754772 1.44067980 1.44535625 1.46640027 1.48744430
[73] 1.55525283 1.52251767
[[6]]
[1] 2.06261590 2.24591498 2.05606950 1.42979766 1.38397289 0.87335404
[7] 0.58749477 0.86898978 0.24053580 0.42165275 0.48711670 0.70751202
[13] 0.76861171 0.87553617 1.27923057 0.49366310 0.45438473 0.53948787
[19] 0.65295872 0.85153272 0.70314776 0.38673864 0.62022675 0.62240888
[25] 0.56130919 0.12488282 -0.03441281 -0.09987676 -0.44028933 -0.74142353
[31] -0.88107997 -0.91817621 -1.18657843 -1.15166432 -1.23240320 -1.15821071
[37] -1.40915588 -1.55099445 -1.57281576 -1.44188785 -1.66010104 -1.90013554
[43] -1.72556499 -1.44188785 -1.50735181 -1.31095994 -1.22367467 -1.04910412
[49] -1.18003203 -0.96181885 -0.69996302 -0.69996302 -0.63449907 -0.59085643
[55] -0.35082193 -0.13260874 -0.35082193 -0.11078742 0.06378312 0.12924708
[61] 0.04196181 0.15106840 0.34746026 0.52203081 0.45656686 0.63113741
[67] 0.91481455 1.06756378 0.95845718 0.76206532 0.74024400 0.78388664
[73] 0.65295872 0.82752927
[[7]]
[1] -2.61296476 -2.52529286 -2.42693486 -2.13369932 -2.00512152 -1.90705078
[7] -1.69275444 -1.57290935 -1.47805593 -1.31425999 -1.14173132 -1.02102445
[13] -0.80293627 -0.59306374 -0.38376573 -0.26776994 -0.16320711 0.04046059
[19] 0.20287792 0.34024566 0.43498418 0.47094920 0.51478515 0.51162529
[25] 0.43101999 0.36069861 0.28704502 0.22218159 0.19391516 0.09400595
[31] 0.03735818 -0.03801898 -0.01434871 -0.02865429 -0.06789407 -0.13936449
[37] -0.19712906 -0.22715130 -0.26966586 -0.25185571 -0.31505302 -0.35986566
[43] -0.28690149 -0.14844192 -0.07432871 -0.08467009 -0.03985745 0.02736151
[49] 0.04115002 0.04229906 0.16409752 0.19339809 0.27153295 0.29164118
[55] 0.32783601 0.45767776 0.54328139 0.60877679 0.67542123 0.78630379
[61] 0.82249861 0.86960934 0.99542944 1.01381411 1.08505472 1.19536276
[67] 1.24821869 1.35910125 1.44815201 1.42861829 1.47860162 1.59695296
[73] 1.64578724 1.82561232