Mathematics

2. write an R function that takes as input binomial responses and covariates and usesthe Newton-Raphson algorithm to compute the coefficients of a binomial GLM. Youcould use the ‘Kidney stone surgery” data or any other dataset that includes at least2 explanatory variables. For the ‘Kidney stone surgery’ data, some of the steps could(but don’t have to) be done as followsFirst enter the dataintercept, small=0, keyhole=p, successes, failuresr1<-C(1,0,0,234,36)r2<-c(1,0,1,81,6)r3<-c(1,1,0,55,25)r4<-c(1,1,1,192,71)Put all data in a matrixdata<-rbind(r1,r2,r3,r4)Write the functionbglm<-function(data){beta<-matrix(c(x,x,x)) # X’s denote the initial values of your choiceDefine the likelihood, its first and second derivativesNewton-Raphson algorithmFinish by returning the vector of coefficientsreturn(beta);Double check your results by comparing them to results from a statistical software.Note that, more general functions can be written by allowing more inputs, such asinitial values etc. (but this is not necessary for the current exercise).Example – Kidney stone surgery and stone sizeTable 6 is from a study investigating the success probabilities of two surgery types forthe removal of kidney stones.1 Success in surgery was determined by the elimination (orreduction to less than 2mm diameter) of the stones after three months. A total of 700operations were classified according to variables Y = surgery outcome (success, failure),X = surgery type ( keyhole, open), and Z 2 stone size (small, large) which here is thecontrol variable.Table 6: Treatment of kidney stonesStone Surgery Outcome PercentSize Type Success Failure SuccessSmall Keyhole 234 36 86.7%Open 81 6 93.1%Large Keyhole 55 25 68.8%Open 192 71 73.0%Total Keyhole 289 61 82.6%Open 273 77 78.0%Table 6 consists of two 2 X 2 tables which are refer to as partial or conditional tables,