• 1 R User Notebook
  • 2 Basic Concepts
    • 2.1 Vectors
    • 2.2 Matrices
    • 2.3 Dataframes
    • 2.4 Tibbles
    • 2.5 Extracting data from Dataframes/Tibbles
    • 2.6 Lists
    • 2.7 Factors
    • 2.8 Dates
    • 2.9 Style Guide
  • 3 Importing & Exporting
    • 3.1 Importing with Base R
    • 3.2 Importing with other packages
      • 3.2.1 Importing a CSV
      • 3.2.2 Importing a TSV
      • 3.2.3 Importing a R data file
      • 3.2.4 Importing a Excel file
      • 3.2.5 Importing a SAS file
      • 3.2.6 Importing JSON (Newline Delimited JSON)
    • 3.3 Exporting
      • 3.3.1 Write to an R dataset
      • 3.3.2 Write to an CSV file
  • 4 Manipulating data
    • 4.1 Basic data wrangling
    • 4.2 Summary statistics
    • 4.3 Conditional statements
    • 4.4 Recode
    • 4.5 Appending columns and rows
    • 4.6 Joins
    • 4.7 Tidy Data
    • 4.8 tibbles
  • 5 Functions
    • 5.1 Function Basics - an example using str()
    • 5.2 Common (base) functions
      • 5.2.1 Strings
      • 5.2.2 Mathematical
      • 5.2.3 Properties & Lookups
      • 5.2.4 Transformation
      • 5.2.5 Other useful functions
    • 5.3 Stringr - more string functions
    • 5.4 Lubridate - more date functions
    • 5.5 Converting data to other formats
      • 5.5.1 JSON
    • 5.6 User Defined Functions
    • 5.7 Chaining/Piping
  • 6 Loops
    • 6.1 Basic for Loop structure
    • 6.2 Looping through a vector
    • 6.3 Creating a List in a Loop
    • 6.4 Looping with apply functions
  • 7 Tables
    • 7.1 Displaying tables - htmlTable
    • 7.2 Displaying Interactive tables - DT
    • 7.3 Creating Tabulations
  • 8 Charts
    • 8.0.1 Static charts with ggplot2
    • 8.0.2 Changing Axes
    • 8.0.3 Chart Themes & Styles
    • 8.0.4 Line Charts (geom_line)
    • 8.0.5 Bar Charts (geom_bar & geom_histogram)
    • 8.0.6 Adding features
    • 8.0.7 Heatmaps
    • 8.0.8 Boxplots
    • 8.0.9 Combination Charts
    • 8.0.10 Scales
    • 8.0.11 ggplot wizard
    • 8.0.12 Interactive charts with ggiraph
    • 8.0.13 Interactive charts with dygraph
  • 9 Maps
    • 9.1 Leaflet
      • 9.1.1 Display a basic map
      • 9.1.2 Add markers, shapes and popups to a map
      • 9.1.3 Add Boundaries
      • 9.1.4 Add Interactivity
      • 9.1.5 Attach extra information to your boundary dataset
      • 9.1.6 Add colour based on area data
    • 9.2 Inserting HTML & Javascript directly into R
  • 10 RMarkdown & Knitr
    • 10.1 Knitr
    • 10.2 YAML
    • 10.3 Modular coding
  • 11 Machine Learning & Predictive Analytics
    • 11.0.1 Decision Trees
  • 12 Text Mining & NLP
    • 12.0.1 Text data
    • 12.0.2 Tidy text
    • 12.0.3 Stemming
    • 12.0.4 Basic text stats
    • 12.0.5 N-Grams analysis
    • 12.0.6 Text Preparation with tm
    • 12.1 Create a Term Document Matrix
      • 12.1.1 From a Corpus (by default words are converted to lowercase less than 3 characters are excluded)
      • 12.1.2 Matrix Managment
    • 12.2 Topic Modelling & Latent Dirichlet Allocation (LDA)
    • 12.3 Clustering - Similarity between topics
    • 12.4 Calculating distance between objects
    • 12.5 Non-Euclidian Distances - Jensen Shannon
    • 12.6 Scaling - Principal Components
    • 12.7 Eigen vectors
    • 12.8 K-Means
    • 12.9 Naive Bayes Classifiers
    • 12.10 TF-IDF Classifiers (Supervised)
    • 12.11 Sentiment Analysis
    • 12.12 Word Bubble
  • 13 Shiny
    • 13.1 Basic Shiny Structure
    • 13.2 User Interface
      • 13.2.1 Layout
      • 13.2.2 Inputs
      • 13.2.3 Outputs
    • 13.3 Server
      • 13.3.1 Linking to UI outputs
      • 13.3.2 Linking to UI inputs
    • 13.4 Shiny Dashboard
  • 14 Bookdown
    • 14.1 Set-up
    • 14.2 Create the book
  • 15 Creating Packages
    • 15.1 Setup Folders
    • 15.2 R folder
    • 15.3 man folder
    • 15.4 Package details
    • 15.5 Package Dependencies
    • 15.6 Build the package
    • 15.7 Making changes
  • 16 Git - Version Control
    • 16.1 Set-up
    • 16.2 Link Git to a GitHub account
    • 16.3 Starting a project with Github
    • 16.4 Hosting a site on Github
  • 17 Other Languages
    • 17.1 SQL
    • 17.2 Python

R User Notebook

11 Machine Learning & Predictive Analytics

11.0.1 Decision Trees

To prodcue decision tress you need to use variuos packages, such as rpart for recursive partioning and caret.

## Create a decision tree
library(rpart)
TreeResults <- rpart::rpart(force~mass + acceleration,
                     data=dataset1,
                     method="class",
                     control = rpart.control(minsplit = 30, cp = 0))
## Visualise the decision tree
rattle::fancyRpartPlot(TreeResults)
## Use model to predict value
predictions <- predict(TreeResults, dataset1, type = "class")
## Evaluate the results
confusionMatrix(predictions, dataset1$force)