# Tidy Random Forest with Ranger

I noticed that most of the R code submitted to Kaggle is written in an old style of coding in R. 
This is fine, but I wanted to reimplement some scripts in 'tidy' manner using the tidyverse.

This script is a reimplementation of Ben Hamners [random forest benchmark](https://www.kaggle.com/benhamner/digit-recognizer/random-forest-benchmark)
script. The randomForest package is very slow and I wanted to train the model on the entire set, 
so I used [ranger](https://github.com/imbs-hl/ranger) instead.

```{r message=FALSE, warning=FALSE}
library(readr)
library(purrr)
library(ranger)

mnist <- read_csv("../input/train.csv")
test <- read_csv("../input/test.csv")

rf_mod <- mnist %>% dplyr::mutate(label = as.factor(label)) %>%
  ranger(label ~ ., data = .)

preds <- predict(rf_mod, data = test)
  
preds %>% `[[`("predictions") %>% 
  tibble::tibble(ImageId = 1:length(.), Label = .) %>% 
  write_csv("rf_predictions.csv")
```
The in-sample model accuracy is `r 1 - rf_mod$prediction.error`. The submission of the test predictions achieves 0.96814.  