Name:
Andrew ID:

Instructions

Simulation and plotting (18 points)

Iteration (14 points)

Functions and debugging (16 points)

predict.fun = function(mod, x1, x2) {
  prob = predict(mod, newdata=data.frame(x1=x1, x2=x2), type="response")
  if (prob <= 0.5) return(0)
  else return(1)
}

predict.fun(log.mod, -10, -10) # Should return 0
predict.fun(log.mod, 10, 10) # Should return 1
predict.fun(log.mod, c(-10,10), c(-10,10)) # Should return c(0,1)
predict.fun2 = function(mod, x1, x2) {
  link = sum(c(x1,x2) * coef(mod))
  if (link <= 0) return(0)
  else return(1)
}

predict.fun2(log.mod, -10, -10) # Should return 0
predict.fun2(log.mod, 10, 10) # Should return 1
predict.fun2(log.mod, c(-10,10), c(-10,10)) # Should return c(0,1)
plot.predictions = function(mod, x1.lim, x2.lim, col0="black", col1="red") {
  # Define x1 and x2 variables over the grid. Fill in the ?? below, then 
  # uncomment the lines
  #x1.grid = seq(??, ??, len=100)
  #x2.grid = seq(??, ??, len=100)
  x1 = rep(x1.grid, times=100)
  x2 = rep(x2.grid, each=100)
  
  # Call predict.fun() to get the predicted classes. (You can change this
  # to predict.fun2() if you want; just use whichever one you got working)
  pred = predict.fun(mod, x1, x2)
  
  # Define the colors based on the predicted classes. Remember that col0 
  # is for class 0, and col1 is for class 1. Fill in the ??, uncomment
  #cols = ifelse(??, ??, ??)
  
  # Plot the grid points with the right colors. Fill in the ??, uncomment
  #plot(??, ??, col=cols, xlab="X1", ylab="X2", pch=20, cex=0.1)
}

Data cleaning and tidyverse (18 points)

nms = c("Surgery", "Age", "Hosp_ID", "Temp_Rect", "Pulse", "Resp", "Temp_Extr",
        "Pulse_Peri", "Mucous", "Cap_Time", "Pain", "Peris", "Ab_Dist", "Tube",
        "Reflux", "Reflux_PH", "Feces", "Abdomen", "Cell_Vol", "Total_Protein",
        "Ab_Appear", "Ab_Total_Prot", "Outcome", "Lesion", "Lesion_Site", 
        "Lesion_Type", "Lesion_Subt", "Lesion_Code")