Please read the following instructions carefully before start answering the questions:
• Please answer ALL TWO questions and Upload your Rmarkdown and output files to FASER.
• There are 100 marks in total
• Please write your code and comments in the R markdown template (.rmd) and save it as “MA318_registrationnum”. For example, if your registration number is 2000999, then save your file as MA318_2000999.rmd. After you have done all your questions, you should run your knit your R markdown to produce either .html or .doc file, the name should be the same as your rmarkdown file.
library(boot) #library
# Q1 1)
binary<-read.csv("binary.csv", header=T)
binary$rank=factor(binary$rank)
head(binary, 10)
# Q1 2) (i)
set.seed(929)
train.index=sample(400, 300)
binary.train=binary[train.index, ]
binary.test=binary[-train.index, ]
# Q1 2) (ii)
binary.glm=glm(admit~., binary.train, family=binomial(link="logit"))
summary(binary.glm)
##
## Call:
## glm(formula = admit ~ ., family = binomial(link = "logit"), data = binary.train)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -4.141479 1.298503 -3.189 0.00143 **
## gre 0.002979 0.001260 2.364 0.01808 *
## gpa 0.710568 0.371863 1.911 0.05603 .
## rank2 -0.650882 0.358755 -1.814 0.06963 .
## rank3 -1.181887 0.393767 -3.001 0.00269 **
## rank4 -1.393155 0.482756 -2.886 0.00390 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 380.51 on 299 degrees of freedom
## Residual deviance: 349.06 on 294 degrees of freedom
## AIC: 361.06
##
## Number of Fisher Scoring iterations: 4
b.logit=coefficients(binary.glm)
est=exp(b.logit)
print(est)
## (Intercept) gre gpa rank2 rank3 rank4
## 0.01589932 1.00298327 2.03514725 0.52158558 0.30669945 0.24829068
Your comments: gre, rank 3 and rank 4 are statistically significant under 5% significant level. for every unit increase in the gre the student is 1.0030 times more likely to be admitted. comapared to students who attended schools in rank 1 students who attended schools in rank 3 are (1-0.307)=69% less likely to be admitted, and students who attended schools in rank 4 are (1-0.25)=75% less likely to be admitted.
# Q1 2) (iii)
cost.rate.train=function(y,prediction)
{
predict.tf=prediction>0.4
error.rate=mean(y!=predict.tf)
return(error.rate)
}
cv.error=cv.glm(data=binary.train, glmfit=binary.glm, cost=cost.rate.train, K=10)
print(cv.error$delta[1])
## [1] 0.3333333
Your comments: The cross validation error on the training set is 0.333333
# Q1 3)
predict.test=predict(binary.glm, binary.test, type='response')
test.tf=rep('0', length(predict.test))
test.tf[predict.test>0.4]='1'
test.error=mean(binary.test$admit!=test.tf)
print(test.error)
## [1] 0.27
Your comments: The test error is 0.27
table() function,
create the confusion matrices on the test set. Report the number of true
positive and false positive.# Q1 4)
Confusion.matrix=table(binary.test$admit,test.tf)
print(Confusion.matrix)
## test.tf
## 0 1
## 0 62 10
## 1 17 11
Your comments: The true positive is 11 The false positive is 17
# Q2 1)
Claim<-read.csv("Claim.csv", header=T)
dim(Claim)
## [1] 865 3
Your comments: The number of observations is 865
# Q2 2)
Claim.glm=glm(num~., Claim, family=poisson(link="log"))
summary(Claim.glm)
##
## Call:
## glm(formula = num ~ ., family = poisson(link = "log"), data = Claim)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.2655 0.4855 -2.607 0.00914 **
## typeMIBUS -1.5096 0.5209 -2.898 0.00375 **
## typeTRUCK -1.3588 0.4736 -2.869 0.00412 **
## genderM -0.0233 0.2856 -0.082 0.93499
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for poisson family taken to be 1)
##
## Null deviance: 343.93 on 864 degrees of freedom
## Residual deviance: 337.46 on 861 degrees of freedom
## AIC: 464.53
##
## Number of Fisher Scoring iterations: 6
b.pois=coefficients(Claim.glm)
est=exp(b.pois)
est
## (Intercept) typeMIBUS typeTRUCK genderM
## 0.2821089 0.2209886 0.2569713 0.9769709
Your Comments: typeMIBUS, and typeTRUCK are statistically significant under 5% significant level.
BUS drove by female driver;
BUS drove by male driver;
MIBUS drove by female driver;
MIBUS drove by male driver;
TRUCK drove by female driver;
TRUCK drove by male driver;
and on average which combination of type and gender will give the largest expected number of claims and which combination will give the smallest expected number of claims.
# Q2 3)
# i)
BF.pois=exp(b.pois[1])
# ii)
BM.pois=exp(b.pois[1]+b.pois[4])
# iii)
MF.pois=exp(b.pois[1]+b.pois[2])
# iv)
MM.pois=exp(b.pois[1]+b.pois[2]+b.pois[4])
# v)
TF.pois=exp(b.pois[1]+b.pois[3])
# vi)
TM.pois=exp(b.pois[1]+b.pois[3]+b.pois[4])
as.numeric(cbind(BF.pois, BM.pois, MF.pois, MM.pois, TF.pois, TM.pois))
## [1] 0.28210893 0.27561220 0.06234285 0.06090715 0.07249391 0.07082444
Your Comments: on average the BUS and Female gender will give the highest expectation of claims, while the MIBUS and the Male gender will give the lowest expectation of claims
# Q2 4)
#difference of the deviance value
343.93-337.46
## [1] 6.47
# difference of the degree of freedom
864-861
## [1] 3
1-pchisq(6.47, 3)
## [1] 0.09085317
Your Comments: given that 0.0908<0.1 we concluded that the fitted model is significantly better than the NULL model ubder 10% significant level