###1.

M= read.csv("Mortgage.csv", header = TRUE, sep = ",")
data1=data.frame(M)
print(head(data1))
##   X       rate age school networth interest points maturities years married
## 1 1 adjustable  38     22    7.558    13.62   2.33        1.5     1      no
## 2 2 adjustable  38     22    7.558    13.62   2.33        1.5     1      no
## 3 3 adjustable  38     22    7.558    13.62   2.33        1.5     1      no
## 4 4 adjustable  38     22    7.556    13.62   2.33        1.5     1      no
## 5 5 adjustable  41     16    7.821    14.00   1.75        1.0     4      no
## 6 6 adjustable  41     16    8.014    14.00   1.75        1.0     4      no
##   first selfemp tdiff margin coborrower liability liquid
## 1   yes      no  1.38   1.50         no      3.69   8.91
## 2   yes      no  1.38   1.50         no      3.69   8.91
## 3   yes      no  1.38   1.50         no      3.69   8.91
## 4   yes      no  1.38   1.50         no      3.89   8.91
## 5   yes     yes  1.38   5.50         no     50.93  12.50
## 6   yes     yes  1.38   4.75         no     50.44  17.74
summary(data1)
##        X             rate                age            school     
##  Min.   : 1.00   Length:78          Min.   :24.00   Min.   : 6.00  
##  1st Qu.:20.25   Class :character   1st Qu.:31.00   1st Qu.:14.00  
##  Median :39.50   Mode  :character   Median :36.50   Median :16.00  
##  Mean   :39.50                      Mean   :36.04   Mean   :16.45  
##  3rd Qu.:58.75                      3rd Qu.:40.50   3rd Qu.:18.00  
##  Max.   :78.00                      Max.   :65.00   Max.   :25.00  
##     networth          interest         points        maturities   
##  Min.   :-0.0560   Min.   :11.76   Min.   :0.000   Min.   :0.420  
##  1st Qu.: 0.3585   1st Qu.:12.50   1st Qu.:1.000   1st Qu.:0.930  
##  Median : 0.7945   Median :13.50   Median :1.660   Median :1.000  
##  Mean   : 3.5040   Mean   :13.25   Mean   :1.498   Mean   :1.058  
##  3rd Qu.: 7.5575   3rd Qu.:13.75   3rd Qu.:2.022   3rd Qu.:1.000  
##  Max.   :17.8600   Max.   :14.50   Max.   :4.340   Max.   :2.380  
##      years          married             first             selfemp         
##  Min.   : 1.000   Length:78          Length:78          Length:78         
##  1st Qu.: 1.000   Class :character   Class :character   Class :character  
##  Median : 2.000   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : 4.205                                                           
##  3rd Qu.: 4.000                                                           
##  Max.   :28.000                                                           
##      tdiff           margin        coborrower          liability    
##  Min.   :1.380   Min.   :-0.900   Length:78          Min.   : 0.00  
##  1st Qu.:1.450   1st Qu.: 1.500   Class :character   1st Qu.: 3.69  
##  Median :1.590   Median : 2.400   Mode  :character   Median : 8.44  
##  Mean   :1.606   Mean   : 2.292                      Mean   :13.42  
##  3rd Qu.:1.640   3rd Qu.: 3.040                      3rd Qu.:19.43  
##  Max.   :2.040   Max.   : 5.500                      Max.   :86.35  
##      liquid       
##  Min.   : 0.0000  
##  1st Qu.: 0.6675  
##  Median : 1.3550  
##  Mean   : 5.6827  
##  3rd Qu.: 8.9100  
##  Max.   :93.4900
##2.

data2=data1[1:20,]
data2$idx=data2$interest-data2$margin
data3=data.frame(data2$rate, data2$age, data2$years, data2$networth, data2$idx)
head(data3)
##   data2.rate data2.age data2.years data2.networth data2.idx
## 1 adjustable        38           1          7.558     12.12
## 2 adjustable        38           1          7.558     12.12
## 3 adjustable        38           1          7.558     12.12
## 4 adjustable        38           1          7.556     12.12
## 5 adjustable        41           4          7.821      8.50
## 6 adjustable        41           4          8.014      9.25
names(data3)=c("R","A","Y","N","I")
head(data3)
##            R  A Y     N     I
## 1 adjustable 38 1 7.558 12.12
## 2 adjustable 38 1 7.558 12.12
## 3 adjustable 38 1 7.558 12.12
## 4 adjustable 38 1 7.556 12.12
## 5 adjustable 41 4 7.821  8.50
## 6 adjustable 41 4 8.014  9.25
summary(data3)
##       R                   A              Y              N         
##  Length:20          Min.   :34.0   Min.   : 1.0   Min.   : 2.419  
##  Class :character   1st Qu.:38.0   1st Qu.: 1.0   1st Qu.: 7.558  
##  Mode  :character   Median :38.0   Median : 1.5   Median : 7.558  
##                     Mean   :40.6   Mean   : 4.3   Mean   : 8.355  
##                     3rd Qu.:42.5   3rd Qu.: 4.0   3rd Qu.: 8.014  
##                     Max.   :57.0   Max.   :28.0   Max.   :17.860  
##        I        
##  Min.   : 8.50  
##  1st Qu.:11.10  
##  Median :11.42  
##  Mean   :11.29  
##  3rd Qu.:12.12  
##  Max.   :13.65
###3

hist(data3$A, main= "age")

hist(data3$I, main="indexhist")

boxplot(data3$Y, col="red", main= "mortgage years plot")

boxplot(data3$N, col="blue", main= "networth plot")

plot(x=data3$A, y=data3$N, main= "networth by age")

library(ggplot2)

sp <- ggplot(data3, aes(x=Y, y=I)) + geom_line()
sp

###5

data5=read.csv("https://raw.githubusercontent.com/Cnadour/HW3/main/Mortgage.csv")
head(data5)
##   X       rate age school networth interest points maturities years married
## 1 1 adjustable  38     22    7.558    13.62   2.33        1.5     1      no
## 2 2 adjustable  38     22    7.558    13.62   2.33        1.5     1      no
## 3 3 adjustable  38     22    7.558    13.62   2.33        1.5     1      no
## 4 4 adjustable  38     22    7.556    13.62   2.33        1.5     1      no
## 5 5 adjustable  41     16    7.821    14.00   1.75        1.0     4      no
## 6 6 adjustable  41     16    8.014    14.00   1.75        1.0     4      no
##   first selfemp tdiff margin coborrower liability liquid
## 1   yes      no  1.38   1.50         no      3.69   8.91
## 2   yes      no  1.38   1.50         no      3.69   8.91
## 3   yes      no  1.38   1.50         no      3.69   8.91
## 4   yes      no  1.38   1.50         no      3.89   8.91
## 5   yes     yes  1.38   5.50         no     50.93  12.50
## 6   yes     yes  1.38   4.75         no     50.44  17.74