Book Image

Learning Predictive Analytics with R

By : Eric Mayor
Book Image

Learning Predictive Analytics with R

By: Eric Mayor

Overview of this book

This book is packed with easy-to-follow guidelines that explain the workings of the many key data mining tools of R, which are used to discover knowledge from your data. You will learn how to perform key predictive analytics tasks using R, such as train and test predictive models for classification and regression tasks, score new data sets and so on. All chapters will guide you in acquiring the skills in a practical way. Most chapters also include a theoretical introduction that will sharpen your understanding of the subject matter and invite you to go further. The book familiarizes you with the most common data mining tools of R, such as k-means, hierarchical regression, linear regression, association rules, principal component analysis, multilevel modeling, k-NN, Naïve Bayes, decision trees, and text mining. It also provides a description of visualization techniques using the basic visualization tools of R as well as lattice for visualizing patterns in data organized in groups. This book is invaluable for anyone fascinated by the data mining opportunities offered by GNU R and its packages.
Table of Contents (23 chapters)
Learning Predictive Analytics with R
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Exercises and Solutions
Index

Working with Naïve Bayes in R


For this example of working with Naïve Bayes in R, we are going to use the Titanic dataset. The classification problem we have is to know whether or not individuals died in the Titanic accident. We will create a training dataset and a testing dataset (in order to test how well the classifier performs).

The first thing we need to know is how to convert the Titanic dataset (of class table) to a data frame:

1  Titanic.df_weighted = data.frame(Titanic)

Let's have a look at the dataset:

 

Class

Sex

Age

Survived

Freq

1

1st

Male

Child

No

0

2

2nd

Male

Child

No

0

3

3rd

Male

Child

No

35

4

Crew

Male

Child

No

0

5

1st

Female

Child

No

0

6

2nd

Female

Child

No

0

7

3rd

Female

Child

No

17

8

Crew

Female

Child

No

0

9

1st

Male

Adult

No

118

10

2nd

Male

Adult

No

154

11

3rd

Male

Adult

No

...