library(MASS)
library(dplyr)
library(openxlsx)
library(stats)
MyData <- read.xlsx("SP_LoanApproval_alum-VF  (1).xlsx")
str(MyData)
## 'data.frame':    200 obs. of  12 variables:
##  $ SUBJECT     : num  1 2 3 4 5 6 7 8 9 10 ...
##  $ PMT_HST     : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ AMT_OWED    : num  1.7 0 32.6 6.6 19.3 0 5.3 22 0 37.7 ...
##  $ SCORE       : num  438 355 771 582 603 338 598 635 342 629 ...
##  $ DEPENDENTS  : num  3 0 4 7 1 2 0 2 3 1 ...
##  $ AGE         : num  28 20 60 57 66 24 59 39 19 26 ...
##  $ EDUCATION   : num  15 15 19 18 19 18 18 20 14 20 ...
##  $ JOB_POSITION: num  3 2 7 5 4 3 4 6 1 6 ...
##  $ INDUSTRY    : num  2 1 3 4 5 7 8 2 3 4 ...
##  $ INCOME      : num  57.2 42.2 106.4 95.5 101.1 ...
##  $ HOME_OWN    : num  1 1 1 0 1 1 1 1 0 0 ...
##  $ BANK_SCORE  : num  5456 7108 4737 3750 3547 ...
head(MyData, n=5) 
##   SUBJECT PMT_HST AMT_OWED SCORE DEPENDENTS AGE EDUCATION JOB_POSITION INDUSTRY
## 1       1       0      1.7   438          3  28        15            3        2
## 2       2       0      0.0   355          0  20        15            2        1
## 3       3       0     32.6   771          4  60        19            7        3
## 4       4       0      6.6   582          7  57        18            5        4
## 5       5       0     19.3   603          1  66        19            4        5
##    INCOME HOME_OWN BANK_SCORE
## 1  57.231        1    5456.02
## 2  42.199        1    7108.08
## 3 106.414        1    4737.18
## 4  95.528        0    3749.66
## 5 101.108        1    3547.16
summary(MyData) 
##     SUBJECT          PMT_HST          AMT_OWED          SCORE      
##  Min.   :  1.00   Min.   :  0.00   Min.   : 0.000   Min.   :312.0  
##  1st Qu.: 50.75   1st Qu.:  0.00   1st Qu.: 9.275   1st Qu.:483.0  
##  Median :100.50   Median :  0.00   Median :23.500   Median :583.0  
##  Mean   :100.50   Mean   : 27.70   Mean   :23.311   Mean   :573.5  
##  3rd Qu.:150.25   3rd Qu.: 27.25   3rd Qu.:36.300   3rd Qu.:669.0  
##  Max.   :200.00   Max.   :238.00   Max.   :49.800   Max.   :850.0  
##    DEPENDENTS         AGE          EDUCATION      JOB_POSITION  
##  Min.   :0.000   Min.   :18.00   Min.   :11.00   Min.   :0.000  
##  1st Qu.:1.000   1st Qu.:29.00   1st Qu.:16.00   1st Qu.:4.000  
##  Median :3.000   Median :42.50   Median :18.00   Median :5.000  
##  Mean   :2.525   Mean   :42.24   Mean   :17.49   Mean   :4.745  
##  3rd Qu.:4.000   3rd Qu.:55.25   3rd Qu.:19.00   3rd Qu.:6.000  
##  Max.   :8.000   Max.   :68.00   Max.   :20.00   Max.   :8.000  
##     INDUSTRY         INCOME          HOME_OWN      BANK_SCORE  
##  Min.   : 0.00   Min.   : 28.30   Min.   :0.00   Min.   :2590  
##  1st Qu.: 3.00   1st Qu.: 83.18   1st Qu.:1.00   1st Qu.:4609  
##  Median : 5.00   Median : 94.81   Median :1.00   Median :5706  
##  Mean   : 5.46   Mean   : 93.74   Mean   :0.81   Mean   :5689  
##  3rd Qu.: 8.00   3rd Qu.:107.39   3rd Qu.:1.00   3rd Qu.:6821  
##  Max.   :10.00   Max.   :135.63   Max.   :1.00   Max.   :8837
names(MyData)
##  [1] "SUBJECT"      "PMT_HST"      "AMT_OWED"     "SCORE"        "DEPENDENTS"  
##  [6] "AGE"          "EDUCATION"    "JOB_POSITION" "INDUSTRY"     "INCOME"      
## [11] "HOME_OWN"     "BANK_SCORE"
Model00 <- lm(BANK_SCORE~., data=MyData)
summary(Model00)
Model01 <- lm(BANK_SCORE~.-(PMT_HST+AMT_OWED+HOME_OWN), data=MyData)
summary(Model01)
## 
## Call:
## lm(formula = BANK_SCORE ~ . - (PMT_HST + AMT_OWED + HOME_OWN), 
##     data = MyData)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1072.01  -517.08   -94.02   453.27  1193.89 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6010.88564  421.87322  14.248  < 2e-16 ***
## SUBJECT        -0.06147    0.71562  -0.086  0.93164    
## SCORE           4.62709    0.36149  12.800  < 2e-16 ***
## DEPENDENTS   -290.81981   23.69051 -12.276  < 2e-16 ***
## AGE          -108.38193   38.93961  -2.783  0.00592 ** 
## EDUCATION     -20.08776   80.37758  -0.250  0.80292    
## JOB_POSITION -160.46074  464.09977  -0.346  0.72991    
## INDUSTRY     -166.09551  367.67961  -0.452  0.65197    
## INCOME         46.54406   77.00631   0.604  0.54628    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 581.2 on 191 degrees of freedom
## Multiple R-squared:  0.8318, Adjusted R-squared:  0.8248 
## F-statistic: 118.1 on 8 and 191 DF,  p-value: < 2.2e-16
plot(MyData$BANK_SCORE,MyData$medv)
abline(Model01)
## Warning in abline(Model01): only using the first two of 9 regression
## coefficients

interpretation:

The linear regression model seems to work well with the data, explaining around 83.47% of the changes in BANK_SCORE. Notably, things like SCORE and DEPENDENTS really matter—they have a big impact on BANK_SCORE. But stuff like AMT_OWED and JOB_POSITION don’t seem to matter much. Overall, the model is solid (p < 2.2e-16), showing that at least one factor significantly affects BANK_SCORE, And the leftover errors and standard error aren’t too bad either, suggesting the model’s predictions are reasonably close to the actual values.

library(MASS)
library(dplyr)
library(openxlsx)
library(stats)
MyData <- read.xlsx("SP_LoanApproval_alum-VF  (1).xlsx")
str(MyData)
## 'data.frame':    200 obs. of  12 variables:
##  $ SUBJECT     : num  1 2 3 4 5 6 7 8 9 10 ...
##  $ PMT_HST     : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ AMT_OWED    : num  1.7 0 32.6 6.6 19.3 0 5.3 22 0 37.7 ...
##  $ SCORE       : num  438 355 771 582 603 338 598 635 342 629 ...
##  $ DEPENDENTS  : num  3 0 4 7 1 2 0 2 3 1 ...
##  $ AGE         : num  28 20 60 57 66 24 59 39 19 26 ...
##  $ EDUCATION   : num  15 15 19 18 19 18 18 20 14 20 ...
##  $ JOB_POSITION: num  3 2 7 5 4 3 4 6 1 6 ...
##  $ INDUSTRY    : num  2 1 3 4 5 7 8 2 3 4 ...
##  $ INCOME      : num  57.2 42.2 106.4 95.5 101.1 ...
##  $ HOME_OWN    : num  1 1 1 0 1 1 1 1 0 0 ...
##  $ BANK_SCORE  : num  5456 7108 4737 3750 3547 ...
head(MyData, n=5) 
##   SUBJECT PMT_HST AMT_OWED SCORE DEPENDENTS AGE EDUCATION JOB_POSITION INDUSTRY
## 1       1       0      1.7   438          3  28        15            3        2
## 2       2       0      0.0   355          0  20        15            2        1
## 3       3       0     32.6   771          4  60        19            7        3
## 4       4       0      6.6   582          7  57        18            5        4
## 5       5       0     19.3   603          1  66        19            4        5
##    INCOME HOME_OWN BANK_SCORE
## 1  57.231        1    5456.02
## 2  42.199        1    7108.08
## 3 106.414        1    4737.18
## 4  95.528        0    3749.66
## 5 101.108        1    3547.16
summary(MyData)
##     SUBJECT          PMT_HST          AMT_OWED          SCORE      
##  Min.   :  1.00   Min.   :  0.00   Min.   : 0.000   Min.   :312.0  
##  1st Qu.: 50.75   1st Qu.:  0.00   1st Qu.: 9.275   1st Qu.:483.0  
##  Median :100.50   Median :  0.00   Median :23.500   Median :583.0  
##  Mean   :100.50   Mean   : 27.70   Mean   :23.311   Mean   :573.5  
##  3rd Qu.:150.25   3rd Qu.: 27.25   3rd Qu.:36.300   3rd Qu.:669.0  
##  Max.   :200.00   Max.   :238.00   Max.   :49.800   Max.   :850.0  
##    DEPENDENTS         AGE          EDUCATION      JOB_POSITION  
##  Min.   :0.000   Min.   :18.00   Min.   :11.00   Min.   :0.000  
##  1st Qu.:1.000   1st Qu.:29.00   1st Qu.:16.00   1st Qu.:4.000  
##  Median :3.000   Median :42.50   Median :18.00   Median :5.000  
##  Mean   :2.525   Mean   :42.24   Mean   :17.49   Mean   :4.745  
##  3rd Qu.:4.000   3rd Qu.:55.25   3rd Qu.:19.00   3rd Qu.:6.000  
##  Max.   :8.000   Max.   :68.00   Max.   :20.00   Max.   :8.000  
##     INDUSTRY         INCOME          HOME_OWN      BANK_SCORE  
##  Min.   : 0.00   Min.   : 28.30   Min.   :0.00   Min.   :2590  
##  1st Qu.: 3.00   1st Qu.: 83.18   1st Qu.:1.00   1st Qu.:4609  
##  Median : 5.00   Median : 94.81   Median :1.00   Median :5706  
##  Mean   : 5.46   Mean   : 93.74   Mean   :0.81   Mean   :5689  
##  3rd Qu.: 8.00   3rd Qu.:107.39   3rd Qu.:1.00   3rd Qu.:6821  
##  Max.   :10.00   Max.   :135.63   Max.   :1.00   Max.   :8837
names(MyData)
##  [1] "SUBJECT"      "PMT_HST"      "AMT_OWED"     "SCORE"        "DEPENDENTS"  
##  [6] "AGE"          "EDUCATION"    "JOB_POSITION" "INDUSTRY"     "INCOME"      
## [11] "HOME_OWN"     "BANK_SCORE"
plot(MyData)

cor(MyData)
##                   SUBJECT      PMT_HST    AMT_OWED        SCORE   DEPENDENTS
## SUBJECT       1.000000000  0.073341609 -0.06372444 -0.062952966 -0.051475819
## PMT_HST       0.073341609  1.000000000  0.02454794 -0.444580249  0.011566269
## AMT_OWED     -0.063724439  0.024547936  1.00000000  0.745319680  0.040137211
## SCORE        -0.062952966 -0.444580249  0.74531968  1.000000000  0.079568509
## DEPENDENTS   -0.051475819  0.011566269  0.04013721  0.079568509  1.000000000
## AGE           0.005187587  0.053172225  0.10899321  0.444152004  0.164523918
## EDUCATION    -0.002983531 -0.004413796  0.12389412  0.173761115  0.149203973
## JOB_POSITION -0.050500267 -0.089369313  0.28810024  0.334696474  0.095507270
## INDUSTRY      0.020995910  0.070680323  0.01633501  0.003850959  0.052813853
## INCOME       -0.009359309  0.022915860  0.20031536  0.334447525  0.149795671
## HOME_OWN     -0.020751266 -0.009431285  0.08653772  0.103476845  0.006779872
## BANK_SCORE   -0.020869765 -0.291458924  0.23478759  0.078513690 -0.460216500
##                       AGE    EDUCATION JOB_POSITION     INDUSTRY       INCOME
## SUBJECT       0.005187587 -0.002983531  -0.05050027  0.020995910 -0.009359309
## PMT_HST       0.053172225 -0.004413796  -0.08936931  0.070680323  0.022915860
## AMT_OWED      0.108993206  0.123894122   0.28810024  0.016335012  0.200315360
## SCORE         0.444152004  0.173761115   0.33469647  0.003850959  0.334447525
## DEPENDENTS    0.164523918  0.149203973   0.09550727  0.052813853  0.149795671
## AGE           1.000000000  0.213102755   0.21098022  0.065783932  0.513086482
## EDUCATION     0.213102755  1.000000000   0.34550386 -0.016523150  0.321322475
## JOB_POSITION  0.210980223  0.345503857   1.00000000  0.065746928  0.633581693
## INDUSTRY      0.065783932 -0.016523150   0.06574693  1.000000000  0.723338564
## INCOME        0.513086482  0.321322475   0.63358169  0.723338564  1.000000000
## HOME_OWN      0.092186022  0.159555467   0.29337623  0.235063615  0.345529821
## BANK_SCORE   -0.694441039 -0.082183006   0.09135801  0.050257392 -0.169468798
##                  HOME_OWN  BANK_SCORE
## SUBJECT      -0.020751266 -0.02086977
## PMT_HST      -0.009431285 -0.29145892
## AMT_OWED      0.086537718  0.23478759
## SCORE         0.103476845  0.07851369
## DEPENDENTS    0.006779872 -0.46021650
## AGE           0.092186022 -0.69444104
## EDUCATION     0.159555467 -0.08218301
## JOB_POSITION  0.293376229  0.09135801
## INDUSTRY      0.235063615  0.05025739
## INCOME        0.345529821 -0.16946880
## HOME_OWN      1.000000000  0.03618320
## BANK_SCORE    0.036183195  1.00000000
Model02 <- lm(BANK_SCORE~PMT_HST+AMT_OWED+HOME_OWN,data=MyData)
summary(Model02)
## 
## Call:
## lm(formula = BANK_SCORE ~ PMT_HST + AMT_OWED + HOME_OWN, data = MyData)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2818.9  -971.4    28.1   976.3  3574.5 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 5354.850    248.427  21.555  < 2e-16 ***
## PMT_HST       -7.694      1.712  -4.496 1.19e-05 ***
## AMT_OWED      21.956      6.047   3.631  0.00036 ***
## HOME_OWN      44.212    234.231   0.189  0.85048    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1295 on 196 degrees of freedom
## Multiple R-squared:  0.1437, Adjusted R-squared:  0.1306 
## F-statistic: 10.96 on 3 and 196 DF,  p-value: 1.095e-06
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
vif(Model02)
##  PMT_HST AMT_OWED HOME_OWN 
## 1.000738 1.008199 1.007681
Model03=lm(BANK_SCORE~., data=MyData)
MyData$inv_BANK_SCORE <- 1/MyData$BANK_SCORE+1
summary(Model03)
## 
## Call:
## lm(formula = BANK_SCORE ~ ., data = MyData)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -985.38 -489.32  -83.12  483.42 1249.67 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  5866.54842  528.49076  11.101  < 2e-16 ***
## SUBJECT        -0.06011    0.71766  -0.084  0.93334    
## PMT_HST        -0.31864    1.53885  -0.207  0.83618    
## AMT_OWED       -6.66954    7.47379  -0.892  0.37332    
## SCORE           5.20283    1.11057   4.685 5.36e-06 ***
## DEPENDENTS   -290.02586   23.71487 -12.230  < 2e-16 ***
## AGE          -114.03081   39.47177  -2.889  0.00432 ** 
## EDUCATION     -26.71794   80.63684  -0.331  0.74076    
## JOB_POSITION -203.78641  465.49974  -0.438  0.66205    
## INDUSTRY     -202.20908  368.82925  -0.548  0.58417    
## INCOME         54.41399   77.22195   0.705  0.48190    
## HOME_OWN       -5.04272  112.95247  -0.045  0.96444    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 580.8 on 188 degrees of freedom
## Multiple R-squared:  0.8347, Adjusted R-squared:  0.825 
## F-statistic:  86.3 on 11 and 188 DF,  p-value: < 2.2e-16

#interpretation: Adjusted R-squared: Both models have an adjusted R-squared of 0.9302, suggesting similar explanatory power, AIC and BIC: Model00 and Model03 have nearly identical AIC values (3762) and similar BIC values (3836 for Model00 and 3837 for Model03), indicating comparable goodness of fit and complexity, Residual Standard Error (RSE): Both models have the same RSE of 366.8, indicating similar average deviations from predicted values.

#In summary, there isn’t a significant difference between the two models in terms of fit. However, Model03’s inclusion of inv_BANK_SCORE doesn’t substantially improve model performance. Thus, if simplicity is preferred without sacrificing predictive power, Model00 is the better choice. However, if there are theoretical reasons or interpretability benefits to include inv_BANK_SCORE, Model03 may be preferred.

#activity 3 #heteroeskedasticity

library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(car)
library(sandwich)


finalModel <- lm(BANK_SCORE ~ ., data = MyData)

plot(fitted(finalModel), resid(finalModel),
     xlab = "Fitted Values",
     ylab = "Residuals",
     main = "Residuals vs. Fitted Values")
abline(h = 0, col = "red")

bptest(finalModel)
## 
##  studentized Breusch-Pagan test
## 
## data:  finalModel
## BP = 39.482, df = 12, p-value = 8.762e-05
coeftest(finalModel, vcov = vcovHC(finalModel, type = "HC1"))
## 
## t test of coefficients:
## 
##                   Estimate  Std. Error  t value  Pr(>|t|)    
## (Intercept)     1.7931e+07  1.5898e+06  11.2784 < 2.2e-16 ***
## SUBJECT        -3.6292e-02  4.8679e-01  -0.0746    0.9406    
## PMT_HST        -3.4318e-01  1.0088e+00  -0.3402    0.7341    
## AMT_OWED        6.2209e+00  4.6145e+00   1.3481    0.1792    
## SCORE           6.4634e-01  7.1982e-01   0.8979    0.3704    
## DEPENDENTS     -1.1226e+02  1.9623e+01  -5.7208 4.147e-08 ***
## AGE            -2.6543e+01  2.8242e+01  -0.9399    0.3485    
## EDUCATION       1.3222e-01  5.4557e+01   0.0024    0.9981    
## JOB_POSITION    3.3444e+01  3.1664e+02   0.1056    0.9160    
## INDUSTRY        1.2996e+01  2.4930e+02   0.0521    0.9585    
## INCOME          3.1434e+00  5.2435e+01   0.0599    0.9523    
## HOME_OWN       -4.2594e+01  7.7150e+01  -0.5521    0.5815    
## inv_BANK_SCORE -1.7921e+07  1.5896e+06 -11.2741 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

#This plot indicates a random scatter of residuals, which suggests there is no obvious pattern of heteroskedasticity, the residuals appear to be spread uniformly across the range of fitted values without forming a specific pattern such as a funnel or a wave, this uniform spread is a good indication that the variances of the errors are constant, and the assumption of homoscedasticity is likely met. #Autocorrelation

acf(resid(finalModel), main="Residuals")

dwtest(finalModel)
## 
##  Durbin-Watson test
## 
## data:  finalModel
## DW = 1.9367, p-value = 0.2969
## alternative hypothesis: true autocorrelation is greater than 0
#install.packages("dynlm")
library(dynlm)
## Warning: package 'dynlm' was built under R version 4.3.3
arModel <- dynlm(BANK_SCORE ~ L(BANK_SCORE, 1) + ., data = MyData)
summary(arModel)
## Warning in summary.lm(arModel): essentially perfect fit: summary may be
## unreliable
## 
## Time series regression with "numeric" data:
## Start = 1, End = 200
## 
## Call:
## dynlm(formula = BANK_SCORE ~ L(BANK_SCORE, 1) + ., data = MyData)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -1.388e-11 -1.253e-13  4.930e-14  2.379e-13  1.531e-12 
## 
## Coefficients:
##                    Estimate Std. Error    t value Pr(>|t|)    
## (Intercept)       9.744e-10  4.911e-09  1.980e-01   0.8429    
## L(BANK_SCORE, 1)  1.000e+00  2.128e-16  4.700e+15   <2e-16 ***
## SUBJECT           2.334e-15  1.319e-15  1.770e+00   0.0784 .  
## PMT_HST           2.141e-15  2.828e-15  7.570e-01   0.4500    
## AMT_OWED         -3.203e-15  1.397e-14 -2.290e-01   0.8190    
## SCORE             1.811e-15  2.191e-15  8.270e-01   0.4095    
## DEPENDENTS        1.377e-14  5.839e-14  2.360e-01   0.8139    
## AGE              -3.658e-14  7.429e-14 -4.920e-01   0.6231    
## EDUCATION        -2.645e-14  1.482e-13 -1.780e-01   0.8586    
## JOB_POSITION     -2.594e-13  8.563e-13 -3.030e-01   0.7623    
## INDUSTRY         -2.163e-13  6.787e-13 -3.190e-01   0.7503    
## INCOME            5.463e-14  1.422e-13  3.840e-01   0.7012    
## HOME_OWN         -2.522e-13  2.078e-13 -1.214e+00   0.2264    
## inv_BANK_SCORE   -9.722e-10  4.909e-09 -1.980e-01   0.8432    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.067e-12 on 186 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 2.591e+31 on 13 and 186 DF,  p-value: < 2.2e-16

the plot of residuals indicates that autocorrelation is not present in the model, the correlations at all lags are within the blue dashed significance bounds, suggesting that the residuals are independent across time, this is a good sign that the model does not violate the assumption of no autocorrelation.