\section{Introduction}\label{r_quickstart_r_quickstart_intro}
This page describes how you can quickly get started using mlpack from R and gives a few examples of usage, and pointers to deeper documentation.

This quickstart guide is also available for \doxyref{Python}{p.}{python_quickstart}, \doxyref{the command-\/line}{p.}{cli_quickstart}, \doxyref{Julia}{p.}{julia_quickstart} and \doxyref{Go}{p.}{go_quickstart}.\section{Installing mlpack binary package}\label{r_quickstart_r_quickstart_install}
Installing the mlpack bindings for R is straightforward; you can just use C\+R\+AN\+:


\begin{DoxyCode}
install.packages('mlpack')
\end{DoxyCode}
\section{Installing mlpack package from source}\label{r_quickstart_r_quickstart_source_install}
Building the R bindings from scratch is a little more in-\/depth, though. For information on that, follow the instructions on the \doxyref{Building mlpack From Source}{p.}{build} page, and be sure to specify {\ttfamily -\/\+D\+B\+U\+I\+L\+D\+\_\+\+R\+\_\+\+B\+I\+N\+D\+I\+N\+GS=ON} to C\+Make; you may need to also set the location of the R program with {\ttfamily -\/\+D\+R\+\_\+\+E\+X\+E\+C\+U\+T\+A\+B\+LE=/path/to/R}.\section{Simple mlpack quickstart example}\label{r_quickstart_r_quickstart_example}
As a really simple example of how to use mlpack from R, let\textquotesingle{}s do some simple classification on a subset of the standard machine learning {\ttfamily covertype} dataset. We\textquotesingle{}ll first split the dataset into a training set and a testing set, then we\textquotesingle{}ll train an mlpack random forest on the training data, and finally we\textquotesingle{}ll print the accuracy of the random forest on the test dataset.

You can copy-\/paste this code directly into R to run it.


\begin{DoxyCode}
if(!requireNamespace("data.table", quietly = TRUE)) \{ install.packages("data.table") \}
suppressMessages(\{
    library("mlpack")
    library("data.table")
\})

# Load the dataset from an online URL.  Replace with 'covertype.csv.gz' if you
# want to use on the full dataset.
df <- fread("https://www.mlpack.org/datasets/covertype-small.csv.gz")

# Split the labels.
labels <- df[, .(label)]
dataset <- df[, label:=NULL]

# Split the dataset using mlpack.
prepdata <- preprocess\_split(input = dataset,
                             input\_labels = labels,
                             test\_ratio = 0.3,
                             verbose = TRUE)

# Train a random forest.
output <- random\_forest(training = prepdata$training,
                        labels = prepdata$training\_labels,
                        print\_training\_accuracy = TRUE,
                        num\_trees = 10,
                        minimum\_leaf\_size = 3,
                        verbose = TRUE)
rf\_model <- output$output\_model

# Predict the labels of the test points.
output <- random\_forest(input\_model = rf\_model,
                        test = prepdata$test,
                        verbose = TRUE)

# Now print the accuracy.  The third return value ('probabilities'), which we
# ignored here, could also be used to generate an ROC curve.
correct <- sum(output$predictions == prepdata$test\_labels)
cat(correct, "out of", length(prepdata$test\_labels), "test points correct",
    correct / length(prepdata$test\_labels) * 100.0, "%\(\backslash\)n")
\end{DoxyCode}


We can see that we achieve reasonably good accuracy on the test dataset (80\%+); if we use the full {\ttfamily covertype.\+csv.\+gz}, the accuracy should increase significantly (but training will take longer).

It\textquotesingle{}s easy to modify the code above to do more complex things, or to use different mlpack learners, or to interface with other machine learning toolkits.\section{What else does mlpack implement?}\label{r_quickstart_r_quickstart_whatelse}
The example above has only shown a little bit of the functionality of mlpack. Lots of other commands are available with different functionality. A full list of each of these commands and full documentation can be found on the following page\+:


\begin{DoxyItemize}
\item {\tt r documentation}
\end{DoxyItemize}

For more information on what mlpack does, see {\tt https\+://www.\+mlpack.\+org/}. Next, let\textquotesingle{}s go through another example for providing movie recommendations with mlpack.\section{Using mlpack for movie recommendations}\label{r_quickstart_r_quickstart_movierecs}
In this example, we\textquotesingle{}ll train a collaborative filtering model using mlpack\textquotesingle{}s {\ttfamily {\tt cf()}} method. We\textquotesingle{}ll train this on the Movie\+Lens dataset from {\tt https\+://grouplens.\+org/datasets/movielens/,} and then we\textquotesingle{}ll use the model that we train to give recommendations.

You can copy-\/paste this code directly into R to run it.


\begin{DoxyCode}
if(!requireNamespace("data.table", quietly = TRUE)) \{ install.packages("data.table") \}
suppressMessages(\{
    library("mlpack")
    library("data.table")
\})

# First, load the MovieLens dataset.  This is taken from files.grouplens.org/
# but reposted on mlpack.org as unpacked and slightly preprocessed data.
ratings <- fread("http://www.mlpack.org/datasets/ml-20m/ratings-only.csv.gz")
movies <- fread("http://www.mlpack.org/datasets/ml-20m/movies.csv.gz")

# Hold out 10% of the dataset into a test set so we can evaluate performance.
predata <- preprocess\_split(input = ratings,
                            test\_ratio = 0.1,
                            verbose = TRUE)

# Train the model.  Change the rank to increase/decrease the complexity of the
# model.
output <- cf(training = predata$training,
             test = predata$test,
             rank = 10,
             verbose = TRUE,
             max\_iteration=2,
             algorithm = "RegSVD")
cf\_model <- output$output\_model

# Now query the 5 top movies for user 1.
output <- cf(input\_model = cf\_model,
             query = matrix(1),
             recommendations = 10,
             verbose = TRUE)

# Get the names of the movies for user 1.
cat("Recommendations for user 1:\(\backslash\)n")
for (i in 1:10) \{
  cat("  ", i, ":", as.character(movies[output$output[i], 3]), "\(\backslash\)n")
\}
\end{DoxyCode}


Here is some example output, showing that user 1 seems to have good taste in movies\+:


\begin{DoxyCode}
Recommendations for user 1:
  0: Casablanca (1942)
  1: Pan's Labyrinth (Laberinto del fauno, El) (2006)
  2: Godfather, The (1972)
  3: Answer This! (2010)
  4: Life Is Beautiful (La Vita è bella) (1997)
  5: Adventures of Tintin, The (2011)
  6: Dark Knight, The (2008)
  7: Out for Justice (1991)
  8: Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1964)
  9: Schindler's List (1993)
\end{DoxyCode}
\section{Next steps with mlpack}\label{r_quickstart_r_quickstart_nextsteps}
After working through this overview to {\ttfamily mlpack}\textquotesingle{}s R package, we hope you are inspired to use {\ttfamily mlpack}\textquotesingle{} in your data science workflow. We recommend as part of your next steps to look at more documentation for the R mlpack bindings\+:


\begin{DoxyItemize}
\item {\tt R mlpack binding documentation}
\end{DoxyItemize}

Also, mlpack is much more flexible from C++ and allows much greater functionality. So, more complicated tasks are possible if you are willing to write C++ (or perhaps Rcpp). To get started learning about mlpack in C++, the following resources might be helpful\+:


\begin{DoxyItemize}
\item {\tt mlpack C++ tutorials}
\item {\tt mlpack build and installation guide}
\item {\tt Simple sample C++ mlpack programs}
\item {\tt mlpack Doxygen documentation homepage} 
\end{DoxyItemize}