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.

Q1 Solution:

  1. Read the “binary.csv” file in R. Treat “rank” as a factor variable and show the first 10 entries of the dataset.
library(boot) #library

# Q1 1)
binary<-read.csv("binary.csv", header=T)
binary$rank=factor(binary$rank)
head(binary, 10)
  1. Conduct a cross-validation on the data set by doing the following steps:
  1. Using your last three digits of your registration number as the random seed, split the data set into training and test set. Your training set should contain 300 randomly selected observations from the data set.
# Q1 2) (i)
set.seed(929)
train.index=sample(400, 300)
binary.train=binary[train.index, ]
binary.test=binary[-train.index, ]
  1. Taking “admit” as the response variable and all other columns as features, fit a logistic regression model on the training set. Comments on which variables are statistically significant associated with admission under 5 percent significance level (0.05) and explain the meaning of coefficients of the significant variables in terms of odds ratio.
# 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.

  1. Define a cost function to compute the classification error rate for the cross-validation pipeline. Use 0.4 as the prediction threshold, i.e. classify as ‘admit’ if the predicted probability is > 0.4. Use the defined cost function to compute the 10-fold cross-validation error of your model on the training set. Report the cross-validation error on training set.
# 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

  1. Using the fitted model in 2), make predictions using 0.4 threshold respectively on the test set and compute and report the test classification error rate. Note that the “admit” columns takes directly values of 0 and 1.
# 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

  1. Using the predictions in 3) and 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 Solutions:

  1. Read the data “Claim.csv” into R and report the number of observations.
# Q2 1)
Claim<-read.csv("Claim.csv", header=T)
dim(Claim)
## [1] 865   3

Your comments: The number of observations is 865

  1. Fit the Poisson regression model to investigate the associations of number of awards with the type of education programs and final scores. Print the summary of your model and identify which covariate(s) is(are) statistically significantly associated with number of accidents under 5 percent significance level.
# 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.

  1. Calculate the expected average number of claims for
  1. BUS drove by female driver;

  2. BUS drove by male driver;

  3. MIBUS drove by female driver;

  4. MIBUS drove by male driver;

  5. TRUCK drove by female driver;

  6. 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

  1. Comment on if the fitted model in part 2) is significantly better than the NULL model under 10 percent significance level, based on the deviance value of the models.
# 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