R Program

profilesnvvkc
Final-Case-Analysis.pdf

Final Case Analysis

8/5/2020

1. Install required Packages

library("tidyverse")

## -- Attaching packages ------------------------------------------------------------------------------------------ tidyverse 1.3.0 --

## v ggplot2 3.3.2 v purrr 0.3.4 ## v tibble 3.0.2 v dplyr 1.0.0 ## v tidyr 1.1.0 v stringr 1.4.0 ## v readr 1.3.1 v forcats 0.5.0

## -- Conflicts --------------------------------------------------------------------------------------------- tidyverse_conflicts() -- ## x dplyr::filter() masks stats::filter() ## x dplyr::lag() masks stats::lag()

library("ggplot2") library("usmap") library("dplyr")

2. Merging and Cleaning

2.1 Read all the data files and create dataframes

data_2012=read.csv(’Newdata_2012.csv’) data_2013=read.csv(’Newdata_2013.csv’) data_2014=read.csv(’Newdata_2014.csv’) data_2015=read.csv(’Newdata_2015.csv’) data_2016=read.csv(’Newdata_2016.csv’) data_2017=read.csv(’Newdata_2017.csv’)

2.1 Stack and Merge dataframes

All_Data_Frames <- rbind(data_2012,data_2013,data_2014,data_2015,data_2016,data_2017)

2.2 Reading the data from States and States Regions CSV files

1

States <- read.csv(’States.csv’) states_regions <- read.csv(’states_regions.csv’)

2.3 Merging the data of States and States Regions using state name

Stregion <- merge(States, states_regions, by.x = ’Geography’, by.y = ’State’)

2.4 Merging all the data into one file using state code as a primary key

Final_DataFrame <- merge(All_Data_Frames,Stregion,by.x="addr_state",by.y="State.Code")

2.5 Number of Columns in the Data after merging and cleaning

ncol(Final_DataFrame)

## [1] 42

2.6 First 10 lines of the data from Final Dataframe

head(Final_DataFrame, 10)

## addr_state loan_amnt term int_rate grade sub_grade ## 1 AK 25000 36 months 7.89% A A5 ## 2 AK 15000 36 months 6.08% A A2 ## 3 AK 15000 36 months 19.03% D D3 ## 4 AK 15000 60 months 14.99% C C5 ## 5 AK 10000 36 months 7.97% A A5 ## 6 AK 12000 60 months 16.29% D D2 ## 7 AK 14500 36 months 13.33% C C3 ## 8 AK 12000 36 months 7.89% A A5 ## 9 AK 9750 36 months 16.99% D D1 ## 10 AK 14400 36 months 12.99% C C2 ## emp_title emp_length home_ownership annual_inc issue_d ## 1 Analyst Programmer V 10+ years MORTGAGE 96000 Apr-15 ## 2 sr. Claims coordinator 9 years MORTGAGE 88000 Nov-17 ## 3 Maintenance Superintendent 1 year OWN 158000 Dec-17 ## 4 Fuel Delivery < 1 year RENT 40000 Jun-14 ## 5 plastic shop manager 10+ years MORTGAGE 85000 May-17 ## 6 IT Specialist 9 years RENT 46000 May-14 ## 7 Office Manager 3 years RENT 40000 Apr-15 ## 8 E7 10+ years OWN 69500 May-16 ## 9 Dental Assistant 3 years OWN 42000 Dec-13 ## 10 solution specialist < 1 year RENT 35000 Apr-16

2

## loan_status purpose title dti delinq_2yrs ## 1 Fully Paid home_improvement Home improvement 8.25 0 ## 2 Fully Paid credit_card Credit card refinancing 28.55 0 ## 3 Fully Paid debt_consolidation Debt consolidation 0.61 0 ## 4 Fully Paid debt_consolidation Debt consolidation 26.61 0 ## 5 Fully Paid debt_consolidation Debt consolidation 11.39 1 ## 6 Fully Paid credit_card Credit card refinancing 15.18 0 ## 7 Fully Paid debt_consolidation Debt consolidation 7.68 0 ## 8 Fully Paid debt_consolidation Debt consolidation 28.68 1 ## 9 Fully Paid other Debt consolidation 4.57 0 ## 10 Fully Paid debt_consolidation Debt consolidation 35.39 0 ## inq_last_6mths mths_since_last_delinq open_acc total_pymnt total_rec_int ## 1 0 52 5 25585.76 585.76 ## 2 0 NA 9 15767.98 767.98 ## 3 0 55 9 16501.11 1501.11 ## 4 0 NA 10 20572.11 5572.11 ## 5 0 19 8 10964.13 964.13 ## 6 0 NA 14 17620.10 5620.10 ## 7 2 NA 3 15727.82 1227.82 ## 8 2 35 15 13510.13 1510.13 ## 9 3 NA 7 12442.98 2692.98 ## 10 0 NA 23 17284.11 2884.11 ## last_pymnt_d last_pymnt_amnt last_credit_pull_d application_type tot_cur_bal ## 1 Jul-15 24043.38 Jul-15 Individual 54067 ## 2 Oct-18 11211.85 Nov-18 Individual 329897 ## 3 Jun-18 13766.62 Sep-18 Individual 4356 ## 4 Sep-17 6967.93 Oct-17 Individual 142854 ## 5 Dec-18 5334.85 Jun-19 Individual 288456 ## 6 May-19 293.57 May-19 Individual 12242 ## 7 Jan-16 9302.40 Apr-19 Individual 10564 ## 8 May-19 375.34 Apr-19 Individual 402066 ## 9 Sep-16 679.41 Oct-16 Individual 3658 ## 10 Aug-18 4183.48 Feb-19 Individual 49382 ## acc_open_past_24mths pub_rec_bankruptcies Orig..Index issue_Month issue_Year ## 1 1 0 323708 Apr 2015 ## 2 1 0 48735 Nov 2017 ## 3 9 0 12602 Dec 2017 ## 4 2 0 142269 Jun 2014 ## 5 0 0 48342 May 2017 ## 6 2 0 149194 May 2014 ## 7 1 0 327717 Apr 2015 ## 8 8 0 63156 May 2016 ## 9 5 0 7356 Dec 2013 ## 10 9 0 70525 Apr 2016 ## Geography Num_Households Median_income_Households ## 1 Alaska 252536 76114 ## 2 Alaska 252536 76114 ## 3 Alaska 252536 76114 ## 4 Alaska 252536 76114 ## 5 Alaska 252536 76114 ## 6 Alaska 252536 76114 ## 7 Alaska 252536 76114 ## 8 Alaska 252536 76114 ## 9 Alaska 252536 76114

3

## 10 Alaska 252536 76114 ## Unemployment.rate..Estimate..Population.16.years.and.over ## 1 7.7 ## 2 7.7 ## 3 7.7 ## 4 7.7 ## 5 7.7 ## 6 7.7 ## 7 7.7 ## 8 7.7 ## 9 7.7 ## 10 7.7 ## Percent_below_poverty population Males Females Region Division X ## 1 10.2 738565 386319 352246 West Pacific NA ## 2 10.2 738565 386319 352246 West Pacific NA ## 3 10.2 738565 386319 352246 West Pacific NA ## 4 10.2 738565 386319 352246 West Pacific NA ## 5 10.2 738565 386319 352246 West Pacific NA ## 6 10.2 738565 386319 352246 West Pacific NA ## 7 10.2 738565 386319 352246 West Pacific NA ## 8 10.2 738565 386319 352246 West Pacific NA ## 9 10.2 738565 386319 352246 West Pacific NA ## 10 10.2 738565 386319 352246 West Pacific NA

3. Analysis

3.1 Find the distribution of number of loans by state, regions and divisions. Describe in your own words the geographic differences in the number of loans are. Also, analyze your results by comparing number of loans per capita. Did you notice any missing states in the Lending Club data? If yes, then find out why.

Number of Loans by State, Region and Division

DS <- Final_DataFrame %>% group_by(addr_state,Region,Division) %>% summarise(num_loans= n()) %>% arrange(desc(num_loans))

## ‘summarise()‘ regrouping output by ’addr_state’, ’Region’ (override with ‘.groups‘ argument)

DS

## Warning: ‘...‘ is not empty. ## ## We detected these problematic arguments: ## * ‘needs_dots‘ ## ## These dots only exist to allow future extensions and should be empty. ## Did you misspecify an argument?

4

## # A tibble: 51 x 4 ## # Groups: addr_state, Region [51] ## addr_state Region Division num_loans ## <chr> <chr> <chr> <int> ## 1 CA West Pacific 239837 ## 2 NY Northeast Middle Atlantic 142729 ## 3 TX South West South Central 142519 ## 4 FL South South Atlantic 121500 ## 5 IL Midwest East North Central 69827 ## 6 NJ Northeast Middle Atlantic 63336 ## 7 PA Northeast Middle Atlantic 59289 ## 8 OH Midwest East North Central 57766 ## 9 GA South South Atlantic 56297 ## 10 VA South South Atlantic 48507 ## # ... with 41 more rows

a. California, Texas, New York and Florida are the states in which the highest amount of loans were issued.

b. Interesting enough, all four states have a approximate interest rate of 13% which is at the same level of the average interest rate for all states (13.24%)

c. California, Texas and New York are all above the average annual income (with the exclusion of Florida), this might give possible indication why most loans are issued in these states.

3.1.1 Number of Loans and Loans per Capita

D1 <- Final_DataFrame %>% group_by(addr_state)%>% summarise(num_loans=n(),tot_population=mean(population))%>% mutate(Loans_per_capita=(num_loans/tot_population)*100000) %>% arrange(desc(Loans_per_capita,tot_population))

## ‘summarise()‘ ungrouping output (override with ‘.groups‘ argument)

D1

## Warning: ‘...‘ is not empty. ## ## We detected these problematic arguments: ## * ‘needs_dots‘ ## ## These dots only exist to allow future extensions and should be empty. ## Did you misspecify an argument?

## # A tibble: 51 x 4 ## addr_state num_loans tot_population Loans_per_capita ## <chr> <int> <dbl> <dbl> ## 1 NV 24515 2887725 849. ## 2 CT 27015 3594478 752. ## 3 NY 142729 19798228 721.

5

## 4 RI 7532 1056138 713. ## 5 NJ 63336 8960161 707. ## 6 MD 40828 5996079 681. ## 7 CO 36214 5436519 666. ## 8 NH 8484 1331848 637. ## 9 WY 3699 583200 634. ## 10 DC 4149 672391 617. ## # ... with 41 more rows

a. As you can see from the above table, Nevada has the maximum loans per capita followed by Connecti- cut.

b. The lowest being Iowa which is at 6% loan per capita at 2 loans.

c. The number of loans is indirectly proportional to loans per capita since California which has the maximum number of loans relatively has a lower loan per capita.

3.1.2 Missing States

States_missing <- anti_join(states_regions,Final_DataFrame, by=c(’State’=’addr_state’ )) States_missing

## State State.Code Region Division X ## 1 Alaska AK West Pacific NA ## 2 Alabama AL South East South Central NA ## 3 Arkansas AR South West South Central NA ## 4 Arizona AZ West Mountain NA ## 5 California CA West Pacific NA ## 6 Colorado CO West Mountain NA ## 7 Connecticut CT Northeast New England NA ## 8 District of Columbia DC South South Atlantic NA ## 9 Delaware DE South South Atlantic NA ## 10 Florida FL South South Atlantic NA ## 11 Georgia GA South South Atlantic NA ## 12 Hawaii HI West Pacific NA ## 13 Iowa IA Midwest West North Central NA ## 14 Idaho ID West Mountain NA ## 15 Illinois IL Midwest East North Central NA ## 16 Indiana IN Midwest East North Central NA ## 17 Kansas KS Midwest West North Central NA ## 18 Kentucky KY South East South Central NA ## 19 Louisiana LA South West South Central NA ## 20 Massachusetts MA Northeast New England NA ## 21 Maryland MD South South Atlantic NA ## 22 Maine ME Northeast New England NA ## 23 Michigan MI Midwest East North Central NA ## 24 Minnesota MN Midwest West North Central NA ## 25 Missouri MO Midwest West North Central NA ## 26 Mississippi MS South East South Central NA ## 27 Montana MT West Mountain NA ## 28 North Carolina NC South South Atlantic NA ## 29 North Dakota ND Midwest West North Central NA

6

## 30 Nebraska NE Midwest West North Central NA ## 31 New Hampshire NH Northeast New England NA ## 32 New Jersey NJ Northeast Middle Atlantic NA ## 33 New Mexico NM West Mountain NA ## 34 Nevada NV West Mountain NA ## 35 New York NY Northeast Middle Atlantic NA ## 36 Ohio OH Midwest East North Central NA ## 37 Oklahoma OK South West South Central NA ## 38 Oregon OR West Pacific NA ## 39 Pennsylvania PA Northeast Middle Atlantic NA ## 40 Rhode Island RI Northeast New England NA ## 41 South Carolina SC South South Atlantic NA ## 42 South Dakota SD Midwest West North Central NA ## 43 Tennessee TN South East South Central NA ## 44 Texas TX South West South Central NA ## 45 Utah UT West Mountain NA ## 46 Virginia VA South South Atlantic NA ## 47 Vermont VT Northeast New England NA ## 48 Washington WA West Pacific NA ## 49 Wisconsin WI Midwest East North Central NA ## 50 West Virginia WV South South Atlantic NA ## 51 Wyoming WY West Mountain NA

Puerto Rico is the only missing state in the Lending Club data since this state did not offer any personal loans.

Final_DataFrame%>% filter(addr_state==’PR’)

## [1] addr_state ## [2] loan_amnt ## [3] term ## [4] int_rate ## [5] grade ## [6] sub_grade ## [7] emp_title ## [8] emp_length ## [9] home_ownership ## [10] annual_inc ## [11] issue_d ## [12] loan_status ## [13] purpose ## [14] title ## [15] dti ## [16] delinq_2yrs ## [17] inq_last_6mths ## [18] mths_since_last_delinq ## [19] open_acc ## [20] total_pymnt ## [21] total_rec_int ## [22] last_pymnt_d ## [23] last_pymnt_amnt ## [24] last_credit_pull_d

7

## [25] application_type ## [26] tot_cur_bal ## [27] acc_open_past_24mths ## [28] pub_rec_bankruptcies ## [29] Orig..Index ## [30] issue_Month ## [31] issue_Year ## [32] Geography ## [33] Num_Households ## [34] Median_income_Households ## [35] Unemployment.rate..Estimate..Population.16.years.and.over ## [36] Percent_below_poverty ## [37] population ## [38] Males ## [39] Females ## [40] Region ## [41] Division ## [42] X ## <0 rows> (or 0-length row.names)

3.2 Compare the average amount of loans granted by all states and divisions. Which states and divisions have the highest and lowest average loan amounts?

Compare_Loan <- Final_DataFrame %>% group_by(addr_state, Division) %>% summarise(Avg_Amt_Loans=mean(loan_amnt)) %>% arrange(desc(Avg_Amt_Loans))

## ‘summarise()‘ regrouping output by ’addr_state’ (override with ‘.groups‘ argument)

Compare_Loan

## Warning: ‘...‘ is not empty. ## ## We detected these problematic arguments: ## * ‘needs_dots‘ ## ## These dots only exist to allow future extensions and should be empty. ## Did you misspecify an argument?

## # A tibble: 51 x 3 ## # Groups: addr_state [51] ## addr_state Division Avg_Amt_Loans ## <chr> <chr> <dbl> ## 1 AK Pacific 17056. ## 2 VA South Atlantic 15952. ## 3 DC South Atlantic 15730. ## 4 MD South Atlantic 15689. ## 5 NJ Middle Atlantic 15684. ## 6 MA New England 15673.

8

## 7 HI Pacific 15586. ## 8 TX West South Central 15569. ## 9 WY Mountain 15466. ## 10 IL East North Central 15315. ## # ... with 41 more rows

Alaska/Pacific has the highest average loan amounts followed by Virginia,DC, Maryland and New Jersey whereas Iowa has the lowest average loan amounts in the country which is located in the West North Central division.

Ascend <- Final_DataFrame %>% group_by(addr_state, Division) %>% summarise(Avg_Amt_Loans=mean(loan_amnt)) %>% arrange(Avg_Amt_Loans)

## ‘summarise()‘ regrouping output by ’addr_state’ (override with ‘.groups‘ argument)

Ascend

## Warning: ‘...‘ is not empty. ## ## We detected these problematic arguments: ## * ‘needs_dots‘ ## ## These dots only exist to allow future extensions and should be empty. ## Did you misspecify an argument?

## # A tibble: 51 x 3 ## # Groups: addr_state [51] ## addr_state Division Avg_Amt_Loans ## <chr> <chr> <dbl> ## 1 IA West North Central 6962. ## 2 MT Mountain 13696. ## 3 VT New England 13705. ## 4 NE West North Central 13864. ## 5 AR West South Central 13901. ## 6 OR Pacific 13917. ## 7 SD West North Central 13946. ## 8 ID Mountain 14065. ## 9 RI New England 14121. ## 10 FL South Atlantic 14139. ## # ... with 41 more rows

Iowa has the lowest average loan amounts in the country which is located in the West North Central division.

Ascend <- Final_DataFrame %>% group_by(Division) %>% summarise(Avg_Amt_Loans=mean(loan_amnt)) %>% arrange(Avg_Amt_Loans)

## ‘summarise()‘ ungrouping output (override with ‘.groups‘ argument)

9

Ascend

## Warning: ‘...‘ is not empty. ## ## We detected these problematic arguments: ## * ‘needs_dots‘ ## ## These dots only exist to allow future extensions and should be empty. ## Did you misspecify an argument?

## # A tibble: 9 x 2 ## Division Avg_Amt_Loans ## <chr> <dbl> ## 1 West North Central 14418. ## 2 East South Central 14459. ## 3 Mountain 14534. ## 4 East North Central 14567. ## 5 South Atlantic 14867. ## 6 Middle Atlantic 14903. ## 7 Pacific 15039. ## 8 New England 15169. ## 9 West South Central 15309.

As per the above table, West North Central Division has the lowest average of loan amounts as compared to 8 other divisions.

3.3 Compare the average interest rate charged and average loan amount by the loan Grade. Do you notice any patterns?

Create a new variable from the dataset and convert into a datatype

Final_DataFrame <- Final_DataFrame %>% mutate(int_rate =as.numeric(sub("%","",Final_DataFrame$int_rate))/100)

Compare_IntLoan <- Final_DataFrame %>% group_by(grade) %>% summarise(Avg_int=mean(int_rate), Avg_loan=mean(loan_amnt)) %>% arrange(grade)

## ‘summarise()‘ ungrouping output (override with ‘.groups‘ argument)

Compare_IntLoan

## Warning: ‘...‘ is not empty. ## ## We detected these problematic arguments: ## * ‘needs_dots‘ ## ## These dots only exist to allow future extensions and should be empty. ## Did you misspecify an argument?

10

## # A tibble: 7 x 3 ## grade Avg_int Avg_loan ## <chr> <dbl> <dbl> ## 1 A 0.0708 14265. ## 2 B 0.106 13688. ## 3 C 0.140 14828. ## 4 D 0.178 15752. ## 5 E 0.214 17895. ## 6 F 0.253 19202. ## 7 G 0.283 20577.

Highest loan grade has the lowest average loan amount and the lowest average interest rate. That being said, a person who has a good credit score and falls under grade A can get a loan at the lowest interest rate.

3.4 Run a frequency distribution of number of loans, average loan amount and average interest rate for each state by year (2012 through 2017). Describe the changing patterns in those numbers.

D2 <- Final_DataFrame %>% group_by(issue_Year) %>% summarise(num_loans=n(),Avg_loan=mean(loan_amnt),Avg_int=mean(int_rate)) %>% arrange(issue_Year)

## ‘summarise()‘ ungrouping output (override with ‘.groups‘ argument)

D2

## Warning: ‘...‘ is not empty. ## ## We detected these problematic arguments: ## * ‘needs_dots‘ ## ## These dots only exist to allow future extensions and should be empty. ## Did you misspecify an argument?

## # A tibble: 6 x 4 ## issue_Year num_loans Avg_loan Avg_int ## <int> <int> <dbl> <dbl> ## 1 2012 53367 13462. 0.136 ## 2 2013 134814 14707. 0.145 ## 3 2014 235629 14870. 0.138 ## 4 2015 421095 15240. 0.126 ## 5 2016 434407 14734. 0.130 ## 6 2017 443579 14845. 0.132

a. There is an increase in the number of loans from the year 2012 through 2017.

b. However, the average loan amount increases from 2012 through 2015 and suddenly dips down to an average similar to the year 2013-14.

c. That being said, the average interest rate was dropped in the year 2015 and was constant through the year 2016 and 2017.

11

3.5 Is there a relationship with the population size of a state and the average loan amount given? Is there a relationship between Grade of loans and median income level in a state?

D5 <- Final_DataFrame %>% group_by(addr_state)%>% summarise(num_loans=n(),tot_population=mean(population))%>% arrange(desc(tot_population))

## ‘summarise()‘ ungrouping output (override with ‘.groups‘ argument)

D5

## Warning: ‘...‘ is not empty. ## ## We detected these problematic arguments: ## * ‘needs_dots‘ ## ## These dots only exist to allow future extensions and should be empty. ## Did you misspecify an argument?

## # A tibble: 51 x 3 ## addr_state num_loans tot_population ## <chr> <int> <dbl> ## 1 CA 239837 38982847 ## 2 TX 142519 27419612 ## 3 FL 121500 20278447 ## 4 NY 142729 19798228 ## 5 IL 69827 12854526 ## 6 PA 59289 12790505 ## 7 OH 57766 11609756 ## 8 GA 56297 10201635 ## 9 NC 48269 10052564 ## 10 MI 45379 9925568 ## # ... with 41 more rows

a. Population size of a state is directly proportional to the average loan amount.

b. For instance, California has the largest population and the highest average loan amount.

c. Since the population is more in the states like California, Texas, Florida and New York due to their sizes, the average loan amounts are also high.

D5_2 <- Final_DataFrame %>% group_by(grade)%>% summarise(median_income=mean(Median_income_Households))%>% arrange(desc(median_income))

## ‘summarise()‘ ungrouping output (override with ‘.groups‘ argument)

12

D5_2

## Warning: ‘...‘ is not empty. ## ## We detected these problematic arguments: ## * ‘needs_dots‘ ## ## These dots only exist to allow future extensions and should be empty. ## Did you misspecify an argument?

## # A tibble: 7 x 2 ## grade median_income ## <chr> <dbl> ## 1 A 60223. ## 2 B 59943. ## 3 G 59851. ## 4 F 59839. ## 5 C 59780. ## 6 D 59706. ## 7 E 59664.

a. Median income is the amount that divides the income distribution into two equal groups, half having income above that amount, and half having income below that amount.

b. The highest median income household receives the highest loan grade level which means that the average interest rate on these loans are relatively lower than the median income in grade E.

3.6 This is an open-ended question where you are asked to share an interesting fact that you found through data analysis.

D6 <- Final_DataFrame %>% group_by(term)%>% summarise(avg_loan_amount=mean(loan_amnt))%>% arrange(desc(avg_loan_amount))

## ‘summarise()‘ ungrouping output (override with ‘.groups‘ argument)

D6

## Warning: ‘...‘ is not empty. ## ## We detected these problematic arguments: ## * ‘needs_dots‘ ## ## These dots only exist to allow future extensions and should be empty. ## Did you misspecify an argument?

## # A tibble: 2 x 2 ## term avg_loan_amount ## <chr> <dbl> ## 1 " 60 months" 20499. ## 2 " 36 months" 12628.

13

I have found the average loan amount per term length as an interesting fact in the lending club data. For an average loan amount of 12628, a person can obtain a personal loan for a 36 month term at a lower average interest if the median income of the household is high. I believe the interest rate is similar in both the instances, however for a 60 months term the median income of the household is low in comparison to a 36 months term loan.

4. Visualization

4.1 Create a plot of interest rates and Grade or a loan and describe the pattern.

Final_DataFrame%>% group_by(grade)%>% summarise(Avg_Loan_Amt=mean(loan_amnt))%>% ggplot(aes(x=grade, y=Avg_Loan_Amt)) + geom_bar(stat="identity",fill = "#FF6666") +

xlab("Grade") + ylab("Average Loan Amount") + ggtitle("plot of interest rates and Grade or a loan")

## ‘summarise()‘ ungrouping output (override with ‘.groups‘ argument)

0

5000

10000

15000

20000

A B C D E F G Grade

A ve

ra ge

L oa

n A

m ou

nt

plot of interest rates and Grade or a loan

14

4.2 Create a map of US states and color code the map with the average amount of loans given.

plot_usmap(regions = "counties") + labs(title = "US Counties", subtitle = "This is a blank map of the counties of the United States.") + theme(panel.background = element_rect(color = "black", fill = "lightblue"))

## Warning: Use of ‘map_df$x‘ is discouraged. Use ‘x‘ instead.

## Warning: Use of ‘map_df$y‘ is discouraged. Use ‘y‘ instead.

## Warning: Use of ‘map_df$group‘ is discouraged. Use ‘group‘ instead.

This is a blank map of the counties of the United States.

US Counties

plot_usmap()

## Warning: Use of ‘map_df$x‘ is discouraged. Use ‘x‘ instead.

## Warning: Use of ‘map_df$y‘ is discouraged. Use ‘y‘ instead.

## Warning: Use of ‘map_df$group‘ is discouraged. Use ‘group‘ instead.

15

plot_usmap(data = statepop, values = "pop_2015", color = "red") + scale_fill_continuous( low = "white", high = "green", name = "Average Amount of Loans", label = scales::comma ) + theme(legend.position = "right")

## Warning: Use of ‘map_df$x‘ is discouraged. Use ‘x‘ instead.

## Warning: Use of ‘map_df$y‘ is discouraged. Use ‘y‘ instead.

## Warning: Use of ‘map_df$group‘ is discouraged. Use ‘group‘ instead.

16

10,000,000

20,000,000

30,000,000

Average Amount of Loans

4.3 Show visually the relationship between the annual income of the recipient and the loan amount obtained from Lending Club.

plot=ggplot(data = Final_DataFrame) + geom_point(mapping = aes(x = annual_inc, y = loan_amnt))

Since the size of the dataset is too large, the variables couldn’t be plotted on the barchart.

4.4 Create a plot that shows the relationship between the length of employment and amount of loan obtained.

Final_DataFrame %>% group_by(emp_length)%>% summarise(Avg_Loan_Amt=mean(loan_amnt))%>% ggplot(aes(x=emp_length, y=Avg_Loan_Amt)) + geom_bar(stat="identity")+

xlab("Emloyee Length") + ylab("Average Loan Amount") + ggtitle("plot that shows the relationship between the length of employment and amount of loan obtained")

## ‘summarise()‘ ungrouping output (override with ‘.groups‘ argument)

17

0

5000

10000

15000

< 1 year 1 year 10+ years2 years 3 years 4 years 5 years 6 years 7 years 8 years 9 years Emloyee Length

A ve

ra ge

L oa

n A

m ou

nt plot that shows the relationship between the length of employment and amount of loan obtained

4.5 Create a “regional” map and show an interesting relationship of your liking.

plot_usmap(include = c("CA", "ID", "NV", "OR", "WA")) + labs(title = "Western US States", subtitle = "These are the states in the Pacific Timezone.")

## Warning: Use of ‘map_df$x‘ is discouraged. Use ‘x‘ instead.

## Warning: Use of ‘map_df$y‘ is discouraged. Use ‘y‘ instead.

## Warning: Use of ‘map_df$group‘ is discouraged. Use ‘group‘ instead.

18

These are the states in the Pacific Timezone.

Western US States

plot_usmap( data = statepop, values = "pop_2015", include = c("AL", "AR", "IL", "IA", "LA", "MN", "MS", "MO", "OK", "WI"), color = "red" ) + scale_fill_continuous( low = "white", high = "green", name = "Population (2012)", label = scales::comma ) + labs(title = "South US States", subtitle = "These are the US states in the Central Time Zone.") + theme(legend.position = "right")

## Warning: Use of ‘map_df$x‘ is discouraged. Use ‘x‘ instead.

## Warning: Use of ‘map_df$y‘ is discouraged. Use ‘y‘ instead.

## Warning: Use of ‘map_df$group‘ is discouraged. Use ‘group‘ instead.

19

5,000,000

7,500,000

10,000,000

12,500,000

Population (2012)

These are the US states in the Central Time Zone.

South US States

plot_usmap( data = statepop, values = "pop_2015", include = c("KS", "NE", "ND", "SD", "TX"), color = "red" ) + scale_fill_continuous( low = "white", high = "green", name = "Population (2012)", label = scales::comma ) + labs(title = "West North Central US States", subtitle = "These are the five states split between the Central Time Zone and the Mountain Time Zone.") + theme(legend.position = "right")

## Warning: Use of ‘map_df$x‘ is discouraged. Use ‘x‘ instead.

## Warning: Use of ‘map_df$y‘ is discouraged. Use ‘y‘ instead.

## Warning: Use of ‘map_df$group‘ is discouraged. Use ‘group‘ instead.

20

10,000,000

20,000,000

Population (2012)

These are the five states split between the Central Time Zone and the Mountain Time Zone.

West North Central US States

plot_usmap( data = statepop, values = "pop_2015", include = c("FL", "IN", "KY", "MI", "TN"), color = "red" ) + scale_fill_continuous( low = "white", high = "green", name = "Population (2012)", label = scales::comma ) + labs(title = "East North Central US States", subtitle = "These are the Five states split between the Central Time Zone and the Eastern Time Zone.") + theme(legend.position = "right")

## Warning: Use of ‘map_df$x‘ is discouraged. Use ‘x‘ instead.

## Warning: Use of ‘map_df$y‘ is discouraged. Use ‘y‘ instead.

## Warning: Use of ‘map_df$group‘ is discouraged. Use ‘group‘ instead.

21

5,000,000

10,000,000

15,000,000

20,000,000 Population (2012)

These are the Five states split between the Central Time Zone and the Eastern Time Zone.

East North Central US States

22

  • 1. Install required Packages
  • 2. Merging and Cleaning
    • 2.1 Read all the data files and create dataframes
    • 2.1 Stack and Merge dataframes
    • 2.2 Reading the data from States and States Regions CSV files
    • 2.3 Merging the data of States and States Regions using state name
    • 2.4 Merging all the data into one file using state code as a primary key
    • 2.5 Number of Columns in the Data after merging and cleaning
    • 2.6 First 10 lines of the data from Final Dataframe
  • 3. Analysis
    • 3.1 Find the distribution of number of loans by state, regions and divisions. Describe in your own words the geographic differences in the number of loans are. Also, analyze your results by comparing number of loans per capita. Did you notice any missing states in the Lending Club data? If yes, then find out why.
      • 3.1.1 Number of Loans and Loans per Capita
      • 3.1.2 Missing States
    • 3.2 Compare the average amount of loans granted by all states and divisions. Which states and divisions have the highest and lowest average loan amounts?
    • 3.3 Compare the average interest rate charged and average loan amount by the loan Grade. Do you notice any patterns?
    • 3.4 Run a frequency distribution of number of loans, average loan amount and average interest rate for each state by year (2012 through 2017). Describe the changing patterns in those numbers.
    • 3.5 Is there a relationship with the population size of a state and the average loan amount given? Is there a relationship between Grade of loans and median income level in a state?
    • 3.6 This is an open-ended question where you are asked to share an interesting fact that you found through data analysis.
  • 4. Visualization
    • 4.1 Create a plot of interest rates and Grade or a loan and describe the pattern.
    • 4.2 Create a map of US states and color code the map with the average amount of loans given.
    • 4.3 Show visually the relationship between the annual income of the recipient and the loan amount obtained from Lending Club.
    • 4.4 Create a plot that shows the relationship between the length of employment and amount of loan obtained.
    • 4.5 Create a ``regional'' map and show an interesting relationship of your liking.