Question #0: Finish all Lab Exercises

How many samples are there? How many variables are there?

Answer: samples are 10000, and variables are 55

glimpse(loans_full_schema)
loans <- select(loans_full_schema,loan_amount,interest_rate,term,grade,state,annual_income,homeownership,debt_to_income)
glimpse(loans)
## Rows: 10,000
## Columns: 8
## $ loan_amount    <int> 28000, 5000, 2000, 21600, 23000, 5000, 24000, 20000, 20…
## $ interest_rate  <dbl> 14.07, 12.61, 17.09, 6.72, 14.07, 6.72, 13.59, 11.99, 1…
## $ term           <dbl> 60, 36, 36, 36, 36, 36, 60, 60, 36, 36, 60, 60, 36, 60,…
## $ grade          <fct> C, C, D, A, C, A, C, B, C, A, C, B, C, B, D, D, D, F, E…
## $ state          <fct> NJ, HI, WI, PA, CA, KY, MI, AZ, NV, IL, IL, FL, SC, CO,…
## $ annual_income  <dbl> 90000, 40000, 40000, 30000, 35000, 34000, 35000, 110000…
## $ homeownership  <fct> MORTGAGE, RENT, RENT, RENT, RENT, OWN, MORTGAGE, MORTGA…
## $ debt_to_income <dbl> 18.01, 5.04, 21.15, 10.16, 57.96, 6.46, 23.66, 16.19, 3…

How many distinct values are there for homeownership variable? Which value is the most common one?

Answer: There are four home ownerships variable which are “any”, “mortgage”, “own”, and “rent”. The most value is 4789 for “mortgage”

unique(loans$homeownership)
## [1] MORTGAGE RENT     OWN     
## Levels:  ANY MORTGAGE OWN RENT
table(loans$homeownership)
## 
##               ANY MORTGAGE      OWN     RENT 
##        0        0     4789     1353     3858

How many distinct interest rates are there? Which value is the most common one?

Answer: There are 58 distinct interest rates, and the most common one is 9.93 for 390.

unique(loans$interest_rate)
##  [1] 14.07 12.61 17.09  6.72 13.59 11.99  6.71 15.04  9.92  9.43 19.03 28.72
## [13] 26.77 15.05  6.08 11.98  7.96  7.34  5.32  6.07 12.62  9.44 20.39  9.93
## [25] 21.45 10.42 18.06 22.91 30.79 17.47  5.31  7.97 14.08 19.42 10.91 16.02
## [37] 13.58 16.01 20.00 21.85 10.90 23.87  7.35 23.88 25.82 10.41 18.45 30.17
## [49] 24.85 25.81 24.84 30.75 29.69 26.30 22.90  6.00 30.65 30.94
sort(table(loans$interest_rate))
## 
## 30.94     6 30.75 30.65 29.69 30.17 30.79  22.9 23.87 25.81 22.91 24.84 28.72 
##     1     3     4     5     9     9    11    13    20    26    28    31    31 
## 23.88 26.77 24.85 25.82  26.3 21.85 20.39 19.42 17.47    20 18.45 21.45 18.06 
##    37    38    42    47    53    90    93   114   124   137   146   172   176 
## 14.07  5.31  6.71 10.41 17.09 16.01 19.03 15.04  6.07  7.96 13.58  5.32  7.34 
##   183   188   192   194   195   196   197   199   202   211   225   234   243 
##  9.92 11.98 12.61  7.97  10.9  6.08  9.43 16.02 15.05 10.91  6.72 14.08  7.35 
##   248   255   264   274   275   277   280   284   304   306   312   318   325 
## 12.62 10.42 13.59  9.44 11.99  9.93 
##   333   346   347   367   376   390

Apply table function to the annual_income variable. Do you think the result is helpful or not?

table(loans$annual_income)
## 
##         0         1      3000      3120      3300      4000      4800      5000 
##        23         1         2         1         1         1         1         2 
##      5208      5235      5500      7200      7500      7800      8000      8500 
##         1         1         1         1         1         1         1         1 
##      9000      9600      9840      9972      9996     10000     10320     10500 
##         4         2         1         1         1        11         1         2 
##     10548     10596     10800     11000     11150     11352     11772     12000 
##         1         1         4         3         1         1         1         9 
##     12036     12250     12276     12300     12480     12696     12816     13000 
##         1         1         1         1         1         1         1         6 
##     13050     13136     13140     13164     13200     13390     13500     13692 
##         1         1         1         1         2         1         1         1 
##     13728     13800     13920     13930     14000     14200     14300     14364 
##         1         1         1         1         5         1         1         1 
##     14400     14500     14508     14560     14568     14616     14928     15000 
##         4         1         1         1         1         1         1        27 
##     15200     15250     15380     15500     15599     15600     15864     15972 
##         1         1         1         1         1         2         1         1 
##     15984     15996     16000     16200     16356     16500     16620     16700 
##         1         1         7         2         1         1         1         1 
##     16752     16848     17000     17200     17340     17453     17500     18000 
##         1         1        14         1         1         1         1        19 
##     18312     18348     18375     18400     18500     18624     18700     18708 
##         1         1         1         1         1         1         1         1 
##     18720     18800     18900     18960     19000     19200     19250  19582.09 
##         1         1         1         1        12         2         1         1 
##     19700     19719     20000     20076     20280     20376     20400     20562 
##         1         1        46         1         1         1         2         1 
##     20567     20600     20712     20800     21000     21049     21120     21320 
##         1         1         1         1        11         1         1         1 
##     21432     21444     21500     21600     21750     21800     21900     21918 
##         1         1         1         3         1         1         1         1 
##     22000     22104     22114     22212     22244  22362.75     22500     22560 
##        20         1         1         1         1         1         1         1 
##     22568   22644.7  22772.25     22800     22848     23000     23040     23077 
##         1         1         1         4         1        24         1         1 
##     23500     23666     23898     23920     24000     24402     24480     24482 
##         3         1         1         1        41         1         1         1 
##     24525     24538     24576     24720     25000     25100     25200     25236 
##         1         1         1         1        92         1         4         1 
##     25252     25320     25356     25400     25465     25500     25560     25600 
##         1         1         1         1         1         5         1         1 
##     25700     25800     25825     25875     25920     25992     26000     26127 
##         1         1         1         1         1         1        33         1 
##     26271     26304     26400     26448     26500     26551     26592     26600 
##         1         1         1         1         3         1         1         1 
##     26694     26895     27000     27200     27278     27354     27480     27500 
##         1         1        21         1         1         1         1         3 
##     27511     27550     27600     27610     27684     27703     27840     27872 
##         1         1         1         1         1         1         1         1 
##     27976     28000     28100  28201.32     28500     28560     28600     28711 
##         1        48         1         1         3         1         1         1 
##     28740     28750     28790     28800     28865     29000     29016     29120 
##         1         1         1         3         1        19         1         2 
##     29200     29250     29300     29388     29400     29415     29500     29744 
##         1         1         1         1         1         1         2         1 
##     29760  29881.32     29982     30000     30163   30205.5     30457     30504 
##         1         1         1       168         1         1         1         1 
##     30564     30600     30720     30784     30800     31000     31200     31250 
##         1         1         2         1         1        32         6         1 
##  31280.04     31300     31304     31360     31500     31536     31600     31656 
##         1         1         1         1         6         1         2         1 
##     31680     31700     31800     31812     31858  31942.63     31972     32000 
##         1         1         1         1         1         1         1        77 
##     32028     32112  32248.24     32300     32340     32344     32357     32500 
##         1         1         1         1         1         1         1         5 
##     32688     32760     32800     32844     32900     33000     33024     33040 
##         1         1         1         2         1        46         1         1 
##     33110   33134.4     33186     33195     33197     33250     33280     33300 
##         1         1         1         1         1         1         2         1 
##     33395  33427.94     33477     33480     33500     33600  33661.32     33701 
##         1         1         1         1         3         4         1         1 
##     33768     33800     33852     33918     33975     33996     33999     34000 
##         1         1         1         1         1         1         1        45 
##     34047     34113     34235     34320     34385  34415.16     34416     34451 
##         1         1         1         2         1         1         1         1 
##     34500     34521     34560     34600  34625.64     34700   34733.5     34771 
##         4         1         3         1         1         1         1         1 
##     34800     34840     34964  34982.21     35000     35200     35300     35360 
##         1         1         1         1       182         1         1         1 
##     35500     35568     35600     35790     35859     35860     36000     36025 
##         2         1         2         1         1         1        73         1 
##     36051     36144     36171     36224     36240     36250     36300     36320 
##         1         1         1         1         1         1         1         1 
##     36331     36400     36420     36433     36500     36536     36598     36609 
##         1         4         2         1         2         1         1         1 
##     36610     36780     36785     36800  36886.32     36900     37000     37020 
##         1         1         1         1         1         1        46         1 
##     37024  37183.56     37200     37260     37262     37400     37440     37447 
##         1         1         1         1         1         1         2         1 
##     37500     37540     37700     37770     37780     37840     38000     38043 
##         5         1         1         1         1         2        70         1 
##     38168     38200     38248     38272     38400     38411     38427     38450 
##         1         1         1         1         7         1         1         1 
##     38455     38464     38500     38550     38560     38600     38666     38678 
##         1         1         3         1         1         2         1         1 
##     38700     38900     38979  38990.47     39000     39053   39332.8     39362 
##         2         1         1         1        47         1         1         1 
##     39400     39442     39500     39520     39579     39600     39800     39804 
##         1         1         2         1         1         2         1         1 
##     39900     39910     39995     40000     40012  40059.96     40250     40280 
##         1         1         1       247         1         1         1         1 
##     40296     40320     40390     40400     40552     40600  40743.25     40758 
##         1         1         1         1         1         1         1         1 
##     40765     40800     40825     41000     41072     41200     41280     41364 
##         1         2         1        35         1         1         1         1 
##     41442     41500     41524     41600  41640.62     41787     41900     41969 
##         1         3         1         6         1         1         1         1 
##     42000     42204     42240     42321     42433     42500     42563   42585.6 
##       121         1         1         1         1         5         1         1 
##     42600     42614     42640  42655.99     42700     42733     42755     42780 
##         1         1         2         1         1         1         1         1 
##     42996     43000     43125  43188.48     43200     43260     43437   43492.8 
##         1        53         1         1         2         1         1         1 
##     43500     43600     43611     43640     43664     43678     43896     43900 
##         3         1         1         1         1         1         1         1 
##     43930     44000     44050     44088     44100     44184     44190     44200 
##         1        37         1         1         1         1         1         3 
##     44211     44240     44268     44346     44393     44500     44600     44640 
##         1         1         1         1         1         7         1         1 
##     44668     44775     44820     44865     44900     45000     45001     45198 
##         1         1         1         1         1       204         1         1 
##     45240     45300     45360     45384     45500     45576     45700     45782 
##         1         1         1         1         2         1         1         1 
##     45804     45930     46000     46080     46128     46219     46236     46280 
##         1         1        40         1         1         1         1         1 
##     46310     46328     46400     46422     46500     46556     46650     46680 
##         1         1         1         1         6         1         1         1 
##     46800     46996     47000     47028     47106     47128     47328     47418 
##         2         1        32         1         1         1         1         1 
##     47476     47500     47595     47600   47611.2     47700  47710.44     47757 
##         1         4         1         1         1         3         1         1 
##     47782     47800     47840     47960     48000     48028     48200     48222 
##         1         2         2         1       116         1         2         1 
##     48300     48370     48391     48425     48450     48500     48509     48580 
##         1         1         1         1         1         2         1         1 
##     48589     48712     48750     48769     48996     49000     49200     49300 
##         1         1         2         1         1        31         1         1 
##     49343     49500     49583     49623     49642     49650     49920     49992 
##         1         7         1         1         1         1         1         1 
##     50000     50066     50120     50129     50160     50220     50256     50320 
##       350         1         1         1         1         1         1         1 
##     50400     50615     50622     50676     50731     50840     50912     50919 
##         2         1         1         1         1         1         1         1 
##     50960     50976     51000     51072     51200     51252     51500     51600 
##         1         1        31         1         2         1         1         1 
##     51632     51754     51800     51882     51883     51900     51921     52000 
##         1         1         1         1         1         1         1        90 
##     52140     52250  52278.85     52300     52325     52400     52420     52440 
##         1         1         1         1         1         1         1         1 
##     52464   52499.2     52500   52505.4     52537     52540     52600     52680 
##         1         1         5         1         1         1         1         1 
##     52755     52764     52780  52821.43     53000  53038.52     53045     53200 
##         1         1         1         1        39         1         1         1 
##     53232     53246     53400     53456     53500   53527.6     53574     53592 
##         1         1         1         1         3         1         1         1 
##  53654.88     53695     53700     53701     53774     53800     53900     53953 
##         1         1         1         1         1         1         1         1 
##     54000     54024     54032   54059.5     54100     54105   54127.1     54164 
##        69         1         1         1         1         1         1         1 
##     54200     54237     54250     54269     54300     54500     54600     54625 
##         3         1         1         1         1         5         3         1 
##     54750     54800     54840     54883     54902     54996     55000     55120 
##         1         1         1         1         1         1       236         1 
##  55160.04     55200     55213     55232     55296     55400     55412     55500 
##         1         3         1         1         1         1         1         1 
##  55523.24     55584     55700     55836     55944     56000     56160     56309 
##         1         1         1         1         1        66         1         1 
##     56368     56400  56419.68     56446     56450     56500     56736     56750 
##         1         2         1         1         1         2         1         1 
##     56800     56820     56891     56912     57000  57056.62     57200     57500 
##         1         1         1         1        48         1         1         9 
##     57506     57564     57600     57626     57820     57845     57920     58000 
##         1         1         2         1         1         1         1        69 
##     58142     58240     58250     58380     58500     58544     58559     58700 
##         1         1         2         1         4         1         1         1 
##     58734     58746     58756     58764     58800     58900     59000     59019 
##         1         1         1         1         4         1        37         1 
##     59054     59142     59182     59200     59300     59390     59500     59600 
##         1         1         1         1         1         1         1         1 
##     59700     59789  59819.97     59869     59871     59991     60000     60100 
##         1         1         1         1         1         1       383         1 
##     60198     60200     60372     60433   60495.2     60500     60600     60705 
##         1         1         1         1         1         1         2         1 
##     60800     60825     60960     61000     61175     61200     61300     61368 
##         1         1         1        31         1         1         1         1 
##     61400  61400.91     61500     61538     61582     61584     61766     61800 
##         1         1         2         1         1         1         1         1 
##     61846     61880     62000     62004     62100     62200     62300     62339 
##         1         1        76         1         1         1         1         1 
##     62400     62500     62628     62712     62800     62823     62914  62933.64 
##        11         4         1         1         1         1         1         1 
##     63000     63024     63071     63130     63215     63235     63288     63330 
##        50         1         1         1         1         1         1         1 
##     63360     63373  63446.53  63477.84     63500     63645     63695     63700 
##         2         1         1         1         4         1         1         1 
##     63780     63938     63950     64000     64106     64449     64600     64655 
##         1         1         1        46         1         1         1         1 
##     64660     64721     64800     64865     64874     64934     64992     65000 
##         1         1         1         1         1         1         1       314 
##     65244  65318.52     65488     65500     65700     65800     65895     65900 
##         1         1         1         3         2         1         1         1 
##     65918     65926     66000     66144     66150     66200     66300     66500 
##         1         1        29         1         1         1         1         2 
##     66800     66872     66900   66913.6     66920     66996     67000     67200 
##         1         1         2         1         1         1        46         3 
##     67400     67500     67600     67700     67745     67760     67764     67790 
##         4         4         3         1         1         1         1         1 
##     67800     67846     68000     68095     68186     68200     68250     68274 
##         1         1        76         1         1         2         1         1 
##     68353     68400     68450     68520     68575     68640     68700     68748 
##         1         1         1         1         1         1         2         1 
##     68750     68810     69000     69077     69108     69116     69131     69200 
##         2         1        28         1         1         1         1         1 
##     69351     69467     69700     69800     69854     69860     69900     69996 
##         1         1         1         1         1         1         2         2 
##     70000     70020     70090     70200     70209     70260     70400     70480 
##       273         1         1         1         1         1         1         1 
##     70500     70540     70566     70866     70900     71000     71104     71240 
##         3         1         1         1         1        41         1         1 
##     71400     71422     71487     71500     72000     72100     72135     72150 
##         1         1         1         3       110         1         1         1 
##  72197.95     72200     72276     72320     72400     72465     72500     72550 
##         1         1         1         1         1         1         3         1 
##     72600     72612     72672     72720     72768     72800     72828     72900 
##         1         1         1         1         1         3         1         1 
##     72949     73000     73364     73400     73460     73500     73623     74000 
##         1        29         1         1         1         1         1        27 
##     74106     74200     74300     74400     74500     74600     74620     74644 
##         1         1         1         1         2         2         1         1 
##     74870     74895     75000     75200     75250     75312     75341     75400 
##         1         1       260         1         2         1         1         1 
##     75500     75610     75696     75800     75894     75999     76000     76012 
##         1         1         1         1         1         1        36         1 
##     76300     76500     76590     76760     76800     76892     77000     77017 
##         1         4         1         1         1         1        26         1 
##  77049.16     77100  77177.88     77400     77500     77856     77900     78000 
##         1         1         1         1         3         1         1        60 
##  78132.55     78500     78648     78762     78816     78900     78927     79000 
##         1         2         1         1         1         1         1        19 
##     79040     79043     79300     79312     79500     79547     79584     79700 
##         2         1         1         1         5         1         1         2 
##     79800     79982     79992     80000     80200     80400     80500  80833.44 
##         1         1         1       248         1         2         2         1 
##     80901  80969.94     81000     81015     81120     81200     81250   81257.8 
##         1         1        23         1         1         1         1         1 
##     81526     81750     81800     81900     82000     82048     82200     82416 
##         1         1         1         1        60         1         1         1 
##     82500   82756.8  82767.78     83000     83004     83043     83200     83207 
##         6         1         1        30         1         1         1         1 
##     83233     83500     83600     83740     83800  83819.67     84000     84240 
##         1         1         1         2         1         1        34         1 
##  84279.78     84362     84396     84400     84500     84646     84676     84800 
##         1         1         1         1         3         1         1         1 
##     85000  85175.04  85208.71     85410     85441     85451     85500     85600 
##       141         1         1         1         1         1         4         1 
##     85605     85750     85765     85800     85820     86000     86110     86500 
##         1         1         1         1         1        37         1         2 
##     86845     86850     87000     87040     87120     87125     87276     87400 
##         1         1        24         1         1         1         1         2 
##     87500     87600     87800     87812     88000     88116     88141     88251 
##         9         1         1         1        29         1         1         1 
##     88272     88300     88400     88500     88600     88932     88944     89000 
##         1         1         1         1         1         1         1        26 
##     89179     89206     89400     89500     89748     90000     90500     90540 
##         1         1         1         1         1       204         2         1 
##     90666     90720     91000     91100     91200     91275     91380     91690 
##         1         1        22         1         2         1         1         1 
##     91900     92000     92200     92290     92400     92465     92500     92600 
##         1        45         1         1         1         1         1         1 
##     92879     92942     93000     93006     93300     93500     93600     93700 
##         1         1        18         1         1         5         2         1 
##     93707     93844     94000     94500     94966     95000     95280     95500 
##         1         1        29         1         1        96         1         2 
##     95504     95543     95634     95731     95800     96000     96152  96355.33 
##         1         1         1         1         1        36         1         1 
##     96400     96500     96512     96774     96800  96821.31     96996     97000 
##         1         2         1         1         2         1         1        21 
##     97500     97598     97600     98000     98241     98281     98460     98560 
##         3         1         1        37         1         1         1         1 
##     98700     98728     99000     99097     99270     99985     1e+05    100100 
##         1         1        18         1         1         1       221         1 
##    100203    100500    100600  100609.6    100800    101000    101200    101400 
##         1         1         1         1         1        22         1         1 
##    101500    101576    101760    101794    101851    101920    102000    102004 
##         1         1         1         1         1         1        23         1 
##    102500    102900    102947    103000    103170    103294    103500    103960 
##         2         1         1        24         1         1         3         1 
##    104000 104176.66    104465    104500    104550    104660    105000    105832 
##        27         1         1         1         1         1        66         1 
##    106000    106080    106090    106300    106500    106686    106883    107000 
##        23         1         1         1         2         1         1        19 
##    107200    107611    108000  108254.9    108367    108407    109000    109500 
##         1         1        26         1         1         1        18         1 
##    109600    109688    109992    110000    110100    110500    111000  111230.9 
##         1         1         1       124         1         2         6         1 
##    111275    111996    112000    112029    112500    112700    112798    112914 
##         1         1        19         1         2         1         1         1 
##    113000 113213.25    113409    113500    114000    114416    115000    115200 
##         8         1         1         2        16         1        62         1 
##    115800    115942    116000    116162    116365    116480    117000    117650 
##         1         1         6         1         2         1        15         1 
##    118000    118500    119000    119016    119216    119568    120000    120690 
##        14         2         4         1         1         1       178         1 
##    120700    120850    121000    121500    121800    122000    123000    123973 
##         1         1         6         1         1        11         7         1 
##    124000    124500    124730    124800    125000    125381    126000    126049 
##         7         2         1         1        89         1        12         1 
##    126200    126402    126700    127000    127200    127563    128000    128500 
##         1         1         1         6         1         1        14         2 
##    129000    129600    130000    130500    130800    131000  131004.7    132000 
##         7         1        80         1         1         4         1        14 
##    132200    132300    132323    132400    132454    132500    132900    133000 
##         1         1         1         1         1         1         1         7 
##    133500    134000    134315  134666.2    134676    135000    135400    135500 
##         1         2         1         1         1        34         1         1 
##    135865    136000    137000    137200    137500    138000    138900    139000 
##         1         5         6         1         2        11         1         8 
##    139200    139500    140000    141000    142000    142500    143000    143559 
##         1         1        75         1        10         2         3         1 
##    144000    145000    145262    146000    147000    148000    148600    148650 
##        14        26         1         4         4         3         1         1 
##    148692    150000    151000    151500    152000    153000    154000    154400 
##         1        99         5         1        11         2         4         1 
##    155000    155555    156000    157000    158000    159000    160000    160482 
##        25         1         7         3         3         3        44         1 
##    161000    161105    162000    163000    164000    164200    165000    165200 
##         2         1         3         4         1         1        22         1 
##    166000    166088    166400    166600    167000    168000    168500    169000 
##         5         1         1         1         5         4         1         2 
##    169880    170000    171000    172000    173180    175000    175500    175900 
##         1        31         1         4         1        29         1         1 
##    175933    176000    177000    179000    179443    180000    182000    182200 
##         1         1         2         1         1        43         4         1 
##    183000    185000    185220    186000    187000    187116    187500    188000 
##         1        22         1         1         1         1         1         2 
##    188500    189000    190000    191000    192000    193000    193783    194000 
##         1         5        23         1         2         1         1         1 
##    195000    196000    197000    197280    198000    198999     2e+05    204000 
##        10         4         1         1         3         1        46         2 
##    205000    206000    208000    208888    209000    210000    211000    212000 
##         9         4         1         1         1         8         1         1 
##    212500    214458    215000    216000    217000    220000    220500    223500 
##         1         1         1         1         1        13         1         1 
##    225000    230000    231000    232000    233000    235000    236000    240000 
##        11         9         1         1         2         5         1        15 
##    245000    249996    250000    252000    254000    255000    256000    258000 
##         6         1        30         1         2         1         1         2 
##    258240    260000    264000    265000    270000    271000    275000    278000 
##         1         8         1         2         1         1         9         1 
##    280000    281000    281125    285000    288000    289000    289139    290000 
##         8         1         1         2         1         1         1         2 
##    294000    299520     3e+05    301000    305000    312221    315000    316317 
##         1         1        24         1         1         1         2         1 
##    318000    319000    320000    325000    325500    330000    332000    340000 
##         1         1         4         3         1         2         1         2 
##    349000    350000    351950    352250    353000    355000    363000    375000 
##         1         9         1         1         1         1         1         3 
##    377000    388000    389000     4e+05    405000    410000    418000    420000 
##         1         1         1         5         1         1         1         1 
##    425000    430000    440000    450000    485000    498000     5e+05    520000 
##         1         1         1         2         1         1         5         1 
##    550000     6e+05    650000     7e+05    740000    750000    780000    793000 
##         2         2         1         1         1         1         2         1 
##    885000    910000   1020000   1050000   1200000   1600001   2300000 
##         1         1         1         1         1         1         1

Answer: It does not help a lot, since there are so many distinct annual incomes from the data base. In the table, it looks a little messy.

ggplot(loans) +
  geom_histogram(mapping = aes(x = interest_rate))
ggplot(data = loans) +
  geom_histogram(mapping = aes(x = interest_rate), bins = 20)
ggplot(data = loans) +
  geom_histogram(mapping = aes(x = interest_rate), bins = 10)
ggplot(data = loans) +
  geom_histogram(mapping = aes(x = interest_rate), binwidth = 5)
ggplot(data = loans) +
  geom_histogram(mapping = aes(x = interest_rate), binwidth = 5, boundary = 10)
ggplot(data = loans) +
  geom_histogram(mapping = aes(x = interest_rate), binwidth = 1, center = 10)
ggplot(data = loans) +
  geom_histogram(mapping = aes(x = interest_rate), binwidth = 1, center = 10) +
  xlim(0, 40)

Create a histogram of loan_amount. Customize your plot to give a graph that looks most reasonable to you.

ggplot(loans) +
  geom_histogram(mapping = aes(x = loan_amount), binwidth = 3000, boundary = 10)

Create a histogram of annual_income. What is the issue with your graph?

ggplot(loans) +
  geom_histogram(mapping = aes(x = annual_income))

Answer: Since there is an outlier about 2.3 million, so it stretches the whole graph to the right, and we don’t have enough data between the outlier and the whole body of data. The graph will look like plain on the right.

ggplot(loans, aes(x = loan_amount)) +
  geom_density()
ggplot(loans, aes(x = loan_amount)) +
  geom_histogram(aes(y = after_stat(density)),  
                 boundary = 0, colour = "black", fill = "white") +
  geom_density(linewidth = 1.2)
ggplot(loans, aes(x = loan_amount)) +
  geom_histogram(aes(y = after_stat(density)), binwidth = 5000, 
                 boundary = 0, colour = "black", fill = "white") +
  geom_density(adjust = 30/8, linewidth = 1.2) #30/8 = 30bins/8bins
ggplot(loans) + 
  geom_histogram(mapping = aes(x = loan_amount, y = after_stat(count/sum(count))), 
                 binwidth = 5000, boundary = 0, colour = "black", fill = "white")

Create a histogram of variable debt_to_income in loans with the following requirements: 1. The plotting range of x is between 0 and 100 2. The binwidth is 2 3. Create a density plot on top of the histogram

ggplot(loans, aes(x = debt_to_income)) +
  geom_histogram(aes(y = after_stat(density)),boundary = 0, binwidth = 2, colour = "black", fill = "white") +
  geom_density(linewidth = 1.2) +
  xlim(0,100)

The distribution of “debt_to_income” is likely right-skewed. This is because most people are unlikely to have debt equal to their entire income, as it would create excessive financial pressure when repaying the debt.

For loans data, create a scatter plot of interest_rate vs debt_to_income with mapping color to grade. What can you learn from the graph?

ggplot(loans) +
  geom_point(aes(x = debt_to_income, y = interest_rate, color = grade))

From the graph, we can see that as the grade increases from A to G, the “debt_to_income” ratio decreases. This suggests that individuals with lower grades tend to take out smaller loans. Additionally, as the grade increases from A to G, the interest rate rises. This implies that individuals with lower grades should borrow less to minimize interest payments. For lenders, this serves as a form of risk hedging.

Question #1: Create a scatter plot of loan_amount vs interest_rate with a color grouping using term variable (please use factor(term) to convert it into a categorical variable). Save your plot to your local folder.

ggplot(loans) +
  geom_point(aes(loan_amount,interest_rate,colour = factor(term)))

ggsave("my_plot.pdf")