#MS4215 Assignment
#Cathal Kennedy
#20244347
library(readxl)
wages <- read_excel("~/Downloads/Wages1.xlsx")
summary(wages)
## Annual_Earnings No_Of_Weeks Gender Education
## Min. : 709 Min. : 5.00 Min. :1.000 Min. :0.0000
## 1st Qu.: 24497 1st Qu.:52.00 1st Qu.:1.000 1st Qu.:0.0000
## Median : 37124 Median :52.00 Median :2.000 Median :0.0000
## Mean : 48301 Mean :49.12 Mean :1.567 Mean :0.1722
## 3rd Qu.: 55833 3rd Qu.:52.00 3rd Qu.:2.000 3rd Qu.:0.0000
## Max. :1045049 Max. :53.00 Max. :2.000 Max. :1.0000
## NA's :440
## Time_paid_employ WeeklyHours age service
## Min. : 0.0 Min. : 1.00 Min. :13.00 Min. :1.000
## 1st Qu.: 9.0 1st Qu.:34.42 1st Qu.:30.00 1st Qu.:2.000
## Median :15.0 Median :37.45 Median :38.00 Median :3.000
## Mean :17.5 Mean :34.38 Mean :39.32 Mean :2.665
## 3rd Qu.:25.0 3rd Qu.:39.00 3rd Qu.:47.00 3rd Qu.:3.000
## Max. :60.0 Max. :59.80 Max. :79.00 Max. :4.000
##
## JobCategory Sector
## Min. :1.000 Min. :1.0
## 1st Qu.:2.000 1st Qu.:1.0
## Median :3.000 Median :5.0
## Mean :2.674 Mean :3.6
## 3rd Qu.:4.000 3rd Qu.:5.0
## Max. :4.000 Max. :7.0
##
#summary statistics
attach(wages)
mean(Annual_Earnings)
## [1] 48301.13
hist(No_Of_Weeks,xlab=("Number of weeks worked"))

mean(No_Of_Weeks)
## [1] 49.1175
sd(No_Of_Weeks)
## [1] 8.423918
wages$Gender.f <- factor(wages$Gender,
levels =c(1,2),
labels = c("male","female"))
table(wages$Gender.f)
##
## male female
## 346 454
wages$EducationLevel.f <- factor(wages$Education,
levels =c(0,1),
labels = c("Low","High"))
table(wages$EducationLevel.f)
##
## Low High
## 298 62
mean(Time_paid_employ)
## [1] 17.5
hist(Time_paid_employ,xlab=("Total number of years of employment"))

mean(WeeklyHours)
## [1] 34.38137
sd(WeeklyHours)
## [1] 9.333031
mean(age)
## [1] 39.3175
median(age)
## [1] 38
wages$Service.f <- factor(wages$service,
levels =c(1,2,3,4),
labels = c("under 2 years", "2 to 5 years","6 to 10 years",
"10+ years"))
table(wages$Service.f)
##
## under 2 years 2 to 5 years 6 to 10 years 10+ years
## 81 268 289 162
wages$JobCategory.f <- factor(wages$JobCategory,
levels =c(1,2,3,4),
labels = c(" management", " professional"," assistant professional",
"clerical"))
table(wages$JobCategory.f)
##
## management professional assistant professional
## 177 216 98
## clerical
## 309
wages$Sector.f <- factor(wages$Sector,
levels =c(1,2,5,7),
labels = c("Industry", " Construction & Transport"," Finance",
"Health & Education"))
table(wages$Sector.f)
##
## Industry Construction & Transport Finance
## 207 180 289
## Health & Education
## 124
#Regression Model
reg1 <- lm(Annual_Earnings ~No_Of_Weeks + wages$Gender.f
+ wages$EducationLevel.f+ WeeklyHours+ age + wages$Service.f +
wages$JobCategory.f+wages$Sector.f,data = wages)
summary(reg1)
##
## Call:
## lm(formula = Annual_Earnings ~ No_Of_Weeks + wages$Gender.f +
## wages$EducationLevel.f + WeeklyHours + age + wages$Service.f +
## wages$JobCategory.f + wages$Sector.f, data = wages)
##
## Residuals:
## Min 1Q Median 3Q Max
## -101894 -23843 -5315 13345 914911
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -14230.2 26731.3 -0.532 0.594833
## No_Of_Weeks 379.6 503.9 0.753 0.451727
## wages$Gender.ffemale -10697.0 7192.6 -1.487 0.137869
## wages$EducationLevel.fHigh -25641.6 10622.7 -2.414 0.016305
## WeeklyHours 1283.2 409.0 3.137 0.001853
## age 657.4 350.8 1.874 0.061746
## wages$Service.f2 to 5 years 4456.9 12880.6 0.346 0.729538
## wages$Service.f6 to 10 years 21755.5 13586.3 1.601 0.110231
## wages$Service.f10+ years 18128.4 16397.2 1.106 0.269678
## wages$JobCategory.f professional -19669.4 9016.5 -2.182 0.029821
## wages$JobCategory.f assistant professional -39037.0 11609.7 -3.362 0.000859
## wages$JobCategory.fclerical -38165.1 10775.9 -3.542 0.000452
## wages$Sector.f Construction & Transport -10814.9 10137.1 -1.067 0.286780
## wages$Sector.f Finance 20032.8 8258.9 2.426 0.015796
## wages$Sector.fHealth & Education -6429.7 11361.3 -0.566 0.571811
##
## (Intercept)
## No_Of_Weeks
## wages$Gender.ffemale
## wages$EducationLevel.fHigh *
## WeeklyHours **
## age .
## wages$Service.f2 to 5 years
## wages$Service.f6 to 10 years
## wages$Service.f10+ years
## wages$JobCategory.f professional *
## wages$JobCategory.f assistant professional ***
## wages$JobCategory.fclerical ***
## wages$Sector.f Construction & Transport
## wages$Sector.f Finance *
## wages$Sector.fHealth & Education
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 61730 on 345 degrees of freedom
## (440 observations deleted due to missingness)
## Multiple R-squared: 0.2398, Adjusted R-squared: 0.2089
## F-statistic: 7.773 on 14 and 345 DF, p-value: 2.54e-14
library(car)
## Loading required package: carData
vif(reg1)#all vif values are <5 , indicating there is no issues with multicollinearity
## GVIF Df GVIF^(1/(2*Df))
## No_Of_Weeks 1.643650 1 1.282049
## wages$Gender.f 1.221495 1 1.105213
## wages$EducationLevel.f 1.519755 1 1.232783
## WeeklyHours 1.283469 1 1.132903
## age 1.641251 1 1.281113
## wages$Service.f 2.403839 3 1.157402
## wages$JobCategory.f 1.569542 3 1.078025
## wages$Sector.f 1.412771 3 1.059283
z_res <- rstandard(reg1)
fit <- fitted (reg1)
hist(z_res)

plot(fit,z_res)

#problem with heteroscedascity, funneling effect, log transformation required.
wages$lnAnnual_Earnings <- log(wages$Annual_Earnings)
reg2 <- lm(lnAnnual_Earnings ~No_Of_Weeks + wages$Service.f
+ wages$EducationLevel.f+ WeeklyHours+ age + +
wages$JobCategory.f+wages$Sector.f+ wages$Gender.f,data = wages)
summary(reg2)
##
## Call:
## lm(formula = lnAnnual_Earnings ~ No_Of_Weeks + wages$Service.f +
## wages$EducationLevel.f + WeeklyHours + age + +wages$JobCategory.f +
## wages$Sector.f + wages$Gender.f, data = wages)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.01321 -0.29640 0.01671 0.32005 2.16725
##
## Coefficients:
## Estimate Std. Error t value
## (Intercept) 7.798983 0.225952 34.516
## No_Of_Weeks 0.029621 0.004259 6.955
## wages$Service.f2 to 5 years -0.010214 0.108876 -0.094
## wages$Service.f6 to 10 years 0.159579 0.114841 1.390
## wages$Service.f10+ years 0.225478 0.138601 1.627
## wages$EducationLevel.fHigh -0.460167 0.089790 -5.125
## WeeklyHours 0.039620 0.003458 11.459
## age 0.007932 0.002965 2.675
## wages$JobCategory.f professional -0.157934 0.076214 -2.072
## wages$JobCategory.f assistant professional -0.450830 0.098134 -4.594
## wages$JobCategory.fclerical -0.590405 0.091086 -6.482
## wages$Sector.f Construction & Transport -0.173733 0.085686 -2.028
## wages$Sector.f Finance 0.192301 0.069811 2.755
## wages$Sector.fHealth & Education -0.212748 0.096034 -2.215
## wages$Gender.ffemale -0.092362 0.060797 -1.519
## Pr(>|t|)
## (Intercept) < 2e-16 ***
## No_Of_Weeks 1.78e-11 ***
## wages$Service.f2 to 5 years 0.92531
## wages$Service.f6 to 10 years 0.16556
## wages$Service.f10+ years 0.10469
## wages$EducationLevel.fHigh 4.97e-07 ***
## WeeklyHours < 2e-16 ***
## age 0.00783 **
## wages$JobCategory.f professional 0.03898 *
## wages$JobCategory.f assistant professional 6.10e-06 ***
## wages$JobCategory.fclerical 3.13e-10 ***
## wages$Sector.f Construction & Transport 0.04337 *
## wages$Sector.f Finance 0.00619 **
## wages$Sector.fHealth & Education 0.02739 *
## wages$Gender.ffemale 0.12963
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5218 on 345 degrees of freedom
## (440 observations deleted due to missingness)
## Multiple R-squared: 0.6514, Adjusted R-squared: 0.6373
## F-statistic: 46.05 on 14 and 345 DF, p-value: < 2.2e-16
z_res2 <- rstandard(reg2)
fit2 <- fitted (reg2)
hist(z_res2)

plot(fit2,z_res2)

anova(reg2) # this shows gender is insignificant,can be removed
## Analysis of Variance Table
##
## Response: lnAnnual_Earnings
## Df Sum Sq Mean Sq F value Pr(>F)
## No_Of_Weeks 1 58.681 58.681 215.528 < 2.2e-16 ***
## wages$Service.f 3 4.907 1.636 6.008 0.0005333 ***
## wages$EducationLevel.f 1 39.502 39.502 145.087 < 2.2e-16 ***
## WeeklyHours 1 43.991 43.991 161.574 < 2.2e-16 ***
## age 1 3.546 3.546 13.025 0.0003529 ***
## wages$JobCategory.f 3 14.260 4.753 17.458 1.417e-10 ***
## wages$Sector.f 3 10.022 3.341 12.269 1.204e-07 ***
## wages$Gender.f 1 0.628 0.628 2.308 0.1296290
## Residuals 345 93.931 0.272
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
reg3 <- lm(lnAnnual_Earnings ~No_Of_Weeks +
+ wages$EducationLevel.f+ WeeklyHours+ age +
wages$JobCategory.f+wages$Sector.f+ wages$Service.f,data = wages)
summary(reg3)
##
## Call:
## lm(formula = lnAnnual_Earnings ~ No_Of_Weeks + +wages$EducationLevel.f +
## WeeklyHours + age + wages$JobCategory.f + wages$Sector.f +
## wages$Service.f, data = wages)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.03576 -0.31486 0.02099 0.31584 2.19078
##
## Coefficients:
## Estimate Std. Error t value
## (Intercept) 7.743265 0.223377 34.665
## No_Of_Weeks 0.029688 0.004267 6.957
## wages$EducationLevel.fHigh -0.463124 0.089939 -5.149
## WeeklyHours 0.040159 0.003446 11.655
## age 0.007952 0.002971 2.677
## wages$JobCategory.f professional -0.164915 0.076219 -2.164
## wages$JobCategory.f assistant professional -0.459496 0.098153 -4.681
## wages$JobCategory.fclerical -0.614441 0.089871 -6.837
## wages$Sector.f Construction & Transport -0.191889 0.085009 -2.257
## wages$Sector.f Finance 0.191292 0.069939 2.735
## wages$Sector.fHealth & Education -0.250979 0.092854 -2.703
## wages$Service.f2 to 5 years -0.009777 0.109081 -0.090
## wages$Service.f6 to 10 years 0.166034 0.114980 1.444
## wages$Service.f10+ years 0.243172 0.138371 1.757
## Pr(>|t|)
## (Intercept) < 2e-16 ***
## No_Of_Weeks 1.74e-11 ***
## wages$EducationLevel.fHigh 4.40e-07 ***
## WeeklyHours < 2e-16 ***
## age 0.00779 **
## wages$JobCategory.f professional 0.03117 *
## wages$JobCategory.f assistant professional 4.10e-06 ***
## wages$JobCategory.fclerical 3.67e-11 ***
## wages$Sector.f Construction & Transport 0.02461 *
## wages$Sector.f Finance 0.00656 **
## wages$Sector.fHealth & Education 0.00721 **
## wages$Service.f2 to 5 years 0.92863
## wages$Service.f6 to 10 years 0.14964
## wages$Service.f10+ years 0.07974 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5228 on 346 degrees of freedom
## (440 observations deleted due to missingness)
## Multiple R-squared: 0.6491, Adjusted R-squared: 0.6359
## F-statistic: 49.23 on 13 and 346 DF, p-value: < 2.2e-16
vif(reg3)
## GVIF Df GVIF^(1/(2*Df))
## No_Of_Weeks 1.643475 1 1.281981
## wages$EducationLevel.f 1.519041 1 1.232494
## WeeklyHours 1.269952 1 1.126922
## age 1.641218 1 1.281100
## wages$JobCategory.f 1.519845 3 1.072259
## wages$Sector.f 1.285711 3 1.042775
## wages$Service.f 2.373959 3 1.154992
z_res3 <- rstandard(reg3)
fit3 <- fitted (reg3)
hist(z_res3)

plot(fit3,z_res3)

anova(reg3) # this shows that service variable is just about statistically significant , ie p<0.05
## Analysis of Variance Table
##
## Response: lnAnnual_Earnings
## Df Sum Sq Mean Sq F value Pr(>F)
## No_Of_Weeks 1 58.681 58.681 214.7161 < 2.2e-16 ***
## wages$EducationLevel.f 1 31.446 31.446 115.0642 < 2.2e-16 ***
## WeeklyHours 1 42.314 42.314 154.8301 < 2.2e-16 ***
## age 1 11.418 11.418 41.7798 3.476e-10 ***
## wages$JobCategory.f 3 17.734 5.911 21.6298 7.309e-13 ***
## wages$Sector.f 3 10.906 3.635 13.3021 3.083e-08 ***
## wages$Service.f 3 2.409 0.803 2.9383 0.0333 *
## Residuals 346 94.560 0.273
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
exp(reg3$coefficients) #management is highest paying role , with professional showing a decrease in 15%,
## (Intercept)
## 2305.9891820
## No_Of_Weeks
## 1.0301331
## wages$EducationLevel.fHigh
## 0.6293147
## WeeklyHours
## 1.0409762
## age
## 1.0079835
## wages$JobCategory.f professional
## 0.8479661
## wages$JobCategory.f assistant professional
## 0.6316017
## wages$JobCategory.fclerical
## 0.5409433
## wages$Sector.f Construction & Transport
## 0.8253987
## wages$Sector.f Finance
## 1.2108133
## wages$Sector.fHealth & Education
## 0.7780386
## wages$Service.f2 to 5 years
## 0.9902702
## wages$Service.f6 to 10 years
## 1.1806134
## wages$Service.f10+ years
## 1.2752886
#assistant professional showing a decrease of 37% and clerical with a decrease of 45%
#Question 2
credit <- read_excel("~/Downloads/Credit Default.xlsx")
summary(credit)
## ID Default Age DebtRatio
## Min. : 43 Min. :0.0000 Min. :22.00 Min. :0.0000
## 1st Qu.: 8390 1st Qu.:0.0000 1st Qu.:39.75 1st Qu.:0.1285
## Median : 16504 Median :0.0000 Median :49.00 Median :0.3044
## Mean : 35720 Mean :0.3767 Mean :49.76 Mean :0.3651
## 3rd Qu.: 53810 3rd Qu.:1.0000 3rd Qu.:59.00 3rd Qu.:0.5033
## Max. :149656 Max. :1.0000 Max. :98.00 Max. :7.3485
## YearlyIncome LatePayment
## Min. : 18.00 Min. :0.0000
## 1st Qu.: 39.60 1st Qu.:0.0000
## Median : 59.20 Median :0.0000
## Mean : 62.89 Mean :0.2656
## 3rd Qu.: 84.00 3rd Qu.:1.0000
## Max. :126.00 Max. :1.0000
attach(credit)
credit$LatePayment.f <- factor(credit$LatePayment,
levels =c(0,1),
labels = c("no","yes"))
logreg <- glm(Default ~ Age + DebtRatio
+ YearlyIncome + credit$LatePayment.f,family=binomial,data = credit)
summary(logreg) #p-value for DebtRatio >0.05 , non-significant
##
## Call:
## glm(formula = Default ~ Age + DebtRatio + YearlyIncome + credit$LatePayment.f,
## family = binomial, data = credit)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.0050 -0.8167 -0.6054 0.8801 2.0716
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.881047 0.328716 2.680 0.00736 **
## Age -0.030126 0.005882 -5.122 3.02e-07 ***
## DebtRatio 0.194799 0.191594 1.017 0.30928
## YearlyIncome -0.008376 0.002829 -2.961 0.00307 **
## credit$LatePayment.fyes 1.859930 0.171527 10.843 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1192.3 on 899 degrees of freedom
## Residual deviance: 1006.3 on 895 degrees of freedom
## AIC: 1016.3
##
## Number of Fisher Scoring iterations: 4
logreg1 <- glm(Default ~ Age
+ YearlyIncome + credit$LatePayment.f,family=binomial,data = credit)
summary(logreg1)
##
## Call:
## glm(formula = Default ~ Age + YearlyIncome + credit$LatePayment.f,
## family = binomial, data = credit)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.0312 -0.8143 -0.6082 0.8863 2.0333
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.959787 0.320475 2.995 0.00275 **
## Age -0.029895 0.005864 -5.098 3.43e-07 ***
## YearlyIncome -0.008693 0.002808 -3.096 0.00196 **
## credit$LatePayment.fyes 1.871240 0.171258 10.926 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1192.3 on 899 degrees of freedom
## Residual deviance: 1007.3 on 896 degrees of freedom
## AIC: 1015.3
##
## Number of Fisher Scoring iterations: 4
#95% Confidence Interval.
round(cbind(exp(logreg1$coefficients),exp(confint(logreg1))),2)
## Waiting for profiling to be done...
## 2.5 % 97.5 %
## (Intercept) 2.61 1.40 4.91
## Age 0.97 0.96 0.98
## YearlyIncome 0.99 0.99 1.00
## credit$LatePayment.fyes 6.50 4.66 9.13
#age,yearly income has no effect, LatePayment significant