Skip to content

Instantly share code, notes, and snippets.

@kmader
Created October 12, 2016 13:00
Show Gist options
  • Save kmader/3a2802760e6f6253f433c8490d52f35f to your computer and use it in GitHub Desktop.
Save kmader/3a2802760e6f6253f433c8490d52f35f to your computer and use it in GitHub Desktop.
Fitting and Running Simple GLM Allstate Models

Fitting the Models

fit_model<-function(in_df) glm(loss~.,
                data = in_df %>% 
                  select(starts_with("cont"), 
                         one_of(keep_cats),
                         loss), 
                family = Gamma(link = "log"))

lil_model<-train_ds %>%
         mutate(big_loss = loss>quantile(loss,0.75)) %>%
        subset(!big_loss) %>% fit_model

big_model<-train_ds %>%
         mutate(big_loss = loss>quantile(loss,0.75)) %>%
        subset(big_loss) %>% fit_model


which_model<-train_ds %>%
  mutate(big_loss = loss>quantile(loss,0.75)) %>%
  select(starts_with("cont"), one_of(keep_cats), big_loss) %>%
  (function(in_df) glm(big_loss ~ ., data = in_df, family = "binomial"))

Applying the models

pred_results<-read_input_data("/Users/mader/Downloads/test.csv")
pred_results<-pred_results %>%
  mutate(pred_loss_lil = predict.glm(lil_model, pred_results, type = "response")) %>%
  mutate(pred_loss_big = predict.glm(big_model, pred_results, type = "response")) %>%
  mutate(pred_isbig = predict.glm(which_model, pred_results, type = "response")) %>%
  mutate(comb_pred = pred_loss_lil+(pred_loss_big-pred_loss_lil)*pred_isbig)
 
 pred_results %>% 
  mutate(loss = comb_pred) %>%
  select(id, loss) %>% 
  write.csv(file = "comb_prediction.csv", row.names = F, quote=F)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment