# Titanic problem starter file... # Be sure to comment each function using # or " " # remember to grab the titanic data, too: # " Here is an example of multi-line quote-based 'comments' ... pr0(obs) takes in one observation (obs) and outputs whether or not that passenger would have survived by our model... In the case of pr0, the prediction is that every passenger perishes (0) " pr0 <- function(obs) { return(0) } # Here is an example of #-style (single-line) comments # # pr1(obs) is a function that takes in one observation (obs) # and outputs whether or not that person would have # survived by our model... # # In the case of pr1, it predicts that female passengers # survive and pr1 <- function(obs) { if (obs$sex == "female") { return(1) } else { return(0) } } " pr_df(FUN,df) takes in FUN: a function that predicts for one observation, such as pr0 or pr1 df: a dataframe with possible many observations to test pr_df returns a vector of predictions, one per row in the dataframe df Call this function as follows: predictions <- pr_df( pr1, t ) where pr1 is the predictor above (or another) and t is the titanic data (or a subset of it) Then, you can check your accuracy with num_correct <- sum( predictions == t$survived ) cat('Number correct:', num_correct) " pr_df <- function(FUN,df) { NUM_ROWS <- nrow(df) #cat("NUM_ROWS is", NUM_ROWS) predictions = vector(mode="numeric",length=NUM_ROWS) for (row in 1:NUM_ROWS) { next_prediction = FUN(df[row,]) predictions[row] = next_prediction } return(predictions) }