Book Image

Advanced Machine Learning with R

By : Cory Lesmeister, Dr. Sunil Kumar Chinnamgari
Book Image

Advanced Machine Learning with R

By: Cory Lesmeister, Dr. Sunil Kumar Chinnamgari

Overview of this book

R is one of the most popular languages when it comes to exploring the mathematical side of machine learning and easily performing computational statistics. This Learning Path shows you how to leverage the R ecosystem to build efficient machine learning applications that carry out intelligent tasks within your organization. You’ll work through realistic projects such as building powerful machine learning models with ensembles to predict employee attrition. Next, you’ll explore different clustering techniques to segment customers using wholesale data and even apply TensorFlow and Keras-R for performing advanced computations. Each chapter will help you implement advanced machine learning algorithms using real-world examples. You’ll also be introduced to reinforcement learning along with its use cases and models. Finally, this Learning Path will provide you with a glimpse into how some of these black box models can be diagnosed and understood. By the end of this Learning Path, you’ll be equipped with the skills you need to deploy machine learning techniques in your own projects.
Table of Contents (30 chapters)
Title Page
Copyright and Credits
About Packt
Contributors
Preface
Index

Creating a new package


Before getting started, you will need to load two packages:

> install.packages("roxygen2")

> install.packages("devtools")

You now want to open File in RStudio and select New Project, which will put you at this point:

Select a new directory as desired, and specify R Package, as shown in the following screenshot:

You will now name your package – I've innovatively called this one package – and select Create Project:

Go to your Files tab in RStudio and you should see several files populated like this:

Notice the folder called R. That is where we will put the R functions for our package. But first, click on Description and fill it out accordingly, and save it. Here is my version, which will be a function to code all missing values in a dataframe to zero:

I've left imports and suggests blank. This is where you would load other packages, such as tidyverse or caret. Now, open up the hello.R function in the R folder, and delete all of it. The following format will work nicely:

  • Title: Your package title of course
  • Description: A brief description
  • Param: The parameters for that function; the arguments
  • Return: The values returned
  • Examples: Provide any examples of how to use the function
  • Export: Here, write the function you desire

Here is the function for our purposes, which just turns all NAs to zero:

#' @title package
#'
#' @description Turns NAs in a dataframe into zeroes
#'
#' @param dataframe
#'
#' @return dataframe
#'
#' @examples
#' dataset <- matrix(sample(c(NA, 1:5), 25, replace = TRUE), 5)
#' df <- as.data.frame(dataset)
#' package::na2zero(df)
#'
#' @export

na2zero <- function(dataframe)
{
 dataframe[is.na(dataframe)] <- 0
 return(dataframe)
}

You will now go to Build - Configure Build Tools and you should end up here:

Click the checkmark for Generate documentation with Roxygen. Doing so will create this popup, which you can close and hit OK. You probably want to rename your function now from hello.R to something relevant. Now comes the moment of truth to build your package. Do this by clicking Build - Clean and Rebuild.

Now you can search for your package, and it should appear:

Click on it and go through the documentation:

There you have it, a useless package, but think of what you can do by packaging your own or your favorite functions, and anyone who inherits your code will thank you.