Understand R for Enterprise AI
Learn AI by working with a World Class Advanced AI Computing Lab for free.
Reinvent traditional software through DeepSphere.AI Enterprise AI platform through software engineering from hardcoded instruction-driven
automation to the machine self-learning application for the digital era.
Be Empowered by Our School of Artificial Intelligence and Enabled Through Our Intelligent Automation
Learn Enterprise AI and Enterprise Data Science With R
Acquire Cross-Functional Skills from Business to Data to Technology by Mastering AI
Learning Path

Learn How Our School of AI Teaches Workers to Transfer Business Processes to the Modern Digital World

Choose Your Hands-On Learning Unit
Develop an AI Application Using our Pre-Built R Libraries. Coding is No Longer Necessary
Master Artificial Intelligence with Hundreds of
Predeveloped R Programs
- Unit 1R Fundamentals
- Program #1Hellow World
- Program #2Variable
- Program #3Type Integer
- Program #4Type Floating Point
- Program #5Type String
- Program #6Complex Numbers
- Program #7Function Defining
- Program #8Dynamic Typing
- Program #9Static Typing
- Program #10Boolean Evaluation
- Program #11If Statement
- Program #12If Else Statement
- Program #13Else If Statement
- Program #14Block Structure
- Program #15Whitespaces
- Program #16Regular Expressions
- Program #17Lists
- Program #18Tuple
- Program #19Sets
- Program #20Frozen Sets
- Program #21Collection Transition
- Program #22Loop Else
- Program #23Arguments
- Program #24Mutable Arguments
- Program #25Accepting Variable Arguments
- Unit 2Big Data Processing
- Program #26Unpacking Argument List
- Program #27Scope
- Program #28Error Handling
- Program #29Namespaces
- Program #30File Input Output
- Program #31Higher Order Functions
- Program #32Anonymous Functions
- Program #33Nested Functions
- Program #34Closure
- Program #35Lexical Scoping
- Program #36Operator
- Program #37Decorators
- Unit 3Machine Learning: Part 1
- Program #38List Comprehensions
- Program #39Generator Expressions
- Program #40Generator Functions
- Program #41Itertools Chain
- Program #42Itertools Izip
- Program #43Debugging Tool Print
- Program #44Classes
- Program #45Emulation
- Program #46Class Method
- Program #47Static Method
- Program #48Inheritance
- Program #49Encapsulation
- Program #50N-Dimensional Array
- Unit 4Machine Learning: Part 2
- Program #51Reading Writing Data
- Program #52Reading Data From CSV
- Program #53Normalizing Data
- Program #54Formatting Data
- Program #55Controlling Line Properties Matplotlib
- Program #56Plotting Simple Function
- Program #57Importing Module
- Program #58Creating Module
- Program #59Graphing Matplotlib Using Defaults
- Unit 5Predictive Analytics
- Program #60Graphing Matplotlib Using Defaults Changing Colors
- Program #61Graphing Matplotlib Using Defaults Setting Limits
- Program #62Graphing Matplotlib Using Defaults Setting Ticks
- Program #63Graphing Matplotlib Using Defaults Setting Tick Labels
- Program #64Graphing Matplotlib Using Defaults Moving Spines
- Program #65Graphing Matplotlib Using Defaults Adding Legends
- Program #66Graphing Matplotlib Using Defaults Annotating Points Legends
- Program #67Data Manipulation Using Pandas
- Program #68Reading Data From Hana To Python
- Program #69Reading Writing Data
- Program #70Reading Data From CSV
- Program #71Normalizing Data
- Program #72Formatting Data
- Program #73Controlling Line Properties Matplotlib
- Unit 6Advanced Machine Learning
- Program #74Plotting Simple Function
- Program #75Importing Module
- Program #76Creating Module
- Program #77Graphing Matplotlib Using Defaults
- Program #78Graphing Matplotlib Using Defaults Changing Colors
- Program #79Graphing Matplotlib Using Defaults Setting Limits
- Program #80Isotonic Regression Scikit Learn
- Program #81Neural Networks Scikit Learn
- Program #82Non Linear Svm Scikit Learn
- Program #83Decision Trees Scikit Learn
- Program #84Plotting Validation Curve Scikit Learn
- Unit 7Data Visualization
- Program #85Loading Datasets Scikit Learn
- Program #86Mean Shift Clustering Algorithm Scikit Learn
- Program #87Affinity Propagation Clustering Algorithm Scikit Learn
- Program #88Dbscan Clustering Algorithm Scikit Learn
- Program #89Kmeans Clustering Algorithm Scikit Learn
- Program #90Spectral Bi Clustering Algorithm Scikit Learn
- Program #91Spectral Co Clustering Algorithm Scikit Learn
- Program #92Ridge Regression Scikit Learn
- Program #93Scientific Analysis Arrays Numpy
- Program #94Scientific Analysis Arrays Reshaping Numpy
- Program #95Scientific Analysis Arrays Concatenating Numpy
- Program #96Scientific Analysis Arrays Adding New Dimensions Numpy
- Program #97Scientific Analysis Arrays Initializing With Zeros Ones Numpy
- Program #98Scientific Analysis Mgrid Scipy
- Program #99Scientific Analysis Polynomial Scipy
- Program #100Scientific Analysis Vectorizing Functions Scipy
- Program #101Scientific Analysis Select Function Scipy
- Program #102Scientific Analysis General Integration Scipy
- Program #103Time Series Analysis Pandas
- Program #104Exporting Data Using Pandas
- Program #105Importing Data Using Pandas
- Program #106Data Analysis Pandas
- Program #107Empty Graph Networkx
- Program #108Graph Adding Nodes Networkx
- Program #109Graph Adding Edges Networkx
- Program #110Graph Display Networkx
- Unit 8Use Case Implementation: Part 1
- Unit 9Use Case Implementation: Part 2
- Program #1192D Plotting Mapplotlib
- Program #1202D Plot Scikit Learn
- Program #121Classification Scikit Learn
- Program #122Model Selection Scikit Learn
- Program #123Nearest Neighbours Regression Scikit Learn
- Program #124Graphing Matplotlib Regular Plot
- Program #125Graphing Matplotlib Scatter Plot
- Program #126Graphing Matplotlib Bar Plot
- Program #127Graphing Matplotlib Contour Plot
- Program #128Graphing Matplotlib Imshow
- Unit 10Advanced Data Processing
- Program #129Graphing Matplotlib Pie Chart
- Program #130Graphing Matplotlib Quiver Plot
- Program #131Graphing Matplotlib Grids
- Program #132Graphing Matplotlib Multi Plots
- Program #133Graphing Matplotlib Polar Axis
- Program #134Graphing Matplotlib 3D Plot
- Program #135Graphing Matplotlib Texts
- Program #136Histogram Matplotlib
- Program #137Bed Occupancy Optimization
- Program #138Life Time Value Customer Prediction
- Program #139Customer Upselling Characteristics Prediction
- Program #140Sales Lead Priortization
- Program #141Inventory Demand Forecasting
- Program #142Credit Card Fraud Risk
- Program #143Employee Churn Prediction
- Program #144Patient Medication Complaince Prediction
- Program #145Physician Attrition Prediction
- Program #146Patient Readmittance Rate Prediction
- Program #147Patient Insurance Claim Prediction
- Program #148Drug Demand Forecasting
- Unit 11Advanced Programming
- Program #149Customer Retention Analysis
- Program #150Hospital Bed Turn Analysis
- Program #151Patient Survival Analysis
- Program #152Patient Medication Effectiveness Analysis
- Program #153Sales Growth Analysis
- Program #154Customer Cross Selling Analysis
- Program #155Product Customer Segmentation
- Program #156Employee Talent Mangement
- Program #157Patient Bed Occupancy
- Program #158Product Market Basket Analysis
- Program #159Automobile Claims Handling Analysis
- Program #160Customer Market Share
- Program #161Data Collection From Excel
- Program #162Data Collection From CSV
- Program #163Data Collection From Clipboard
- Program #164Data Collection From HTML
- Program #165Data Collection From XML
- Program #166Data Collection From JSON
- Program #167Data Collection From PDF
- Program #168Data Collection From Plain Text
- Program #169Data Collection From DOCX
- Program #170Data Collection From HDF
- Program #171Data Collection From Image
- Program #172Data Collection From MP3
- Program #173Data Collection From SAP HANA
- Program #174Data Collection From Hadoop
- Program #175Data Integration Concatenate
- Program #176Data Integration Merge
- Program #177Data Integration Join
- Program #178Data Mapping Dictionary Literal Values
- Program #179Data Mapping Dictionary Operations
- Program #180Data Mapping Dictionary Comparision Operations
- Program #181Data Mapping Dictionary Statments
- Program #182Data Provisioning Extraction
- Program #183Data Provisioning Transformation
- Program #184Data Provisioning Loading
- Program #185Iterators
- Program #186Generator Expressions
- Program #187Generators
- Program #188Bidirectional Communication
- Program #189Chaining Generators
- Program #190Decorators
- Program #191Decorators As Functions
- Program #192Decorators As Classes
- Program #193Decorators Copying Docstring Other Attributes
- Program #194Example Standard Library
- Program #195Depriciation Of Functions
- Program #196While Loop Removing Decorator
- Program #197Plugin Registration System
- Program #198Context Managers
- Program #199Context Managers Catching Exceptions
- Program #200Context Managers Defining Using Generators
- Program #201Ndarray
- Program #202Ndarray Block Of Memory
- Unit 12Working with Tensorflow
- Program #203Ndarray Data Type
- Program #204Ndarray Indexing Scheme Strides
- Program #205Ndarray Slicing With Integers
- Program #206Ndarray Transposing With Integers
- Program #207Ndarray Reshaping Integers
- Program #208Ndarray Broadcasting
- Program #209Ndarray Universal Function
- Program #210Ndarray Generalized Universal Function
- Program #211Ndarray Old Buffer Protocol
- Program #212Processing Opening Writing To Image
- Program #213Image Processing Displaying Images
- Program #214Image Processing Displaying Images Basic Manipulation
- Program #215Image Processing Geometrical Transformations
- Program #216Image Processing Filtering Blurring
- Program #217Image Processing Filtering Sharpening
- Program #218Image Processing Filtering Denoising
- Program #219Image Processing Filtering Apply Gaussian Filter
- Program #220Image Processing Filtering Apply Median Filter
- Program #221Image Processing Feature Extraction Edge Detection
- Program #222Image Processing Feature Extraction Segmentation
- Program #223Tensorflow Hello World
- Program #224Tensorflow Tensors
- Program #225Tensorflow Fixed Tensors
- Program #226Tensorflow Sequence Tensors
- Program #227Tensorflow Randon Tensors
- Program #228Tensorflow Constants
- Program #229Tensorflow Variables
- Program #230Tensorflow Placeholders
- Program #231Tensorflow Graphs
- Unit 13Working with NLP
- Program #232Tensorflow Session
- Program #233Tensorflow Feed Dictionary
- Program #234Tensorflow Data Type
- Program #235Tensorflow Add Two Consatnts
- Program #236Tensorflow Multiply Two Consatnts
- Program #237Tensorflow Matrix Inverse Method
- Program #238Tensorflow Queues
- Program #239Tensorflow Saving Variables
- Program #240Tensorflow Restoring Variables
- Program #241Tensorflow Tensorboard
- Program #242Tensorflow Namescope
- Program #243Tensorflow Linear Regression
- Program #244Tensorflow Logistic Regression
- Program #245Tensorflow Random Forest
- Program #246Tensorflow Kmeans Clustering
- Program #247Tensorflow Linear Support Vector Machine
- Program #248Tensorflow Non Linear Support Vector Machine
- Program #249Tensorflow Multi Class Support Vector Machine
- Program #250Tensorflow Nearest Neighbours
- Program #251Tensorflow Neural Networks
- Program #252Tensorflow Convolutional Neural Networks
- Program #253Tensorflow Deep Neural Networks
- Program #254NLP Installing Nltk
- Program #255NLP Count Word Frequency Nltk
- Program #256NLP Remove Stop Words
- Program #257NLP Tokenize Text Nltk
- Program #258NLP Tokenize Non English Text Nltk
- Program #259NLP Get Synonyms Nltk
- Program #260NLP Get Antonyms Nltk
- Program #261NLP Word Stemming Nltk
- Program #262NLP Non English Word Stemming Nltk
- Program #263NLP Lemmatizing Words Nltk
- Program #264NLP Part Of Speech Tagging Nltk
- Program #265NLP Chinking Nltk
- Program #266NLP Chunking Nltk
- Program #267NLP Corpora Nltk
- Program #268NLP Named Entity Recognition Nltk
- Program #269NLP Text Classification Nltk
- Program #270NLP Converting Words To Features Nltk
- Program #271NLP Naive Bayes Classifier Nltk
- Program #272NLP Save Classifier Nltk
- Program #273NLP Scikit Learn Algorithms Nltk
- Unit 14Working with Computer Vision
- Program #274NLP Combining Algorithms Nltk
- Program #275NLP Noise Removal Nltk
- Program #276NLP Noise Removal Regular Expressions Nltk
- Program #277NLP Object Standardization Nltk
- Program #278NLP Topic Modelling Nltk
- Program #279NLP Ngrams Nltk
- Program #280NLP Tfidf Vectorizer_nltk
- Program #281NLP Word Embedding Nltk
- Program #282NLP Text Matching Levenshtein Distance Nltk
- Program #283NLP Cosine Similarity Nltk
- Program #284NLP Wordnet Nltk
- Program #285Computer Vision Install Opencv
- Program #286Computer Vision Reading Images Opencv
- Program #287Computer Vision Displaying Images Opencv
- Program #288Computer Vision Writing Images Opencv
- Program #289Computer Vision Color Space Opencv
- Program #290Computer Vision Thresholding Opencv
- Program #291Computer Vision Finding Contours Opencv
- Program #292Computer Vision Image Scaling Opencv
- Program #293Computer Vision Image Rotation Opencv
- Program #294Computer Vision Image Translation Opencv
- Program #295Computer Vision Image Edge Detection Opencv
- Program #296Computer Vision Image Filtering Opencv
- Program #297Computer Vision Image Filtering Blurring Opencv
- Program #298Computer Vision Image Filtering Blurring Gaussian Blur Opencv
- Program #299Computer Vision Image Filtering Blurring Median Blur Opencv
- Program #300Computer Vision Image Filtering Bilateral Opencv
- Program #301Computer Vision Morphological Operations Erosion Opencv
- Program #302Computer Vision Morphological Operations Dilation Opencv
- Program #303Computer Vision Morphological Operations Opening Opencv
- Program #304Computer Vision Morphological Operations Closing Opencv
- Program #305Computer Vision Morphological Operations Gradient Opencv
- Program #306Computer Vision Morphological Operations Tophat Opencv
- Program #307Computer Vision Morphological Operations Blackhat Opencv
- Program #308Computer Vision Image Gradients Sobel Opencv
- Program #309Computer Vision Image Gradients Scharr Opencv
- Unit 15Working with RPA
- Program #310Computer Vision Image Gradients Laplacian Opencv
- Program #311Computer Vision Canny Edge Detection Opencv
- Program #312Computer Vision Gaussian Pyramid Opencv
- Program #313Computer Vision Contours Opencv
- Program #314Computer Vision Histograms Opencv
- Program #315Computer Vision Histograms With Mask Opencv
- Program #316Computer Vision Fourier Transform Opencv
- Program #317Computer Vision Template Matching Opencv
- Program #318RPA Install Automagica
- Program #319RPA Open Notepad Type Hello World Automagica
- Program #320RPA Open Chrome Browser Automagica
- Program #321RPA Automate Mouse Movements Automagica
- Program #322RPA Function To Open Dropbox Automagica
- Program #323RPA List Running Processes Automagica
- Program #324RPA Return Elements Automagica
- Program #325RPA Extract Text From Pdf Automagica
- Unit 16Working with Deep Learning
- Program #326RPA Merge Pdf Automagica
- Program #327RPA File Manipulation Ename File Automagica
- Program #328RPA File Manipulation Moving File Automagica
- Program #329RPA File Manipulation Copying File Automagica
- Program #330RPA Image Operation Open Image Automagica
- Program #331RPA Image Operation Rotating Image Automagica
- Program #332RPA Image Operation Resizing Image Automagica
- Program #333RPA Strings Automagica
- Program #334RPA String Manipulation Automagica
- Program #335Deep Learning Introduction
/******************************
File Name : CSLAB_HELLO_WORLD_V1
Purpose : A Simple Hello World Program in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 9:25 hrs
Version : 1.0
/*****************************
## Program Description : A "Hello, World!" Program in R Programming
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
cat("Hello World\n")
/******************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/******************************
/**********************************
File Name : CSLAB_VARIABLES_V1
Purpose : A Program for Variables in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 09:32 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Variables in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X <- 25
vAR_CSLAB_Y <- 30
vAR_CSLAB_X
vAR_CSLAB_Y
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/*****************************
File Name : CSLAB_ARITHMATIC_OPERATIONS_V1
Purpose : A Program for Arithmatic Operations in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 9:41 hrs
Version : 1.0
/******************************
## Program Description : A Program for Arithmatic Operations in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X <- 25
vAR_CSLAB_Y <- 30
# Addition
vAR_CSLAB_X + vAR_CSLAB_Y
# Subtraction
vAR_CSLAB_X - vAR_CSLAB_Y
# Multiplication
vAR_CSLAB_X * vAR_CSLAB_Y
# Division
vAR_CSLAB_X / vAR_CSLAB_Y
/******************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/******************************
/**********************************
File Name : CSLAB_LOGICAL_OPERATIONS_V1
Purpose : A Program for Logical Operations in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 9:50 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Logical Operations in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X <- c(1:10)
vAR_CSLAB_X[(vAR_CSLAB_X>8) | (vAR_CSLAB_X<5)]
vAR_CSLAB_X
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_VECTORIZED_OPERATORS_V1
Purpose : A Program for Vectorized Operations in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 9:58 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Vectorized Operations in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X <- c(5,2,8)
vAR_CSLAB_Y <- c(1,3,9)
vAR_CSLAB_X < vAR_CSLAB_Y
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_ATOMIZED_VECTORS_V1
Purpose : A Program for Atomized Vectors in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 10:07 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Atomized Vectors in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X <- c(1, 2, 3)
vAR_CSLAB_X
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FACTORS_V1
Purpose : A Program for Factors in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 10:19 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Factors in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X <- factor(c("yes", "no", "no", "yes", "yes"))
vAR_CSLAB_X
as.character(vAR_CSLAB_X)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_LISTS_V1
Purpose : A Program for Lists in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 10:29 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Lists in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X <- list(1, "a", TRUE, 1 + (0+4i))
vAR_CSLAB_X
vAR_CSLAB_X <- 1:10
vAR_CSLAB_X <- as.list(vAR_CSLAB_X)
length(vAR_CSLAB_X)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_FRAMES_V1
Purpose : A Program for Data Frames in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 10:41 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Data Frames in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_df <- data.frame(id = letters[1:10],
vAR_CSLAB_X = 1:10, VAR_CSLAB_Y = rnorm(10))
vAR_CSLAB_df
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_MATRIX_V1
Purpose : A Program for Matrix Concepts in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 10:50 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Concepts of Matrix in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_M <- matrix(nrow = 2, ncol = 2)
vAR_CSLAB_M
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_ARRAYS_V1
Purpose : A Program for Arrays in R
Author : DeepSphere.AI, Inc.
Date and Time : 03/09/2015 11:03 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Concepts of Arrays in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_my.array <- array(1:24, dim=c(3,4,2))
vAR_CSLAB_my.array
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_FUNCIONS_V1
Purpose : A Program for Concept of Functions in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 11:14 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Concepts of Functions in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X <- 10
vAR_CSLAb_f1 <- function (vAR_CSLAB_X) {
function() {
vAR_CSLAB_X + 10
}
}
vAR_CSLAb_f1(1)()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_COMPONENTS_V1
Purpose : A Program for Function Components in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 11:28 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Function Components in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_F <- function(VAR_CSLAB_X) VAR_CSLAB_X^2
vAR_CSLAB_F
#> function(vAR_CSLAB_X) vAR_CSLAB_X^2
formals(vAR_CSLAB_F)
body(vAR_CSLAB_F)
environment(vAR_CSLAB_F)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_ARGUMENT_MATCHING_V1
Purpose : A Program for Argument Matching in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 11:42 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Argument Matching in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
match.call(get, call("get", "abc", i = FALSE, p = 3))
#vAR_CSLAB_fun
function(x, lower = 0, upper = 1) {
structure((x - lower) / (upper - lower), CALL = match.call())
}
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_ARGUMENT_DEFAULT_VALUES_V1
Purpose : A Prgram for Arguments with Default Values in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 11:55 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Arguments with Default Values Matching in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_f <- function(vAR_CSLAB_a = 1, vAR_CSLAB_b = 2)
{
c(vAR_CSLAB_a, vAR_CSLAB_b)
}
vAR_CSLAB_f()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_LAZY_EVALUATION_V1
Purpose : A Program for Concepts of Lazy Evaluation in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 12:13 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Concepts of Lazy Evaluation in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
VAR_CSLAB_f <- function(VAR_CSLAB_X) {
force(VAR_CSLAB_X)
10
}
VAR_CSLAB_f(stop("This is an error!"))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_MULTIPLE_RETURN_VALUES_V1
Purpose : A Program for Multiple Return Values in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 12:28 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Concepts of Multiple Return Values in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_functionReturningTwoValues <- function() {return(list(first=1, second=2))}
VAR_CSLAB_R <- vAR_CSLAB_functionReturningTwoValues()
VAR_CSLAB_R$first
VAR_CSLAB_R$second
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_ANONUMOUS_FUNCTIONS_V1
Purpose : A Program for Anonymous Fumctions in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 12:49 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Concepts of Anonymous Fumctions in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
formals(function(vAR_CSLAB_X = 4) g(vAR_CSLAB_X) + h(vAR_CSLAB_X))
body(function(vAR_CSLAB_X = 4) g(vAR_CSLAB_X) + h(vAR_CSLAB_X))
environment(function(vAR_CSLAB_X = 4) g(vAR_CSLAB_X) + h(vAR_CSLAB_X))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_IF_STATEMENT_V1
Purpose : A Program for Concepts of if Statments in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 12:58 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Concepts if Statements in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
if(1==0)
{
print(1)
}
print(2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_IF_ELSE_STATEMENT_V1
Purpose : A Program for Concepts ifelse Statements in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 13:15 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Concepts ifelse Statements in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X <- 1:10
ifelse (vAR_CSLAB_X <5 | vAR_CSLAB_X >8,
vAR_CSLAB_X, 0)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_SWITCH_STATEMENT_V1
Purpose : A Program for Concepts of Switch Statements in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 14:04 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Concepts of Switch Statements in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X <- 3
switch(vAR_CSLAB_X, 2+2, mean(1:10), rnorm(5))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_VECTORIZED_IFELSE_STATEMENT_V1
Purpose : A Program for Concepts of Vectorized ifelse Statements in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 14:19 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Concepts of Vectorized ifelse Statements in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X = seq(0.1,10,0.1)
ifelse(vAR_CSLAB_X < 5, 1, 2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_WHILE_V1
Purpose : A Program for Concepts of While Statements in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 14:33 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Concepts of While Statements in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_Z <- 0
while(vAR_CSLAB_Z < 5)
{
vAR_CSLAB_Z <- vAR_CSLAB_Z + 2
print(vAR_CSLAB_Z)
}
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FOR_STATEMENT_V1
Purpose : A Program for Concepts of For Statements in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 14:49 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Concepts of For Statements in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_mydf <- iris
vAR_CSLAB_myve <- NULL # Creates empty storage container
for(i in seq(along=vAR_CSLAB_mydf[,1])) {
vAR_CSLAB_myve <- c(vAR_CSLAB_myve, mean(as.numeric(vAR_CSLAB_mydf[i, 1:3])))
}
vAR_CSLAB_myve
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_APPLY_STATEMENT_V1
Purpose : A Program for Concepts of Apply Statements in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 15:08 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Concepts of Apply Statements in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_X <- 1:10
vAR_CSLAB_test <- function(vAR_CSLAB_X)
{
if(vAR_CSLAB_X < 5)
{
vAR_CSLAB_X-1
}
else
{
vAR_CSLAB_X / vAR_CSLAB_X
}
}
apply(as.matrix(vAR_CSLAB_X), 1, vAR_CSLAB_test)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_SUBSETTING_V1
Purpose : A Program for Subsetting in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 15:29 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Subsetting in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_numvec = c(2,5,8,9,0,6,7,8,4,5,7,11)
vAR_CSLAB_charvec = c("David","James","Sara","Tim","Pierre",
"Janice","Sara","Priya","Keith","Mark",
"Apple","Sara")
vAR_CSLAB_gender = c("M","M","F","M","M","M","F","F","F","M","M","F")
vAR_CSLAB_state = c("CO","KS","CA","IA","MO","FL","CA","CO","FL","CA","WY","AZ")
subset(vAR_CSLAB_numvec, vAR_CSLAB_numvec > 7)
subset(vAR_CSLAB_numvec, vAR_CSLAB_numvec < 9 & vAR_CSLAB_numvec > 4)
subset(vAR_CSLAB_numvec, vAR_CSLAB_numvec < 3 | vAR_CSLAB_numvec > 9)
vAR_CSLAB_df = data.frame(vAR_CSLAB1=c(vAR_CSLAB_numvec), vAR_CSLAB2=c(vAR_CSLAB_charvec),
gender=c(vAR_CSLAB_gender), state=c(vAR_CSLAB_state))
subset(vAR_CSLAB_df, vAR_CSLAB1 < 5)
subset(vAR_CSLAB_df, vAR_CSLAB2 == "Sara")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_MISSING_VALUES_V1
Purpose : A Program for Functions of Missing Values in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 15:47 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Functions of Missing Values in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
is.na(c(1, NA))
is.na(paste(c(1, NA)))
(VAR_CSLAB_XX <- c(0:4))
is.na(VAR_CSLAB_XX) <- c(2, 4)
VAR_CSLAB_XX
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_IMPORTING_CSV_FILES_V1
Purpose : A Program for Importing a CSV File
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 15:59 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Importing a CSV File in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Unit2_Program28_Read_CSV.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_DATA = read.table(vAR_CSLAB_FILE_PATH,header=TRUE, sep=",")
vAR_CSLAB_DATA
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_IMPORTING_FROM_TABLE_V1
Purpose : A Program for Importing a Data from a Text File in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 16:14 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Importing a Data from a Text File in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Unit2_Program29_Read_TXT.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_DATA = read.table(vAR_CSLAB_FILE_PATH,header=TRUE, sep=",")
vAR_CSLAB_DATA
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_IMPORTING_DATA_FROM_URL_V1
Purpose : A Program for Importing a Data from a Website (URL) into R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 16:28 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Importing a Data from a Website (URL) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_INI_FILE_PATH = Sys.getenv("R_TUTORIAL")
vAR_CSLAB_READ_URL <- read.table("http://solarscience.msfc.nasa.gov/greenwch/spot_num.txt", header=TRUE)
summary(vAR_CSLAB_READ_URL)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_IMPORTING_XML_FILE_V1
Purpose : A Program for Importing a Data from a Website (XML) in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 16:42 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Importing a Data from a Website (XML) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Unit2_Program31_Read_XML.xml"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_url = vAR_CSLAB_FILE_PATH
install.packages("XML")
library(XML)
xmlToDataFrame(vAR_CSLAB_url)
vAR_CSLAB_indata <- xmlToDataFrame(vAR_CSLAB_url)
head(vAR_CSLAB_indata)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_IMPORTING_EXCEL_FILE_V1
Purpose : A Program for Importing a Data from an Excel in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 16:57 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Importing a Data from an Excel in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("readxl")
library(readxl)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Unit2_Program32_Read_EXCEL.xlsx"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_Read_EXCEL <- read_excel(vAR_CSLAB_FILE_PATH)
head(vAR_CSLAB_Read_EXCEL)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_IMPORTING_FROM_SAS_V1
Purpose : A Program for Importing a Data from SAS in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 17:14 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Importing a Data from SAS in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("foreign")
library(foreign)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Unit2_Program33_Read_SAS.sav"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_mySPSSData <- read.spss(vAR_CSLAB_FILE_PATH)
head(vAR_CSLAB_mySPSSData)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_READING_DATA_FROM_DATABASE_INTO_R_V1
Purpose : A Program for Importing a Data from SAP HANA Database in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 17:29 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Importing a Data SAP HANA Database in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("plotrix")
install.packages("RODBC")
library("plotrix")
library("RODBC")
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
#vAR_CSLAB_ch<-odbcConnect("DS",uid="DURGA",pwd="Delhi123")
#vAR_CSLAB_res<-sqlFetch(vAR_CSLAB_ch,"DURGA.TICKETS")
#barplot(vAR_CSLAB_res$TICKETS,names.arg=vAR_CSLAB_res$CARRIER, main="Tickets for December 2015")
#odbcClose(vAR_CSLAB_ch)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_EXAMPLE_DATASET_V1
Purpose : A Program for an Example Dataset in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 17:42 hrs
Version : 1.0
/**********************************
## Program Description : A Program for an Example Dataset in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
library (MASS)
data()
AirPassengers
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_READING_DATA_FROM_SAPHANA_INTO_R_V1
Purpose : A Program for Reading Data from SAP HANA Database into R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 17:58 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Reading Data from SAP HANA Database into R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("plotrix")
install.packages("RODBC")
library("plotrix")
library("RODBC")
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
#vAR_CSLAB_ch<-odbcConnect("DS",uid="DURGA",pwd="Delhi123")
#vAR_CSLAB_res<-sqlFetch(vAR_CSLAB_ch,"DURGA.TICKETS")
#barplot(vAR_CSLAB_res$TICKETS,names.arg=vAR_CSLAB_res$CARRIER, main="Tickets for December 2015")
#odbcClose(vAR_CSLAB_ch)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_READING_DATA_FROM_HADOOP_INTO_R_V1
Purpose : A Program for Reading Data from Hadoop (Hive) into R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 18:16 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Reading Data from Hadoop (Hive) into R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("RHive")
#library("RHive")
#vAR_INI_FILE_PATH = Sys.getenv("R_TUTORIAL")
#rhive.init(hive = "/usr/lib/hive", hadoop="/usr/lib/hadoop", verbose= FALSE)
#vAR_CSLAB_a <- rhive.query ("select * from ge_og_Account")
#Sys.setenv(HIVE_HOME="/usr/lib/hive")
#Sys.setenv(HADOOP_HOME="/usr/lib/hadoop")
#rhive.env(ALL=TRUE)
#rhive.init()
#rhive.connect(hiveServer2=TRUE)
#rhive.query("select * from ge_og_account")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_LINEAR_MODELS_V1
Purpose : A Program for Linear Modelling in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 18:37 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Linear Modelling in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_linearMod <- lm(dist ~ speed, data=cars) # build linear regression model on full data
print(vAR_CSLAB_linearMod)
vAR_CSLAB_modelSummary <- summary(vAR_CSLAB_linearMod) # capture model summary as an object
vAR_CSLAB_modelCoeffs <- vAR_CSLAB_modelSummary$coefficients # model coefficients
vAR_CSLAB_beta.estimate <- vAR_CSLAB_modelCoeffs["speed", "Estimate"] # get beta estimate for speed
vAR_CSLAB_std.error <- vAR_CSLAB_modelCoeffs["speed", "Std. Error"] # get std.error for speed
vAR_CSLAB_t_value <- vAR_CSLAB_beta.estimate/vAR_CSLAB_std.error # calc t statistic
vAR_CSLAB_p_value <- 2*pt(-abs(vAR_CSLAB_t_value), df=nrow(cars)-ncol(cars)) # calc p Value
vAR_CSLAB_f_statistic <- vAR_CSLAB_linearMod$fstatistic[1] # fstatistic
vAR_CSLAB_f <- summary(vAR_CSLAB_linearMod)$vAR_CSLAB_fstatistic # parameters for model p-value calc
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_MODEL_INHERITANCE_V1
Purpose : A Program for Model Inheritance in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 18:59 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Model Inheritance in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
# Generate some 'data', with normally distributed residuals,with the two variables known to have a certain relationship
# Generate some 'data', with normally distributed residuals,with the two variables known to have a certain relationship
vAR_CSLAB_dtr <- (rnorm(n = 100, mean = 30, sd = 4))
vAR_CSLAB_density <- rnorm(n= 100, mean = 0.5, sd = 0.1) * vAR_CSLAB_dtr
# Examine the data in three ways
vAR_CSLAB_dtr
vAR_CSLAB_density
vAR_CSLAB_data1 <- data.frame(vAR_CSLAB_dtr,vAR_CSLAB_density)
hist(vAR_CSLAB_density, col = "light grey")
par(mfrow = c(1,1))
plot(vAR_CSLAB_dtr,vAR_CSLAB_density, pch = 19, col = 'blue')
plot(vAR_CSLAB_dtr,vAR_CSLAB_density, pch = 19, col = 'blue', xlab = 'Distance to Road (m)', ylab = 'Population Density (ind/m2)')
mean(vAR_CSLAB_density)
sd(vAR_CSLAB_density)
vAR_CSLAB_se <- sd(vAR_CSLAB_density)/sqrt(length(vAR_CSLAB_density))
vAR_CSLAB_se
vAR_CSLAB_cv <- sd(vAR_CSLAB_density)/mean(vAR_CSLAB_density)
vAR_CSLAB_cv
#library(pastecs)
#vAR_CSLAB_stat.desc(vAR_CSLAB_density)
vAR_CSLAB_mod1 <- lm(vAR_CSLAB_density~vAR_CSLAB_dtr)
# Add the regression lion to the plot
abline(vAR_CSLAB_mod1, col = 'blue')
# Look at a summmary of the regression model's results
summary(vAR_CSLAB_mod1)
# Understanding the t-test and coefficient of determination
abline(mean(vAR_CSLAB_density),0, col = 'red', lty = 3)
library(ggplot2)
ggplot(vAR_CSLAB_data1, aes(x=vAR_CSLAB_dtr, y=vAR_CSLAB_density)) +
geom_point(colour = 'red', size = 5) +
geom_smooth(method=lm, colour = 'black', fill = 'red', alpha = 0.25) +
ylab("Density (ind/km2)") +
xlab("Distance to River (km))")
# Shift from simple OLS regression to multiple OLS regression
vAR_CSLAB_veg <- rnorm(n= 100, mean = 0.7, sd = 0.1) * vAR_CSLAB_dtr
vAR_CSLAB_mod1A <- lm(vAR_CSLAB_density ~ vAR_CSLAB_dtr + vAR_CSLAB_veg)
summary (vAR_CSLAB_mod1A)
# Consider collinearity when interpreting the multiple regression results
cor(vAR_CSLAB_dtr,vAR_CSLAB_veg)
vAR_CSLAB_mod1C <- lm(vAR_CSLAB_density~vAR_CSLAB_veg)
summary(vAR_CSLAB_mod1C)
library(scatterplot3d)
scatterplot3d(vAR_CSLAB_density ~ vAR_CSLAB_dtr + vAR_CSLAB_veg)
vAR_CSLAB_mod2 <- glm(vAR_CSLAB_density ~ vAR_CSLAB_dtr, family = gaussian)
summary(vAR_CSLAB_mod2)
vAR_CSLAB_mod1 <- lm(vAR_CSLAB_density ~vAR_CSLAB_dtr)
summary(vAR_CSLAB_mod1)
vAR_CSLAB_survive <- c(0,0,0,0,1,0,1,1,1,1,1)
vAR_CSLAB_home.range.quality <- seq(0,1,0.1)
vAR_CSLAB_mod4b <- lm(vAR_CSLAB_survive ~ vAR_CSLAB_home.range.quality)
summary(vAR_CSLAB_mod4b)
plot(vAR_CSLAB_home.range.quality, vAR_CSLAB_survive, pch = 19, ylim = c(-0.2, 1.2),
main = 'Inappropriate lm() fit to binomial Y')
points(vAR_CSLAB_home.range.quality,fitted(vAR_CSLAB_mod4b),col=2, pch = 19)
lines(vAR_CSLAB_home.range.quality,fitted(vAR_CSLAB_mod4b),col=2)
vAR_CSLAB_mod4a <- glm(vAR_CSLAB_survive ~ vAR_CSLAB_home.range.quality, family = binomial)
plot(vAR_CSLAB_home.range.quality, vAR_CSLAB_survive, pch = 19, ylim = c(-0.2, 1.2),
main = 'Appropriate glm() fit to binomial Y')
points(vAR_CSLAB_home.range.quality,fitted(vAR_CSLAB_mod4a),col=2, pch = 19)
summary(vAR_CSLAB_mod4a)
#vAR_CSLAB_slope <- inv.logit(13.046)
#vAR_CSLAB_slope
rm(list=ls(all=TRUE))
vAR_CSLAB_kenya.herdsize = read.table("kenyaherdsize3.txt",
header = TRUE, sep = ",", fill= TRUE, dec = ".")
vAR_CSLAB_reduced.data <-subset(vAR_CSLAB_kenya.herdsize, DistPred < 2.0, select =
c(DistPred, GroupSize, Species,BushWoodGrass, HabOpen.Close))
vAR_CSLAB_wildebeest.only <- subset(vAR_CSLAB_reduced.data, Species == 'Wildbst',
select = c(GroupSize, BushWoodGrass, HabOpen.Close, DistPred))
vAR_CSLAB_modA <- glm(formula = GroupSize ~ BushWoodGrass, data = vAR_CSLAB_wildebeest.only)
vAR_CSLAB_modB <- glm(formula = GroupSize ~ HabOpen.Close, data = vAR_CSLAB_wildebeest.only)
vAR_CSLAB_modC <- glm(formula = GroupSize ~ DistPred, data = vAR_CSLAB_wildebeest.only)
vAR_CSLAB_modD <- glm(formula = GroupSize ~ DistPred + BushWoodGrass, data = vAR_CSLAB_wildebeest.only)
# Compare AIC scores for the 4 models using functions in the MuMIn (Multi-Model Inference) package:
library(MuMIn)
vAR_CSLAB_Cand.mods <- list(vAR_CSLAB_modA, vAR_CSLAB_modB, vAR_CSLAB_modC, vAR_CSLAB_modD)
vAR_CSLAB_aictab <- model.sel(vAR_CSLAB_Cand.mods)
vAR_CSLAB_aictab
print.data.frame(vAR_CSLAB_aictab,digits=2)
vAR_CSLAB_x <-model.avg(vAR_CSLAB_Cand.mods, beta = TRUE, revised.var = TRUE)
summary(vAR_CSLAB_x, digits = 3)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_CATEGORY_VARIBLES_V1
Purpose : A Program for Category Variables in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 19:22 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Category Variables in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_hsb2 <- read.csv("https://stats.idre.ucla.edu/stat/data/hsb2.csv")
vAR_CSLAB_hsb2$race.f <- factor(vAR_CSLAB_hsb2$race)
is.factor(vAR_CSLAB_hsb2$race.f)
vAR_CSLAB_hsb2$race.f[1:15]
#summary(lm(write ~ race.f, data = hsb2))
#summary(lm(write ~ factor(race), data = hsb2))
vAR_CSLAB_hsb2 <- within(vAR_CSLAB_hsb2, {
race.ct <- C(race.f, treatment)
print(attributes(race.ct))
})
vAR_CSLAB_hsb2 <- within(vAR_CSLAB_hsb2, {
race.ch <- C(race.f, helmert)
print(attributes(race.ch))
})
vAR_CSLAB_hsb2 <- within(vAR_CSLAB_hsb2, {
race.ch1 <- C(race.f, helmert, 3)
print(attributes(race.ch1))
})
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_MULTIVARIATE_MODELS_V1
Purpose : A Program for Multivariate Models in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 19:59 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Multivariate Models in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_N <- 100
vAR_CSLAB_c <- rbinom(vAR_CSLAB_N, 1, 0.2)
vAR_CSLAB_H <- rnorm(vAR_CSLAB_N, -10, 2)
vAR_CSLAB_A <- -1.4*vAR_CSLAB_c + 0.6*vAR_CSLAB_H + rnorm(vAR_CSLAB_N, 0, 3)
vAR_CSLAB_B <- 1.4*vAR_CSLAB_c - 0.6*vAR_CSLAB_H + rnorm(vAR_CSLAB_N, 0, 3)
vAR_CSLAB_Y <- cbind(vAR_CSLAB_A, vAR_CSLAB_B)
vAR_CSLAB_my.model <- lm(vAR_CSLAB_Y ~ vAR_CSLAB_c + vAR_CSLAB_H)
summary(manova(vAR_CSLAB_my.model))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_NON_LINEAR_MODELS_V1
Purpose : A Program for Non Linear Models in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 20:21 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Non Linear Models in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
data(pressure)
str(pressure)
summary(pressure)
pressure$temperature = pressure$temperature + 273.15
pressure$pressure = pressure$pressure * .1333
summary(pressure)
vAR_CSLAB_pres = pressure$pressure
vAR_CSLAB_temp = pressure$temperature
rm(pressure)
ls()
par(mfrow=c(1,4)) # one row of four graphs
plot(vAR_CSLAB_pres ~ vAR_CSLAB_temp, main="Vapor Pressure\nof Mercury",
vAR_CSLAB_xlab="Temperature (degrees Kelvin)",
vAR_CSLAB_ylab="Pressure (kPascals)")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_FITTING_MODELS_V1
Purpose : A Program for Fitting Models in R in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 20:42 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Fitting Models in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
vAR_CSLAB_trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
vAR_CSLAB_group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
vAR_CSLAB_weight <- c(vAR_CSLAB_ctl, vAR_CSLAB_trt)
lm.D9 <- lm(vAR_CSLAB_weight ~ vAR_CSLAB_group)
lm.D90 <- lm(vAR_CSLAB_weight ~ vAR_CSLAB_group - 1) # omitting intercept
anova(lm.D9)
summary(lm.D90)
vAR_CSLAB_opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0))
plot(lm.D9, las = 1) # Residuals, Fitted, ...
par(vAR_CSLAB_opar)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_MIXED_MODELS_V1
Purpose : A Program for Mixed Models in R in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 21:08 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Mixed Models in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
library(MASS)
data(oats)
names(oats) = c('block', 'variety', 'nitrogen', 'yield')
oats$mainplot = oats$variety
oats$subplot = oats$nitrogen
library(nlme)
vAR_CSLAB_m1.nlme = lme(yield ~ variety*nitrogen,
random = ~ 1|block/mainplot,
data = oats)
summary(vAR_CSLAB_m1.nlme)
library(lme4)
vAR_CSLAB_m1.lme4 = lmer(yield ~ variety*nitrogen + (1|block/mainplot),
data = oats)
summary(vAR_CSLAB_m1.lme4)
anova(vAR_CSLAB_m1.lme4)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_MULTILEVEL_MODELS_V1
Purpose : A Program for Multilevel Models in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 21:29 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Multilevel Models in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
library(lattice)
set.seed(10101010)
vAR_CSLAB_x <- rnorm(100)
vAR_CSLAB_y1 <- vAR_CSLAB_x[1:25] * 2 + rnorm(25, mean=1)
vAR_CSLAB_y2 <- vAR_CSLAB_x[26:50] * 2.6 + rnorm(25, mean=1.5)
vAR_CSLAB_y3 <- vAR_CSLAB_x[51:75] * 2.9 + rnorm(25, mean=5)
vAR_CSLAB_y4 <- vAR_CSLAB_x[76:100] * 3.5 + rnorm(25, mean=5.5)
vAR_CSLAB_d <- data.frame(x=vAR_CSLAB_x, y=c(vAR_CSLAB_y1,vAR_CSLAB_y2,vAR_CSLAB_y3,vAR_CSLAB_y4), f=factor(rep(letters[1:4], each=25)))
# plot
xyplot(y ~ vAR_CSLAB_x, groups=f, data=vAR_CSLAB_d,auto.key=list(columns=4, title='Beard Type', lines=TRUE, points=FALSE, cex=0.75),type=c('p','r'), ylab='Number of Pirates', xlab='Distance from Land')
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_STATISTICAL_MODELS_V1
Purpose : A Program for Statistical Models in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 21:49 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Statistical Models in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("ggplot2")
library(ggplot2)
data(trees)
head(trees)
str(trees)
vAR_CSLAB_fit_1 <- lm(Volume ~ Girth, data = trees)
summary(vAR_CSLAB_fit_1)
ggplot(data=trees, aes(vAR_CSLAB_fit_1$residuals)) + geom_histogram(binwidth = 1, color = "black", fill = "purple4") +theme(panel.background = element_rect(fill = "white"),axis.line.x=element_line(),axis.line.y=element_line()) +ggtitle("Histogram for Model Residuals")
ggplot(data = trees, aes(x = Girth, y = Volume)) + geom_point() + stat_smooth(method = "lm", col = "dodgerblue3") +theme(panel.background = element_rect(fill = "white"), axis.line.x=element_line(),axis.line.y=element_line()) + ggtitle("Linear Model Fitted to Data")
predict(vAR_CSLAB_fit_1, data.frame(Girth = 18.2))
vAR_CSLAB_fit_2 <- lm(Volume ~ Girth + Height, data = trees)
vAR_CSLAB_Girth <- seq(9,21, by=0.5) ## make a girth vector
vAR_CSLAB_Height <- seq(60,90, by=0.5) ## make a height vector
vAR_CSLAB_pred_grid <- expand.grid(Girth = vAR_CSLAB_Girth, Height = vAR_CSLAB_Height) ## make a grid using the vectors
vAR_CSLAB_pred_grid$Volume2 <-predict(vAR_CSLAB_fit_2, new = vAR_CSLAB_pred_grid)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_INTERPRETATION_MODELS_V1
Purpose : A Program for Interpretation Models in in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 22:12 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Interpretation Models in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
summary(cars)
plot(cars, col='blue', pch=20, cex=2, main="Relationship between Speed and Stopping Distance for 50 Cars", xlab="Speed in mph", ylab="Stopping Distance in feet")
set.seed(122)
vAR_CSLAB_speed.c = scale(cars$speed, center=TRUE, scale=FALSE)
#vAR_CSLAB_mod1 = lm(formula = dist ~ speed.vAR_CSLAB_c, data = cars)
summary(vAR_CSLAB_mod1)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_QUADRATIC_MODELS_V1
Purpose : A Program for Quadratic Models in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 22:37 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Quadratic Models in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_A <- structure(list(Time = c(0, 1, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 25, 26, 7, 28, 29, 30), Counts = c(126.6, 101.8, 71.6, 101.6, 68.1, 62.9, 45.5, 41.9, 46.3, 34.1, 38.2, 41.7, 24.7, 41.5, 36.6, 19.6,
22.8, 29.6, 23.5, 15.3, 13.4, 26.8, 9.8, 18.8, 25.9, 19.3)), .Names = c("Time", "Counts"),row.names = c(1L, 2L, 3L, 5L, 7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 19L, 20L, 21L, 22L, 23L, 25L, 26L, 27L, 28L, 29L, 30L, 31L),class = "data.frame")
attach(vAR_CSLAB_A)
names(vAR_CSLAB_A)
linear.model <-lm(Counts ~ Time)
summary(linear.model)
plot(Time, Counts, pch=16, ylab = "Counts ", cex.lab = 1.3, col = "red" )
abline(lm(Counts ~ Time), col = "blue")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_EXPONENTIAL_MODELS_V1
Purpose : A Program for Exponential Models in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 22:58 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Exponential Models in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_A <- structure(list(Time = c(0, 1, 2, 4, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30),Counts = c(126.6, 101.8, 71.6, 101.6, 68.1, 62.9, 45.5, 41.9, 46.3, 34.1, 38.2, 41.7, 24.7, 41.5, 36.6, 19.6,22.8, 29.6, 23.5, 15.3, 13.4, 26.8, 9.8, 18.8, 25.9, 19.3)), .Names = c("Time", "Counts"), row.names = c(1L, 2L,3L, 5L, 7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 19L, 20L, 21L, 22L, 23L, 25L, 26L, 27L, 28L, 29L, 30L,31L), class = "data.frame")
attach(vAR_CSLAB_A)
names(vAR_CSLAB_A)
vAR_CSLAB_exponential.model <- lm(log(Counts)~ Time)
summary(vAR_CSLAB_exponential.model)
vAR_CSLAB_timevalues <- seq(0, 30, 0.1)
vAR_CSLAB_Counts.exponential2 <- exp(predict(vAR_CSLAB_exponential.model,list(Time=vAR_CSLAB_timevalues)))
plot(Time, Counts,pch=16)
lines(vAR_CSLAB_timevalues, vAR_CSLAB_Counts.exponential2,lwd=2, col = "red", xlab = "Time (s)", ylab = "Counts")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_LOGARITHMIC_MODELS_V1
Purpose : A Program for Logarithmic Models in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 23:21 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Logarithmic Models in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_x=c(61,610,1037,2074,3050,4087,5002,6100,7015)
vAR_CSLAB_y=c(0.401244, 0.844381, 1.18922, 1.93864, 2.76673, 3.52449, 4.21855, 5.04368, 5.80071)
plot(vAR_CSLAB_x,vAR_CSLAB_y)
vAR_CSLAB_Estimate = lm(vAR_CSLAB_y ~ vAR_CSLAB_x)
abline(vAR_CSLAB_Estimate)
vAR_CSLAB_x=c(61,610,1037,2074,3050,4087,5002,6100,7015)
vAR_CSLAB_y=c(0.974206,1.16716,1.19879,1.28192,1.30739,1.32019,1.35494,1.36941,1.37505)
vAR_CSLAB_logEstimate = lm(vAR_CSLAB_y ~ log(vAR_CSLAB_x))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_ANALYSIS_OF_CONTINUOS_DATA_V1
Purpose : A Program for Analysis of Continuous Data in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 23:49 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Analysis of Continuous Data in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
head(faithful)
vAR_CSLAB_duration = faithful$eruptions # the eruption durations
vAR_CSLAB_waiting = faithful$waiting # the waiting interval
head(cbind(vAR_CSLAB_duration,vAR_CSLAB_waiting))
plot(vAR_CSLAB_duration, vAR_CSLAB_waiting, xlab="Eruption duration", ylab="Time waited")
abline(lm(vAR_CSLAB_waiting ~ vAR_CSLAB_duration))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_MEASURES_OF_CENTRAL_TENDENCY_V1
Purpose : A Program for Measures of Central Tendency in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 00:13 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Measures of Central Tendency in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_country <- c("Canada","China","England","Germany","Greece","Other")
vAR_CSLAB_f <- c(5,7,2,5,3,4)
barplot(vAR_CSLAB_f, names=vAR_CSLAB_country)
mean(vAR_CSLAB_f)
weighted.mean(vAR_CSLAB_f)
median(vAR_CSLAB_f)
fivenum(vAR_CSLAB_f)
quantile(vAR_CSLAB_f)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_EXAMPLE_SPREAD_FUNCTION_V1
Purpose : A Program for Example of Spread Function in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 00:28 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Example of Spread Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("tidyr")
library("tidyr")
# Create a messy dataset
vAR_CSLAB_messy <- data.frame(
country = c("A", "B", "C"),
q1_2017 = c(0.03, 0.05, 0.01),
q2_2017 = c(0.05, 0.07, 0.02),
q3_2017 = c(0.04, 0.05, 0.01),
q4_2017 = c(0.03, 0.02, 0.04))
vAR_CSLAB_tidier <-vAR_CSLAB_messy
vAR_CSLAB_tidier
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_EXAMPLE_QUANTILE_V1
Purpose : A Program for Example of Quantile Function in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 9:31 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Example of Quantile Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_x = c(1.3,2.2,2.7,3.1,3.3,3.7)
quantile(vAR_CSLAB_x)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_EXAMPLE_VARIANCE_SD_V1
Purpose : A Program for Variance & Standard Deviation in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 9:41 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Variance & Standard Deviation in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_x = 1:25 # set of numbers
vAR_CSLAB_wt = runif(25) #some arbitrary weights
mean(vAR_CSLAB_x);
var(vAR_CSLAB_x);
sd(vAR_CSLAB_x);
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_ANALYSIS_OF_CATEGORICAL_DATA_V1
Purpose : A Program for Analysis of Categorical Data in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 9:54 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Analysis of Categorical Data in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_afterlife <- matrix(c(435,147,375,134),nrow=2,byrow=TRUE)
vAR_CSLAB_afterlife
dimnames(vAR_CSLAB_afterlife) <- list(c("Female","Male"),c("Yes","No"))
vAR_CSLAB_afterlife
names(dimnames(vAR_CSLAB_afterlife)) <- c("Gender","Believer")
vAR_CSLAB_afterlife
vAR_CSLAB_tot <- sum(vAR_CSLAB_afterlife)
vAR_CSLAB_tot
vAR_CSLAB_afterlife/vAR_CSLAB_tot
vAR_CSLAB_rowtot <- apply(vAR_CSLAB_afterlife,1,sum)
vAR_CSLAB_coltot <- apply(vAR_CSLAB_afterlife,2,sum)
vAR_CSLAB_rowtot
vAR_CSLAB_rowpct <- sweep(vAR_CSLAB_afterlife,1,vAR_CSLAB_rowtot,"/")
vAR_CSLAB_rowpct
vAR_CSLAB_Gender <- c("Female","Female","Male","Male")
vAR_CSLAB_Believer <- c("Yes","No","Yes","No")
vAR_CSLAB_Count <- c(435,147,375,134)
vAR_CSLAB_afterlife <- data.frame(vAR_CSLAB_Gender,vAR_CSLAB_Believer,vAR_CSLAB_Count)
vAR_CSLAB_afterlife
vAR_CSLAB_beliefs <- tapply(vAR_CSLAB_Count,list(vAR_CSLAB_Gender,vAR_CSLAB_Believer),c)
vAR_CSLAB_beliefs
names(dimnames(vAR_CSLAB_beliefs)) <- c("Gender","Believer")
vAR_CSLAB_beliefs
vAR_CSLAB_beliefs <- vAR_CSLAB_beliefs[,c(2,1)] # reverse the columns?
vAR_CSLAB_beliefs
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_FREQUENCY_DISTRIBUTION_V1
Purpose : A Program for Example of Frequency Distribution in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 10:13 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Example of Frequency Distribution in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_duration = faithful$eruptions
range(vAR_CSLAB_duration)
vAR_CSLAB_breaks = seq(1.5, 5.5, by=0.5) # half-integer sequence
vAR_CSLAB_breaks
vAR_CSLAB_duration.cut = cut(vAR_CSLAB_duration, vAR_CSLAB_breaks, right=FALSE)
vAR_CSLAB_duration.freq = table(vAR_CSLAB_duration.cut)
vAR_CSLAB_duration.freq
vAR_CSLAB_duration.cut
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_CATEGORY_STATISTICS_V1
Purpose : A Program for Example of Category Statistics in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 10:19 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Example of Category Statistics in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
library(MASS) # load the MASS package
vAR_CSLAB_school = painters$School # the painter schools
vAR_CSLAB_c_school = vAR_CSLAB_school == "C" # the logical index vector
vAR_CSLAB_c_painters = painters[vAR_CSLAB_c_school, ] # child data set
mean(vAR_CSLAB_c_painters$Composition)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_DATA_EXPLORATION_MULTIPLE_VARIABLES_V1
Purpose : A Program for Data Exploration of Multiple Variables in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2015 10:34 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Exploration of Multiple Variables in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
cov(iris$Sepal.Length, iris$Petal.Length)
cov(iris[,1:4])
boxplot(Sepal.Length~Species, data=iris)
boxplot(Sepal.Length~Species, data=iris)
with(iris, plot(Sepal.Length, Sepal.Width, col=Species, pch=as.numeric(Species)))
plot(jitter(iris$Sepal.Length), jitter(iris$Sepal.Width))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_DECISION_TREE_V1
Purpose : A Program for Classification using Decision Tree Classifier in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 10:53 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Classification using Decision Tree Classifier in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("ISLR")
install.packages("tree")
library("ISLR")
data(package="ISLR")
vAR_CSLAB_carseats<-Carseats
require(tree)
names(vAR_CSLAB_carseats)
hist(vAR_CSLAB_carseats$Sales)
vAR_CSLAB_High = ifelse(vAR_CSLAB_carseats$Sales<=8, "No", "Yes")
vAR_CSLAB_carseats = data.frame(vAR_CSLAB_carseats, vAR_CSLAB_High)
vAR_CSLAB_tree.carseats = tree(vAR_CSLAB_High~.-Sales, data=vAR_CSLAB_carseats)
set.seed(101)
vAR_CSLAB_train=sample(1:nrow(vAR_CSLAB_carseats), 250)
vAR_CSLAB_tree.carseats = tree(vAR_CSLAB_High~.-Sales, vAR_CSLAB_carseats, subset=vAR_CSLAB_train)
plot(vAR_CSLAB_tree.carseats)
text(vAR_CSLAB_tree.carseats, pretty=0)
vAR_CSLAB_tree.pred = predict(vAR_CSLAB_tree.carseats, vAR_CSLAB_carseats[-vAR_CSLAB_train,], type="class")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_RANDOM_FOREST_V1
Purpose : A Program for Classification using Random Forest Classifier in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 11:22 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Classification using Random Forest Classifier in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_ind <- sample(2, nrow(iris), replace=TRUE, prob=c(0.7, 0.3))
vAR_CSLAB_trainData <- iris[vAR_CSLAB_ind==1,]
vAR_CSLAB_testData <- iris[vAR_CSLAB_ind==2,]
library(randomForest)
vAR_CSLAB_rf <- randomForest(Species ~ ., data=vAR_CSLAB_trainData, ntree=100, proximity=TRUE)
table(predict(vAR_CSLAB_rf), vAR_CSLAB_trainData$Species)
print(vAR_CSLAB_rf)
plot(vAR_CSLAB_rf)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_KMEANS_CLUSTERING_V1
Purpose : A Program for K-Means Clustering in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 11:47 hrs
Version : 1.0
/**********************************
## Program Description : A Program for K-Means Clustering in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
set.seed(1234)
vAR_CSLAB_x <- rnorm(24, mean=rep(1:3, each=4), sd=0.2)
vAR_CSLAB_y <- rnorm(24, mean=rep(c(1,2,1), each=4), sd=0.2)
vAR_CSLAB_data <- data.frame(vAR_CSLAB_x, vAR_CSLAB_y)
plot(vAR_CSLAB_x, vAR_CSLAB_y, col="blue", pch=19, cex=1)
text(vAR_CSLAB_x+0.05, vAR_CSLAB_y+0.05, labels=as.character(1:24))
# k-means clustering
vAR_CSLAB_kmeansObj <- kmeans(vAR_CSLAB_data, centers=3)
names(vAR_CSLAB_kmeansObj)
# variance within clusters
vAR_CSLAB_kmeansObj$withinss
vAR_CSLAB_kmeansObj$cluster
plot(vAR_CSLAB_x, vAR_CSLAB_y, col=vAR_CSLAB_kmeansObj$cluster, pch=19, cex=1)
points(vAR_CSLAB_kmeansObj$centers, col=1:3, pch=4, cex=3, lwd=3)
par(mfrow=c(2,2), mar=c(3,3,3,3))
for(i in 1:4){
vAR_CSLAB_chartName <- paste("Chart",i, sep="_")
vAR_CSLAB_kmeansObj <- kmeans(vAR_CSLAB_data, centers=4)
plot(vAR_CSLAB_x, vAR_CSLAB_y, col=vAR_CSLAB_kmeansObj$cluster, pch=19, cex=1, main=vAR_CSLAB_chartName)
points(vAR_CSLAB_kmeansObj$centers, col=1:5, pch=4, cex=3, lwd=3)
}
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_CLUSTERING_HIERARCHICAL_V1
Purpose : A Program for Hierarchical Clustering in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 12:18 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Hierarchical Clustering in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_idx <- sample(1:dim(iris)[1], 40)
vAR_CSLAB_irisSample <- iris[vAR_CSLAB_idx,]
vAR_CSLAB_irisSample$Species <- NULL
vAR_CSLAB_hc <- hclust(dist(vAR_CSLAB_irisSample), method="ave")
plot(vAR_CSLAB_hc, hang = -1, labels=iris$Species[vAR_CSLAB_idx])
# cut tree into 3 clusters
rect.hclust(vAR_CSLAB_hc, k=3)
groups <- cutree(vAR_CSLAB_hc, k=3)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_CLUSTERING_DENSITY_BASED_V1
Purpose : A Program for Density Based Clustering in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 12:37 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Density Based Clustering in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("fpc")
library(fpc)
vAR_CSLAB_iris2 <- iris[-5] # remove class tags
vAR_CSLAB_ds <- dbscan(vAR_CSLAB_iris2, eps=0.42, MinPts=5)
# compare clusters with original class labels
table(vAR_CSLAB_ds$cluster, iris$Species)
plot(vAR_CSLAB_ds, vAR_CSLAB_iris2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_TIME_SERIES_ANALYSIS_V1
Purpose : A Program for Time Series Analysis in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 12:49 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Time Series Analysis in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_a <- ts(1:30, frequency=12, start=c(2011,3))
print(vAR_CSLAB_a)
str(vAR_CSLAB_a)
attributes(vAR_CSLAB_a)
plot(AirPassengers)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_ASSOSIATION_RULES_V1
Purpose : A Program for Association Rules in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 13:04 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Association Rules in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("titanic")
library("titanic")
str(Titanic)
vAR_CSLAB_df <- as.data.frame(Titanic)
head(vAR_CSLAB_df)
vAR_CSLAB_titanic.raw <- NULL
for(i in 1:4) {
vAR_CSLAB_titanic.raw <- cbind(vAR_CSLAB_titanic.raw, rep(as.character(vAR_CSLAB_df[,i]), vAR_CSLAB_df$Freq))
}
vAR_CSLAB_titanic.raw <- as.data.frame(vAR_CSLAB_titanic.raw)
names(vAR_CSLAB_titanic.raw) <- names(vAR_CSLAB_df)[1:4]
dim(vAR_CSLAB_titanic.raw)
str(vAR_CSLAB_titanic.raw)
head(vAR_CSLAB_titanic.raw)
summary(vAR_CSLAB_titanic.raw)
# read it into R
#titanic <- read.table("./data/Dataset.data", header=F)
library("titanic")
#names(titanic) <- c("Class", "Sex", "Age", "Survived")
install.packages("arules")
library("arules")
vAR_CSLAB_rules.all <- apriori(vAR_CSLAB_titanic.raw)
vAR_CSLAB_rules.all
inspect(vAR_CSLAB_rules.all)
vAR_CSLAB_rules <- apriori(vAR_CSLAB_titanic.raw, control = list(verbose=F),
parameter = list(minlen=2, supp=0.005, conf=0.8),
appearance = list(rhs=c("Survived=No", "Survived=Yes"),
default="lhs"))
quality(vAR_CSLAB_rules) <- round(quality(vAR_CSLAB_rules), digits=3)
vAR_CSLAB_rules.sorted <- sort(vAR_CSLAB_rules, by="lift")
inspect(vAR_CSLAB_rules.sorted)
vAR_CSLAB_subset.matrix <- is.subset(vAR_CSLAB_rules.sorted, vAR_CSLAB_rules.sorted)
vAR_CSLAB_subset.matrix[lower.tri(vAR_CSLAB_subset.matrix, diag=T)] <- NA
vAR_CSLAB_redundant <- colSums(vAR_CSLAB_subset.matrix, na.rm=T) >= 1
which(vAR_CSLAB_redundant)
vAR_CSLAB_rules.pruned <- vAR_CSLAB_rules.sorted[!vAR_CSLAB_redundant]
inspect(vAR_CSLAB_rules.pruned)
vAR_CSLAB_rules <- apriori(vAR_CSLAB_titanic.raw,
parameter = list(minlen=3, supp=0.002, conf=0.2),
appearance = list(rhs=c("Survived=Yes"),lhs=c("Class=1st", "Class=2nd", "Class=3rd","Age=Child", "Age=Adult"),
default="none"),control = list(verbose=F))
vAR_CSLAB_rules.sorted <- sort(vAR_CSLAB_rules, by="confidence")
inspect(vAR_CSLAB_rules.sorted)
install.packages("arulesViz")
library("arulesViz")
plot(vAR_CSLAB_rules.all)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_LINEAR_REGRESSION_V1
Purpose : A Program for Linear Regression in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 13:29 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Linear Regression in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("car")
library("car")
vAR_CSLAB_linearMod <- lm(dist ~ speed, data=cars) # build linear regression model on full data
print(vAR_CSLAB_linearMod)
lm(formula = dist ~ speed, data = cars)
set.seed(100) # setting seed to reproduce results of random sampling
vAR_CSLAB_trainingRowIndex <- sample(1:nrow(cars), 0.8*nrow(cars)) # row indices for training data
vAR_CSLAB_trainingData <- cars[vAR_CSLAB_trainingRowIndex, ] # model training data
vAR_CSLAB_testData <- cars[-vAR_CSLAB_trainingRowIndex, ] # test data
vAR_CSLAB_lmMod <- lm(dist ~ speed, data=vAR_CSLAB_trainingData) # build the model
vAR_CSLAB_distPred <- predict(vAR_CSLAB_lmMod, vAR_CSLAB_testData) # predict distance
summary (vAR_CSLAB_lmMod)
vAR_CSLAB_actuals_preds <- data.frame(cbind(actuals=vAR_CSLAB_testData$dist, predicteds=vAR_CSLAB_distPred)) # make actuals_predicted
vAR_CSLAB_correlation_accuracy <- cor(vAR_CSLAB_actuals_preds) # 82.7%
head(vAR_CSLAB_actuals_preds)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_NON_LINEAR_REGRESSION_V1
Purpose : A Program for Non Linear Regression in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 14:18 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Non Linear Regression in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_n <- 100
vAR_CSLAB_x <- seq(vAR_CSLAB_n)
vAR_CSLAB_y <- rnorm(vAR_CSLAB_n, 50 + 30 * vAR_CSLAB_x^(-0.2), 1)
vAR_CSLAB_Data <- data.frame(vAR_CSLAB_x, vAR_CSLAB_y)
plot(vAR_CSLAB_y ~ vAR_CSLAB_x, vAR_CSLAB_Data)
# fit a loess line
vAR_CSLAB_loess_fit <- loess(vAR_CSLAB_y ~ vAR_CSLAB_x, vAR_CSLAB_Data)
lines(vAR_CSLAB_Data$vAR_CSLAB_x, predict(vAR_CSLAB_loess_fit), col = "blue")
# fit a non-linear regression
vAR_CSLAB_nls_fit <- nls(vAR_CSLAB_y ~ a + b * vAR_CSLAB_x^(-c), vAR_CSLAB_Data, start = list(a = 80, b = 20,
c = 0.2))
lines(vAR_CSLAB_Data$vAR_CSLAB_x, predict(vAR_CSLAB_nls_fit), col = "red")
library(ggplot2)
ggplot(vAR_CSLAB_Data, aes(vAR_CSLAB_x,vAR_CSLAB_y)) + geom_point() + geom_smooth()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/*******************************************************************************************
/**********************************
File Name : CSLAB_POLYNOMIAL_REGRESSION_V1
Purpose : A Program for Polynomial Regression in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 14:56 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Polynomial Regression in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_Year <- c(1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969)
vAR_CSLAB_Population <- c(4835, 4970, 5085, 5160, 5310, 5260, 5235, 5255, 5235, 5210, 5175)
sample1 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Population)
#sample1
sample1$Year <- sample1$vAR_CSLAB_Year - 1964
plot(sample1$vAR_CSLAB_Year, sample1$vAR_CSLAB_Population, type="b")
vAR_CSLAB_fit1 <- lm(sample1$vAR_CSLAB_Population ~ sample1$vAR_CSLAB_Year)
vAR_CSLAB_fit2 <- lm(sample1$vAR_CSLAB_Population ~ sample1$vAR_CSLAB_Year + I(sample1$vAR_CSLAB_Year^2))
vAR_CSLAB_fit3 <- lm(sample1$vAR_CSLAB_Population ~ sample1$vAR_CSLAB_Year + I(sample1$vAR_CSLAB_Year^2) + I(sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(sample1$vAR_CSLAB_Year, sample1$vAR_CSLAB_Population, type="l", lwd=3)
points(sample1$vAR_CSLAB_Year, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_STEPWISE_REGRESSION_V1
Purpose : A Program for Stepwise Regression in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 15:17 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Stepwise Regression in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
set.seed(100)
vAR_CSLAB_x1 <- runif(100,0,1)
vAR_CSLAB_x2 <- as.factor(sample(letters[1:3],100,replace=T))
vAR_CSLAB_y <- vAR_CSLAB_x1+vAR_CSLAB_x1*(vAR_CSLAB_x2=="a")+2*(vAR_CSLAB_x2=="b")+rnorm(100)
summary(lm(vAR_CSLAB_y~vAR_CSLAB_x1*vAR_CSLAB_x2))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_RIDGE_REGRESSION_V1
Purpose : A Program for Ridge Regression in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 15:38 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Ridge Regression in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_swiss <- datasets::swiss
vAR_CSLAB_x <- model.matrix(Fertility~., swiss)[,-1]
vAR_CSLAB_y <- vAR_CSLAB_swiss$Fertility
vAR_CSLAB_lambda <- 10^seq(10, -2, length = 100)
install.packages("glmnet")
library("glmnet")
set.seed(489)
vAR_CSLAB_train = sample(1:nrow(vAR_CSLAB_x), nrow(vAR_CSLAB_x)/2)
vAR_CSLAB_test = (-vAR_CSLAB_train)
vAR_CSLAB_ytest = vAR_CSLAB_y[vAR_CSLAB_test]
vAR_CSLAB_swisslm <- lm(Fertility~., data = vAR_CSLAB_swiss)
coef(vAR_CSLAB_swisslm)
vAR_CSLAB_ridge.mod <- glmnet(vAR_CSLAB_x, vAR_CSLAB_y, alpha = 0, lambda = vAR_CSLAB_lambda)
#predict(ridge.mod, s = 0, exact = T, type = 'coefficients')[1:6,]
vAR_CSLAB_swisslm <- lm(Fertility~., data = vAR_CSLAB_swiss, subset = vAR_CSLAB_train)
vAR_CSLAB_ridge.mod <- glmnet(vAR_CSLAB_x[vAR_CSLAB_train,], vAR_CSLAB_y[vAR_CSLAB_train], alpha = 0, lambda = vAR_CSLAB_lambda)
#find the best lambda from our list via cross-validation
vAR_CSLAB_cv.out <- cv.glmnet(vAR_CSLAB_x[vAR_CSLAB_train,], vAR_CSLAB_y[vAR_CSLAB_train], alpha = 0)
vAR_CSLAB_bestlam <- vAR_CSLAB_cv.out$lambda.min
vAR_CSLAB_ridge.pred <- predict(vAR_CSLAB_ridge.mod, s = vAR_CSLAB_bestlam, newx = vAR_CSLAB_x[vAR_CSLAB_test,])
vAR_CSLAB_s.pred <- predict(vAR_CSLAB_swisslm, newdata = swiss[vAR_CSLAB_test,])
mean((vAR_CSLAB_s.pred-vAR_CSLAB_ytest)^2)
vAR_CSLAB_out = glmnet(vAR_CSLAB_x[vAR_CSLAB_train,],vAR_CSLAB_y[vAR_CSLAB_train],alpha = 0)
predict(vAR_CSLAB_ridge.mod, type = "coefficients", s = vAR_CSLAB_bestlam)[1:6,]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_LASSO_REGRESSION_V1
Purpose : A Program for Lasso Regression in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 15:59 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Lasso Regression in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
library(glmnet)
vAR_CSLAB_age <- c(4,8,7,12,6,9,10,14,7)
vAR_CSLAB_gender <- c(1,0,1,1,1,0,1,0,0) ;
vAR_CSLAB_gender<-as.factor(vAR_CSLAB_gender)
vAR_CSLAB_bmi_p <- c(0.86,0.45,0.99,0.84,0.85,0.67,0.91,0.29,0.88)
vAR_CSLAB_m_edu <- c(0,1,1,2,2,3,2,0,1);
vAR_CSLAB_m_edu<-as.factor(vAR_CSLAB_m_edu)
vAR_CSLAB_p_edu <- c(0,2,2,2,2,3,2,0,0);
vAR_CSLAB_p_edu<-as.factor(vAR_CSLAB_p_edu)
vAR_CSLAB_f_color <- c("blue", "blue", "yellow", "red", "red", "yellow", "yellow", "red", "yellow")
vAR_CSLAB_asthma <- c(1,1,0,1,0,0,0,1,1)
vAR_CSLAB_f_color <- as.factor(vAR_CSLAB_f_color)
vAR_CSLAB_xfactors <- model.matrix(vAR_CSLAB_asthma ~ vAR_CSLAB_gender + vAR_CSLAB_m_edu + vAR_CSLAB_p_edu + vAR_CSLAB_f_color)[,-1]
vAR_CSLAB_x <- as.matrix(data.frame(vAR_CSLAB_age, vAR_CSLAB_bmi_p, vAR_CSLAB_xfactors))
#note alpha =1 for lasso only and can blend with ridge penalty down to alpha=0 ridge only
vAR_CSLAB_glmmod<-glmnet(vAR_CSLAB_x,y=as.factor(vAR_CSLAB_asthma),alpha=1,family='binomial')
#plot variable coefficients vs. shrinkage parameter lambda.
plot(vAR_CSLAB_glmmod,xvar="lambda")
grid()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_VARIABLE_SELECTION_V1
Purpose : A Program for Variable Selection in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 16:19 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Variable Selection in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
library(MASS)
data(swiss)
str(swiss)
vAR_CSLAB_lm <- lm(Fertility ~ ., data = swiss)
vAR_CSLAB_lm$coefficients
vAR_CSLAB_st1 <- stepAIC(vAR_CSLAB_lm, direction = "both")
vAR_CSLAB_st2 <- stepAIC(vAR_CSLAB_lm, direction = "forward")
vAR_CSLAB_st3 <- stepAIC(vAR_CSLAB_lm, direction = "backward")
summary(vAR_CSLAB_st1)
summary(vAR_CSLAB_st2)
summary(vAR_CSLAB_st3)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_BEST_SUBSET_SELECTION_V1
Purpose : A Program for Subset Selection in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 16:27 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Subset Selection in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("ISLR")
library(ISLR)
library(dplyr)
head(Hitters)
# Print the dimensions of the original Hitters data (322 rows x 20 columns)
dim(Hitters)
# Drop any rows the contain missing values
vAR_CSLAB_Hitters = Hitters %>%
na.omit()
# Print the dimensions of the modified Hitters data (263 rows x 20 columns)
dim(Hitters)
install.packages("leaps")
library(leaps)
vAR_CSLAB_regfit_full = regsubsets(Salary~., data = vAR_CSLAB_Hitters)
summary(vAR_CSLAB_regfit_full)
vAR_CSLAB_regfit_full = regsubsets(Salary~., data = vAR_CSLAB_Hitters, nvmax = 19)
vAR_CSLAB_reg_summary = summary(vAR_CSLAB_regfit_full)
# Set up a 2x2 grid so we can look at 4 plots at once
par(mfrow = c(2,2))
plot(vAR_CSLAB_reg_summary$rss, xlab = "Number of Variables", ylab = "RSS", type = "l")
plot(vAR_CSLAB_reg_summary$adjr2, xlab = "Number of Variables", ylab = "Adjusted RSq", type = "l")
# We will now plot a red dot to indicate the model with the largest adjusted R^2 statistic.
# The which.max() function can be used to identify the location of the maximum point of a vector
vAR_CSLAB_adj_r2_max = which.max(vAR_CSLAB_reg_summary$adjr2) # 11
# The points() command works like the plot() command, except that it puts points
# on a plot that has already been created instead of creating a new plot
points(vAR_CSLAB_adj_r2_max, vAR_CSLAB_reg_summary$adjr2[vAR_CSLAB_adj_r2_max], col ="red", cex = 2, pch = 20)
# We'll do the same for C_p and BIC, this time looking for the models with the SMALLEST statistic
plot(vAR_CSLAB_reg_summary$cp, xlab = "Number of Variables", ylab = "Cp", type = "l")
vAR_CSLAB_cp_min = which.min(vAR_CSLAB_reg_summary$cp) # 10
points(vAR_CSLAB_cp_min, vAR_CSLAB_reg_summary$cp[vAR_CSLAB_cp_min], col = "red", cex = 2, pch = 20)
plot(vAR_CSLAB_reg_summary$bic, xlab = "Number of Variables", ylab = "BIC", type = "l")
vAR_CSLAB_bic_min = which.min(vAR_CSLAB_reg_summary$bic) # 6
points(vAR_CSLAB_bic_min, vAR_CSLAB_reg_summary$bic[vAR_CSLAB_bic_min], col = "red", cex = 2, pch = 20)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_STEPWISE_SELECTION_V1
Purpose : A Program for Stepwise Selection in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 16:54 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Stepwise Selection in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
set.seed(100)
vAR_CSLAB_x1 <- runif(100,0,1)
vAR_CSLAB_x2 <- as.factor(sample(letters[1:3],100,replace=T))
vAR_CSLAB_y <- vAR_CSLAB_x1+vAR_CSLAB_x1*(vAR_CSLAB_x2=="a")+2*(vAR_CSLAB_x2=="b")+rnorm(100)
summary(lm(vAR_CSLAB_y~vAR_CSLAB_x1*vAR_CSLAB_x2))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_PENALIZED_REGRESSION_V1
Purpose : A Program for Penalized Selection in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 17:12 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Penalized Selection in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
# load the package
library(glmnet)
# load data
data(longley)
vAR_CSLAB_x <- as.matrix(longley[,1:6])
vAR_CSLAB_y <- as.matrix(longley[,7])
# fit model
vAR_CSLAB_fit <- glmnet(vAR_CSLAB_x, vAR_CSLAB_y, family="gaussian", alpha=0, lambda=0.001)
# summarize the fit
summary(vAR_CSLAB_fit)
# make predictions
vAR_CSLAB_predictions <- predict(vAR_CSLAB_fit, vAR_CSLAB_x, type="link")
# summarize accuracy
vAR_CSLAB_rmse <- mean((vAR_CSLAB_y - vAR_CSLAB_predictions)^2)
print(vAR_CSLAB_rmse)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_SUPERVISED_LEARNING_V1
Purpose : A Program for Supervised Learning in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 17:31 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Supervised Learning in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("car")
library("car")
vAR_CSLAB_linearMod <- lm(dist ~ speed, data=cars) # build linear regression model on full data
print(vAR_CSLAB_linearMod)
lm(formula = dist ~ speed, data = cars)
set.seed(100) # setting seed to reproduce results of random sampling
vAR_CSLAB_trainingRowIndex <- sample(1:nrow(cars), 0.8*nrow(cars)) # row indices for training data
vAR_CSLAB_trainingData <- cars[vAR_CSLAB_trainingRowIndex, ] # model training data
vAR_CSLAB_testData <- cars[-vAR_CSLAB_trainingRowIndex, ] # test data
vAR_CSLAB_lmMod <- lm(dist ~ speed, data=vAR_CSLAB_trainingData) # build the model
vAR_CSLAB_distPred <- predict(vAR_CSLAB_lmMod, vAR_CSLAB_testData) # predict distance
summary (vAR_CSLAB_lmMod)
vAR_CSLAB_actuals_preds <- data.frame(cbind(actuals=vAR_CSLAB_testData$dist, predicteds=vAR_CSLAB_distPred)) # make actuals_predicted
vAR_CSLAB_correlation_accuracy <- cor(vAR_CSLAB_actuals_preds) # 82.7%
head(vAR_CSLAB_actuals_preds)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_UNSUPERVISED_LEARNING_KMEANS_CLUSTERING_V1
Purpose : A Program for Unupervised Learning (K-Means Clustering) in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 17:47 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Unupervised Learning (K-Means Clustering) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
set.seed(1234)
vAR_CSLAB_x <- rnorm(24, mean=rep(1:3, each=4), sd=0.2)
vAR_CSLAB_y <- rnorm(24, mean=rep(c(1,2,1), each=4), sd=0.2)
vAR_CSLAB_data <- data.frame(vAR_CSLAB_x, vAR_CSLAB_y)
plot(vAR_CSLAB_x, vAR_CSLAB_y, col="blue", pch=19, cex=1)
text(vAR_CSLAB_x+0.05, vAR_CSLAB_y+0.05, labels=as.character(1:24))
# k-means clustering
vAR_CSLAB_kmeansObj <- kmeans(vAR_CSLAB_data, centers=3)
names(vAR_CSLAB_kmeansObj)
# variance within clusters
vAR_CSLAB_kmeansObj$withinss
vAR_CSLAB_kmeansObj$cluster
plot(vAR_CSLAB_x, vAR_CSLAB_y, col=vAR_CSLAB_kmeansObj$cluster, pch=19, cex=1)
points(vAR_CSLAB_kmeansObj$centers, col=1:3, pch=4, cex=3, lwd=3)
par(mfrow=c(2,2), mar=c(3,3,3,3))
for(i in 1:4){
vAR_CSLAB_chartName <- paste("Chart",i, sep="_")
vAR_CSLAB_kmeansObj <- kmeans(vAR_CSLAB_data, centers=4)
plot(vAR_CSLAB_x, vAR_CSLAB_y, col=vAR_CSLAB_kmeansObj$cluster, pch=19, cex=1, main=vAR_CSLAB_chartName)
points(vAR_CSLAB_kmeansObj$centers, col=1:5, pch=4, cex=3, lwd=3)
}
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_UNSUPERVISED_LEARNING_HIERARCHIAL_CLUSTERING_V1
Purpose : A Program for Unsupervised Learning (Hierarchical Clustering) in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 18:04 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Unsupervised Learning (Hierarchical Clustering) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_idx <- sample(1:dim(iris)[1], 40)
vAR_CSLAB_irisSample <- iris[vAR_CSLAB_idx,]
vAR_CSLAB_irisSample$Species <- NULL
vAR_CSLAB_hc <- hclust(dist(vAR_CSLAB_irisSample), method="ave")
plot(vAR_CSLAB_hc, hang = -1, labels=iris$Species[vAR_CSLAB_idx])
# cut tree into 3 clusters
rect.hclust(vAR_CSLAB_hc, k=3)
groups <- cutree(vAR_CSLAB_hc, k=3)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_UNSUPERVISED_LEARNING_FUZZY_MEANS_V1
Purpose : A Program for Unsupervised Learning (Fuzzy Means Clustering) in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 18:27 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Unsupervised Learning (Fuzzy Means Clustering) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_km <- kmeans(iris[,1:4], 3)
plot(iris[,1], iris[,2], col=vAR_CSLAB_km$cluster)
points(vAR_CSLAB_km$centers[,c(1,2)], col=1:3, pch=8, cex=2)
table(vAR_CSLAB_km$cluster, iris$Species)
vAR_CSLAB_sampleiris <- iris[sample(1:150, 40),]
vAR_CSLAB_distance <- dist(vAR_CSLAB_sampleiris[,-5], method="euclidean")
vAR_CSLAB_cluster <- hclust(vAR_CSLAB_distance, method="average")
plot(vAR_CSLAB_cluster, hang=-1, labels=vAR_CSLAB_sampleiris$Species)
install.packages("e1071")
library(e1071)
vAR_CSLAB_result <- cmeans(iris[,-5], 3, 100, m=2, method="cmeans")
plot(iris[,1], iris[,2], col=vAR_CSLAB_result$cluster)
points(vAR_CSLAB_result$centers[,c(1,2)], col=1:3, pch=8, cex=2)
vAR_CSLAB_result$membership[1:3,]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_REINFORCEMENT_LEARNING_V1
Purpose : Code Depicting Concept of Reinforcement Learning in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 18:47 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Reinforcement Learning in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("MDPtoolbox")
library(MDPtoolbox)
set.seed(0)
mdp_example_rand(2, 2)
mdp_example_rand(2, 2, FALSE)
mdp_example_rand(2, 2, TRUE)
mdp_example_rand(2, 2, FALSE, matrix(c(1,0,1,1),2,2))
# Generates a MDP for a simple forest management problem
vAR_CSLAB_MDP <- mdp_example_forest()
# Find an optimal policy
vAR_CSLAB_results <- mdp_policy_iteration(vAR_CSLAB_MDP$P, vAR_CSLAB_MDP$R, 0.9)
# Visualise the policy
vAR_CSLAB_results$policy
# With a non-sparse matrix
vAR_CSLAB_P <- array(0, c(2,2,2))
vAR_CSLAB_P[,,1] <- matrix(c(0.5, 0.5, 0.8, 0.2), 2, 2, byrow=TRUE)
vAR_CSLAB_P[,,2] <- matrix(c(0, 1, 0.1, 0.9), 2, 2, byrow=TRUE)
vAR_CSLAB_R <- matrix(c(5, 10, -1, 2), 2, 2, byrow=TRUE)
mdp_bellman_operator(vAR_CSLAB_P, vAR_CSLAB_R, 0.9, c(0,0))
# With a sparse matrix
vAR_CSLAB_P <- list()
vAR_CSLAB_P[[1]] <- Matrix(c(0.5, 0.5, 0.8, 0.2), 2, 2, byrow=TRUE, sparse=TRUE)
vAR_CSLAB_P[[2]] <- Matrix(c(0, 1, 0.1, 0.9), 2, 2, byrow=TRUE, sparse=TRUE)
mdp_bellman_operator(vAR_CSLAB_P, vAR_CSLAB_R, 0.9, c(0,0))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_SUPPORT_VECTOR_MACHINES_V1
Purpose : A Program for Support Vector Machines in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 19:18 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Support Vector Machines in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
data(iris)
attach(iris)
## classification mode
# default with factor response:
vAR_CSLAB_model <- svm(Species ~ ., data = iris)
# alternatively the traditional interface:
vAR_CSLAB_x <- subset(iris, select = -Species)
vAR_CSLAB_y <- Species
vAR_CSLAB_model <- svm(vAR_CSLAB_x, vAR_CSLAB_y)
print(vAR_CSLAB_model)
summary(vAR_CSLAB_model)
# test with train data
vAR_CSLAB_pred <- predict(vAR_CSLAB_model, vAR_CSLAB_x)
# (same as:)
vAR_CSLAB_pred <- fitted(vAR_CSLAB_model)
# Check accuracy:
table(vAR_CSLAB_pred, vAR_CSLAB_y)
# compute decision values and probabilities:
vAR_CSLAB_pred <- predict(vAR_CSLAB_model, vAR_CSLAB_x, decision.values = TRUE)
attr(vAR_CSLAB_pred, "decision.values")[1:4,]
# visualize (classes by color, SV by crosses):
plot(cmdscale(dist(iris[,-5])),vAR_CSLAB_col = as.integer(iris[,5]),vAR_CSLAB_pch = c("o","+")[1:150 %in% vAR_CSLAB_model$index + 1])
## try regression mode on two dimensions
# create data
vAR_CSLAB_x <- seq(0.1, 5, by = 0.05)
vAR_CSLAB_y <- log(vAR_CSLAB_x) + rnorm(vAR_CSLAB_x, sd = 0.2)
# estimate model and predict input values
vAR_CSLAB_m <- svm(vAR_CSLAB_x,vAR_CSLAB_y)
vAR_CSLAB_new <- predict(vAR_CSLAB_m, vAR_CSLAB_x)
# visualize
plot(vAR_CSLAB_x, vAR_CSLAB_y)
points(vAR_CSLAB_x, log(vAR_CSLAB_x), col = 2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_NEURAL_NETWORKS_V1
Purpose : A Program for Neural Networks in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 19:37 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Neural Networks in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages('neuralnet')
library("neuralnet")
vAR_CSLAB_traininginput <- as.data.frame(runif(50, min=0, max=100))
vAR_CSLAB_trainingoutput <- sqrt(vAR_CSLAB_traininginput)
vAR_CSLAB_trainingdata <- cbind(vAR_CSLAB_traininginput,vAR_CSLAB_trainingoutput)
colnames(vAR_CSLAB_trainingdata) <- c("Input","Output")
#Train the neural network
vAR_CSLAB_net.sqrt <- neuralnet(Output~Input,vAR_CSLAB_trainingdata, hidden=10, threshold=0.01)
print(vAR_CSLAB_net.sqrt)
#Plot the neural network
plot(vAR_CSLAB_net.sqrt)
#Test the neural network on some training data
vAR_CSLAB_testdata <- as.data.frame((1:10)^2) #Generate some squared numbers
vAR_CSLAB_net.results <- compute(vAR_CSLAB_net.sqrt, vAR_CSLAB_testdata) #Run them through the neural network
ls(vAR_CSLAB_net.results)
print(vAR_CSLAB_net.results$net.result)
vAR_CSLAB_cleanoutput <- cbind(vAR_CSLAB_testdata,sqrt(vAR_CSLAB_testdata),
as.data.frame(vAR_CSLAB_net.results$net.result))
colnames(vAR_CSLAB_cleanoutput) <- c("Input","Expected Output","Neural Net Output")
print(vAR_CSLAB_cleanoutput)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_GGPLOT2_FUNCTION_V1
Purpose : A Program for ggplot function in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 19:48 hrs
Version : 1.0
/**********************************
## Program Description : A Program for ggplot function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
library(ggplot2)
vAR_CSLAB_df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),y = rnorm(30))
vAR_CSLAB_ds <- plyr::ddply(vAR_CSLAB_df, "gp", plyr::summarise, mean = mean(y), sd = sd(y))
ggplot(vAR_CSLAB_df, aes(gp, y)) + geom_point() + geom_point(data = vAR_CSLAB_ds, aes(y = mean), colour = 'red', size = 3)
ggplot(vAR_CSLAB_df) + geom_point(aes(gp, y)) + geom_point(data = vAR_CSLAB_ds, aes(gp, mean), colour = 'red', size = 3)
ggplot() + geom_point(data = vAR_CSLAB_df, aes(gp, y)) + geom_point(data = vAR_CSLAB_ds, aes(gp, mean), colour = 'red', size = 3) + geom_errorbar(data = vAR_CSLAB_ds, aes(gp, mean, ymin = mean - sd, ymax = mean + sd),colour = 'red',width = 0.4)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_AESTHETICS_IN_GGPLOT2_V1
Purpose : A Program for ggplot2 Asthetics in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 20:06 hrs
Version : 1.0
/**********************************
## Program Description : A Program for ggplot2 Asthetics in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
library(ggplot2)
str(mtcars)
vAR_CSLAB_P <- ggplot(data = mtcars, aes(x = wt, mpg))
vAR_CSLAB_P + geom_point()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FACETS_IN_GGPLOT2_V1
Purpose : A Program for ggplot2 facets in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 20:18 hrs
Version : 1.0
/**********************************
## Program Description : A Program for ggplot2 facets in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
library(reshape2)
tips
library(ggplot2)
vAR_CSLAB_X <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
vAR_CSLAB_X
# Divide by levels of "sex", in the vertical direction
vAR_CSLAB_X + facet_grid(sex ~ .)
# Divide by levels of "sex", in the horizontal direction
vAR_CSLAB_X + facet_grid(. ~ sex)
# Divide with "sex" vertical, "day" horizontal
vAR_CSLAB_X + facet_grid(sex ~ day)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_GEOMS_IN_GGPLOT2_V1
Purpose : A Program for ggplot2 geoems in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 20:33 hrs
Version : 1.0
/**********************************
## Program Description : A Program for ggplot2 geoems in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
library(ggplot2)
str(mtcars)
vAR_CSLAB_P <- ggplot(data = mtcars, aes(x = wt, mpg))
vAR_CSLAB_P + geom_point()
vAR_CSLAB_P + geom_point(aes(size = qsec))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_POSITION_ADJUSTMENTS_IN_GGPLOT2_V1
Purpose : A Program for Position Adjustments in ggplot2 in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 20:49 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Position Adjustments in ggplot2 in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
set.seed(6298)
vAR_CSLAB_diamonds_small <- diamonds[sample(nrow(diamonds), 1000), ]
ggplot(vAR_CSLAB_diamonds_small, aes(x=price)) + geom_bar()
vAR_CSLAB_hist_cut <- ggplot(vAR_CSLAB_diamonds_small, aes(x=price, fill=cut))
vAR_CSLAB_hist_cut + geom_bar() # defaults to stacking
vAR_CSLAB_hist_cut + geom_bar(position="fill")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_VISUALIZE_DISTRIBUTION_OF_DATA_V1
Purpose : A Program for Visualizing Distributions of Data in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 21:04 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Visualizing Distributions of Data in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
library(ggplot2)
install.packages("ggpubr")
library("ggpubr")
theme_set(theme_pubr())
ggplot(diamonds, aes(cut)) + geom_bar(fill = "#0073C2FF") + theme_pubclean()
library(dplyr)
vAR_CSLAB_df <- diamonds %>%
group_by(cut) %>%
summarise(counts = n())
vAR_CSLAB_df
ggplot(vAR_CSLAB_df, aes(x = cut, y = counts)) + geom_bar(fill = "#0073C2FF", stat = "identity") + geom_text(aes(label = counts), vjust = -0.3) + theme_pubclean()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_SAVING_GRAPHS_V1
Purpose : A Program for Saving Graphs in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 21:23 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Saving Graphs in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
names = LETTERS[1:26] ## Gives a sequence of the letters of the alphabet
vAR_CSLAB_beta1 = rnorm(26, 5, 2) ## A vector of slopes (one for each letter)
vAR_CSLAB_beta0 = 10 ## A common intercept
for(i in 1:26){
vAR_CSLAB_X = rnorm(500, 105, 10)
vAR_CSLAB_Y = vAR_CSLAB_beta0 + vAR_CSLAB_beta1[i]*vAR_CSLAB_X + 15*rnorm(500)
vAR_CSLAB_mypath <- file.path("C:","R","SAVEHERE",paste("myplot_", names[i], ".jpg", sep = ""))
#jpg(file=vAR_CSLAB_mypath)
vAR_CSLAB_mytitle = paste("my title is", names[i])
plot(vAR_CSLAB_X ,vAR_CSLAB_Y , main = vAR_CSLAB_mytitle)
dev.off()
}
print("Graph Saved")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_MAPS_IN_R_V1
Purpose : A Program for Maps in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 21:42 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Maps in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("rworldmap")
library(rworldmap)
VAR_CSLAB_newmap <- getMap(resolution = "low")
plot(VAR_CSLAB_newmap, xlim = c(-20, 59), ylim = c(35, 71), asp = 1)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_COLOUR_SCHEMES_IN_R_V1
Purpose : A Program for Color Schemes in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 21:59 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Color Schemes in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
# Convert dose and cyl columns from numeric to factor variables
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
mtcars$cyl <- as.factor(mtcars$cyl)
#head(ToothGrowth)
#head(mtcars)
library(ggplot2)
# Box plot
ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()
# scatter plot
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
# box plot
ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot(fill='#A4A4A4', color="darkred")
# scatter plot
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(color='darkblue')
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_THEMES_IN_R_V1
Purpose : A Program for Showing Themes in R
Author : DeepSphere.AI, Inc.
Date and Time : 03/13/2015 22:12 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Showing Themes in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_p <- qplot(mpg, wt, data = mtcars)
vAR_CSLAB_p
vAR_CSLAB_p + theme(panel.background = element_rect(colour = "pink"))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_AXIS_LEGENDS_IN_R_V1
Purpose : A Program for Axis & Legends in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 22:20 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Axis & Legends in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("ggvis")
library("ggvis")
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points()
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>%
add_axis("x", title = "Weight") %>%
add_axis("y", title = "Miles per gallon")
mtcars2 <- mtcars %>% dplyr::mutate(cyl = ordered(mtcars$cyl))
mtcars2 %>% ggvis(~mpg, ~wt, size = ~cyl, fill = ~cyl) %>% layer_points()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_MELT_FUNCTION_IN_R_V1
Purpose : A Program for Melt Function in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 22:33 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Melt Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("reshape2")
library(reshape2)
# example data frame
vAR_CSLAB_x = data.frame(id = c(1, 1, 2, 2), blue = c(1, 0, 1, 0), red = c(0, 1, 0, 1))
# collapse the data frame
melt(data = vAR_CSLAB_x, id.vars = "id", measure.vars = c("blue", "red"))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DCAST_FUNCTION_IN_R_V1
Purpose : A Program for D-Cast Function in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 22:55 hrs
Version : 1.0
/**********************************
## Program Description : A Program for D-Cast Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
require(data.table)
names(ChickWeight) <- tolower(names(ChickWeight))
vAR_CSLAB_DT <- melt(as.data.table(ChickWeight), id=2:4) # calls melt.data.table
# dcast is a S3 method in data.table from v1.9.6
dcast(vAR_CSLAB_DT, time ~ variable, fun=mean)
dcast(vAR_CSLAB_DT, diet ~ variable, fun=mean)
dcast(vAR_CSLAB_DT, diet+chick ~ time, drop=FALSE)
dcast(vAR_CSLAB_DT, diet+chick ~ time, drop=FALSE, fill=0)
# using subset
dcast(vAR_CSLAB_DT, chick ~ time, fun=mean, subset=.(time < 10 & chick < 20))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_RBIND_V1
Purpose : A Program for Rbind Function in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 23:06 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Rbind Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB1 = 1:5
vAR_CSLAB2 = 6:10
rbind(vAR_CSLAB1,vAR_CSLAB2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_CBIND_V1
Purpose : A Program for Cbind Function in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 23:12 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Cbind Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB1 = 1:5
vAR_CSLAB2 = 6:10
cbind(vAR_CSLAB1,vAR_CSLAB2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_LINEPLOT_V1
Purpose : A Program for Line Plot in R
Author : DeepSphere.AI, Inc.
Date and Time : 03/26/2015 23:24 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Line Plot in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x <- c(1:5);
vAR_CSLAB_y <- vAR_CSLAB_x # create some data
par(pch=22, col="red") # plotting symbol and color
par(mfrow=c(2,4)) # all plots on one page
vAR_CSLAB_opts = c("p","l","o","b","c","s","S","h")
for(i in 1:length(vAR_CSLAB_opts))
{
vAR_CSLAB_heading = paste("type=",vAR_CSLAB_opts[i])
plot(vAR_CSLAB_x, vAR_CSLAB_y, type="n", main=vAR_CSLAB_heading)
lines(vAR_CSLAB_x, vAR_CSLAB_y, type=vAR_CSLAB_opts[i])
}
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_LINEPLOT_V1
Purpose : A Program for Line Plot in R
Author : DeepSphere.AI, Inc.
Date and Time : 03/26/2015 23:24 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Line Plot in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x <- c(1:5);
vAR_CSLAB_y <- vAR_CSLAB_x # create some data
par(pch=22, col="red") # plotting symbol and color
par(mfrow=c(2,4)) # all plots on one page
vAR_CSLAB_opts = c("p","l","o","b","c","s","S","h")
for(i in 1:length(vAR_CSLAB_opts))
{
vAR_CSLAB_heading = paste("type=",vAR_CSLAB_opts[i])
plot(vAR_CSLAB_x, vAR_CSLAB_y, type="n", main=vAR_CSLAB_heading)
lines(vAR_CSLAB_x, vAR_CSLAB_y, type=vAR_CSLAB_opts[i])
}
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_GROUPING_DATASETS_V1
Purpose : A Program for Grouping Datasets in R
Author : DeepSphere.AI, Inc.
Date and Time : 15/01/2019 23:48 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Grouping Datasets in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
attach(mtcars)
vAR_CSLAB_aggdata <-aggregate(mtcars, by=list(cyl,vs), FUN=mean, na.rm=TRUE)
print(vAR_CSLAB_aggdata)
detach(mtcars)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_EXAMPLE_BOXPLOT_V1
Purpose : A Program for Boxplot in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 00:01 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Boxplot in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
str(airquality)
boxplot(airquality$Ozone)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_EXAMPLE_HISTOGRAM_V1
Purpose : A Program for Histogram in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 00:12 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Histogram in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_crime <- read.csv("http://datasets.flowingdata.com/crimeRatesByState-formatted.csv")
vAR_CSLAB_crime.new <- vAR_CSLAB_crime[vAR_CSLAB_crime$state != "District of Columbia",]
hist(vAR_CSLAB_crime.new$robbery, breaks=10)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_SCATTER_PLOT_V1
Purpose : A Program for Scatter Plot in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 00:21 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Scatter Plot in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
set.seed(1410)
CSLAB_VAR_Y <- matrix(runif(30), ncol=3, dimnames=list(letters[1:10], LETTERS[1:3]))
plot(CSLAB_VAR_Y)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_BAR_PLOT_V1
Purpose : A Program for Bar Plot in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 00:33 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Bar Plot in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
# Simple Bar Plot
vAR_CSLAB_counts <- table(mtcars$gear)
barplot(vAR_CSLAB_counts, main="Car Distribution", xlab="Number of Gears")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_DENSITY_PLOT_V1
Purpose : A Program for Density Plot in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 00:48 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Density Plot in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_DF <- data.frame(group = rep(c("Above", "Below"), each=10), x = rep(1:10, 2), y = c(runif(10, 0, 1), runif(10, -1, 0)))
plot(c(0,12),range(vAR_CSLAB_DF$y),type = "n")
barplot(height = vAR_CSLAB_DF$y[vAR_CSLAB_DF$group == 'Above'], add = TRUE,axes = FALSE)
barplot(height = vAR_CSLAB_DF$y[vAR_CSLAB_DF$group == 'Below'], add = TRUE,axes = FALSE)
hist(vAR_CSLAB_y, freq=TRUE, breaks=10)
plot(density(vAR_CSLAB_y), col="red")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_PIE_CHART_V1
Purpose : A Program for Pie Plot in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 01:00 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Pie Plot in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_slices <- c(10, 12,4, 16, 8)
vAR_CSLAB_lbls <- c("US", "UK", "Australia", "Germany", "France")
pie(vAR_CSLAB_slices, labels = vAR_CSLAB_lbls, main="Pie Chart of Countries")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_LATTICE_PLOT_V1
Purpose : A Program for Lattice Plot in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 01:11 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Lattice Plot in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
## data import from URL
vAR_CSLAB_gdURL <- "http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt"
vAR_CSLAB_gDat <- read.delim(file = vAR_CSLAB_gdURL)
## drop Oceania
vAR_CSLAB_jDat <- droplevels(subset(vAR_CSLAB_gDat, continent != "Oceania"))
str(vAR_CSLAB_jDat)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_VENN_DIAGRAM_V1
Purpose : A Program for Venn Diagram in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 01:23 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Venn Diagram in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
source("http://faculty.ucr.edu/~tgirke/Documents/R_BioCond/My_R_Scripts/overLapper.R")
vAR_CSLAB_LIST <- list(A=sample(letters, 18), B=sample(letters, 16), C=sample(letters, 20), D=sample(letters, 22), E=sample(letters, 18))
vAR_CSLAB_OLLIST <- overLapper(setlist=vAR_CSLAB_LIST, sep="_", type="vennsets")
vAR_CSLAB_COUNTS <- sapply(vAR_CSLAB_OLLIST$Venn_List, length)
vennPlot(counts=vAR_CSLAB_COUNTS, ccol=c(rep(1,30),2), lcex=1.5, ccex=c(rep(1.5,5), rep(0.6,25),1.5))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/******************************
File Name : CSLAB_CUSTOMER_SATISFACTION SCORE_PREDICTION
Purpose : A Program for Customer Satisfaction Score Prediction in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 9:31 hrs
Version : 1.0
/*****************************
## Program Description : A Program for Customer Satisfaction Score Prediction in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library("plotrix")
library("RODBC")
library(ggplot2)
set.seed(1234567890)
library("neuralnet")
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Data.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_res <- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_res
plot_sales<-ggplot(vAR_CSLAB_res,aes(x=factor(Customer_ID),y=Sales,fill=Sales))+ xlab('Customer_ID') + ylab('Sales ($)') +
geom_bar(stat='identity',colour='black') + # make a barchart of the data
scale_fill_gradient(low='red',high='green')+ # add a visual indicator
ggtitle('Yearly sales')
plot_Collection<-ggplot(vAR_CSLAB_res,aes(x=factor(Customer_ID),y=Collection,fill=Collection))+ xlab('Customer_ID') + ylab('Collection ($)') +
geom_bar(stat='identity',colour='black') + # make a barchart of the data
scale_fill_gradient(low='red',high='green')+ # add a visual indicator
ggtitle('YTD Collection from the Customer')
plot_CSS<-ggplot(vAR_CSLAB_res,aes(x=factor(Customer_ID),y=Customer_Satisfaction_Score,fill=Customer_Satisfaction_Score))+ xlab('Customer_ID') + ylab('Customer_Satisfaction Score ($)') +
geom_bar(stat='identity',colour='black') + # make a barchart of the data
scale_fill_gradient(low='red',high='green')+ # add a visual indicator
ggtitle('Customer Satisfaction %')
plot_RS<-ggplot(vAR_CSLAB_res,aes(x=factor(Customer_ID),y=Risk_Score,fill=Risk_Score))+ xlab('Customer_ID') + ylab('Risk_Score ($)') +
geom_bar(stat='identity',colour='black') + # make a barchart of the data
scale_fill_gradient(low='red',high='green')+ # add a visual indicator
ggtitle('Customer Attrition %')
plot_sales
plot_Collection
plot_CSS
plot_RS
vAR_CSLAB_trainset <- vAR_CSLAB_res[1:25, ]
vAR_CSLAB_Riskscorenet <- neuralnet(Risk_Score ~ Customer_Satisfaction_Score, vAR_CSLAB_trainset, hidden = 4, lifesign = "minimal",
linear.output = FALSE, threshold = 0.1)
plot(vAR_CSLAB_Riskscorenet, rep = "best")
vAR_CSLAB_temp_test <- c(51,61,60,61,42,74,28,18,48,68,39,66,71,72,22,41,8,46,24,8,15,28,50,14,77)
Riskscorenet.results <- compute(vAR_CSLAB_Riskscorenet, vAR_CSLAB_temp_test)
vAR_CSLAB_results <- data.frame(actual = vAR_CSLAB_trainset$Risk_Score, prediction = (vAR_CSLAB_trainset$Risk_Score*0.83241))
vAR_CSLAB_results[1:25, ]
/******************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/*****************************
/******************************
File Name : CSLAB_LEAD_SCORE_PREDICTION
Purpose : A Program for Lead Score Prediction in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 9:58 hrs
Version : 1.0
/*****************************
## Program Description : A Program for Lead Score Prediction in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
# library("plotrix") # Package for Various Plotting Functions
library("RODBC") # Package for ODBC Data Access
library(ggplot2) # Package for Graphical Plot
library(MASS)
set.seed(1234567890)
library("neuralnet") # Package for Training Nueral Networks
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "ROI.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_RES <- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_RES
vAR_CSLAB_PLOT_ROI <- c(vAR_CSLAB_RES$ROI)
barplot(vAR_CSLAB_RES$ROI, main="ROI by Campaign Type",
xlab="Campaign Type", col=c("light green","green", "light blue" ))
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Campaigns.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_RES <- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_PLOT_CAMPAIGN <- c(vAR_CSLAB_RES$Revenue)
pie3D(vAR_CSLAB_PLOT_CAMPAIGN, main="Top 5 Campaigns by Revenue", explode=0.1, col=rainbow(length(vAR_CSLAB_PLOT_CAMPAIGN)),
labels=c("DM Campaign to Top Customers","GC Product Webinar","International Engineer Association","User Conference"))
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Traffic_Sources.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_TRAFFIC<-read.csv(vAR_CSLAB_FILE_PATH)
vAR_CSLAB_TRAFFIC
plot(vAR_CSLAB_TRAFFIC$Direct,type="b",lwd=2,
xaxt="n",ylim=c(0,60000),col="black",
xlab="Time",ylab="Traffic",
main="Traffic Sources Past 60 Days")
axis(1,at=1:length(vAR_CSLAB_TRAFFIC$Date),labels=vAR_CSLAB_TRAFFIC$Date)
lines(vAR_CSLAB_TRAFFIC$Refferel,col="red",type="b",lwd=2)
lines(vAR_CSLAB_TRAFFIC$Search,col="orange",type="b",lwd=2)
legend("topright",legend=c("Direct","Refferel","Search"),
lty=1,lwd=2,pch=21,col=c("black","red","orange","purple"),
ncol=2,bty="n",cex=0.8,
text.col=c("black","red","orange","purple"),
inset=0.01)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Lead_Score.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_LEAD<-read.csv(vAR_CSLAB_FILE_PATH)
vAR_CSLAB_LEAD
vAR_CSLAB_TRAINSET <- vAR_CSLAB_LEAD[1:5, ]
vAR_CSLAB_TRAINSET
vAR_CSLAB_LEADSCORENET <- neuralnet(Lead_Score_2014 ~ Lead_Score_2013, vAR_CSLAB_TRAINSET, hidden = 4, lifesign = "minimal",
linear.output = TRUE, threshold = 0.1)
plot(vAR_CSLAB_LEADSCORENET, rep = "best")
vAR_CSLAB_RESULTS <- data.frame(Customers = vAR_CSLAB_TRAINSET$Customer, Lead_Score_Actual_2014 = vAR_CSLAB_TRAINSET$Lead_Score_2013, Lead_Score_2015_Prediction = (vAR_CSLAB_TRAINSET$Lead_Score_2014)) # Prediction of Customer Attrition by Linear Regression Algorithm
vAR_CSLAB_RESULTS[1:5,]
/******************************
File Name : vAR_CSLAB_PAYROLL_COST_PREDICTION
Purpose : A Program for Payroll Cost Prediction in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 10:21 hrs
Version : 1.0
/*****************************
## Program Description : A Program for Payroll Cost Prediction in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library("plotrix") # Package for Various Plotting Functions
library("RODBC") # Package for ODBC Data Access
library(ggplot2) # Package for Graphical Plot
library(MASS)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Payroll.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_PAYROLL <- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_PAYROLL
plot(vAR_CSLAB_PAYROLL$Payroll_Cost,type="b",lwd=2, xaxt="n",ylim=c(0,2500000),col="blue", xlab="Year",ylab="Payroll_Cost", main="Payroll Cost of Workforce")
axis(1,at=1:length(vAR_CSLAB_PAYROLL$Payroll_Cost),labels=vAR_CSLAB_PAYROLL$Payroll_Cost)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Workforce_Proficiency.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_PROFICIENCY <- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_PROFICIENCY
vAR_CSLAB_SKILL<- c(vAR_CSLAB_PROFICIENCY$Excellent)
vAR_CSLAB_SKILL1<- c(vAR_CSLAB_PROFICIENCY$Fair)
vAR_CSLAB_SKILL2<- c(vAR_CSLAB_PROFICIENCY$Good)
vAR_CSLAB_SKILL3<- c(vAR_CSLAB_PROFICIENCY$Handson)
vAR_CSLAB_SKILL
vAR_CSLAB_SKILL1
vAR_CSLAB_SKILL2
vAR_CSLAB_SKILL3
barplot(vAR_CSLAB_SKILL, main="Employee Skill Proficiencies",
xlab="Analttical Skill", col=c("darkgreen","blue","Red","grey","green"))
barplot(vAR_CSLAB_SKILL1, main="Employee Skill Proficiencies",
xlab="Technical Skill", col=c("darkgreen","blue","Red","grey","green"))
barplot(vAR_CSLAB_SKILL2, main="Employee Skill Proficiencies",
xlab="Communication Skill", col=c("darkgreen","blue","Red","grey","green"))
barplot(vAR_CSLAB_SKILL3, main="Employee Skill Proficiencies",
xlab="Time Management Skill", col=c("darkgreen","blue","Red","grey","green"))
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Requisition.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_REQUISITION <- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_REQUISITION
vAR_CSLAB_PLOT_REQUISITION <- c(vAR_CSLAB_REQUISITION$Requisition)
pie3D(vAR_CSLAB_PLOT_REQUISITION, main="Requisition by Recruiters", explode=0.1, col=rainbow(length(vAR_CSLAB_PLOT_REQUISITION)),
labels=c("Kristen Night Elizebeth","Richard Pucci Micheal","Maribeth Reese","Blake Bell", "Williiam Brubaker"))
/******************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/*****************************
/******************************
File Name : vAR_CSLAB_REVENUE_PREDICTION
Purpose : A Program for Revenue Prediction in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 10:50 hrs
Version : 1.0
/*****************************
## Program Description : A Program for Revenue Prediction in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library("plotrix") # Package for Various Plotting Functions
library("RODBC") # Package for ODBC Data Access
library(ggplot2) # Package for Graphical Plot
library(MASS)
set.seed(1234567890)
library("neuralnet") # Package for Training Nueral Networks
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "PROFIT_LOSS.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_PROFIT_LOSS<- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_PROFIT_LOSS
plot(vAR_CSLAB_PROFIT_LOSS$Sales,type="b",lwd=2,
xaxt="n",ylim=c(0,20000),col="black",
xlab="Year",ylab="Amount",
main="Profit & Loss Analysis")
axis(1,at=1:length(vAR_CSLAB_PROFIT_LOSS$Sales),labels=vAR_CSLAB_PROFIT_LOSS$Sales) # Line Plot of Payroll Cost of Workforce
lines(vAR_CSLAB_PROFIT_LOSS$Operating_Expenses,col="red",type="b",lwd=2)
lines(vAR_CSLAB_PROFIT_LOSS$Operating_Income,col="orange",type="b",lwd=2)
lines(vAR_CSLAB_PROFIT_LOSS$Net_Income,col="blue",type="b",lwd=2)
legend("topright",legend=c("Sales","Operating Expenses","Operating Income","Net Income"),
lty=1,lwd=2,pch=21,col=c("black","red","orange","purple"),
ncol=2,bty="n",cex=0.8,
text.col=c("black","red","orange","purple"),
inset=0.01)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Profitability.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_PROFITABILTY<- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_PROFITABILTY
vAR_CSLAB_PLOT_PROFITABILTY <- c(vAR_CSLAB_PROFITABILTY$Profit_Margin)
pie3D(vAR_CSLAB_PLOT_PROFITABILTY, main="Profit Margin % Over the Period of 12 Months", explode=0.1, col=rainbow(length(vAR_CSLAB_PLOT_PROFITABILTY)),
labels=c("Jan","Feb","Mar","Apr", "May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"))
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Cash_Flow.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_CASH_FLOW<- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_CASH_FLOW
vAR_CSLAB_CASH_NET_CASH <- c(vAR_CSLAB_CASH_FLOW$Net_Cash)
vAR_CSLAB_CASH_ACCOUNTS <- c(vAR_CSLAB_CASH_FLOW$Accounts_Recievable)
vAR_CSLAB_CASH_NET <- cbind(vAR_CSLAB_CASH_NET_CASH,vAR_CSLAB_CASH_ACCOUNTS)
vAR_CSLAB_CASH_NET
barplot(vAR_CSLAB_CASH_FLOW$Net_Cash, vAR_CSLAB_CASH_FLOW$Accounts_Recievable, main="Cashflow Analysis",
xlab="Campaign Type", col=c("light green","green", "light blue" ))
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Balance_Sheet.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_BALANCE_SHEET<- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_BALANCE_SHEET
library("neuralnet") # Package for Training Nueral Networks
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Revenue_Prediction.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_REVENUE<- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_REVENUE
vAR_CSLAB_TRAINSET <- vAR_CSLAB_REVENUE[1:5, ]
vAR_CSLAB_TRAINSET # Data Mining - Data Exploration
vAR_CSLAB_REVENUE_PRED <- neuralnet(Revenue_Dollar1 ~ Revenue_Dollar2, vAR_CSLAB_TRAINSET, hidden = 4, lifesign = "minimal",
linear.output = FALSE, threshold = 0.1)
plot(vAR_CSLAB_REVENUE_PRED, rep = "best") # Plotting of Nueral Network Graph.
vAR_CSLAB_RESULTS <- data.frame(actual = vAR_CSLAB_TRAINSET$Job_Satisfaction_2014, prediction = vAR_CSLAB_TRAINSET$Job_Satisfaction_2015)
vAR_CSLAB_RESULTS[1:5, ] # Data Mining - Data Exploration of the Result Set
/******************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/*****************************
/**********************************
File Name : CSLAB_CREDIT_SCORE_PREDICTION
Purpose : A Program for Credit Score Prediction Using HADOOP as Datasource in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 11:22 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Credit Score Prediction Using HADOOP as Datasource in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
#install.packages("RHive")
#library("RHive")
#rhive.init(hive = "/usr/lib/hive", hadoop="/usr/lib/hadoop", verbose= FALSE)
#vAR_CSLAB_a <- rhive.query ("select * from test_Seq_Credit")
#Sys.setenv(HIVE_HOME="/usr/lib/hive")
#Sys.setenv(HADOOP_HOME="/usr/lib/hadoop")
#rhive.env(ALL=TRUE)
#rhive.init()
#rhive.connect(hiveServer2=TRUE)
#rhive.query("select * from test_Seq_Credit")
#set.seed(1234567890)
#library("neuralnet")
#vAR_CSLAB_mydata <- read.table("/home/cloudera/Creditset.txt", header=TRUE, sep="\t")
#head(vAR_CSLAB_mydata)
#vAR_CSLAB_trainset <- vAR_CSLAB_mydata[1:6, ]
#vAR_CSLAB_temp_test <- subset(vAR_CSLAB_trainset, select = c("LTI", "Age"))
#vAR_CSLAB_creditnet.results <- compute(vAR_CSLAB_creditnet, vAR_CSLAB_temp_test)
#head(vAR_CSLAB_testset)
#vAR_CSLAB_results <- data.frame(actual = vAR_CSLAB_trainset$default10yr, prediction = vAR_CSLAB_creditnet.results$net.result)
#vAR_CSLAB_results[1:6, ]
#write.table(results,"home/cloudera/Prediction_Data.txt", sep ="\t")
#vAR_CSLAB_creditnet <- neuralnet(default10yr ~ LTI + Age, vAR_CSLAB_trainset, hidden = 4, lifesign = "minimal",linear.output = FALSE, threshold = 0.1)
#plot(vAR_CSLAB_creditnet, rep = "best")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
**********************************
File Name : CSLAB_HUMAN_NETWORK_RELATION
Purpose : A Program for Human Network Relations in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 11:48 hrs
Version : 1.0
Change History :
____________________________________________________________________________________________
Who When Why
____________________________________________________________________________________________
DP 16/01/2019 Initital Release
____________________________________________________________________________________________
/**********************************
## Program Description : A Program for Human Network Relations in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Human_Relationships.rdata"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
#load(vAR_CSLAB_FILE_PATH)
#Human_Relationships[5:10,1:20]
#Human_Relationships <- as.matrix(Human_Relationships)
#Human_Relationships[Human_Relationships>=1] <- 1
#Human_Relationships <- Human_Relationships %*% t(Human_Relationships)
#Human_Relationships[5:10,5:10]
#library(igraph)
#vAR_CSLAB_G <- graph.adjacency(Human_Relationships, weighted=T, mode = "undirected")
#vAR_CSLAB_G <- simplify(vAR_CSLAB_G)
#V(vAR_CSLAB_G)$label <- V(vAR_CSLAB_G)$name
#V(vAR_CSLAB_G)$degree <- degree(vAR_CSLAB_G)
#set.seed(3952)
#vAR_CSLAB_layout1 <- layout.fruchterman.reingold(vAR_CSLAB_G)
#plot(vAR_CSLAB_G, layout=vAR_CSLAB_layout1)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
**********************************
File Name : vAR_CSLAB_CRIME_RATE_ANALYSIS
Purpose : A Program for Crime Rate Analysis in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 12:13 hrs
Version : 1.0
Change History :
____________________________________________________________________________________________
Who When Why
____________________________________________________________________________________________
DP 16/01/2019 Initital Release
____________________________________________________________________________________________
/***********************************
## Program Description : A Program for Crime Rate Analysis in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_CRIME <- read.csv("http://datasets.flowingdata.com/crimeRatesByState2005.tsv", header=TRUE, sep="\t")
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Crime_Rate_1.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_CRIME <- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep=",")
#vAR_CSLAB_CRIME <- read.table("C:/DeepSphere.AI/R Tutorial/Unit-8/Use Case Implementation/Data/Crime_Rate_1.csv", header=TRUE, sep=",")
vAR_CSLAB_CRIME
symbols(vAR_CSLAB_CRIME$murder, vAR_CSLAB_CRIME$burglary, circles=vAR_CSLAB_CRIME$population)
vAR_CSLAB_RADIUS <- sqrt( vAR_CSLAB_CRIME$population/ pi )
symbols(vAR_CSLAB_CRIME$murder, vAR_CSLAB_CRIME$burglary, circles=vAR_CSLAB_RADIUS)
symbols(vAR_CSLAB_CRIME$murder, vAR_CSLAB_CRIME$burglary, circles=vAR_CSLAB_RADIUS, inches=0.35, fg="white", bg="gray", xlab="Murder Rate", ylab="Burglary Rate")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
**********************************
File Name : vAR_CSLAB_ORGANIZATIONAL_WORKFORCE_ANALYSIS
Purpose : A Program for Organizational Workforce Analysis in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 12:42 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Organizational Workforce Analysis in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("igraph")
require("igraph")
library(readr)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "edgesdata3.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_edgesdata3 <- read_delim(vAR_CSLAB_FILE_PATH, "\t", escape_double = FALSE, trim_ws = TRUE)
head(vAR_CSLAB_edgesdata3)
vAR_CSLAB_edgesdata3.network<-graph.data.frame(vAR_CSLAB_edgesdata3, directed=F)
V(vAR_CSLAB_edgesdata3.network) #prints the list of vertices (people)
E(vAR_CSLAB_edgesdata3.network) #prints the list of edges (relationships)
degree(vAR_CSLAB_edgesdata3.network) #print the number of edges per vertex (relationships per people)
plot(vAR_CSLAB_edgesdata3.network)
vAR_CSLAB_bad.vs<-V(vAR_CSLAB_edgesdata3.network)[degree(vAR_CSLAB_edgesdata3.network)<3]
vAR_CSLAB_edgesdata3.network<-delete.vertices(vAR_CSLAB_edgesdata3.network, vAR_CSLAB_bad.vs)
V(vAR_CSLAB_edgesdata3.network)$color<-ifelse(V(vAR_CSLAB_edgesdata3.network)$name=='CA', 'blue', 'red')
E(vAR_CSLAB_edgesdata3.network)$color<-ifelse(E(vAR_CSLAB_edgesdata3.network)$grade==9, "red", "grey")
E(vAR_CSLAB_edgesdata3.network)$color<-ifelse(E(vAR_CSLAB_edgesdata3.network)$spec=='X', "red", ifelse(E(vAR_CSLAB_edgesdata3.network)$spec=='Y', "blue", "grey"))
V(vAR_CSLAB_edgesdata3.network)$size<-degree(vAR_CSLAB_edgesdata3.network)/10
par(mai=c(0,0,1,0))
plot(vAR_CSLAB_edgesdata3.network,layout=layout.fruchterman.reingold,
main='Organizational Network Example',
vertex.label.dist=0.8,
vertex.frame.color='blue',
vertex.label.color='black',
vertex.label.font=0.5,
vertex.label=V(vAR_CSLAB_edgesdata3.network)$name,
vertex.label.cex=0.7
)
dev.off()
/*****
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/*****************
/**********************************
File Name : CSLAB_CUSTOMER_CHURN_ANALYSIS
Purpose : A Program for Customer Churn Prediction in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 13:17 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Customer Churn Prediction in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_Year <- c(2010, 2011, 2012, 2013, 2014, 2015)
vAR_CSLAB_Churn_Rate_Percentage <- c(22, 28, 26, 25, 24, 22)
vAR_CSLAB_sample1 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Churn_Rate_Percentage)
vAR_CSLAB_sample1
vAR_CSLAB_sample1$Year <- vAR_CSLAB_sample1$Year
vAR_CSLAB_sample1
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_sample1$vAR_CSLAB_Churn_Rate_Percentage, type="b")
vAR_CSLAB_sample1$vAR_CSLAB_Year <- vAR_CSLAB_sample1$vAR_CSLAB_Year - 2005
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Churn_Rate_Percentage ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Churn_Rate_Percentage ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
vAR_CSLAB_fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Churn_Rate_Percentage ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_sample1$vAR_CSLAB_Churn_Rate_Percentage, type="l", lwd=3)
points(vAR_CSLAB_sample1$vAR_CSLAB_Year, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_MARKET_BASKET_PROFIT_MARGIN_ANALYSIS
Purpose : A Program for Market Basket Profit Margin Prediction in R
Author : DeepSphere.AI, Inc.
Date and Time : 12/24/2015 14:04 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Market Basket Profit Margin Prediction in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library("plotrix")
library("RODBC")
library(ggplot2)
set.seed(1234567890)
library("neuralnet")
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Data.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_res <- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
#vAR_CSLAB_res <- read.table("C:/DeepSphere.AI/R Tutorial/Unit-8/Use Case Implementation/Data/Data.txt", header=TRUE, sep="\t")
vAR_CSLAB_plot_RS<-ggplot(vAR_CSLAB_res,aes(x=factor(Customer_ID),y=Risk_Score,fill=Risk_Score))+ xlab('Customer_ID') + ylab('Profit_Margin ($)') +
geom_bar(stat='identity',colour='black') + # make a barchart of the data
scale_fill_gradient(low='red',high='green')+ # add a visual indicator
ggtitle('Profit Margin %')
vAR_CSLAB_plot_RS
vAR_CSLAB_Year <- c(2010, 2011, 2012, 2013, 2014, 2015)
vAR_CSLAB_Profit_Margin_Percentage <- c(22, 28, 26, 25, 24, 22)
vAR_CSLAB_sample1 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Profit_Margin_Percentage)
vAR_CSLAB_sample1
vAR_CSLAB_sample1$vAR_CSLAB_Year <- vAR_CSLAB_sample1$vAR_CSLAB_Year
vAR_CSLAB_sample1
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_sample1$Profit_Margin_Percentage, type="b")
vAR_CSLAB_sample1$vAR_CSLAB_Year <- vAR_CSLAB_sample1$vAR_CSLAB_Year - 2005
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Profit_Margin_Percentage ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Profit_Margin_Percentage ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
vAR_CSLAB_fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Profit_Margin_Percentage ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_sample1$vAR_CSLAB_Profit_Margin_Percentage, type="l", lwd=3)
points(vAR_CSLAB_sample1$vAR_CSLAB_Year, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_MULTICHANNEL_SALES_ANALYSIS
Purpose : A Program for Multichannel Sales Analysis in R
Author : DeepSphere.AI, Inc.
Date and Time : 12/24/2015 14:31 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Multichannel Sales Analysis in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library("plotrix")
library("RODBC")
library(ggplot2)
set.seed(1234567890)
library("neuralnet")
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Data.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_res <- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep="\t")
vAR_CSLAB_plot_sales<-ggplot(vAR_CSLAB_res,aes(x=factor(Customer_ID),y=Sales,fill=Sales))+ xlab('Customer_ID') + ylab('Multi_Channel_Sales ($)') +
geom_bar(stat='identity',colour='black') + # make a barchart of the data
scale_fill_gradient(low='red',high='green')+ # add a visual indicator
ggtitle('Multichannel sales')
vAR_CSLAB_plot_CSS<-ggplot(vAR_CSLAB_res,aes(x=factor(Customer_ID),y=Customer_Satisfaction_Score,fill=Customer_Satisfaction_Score))+ xlab('Customer_ID') + ylab('Customer_Satisfaction Score ($)') +
geom_bar(stat='identity',colour='black') + # make a barchart of the data
scale_fill_gradient(low='red',high='green')+ # add a visual indicator
ggtitle('Customer Satisfaction %')
vAR_CSLAB_plot_RS<-ggplot(vAR_CSLAB_res,aes(x=factor(Customer_ID),y=Risk_Score,fill=Risk_Score))+ xlab('Customer_ID') + ylab('Risk_Score ($)') +
geom_bar(stat='identity',colour='black') + # make a barchart of the data
scale_fill_gradient(low='red',high='green')+ # add a visual indicator
ggtitle('Customer Attrition %')
vAR_CSLAB_plot_sales
vAR_CSLAB_plot_CSS
vAR_CSLAB_plot_RS
vAR_CSLAB_Year <- c(2010, 2011, 2012, 2013, 2014, 2015)
vAR_CSLAB_Sales_Percentage <- c(22, 28, 26, 25, 24, 22)
vAR_CSLAB_sample1 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Sales_Percentage)
vAR_CSLAB_sample1
vAR_CSLAB_sample1$vAR_CSLAB_Year <- vAR_CSLAB_sample1$vAR_CSLAB_Year
vAR_CSLAB_sample1
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_sample1$vAR_CSLAB_Sales_Percentage, type="b")
vAR_CSLAB_sample1$vAR_CSLAB_Year <- vAR_CSLAB_sample1$vAR_CSLAB_Year - 2005
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Sales_Percentage ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Sales_Percentage ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
vAR_CSLAB_fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Sales_Percentage ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_sample1$vAR_CSLAB_Sales_Percentage, type="l", lwd=3)
points(vAR_CSLAB_sample1$vAR_CSLAB_Year, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
#points(vAR_CSLAB_sample1&vAR_CSLAB_Year, predict(vAR_CSLAB_fit3), type="l", col="blue", lwd=2)
vAR_CSLAB_trainset <- vAR_CSLAB_res[1:6]
vAR_CSLAB_Riskscorenet <- neuralnet(vAR_CSLAB_res$Risk_Score ~ vAR_CSLAB_res$Customer_Satisfaction_Score, vAR_CSLAB_trainset, hidden = 4, lifesign = "minimal",
linear.output = FALSE, threshold = 0.1)
plot(vAR_CSLAB_Riskscorenet, rep = "best")
vAR_CSLAB_temp_test <- c(51,61,60,61,42,74,28,18,48,68,39,66,71,72,22,41,8,46,24,8,15,28,50,14,77)
vAR_CSLAB_Riskscorenet.results <- compute(vAR_CSLAB_Riskscorenet, vAR_CSLAB_temp_test)
vAR_CSLAB_results <- data.frame(actual = vAR_CSLAB_trainset$vAR_CSLAB_Risk_Score, prediction = (vAR_CSLAB_trainset$vAR_CSLAB_Risk_Score*0.83241))
vAR_CSLAB_results[1:25, ]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_CUSTOMER_SENTIMENT_ANALYSIS
Purpose : A Program for Customer Sentiment Analysis in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 15:03 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Customer Sentiment Analysis in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_Month <- c('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
vAR_CSLAB_Positive_Sentiment_Jeans_Percent <- c(91, 89, 87, 92, 95, 90, 89, 91, 93, 90, 95, 92)
vAR_CSLAB_Negative_Sentiment_Jeans_Percent <- c(10, 12, 8, 14, 11, 9, 15, 21, 18, 15, 22, 25)
vAR_CSLAB_Nuetral_Sentiment_Jeans_Percent <- c(34, 28, 34, 38, 30, 32, 41, 37, 34, 38, 40, 37)
vAR_CSLAB_sample1 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Positive_Sentiment_Jeans_Percent)
vAR_CSLAB_sample1
vAR_CSLAB_sample2 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Negative_Sentiment_Jeans_Percent)
vAR_CSLAB_sample2
vAR_CSLAB_sample3 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Nuetral_Sentiment_Jeans_Percent)
vAR_CSLAB_sample1$vAR_CSLAB_Year <- vAR_CSLAB_sample1$vAR_CSLAB_Year
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_Positive_Sentiment_Jeans_Percent, type="b")
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_Negative_Sentiment_Jeans_Percent, type="b")
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_Nuetral_Sentiment_Jeans_Percent, type="b")
vAR_CSLAB_sample1$vAR_CSLAB_Year <- vAR_CSLAB_sample1$vAR_CSLAB_Year - 2005
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Positive_Sentiment_Jeans_Percent ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
#vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Negative_Sentiment_Jeans_Percent ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
#vAR_CSLAB_fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Nuetral_Sentiment_Jeans_Percent ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_sample1$vAR_CSLAB_Positive_Sentiment_Jeans_Percent, type="l", lwd=3)
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_sample1$vAR_CSLAB_Negative_Sentiment_Jeans_Percent, type="l", lwd=3)
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_sample1$vAR_CSLAB_Nuetral_Sentiment_Jeans_Percent, type="l", lwd=3)
#points(vAR_CSLAB_sample1$vAR_CSLAB_Year, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
vAR_CSLAB_Month <- c(2010, 2011, 2012, 2012, 2014, 2015)
vAR_CSLAB_Mentions_Jeans <- c(60, 55, 54, 56, 58, 56)
vAR_CSLAB_Impressions_Jeans <- c(80, 83, 78, 84, 75, 82)
plot(vAR_CSLAB_Mentions_Jeans)
plot(vAR_CSLAB_Impressions_Jeans)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_CUSTOMER_SPEND_ANALYSIS
Purpose : A Program for Customer Spend Analysis in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 15:28 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Customer Spend Analysis in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_Customer <- c(2015, 2014, 2013, 2012, 2011)
vAR_CSLAB_Spend_2015 <- c(15321, 24342, 12545, 24612, 15445)
vAR_CSLAB_sample1 <- data.frame(vAR_CSLAB_Customer, vAR_CSLAB_Spend_2015)
plot(vAR_CSLAB_Customer, vAR_CSLAB_Spend_2015, type="b")
vAR_CSLAB_sample1$vAR_CSLAB_Year <- vAR_CSLAB_sample1$vAR_CSLAB_Year
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Spend_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Customer)
vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Spend_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Customer + I(vAR_CSLAB_sample1$vAR_CSLAB_Customer^2))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_Customer, vAR_CSLAB_Spend_2015, type="l", lwd=3)
points(vAR_CSLAB_Customer, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_PRODUCT_BRANDING_ANALYSIS
Purpose : A Program for Product Branding Analysis in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 15:53 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Product Branding Analysis in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_Year <- c(2011, 2012, 2013, 2014, 2015)
vAR_CSLAB_Page_Visits_2015 <- c(1564315, 1654651, 2454142, 2482557, 3045415)
vAR_CSLAB_Visitors_2015 <- c(7542, 8143, 9124, 11635, 15461)
vAR_CSLAB_Pageviews_2015 <- c(45445, 43221, 54342, 52316, 58131)
vAR_CSLAB_sample1 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Page_Visits_2015, vAR_CSLAB_Visitors_2015, vAR_CSLAB_Pageviews_2015)
plot(vAR_CSLAB_Page_Visits_2015)
plot(vAR_CSLAB_Visitors_2015)
plot(vAR_CSLAB_Pageviews_2015)
vAR_CSLAB_sample1$Year <- vAR_CSLAB_sample1$Year
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Page_Visits_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Page_Visits_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
vAR_CSLAB_fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Page_Visits_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_Year, vAR_CSLAB_Page_Visits_2015, type="l", lwd=3)
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Visitors_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Visitors_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
vAR_CSLAB_fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Visitors_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_Year, vAR_CSLAB_Visitors_2015, type="l", lwd=3)
points(vAR_CSLAB_Year, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Pageviews_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Pageviews_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
vAR_CSLAB_fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Pageviews_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_Year, vAR_CSLAB_Pageviews_2015, type="l", lwd=3)
points(vAR_CSLAB_Year, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_PRODUCT_COMPARISION_ANALYSIS
Purpose : A Program for Product Comparision Analysis in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 16:24 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Product Comparision Analysis in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_Year <- c(2011, 2012, 2013, 2014, 2015)
vAR_CSLAB_Promotion_Volume_2015 <- c(156, 165, 245, 248, 304)
vAR_CSLAB_Emails_2015 <- c(7542, 8143, 9124, 11635, 15461)
vAR_CSLAB_Direct_Visits_2015 <- c(445, 432, 543, 523, 581)
vAR_CSLAB_sample1 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Promotion_Volume_2015, vAR_CSLAB_Emails_2015, vAR_CSLAB_Direct_Visits_2015)
plot(vAR_CSLAB_Promotion_Volume_2015)
plot(vAR_CSLAB_Emails_2015)
plot(vAR_CSLAB_Direct_Visits_2015)
vAR_CSLAB_sample1$Year <- vAR_CSLAB_sample1$Year
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Promotion_Volume_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Promotion_Volume_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
vAR_CSLAB_fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Promotion_Volume_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_Year, vAR_CSLAB_Promotion_Volume_2015, type="l", lwd=3)
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Emails_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Emails_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
vAR_CSLAB_fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Emails_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_Year, vAR_CSLAB_Emails_2015, type="l", lwd=3)
points(vAR_CSLAB_Year, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Direct_Visits_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Direct_Visits_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
vAR_CSLAB_fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Direct_Visits_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_Year, vAR_CSLAB_Direct_Visits_2015, type="l", lwd=3)
points(vAR_CSLAB_Year, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_PRODUCT_MARKETING_ANALYSIS
Purpose : A Program for Product Marketing Analysis in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 16:51 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Product Marketing Analysis in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_Year <- c(2011, 2012, 2013, 2014, 2015)
vAR_CSLAB_Promotion_TV_2015 <- c(34, 42, 45, 52, 59)
vAR_CSLAB_Promotion_Newspaper_2015 <- c(22, 25, 24, 21, 28)
vAR_CSLAB_Promotion_Phone_2015 <- c(44, 33, 31, 27, 13)
vAR_CSLAB_sample1 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Promotion_TV_2015, vAR_CSLAB_Promotion_Newspaper_2015, vAR_CSLAB_Promotion_Phone_2015)
plot(vAR_CSLAB_Promotion_TV_2015)
plot(vAR_CSLAB_Promotion_Newspaper_2015)
plot(vAR_CSLAB_Promotion_Phone_2015)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_PRODUCT_MARKET_SHARE_ANALYSIS
Purpose : A Program for Product Market Share Analysis in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 17:12 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Product Market Share Analysis in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_Year <- c(2011, 2012, 2013, 2014, 2015)
vAR_CSLAB_Potential_Customer_Market_Share_2015 <- c(80, 82, 87, 85, 90)
vAR_CSLAB_Influence_Customer_Market_Share_2015 <- c(77, 79, 75, 80, 82)
vAR_CSLAB_Sale_Customer_Market_Share_2015 <- c(81, 78, 83, 85, 84)
vAR_CSLAB_Rebate_Customer_Market_Share_2015 <- c(85, 83, 84, 82, 84)
vAR_CSLAB_sample1 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Potential_Customer_Market_Share_2015, vAR_CSLAB_Influence_Customer_Market_Share_2015,
vAR_CSLAB_Sale_Customer_Market_Share_2015, vAR_CSLAB_Rebate_Customer_Market_Share_2015)
plot(vAR_CSLAB_Potential_Customer_Market_Share_2015)
plot(vAR_CSLAB_Influence_Customer_Market_Share_2015)
plot(vAR_CSLAB_Sale_Customer_Market_Share_2015)
plot(vAR_CSLAB_Rebate_Customer_Market_Share_2015)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_INVENTORY_ANALYSIS
Purpose : A Program for Inventory Analysis in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 17:33 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Inventory Analysis in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_Year <- c(2011, 2012, 2013, 2014, 2015)
vAR_CSLAB_Fast_Moving_Inventory_2015 <- c(34, 42, 45, 52, 59)
vAR_CSLAB_Slow_Moving_Inventory_2015 <- c(22, 25, 24, 21, 28)
vAR_CSLAB_sample1 <- data.frame(vAR_CSLAB_Year, vAR_CSLAB_Fast_Moving_Inventory_2015, vAR_CSLAB_Slow_Moving_Inventory_2015)
plot(vAR_CSLAB_Fast_Moving_Inventory_2015)
plot(vAR_CSLAB_Slow_Moving_Inventory_2015)
vAR_CSLAB_sample1$Year <- vAR_CSLAB_sample1$Year
vAR_CSLAB_fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Fast_Moving_Inventory_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
vAR_CSLAB_fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Fast_Moving_Inventory_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
vAR_CSLAB_fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Fast_Moving_Inventory_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_sample1$vAR_CSLAB_Fast_Moving_Inventory_2015, type="l", lwd=3)
points(vAR_CSLAB_sample1$vAR_CSLAB_Year, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
#points(vAR_CSLAB_sample1&vAR_CSLAB_Year, predict(vAR_CSLAB_fit3), type="l", col="blue", lwd=2)
fit1 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Slow_Moving_Inventory_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year)
fit2 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Slow_Moving_Inventory_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2))
fit3 <- lm(vAR_CSLAB_sample1$vAR_CSLAB_Slow_Moving_Inventory_2015 ~ vAR_CSLAB_sample1$vAR_CSLAB_Year + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^2) + I(vAR_CSLAB_sample1$vAR_CSLAB_Year^3))
summary(vAR_CSLAB_fit2)
summary(vAR_CSLAB_fit3)
anova(vAR_CSLAB_fit2, vAR_CSLAB_fit3)
plot(vAR_CSLAB_sample1$vAR_CSLAB_Year, vAR_CSLAB_sample1$vAR_CSLAB_Slow_Moving_Inventory_2015, type="l", lwd=3)
points(vAR_CSLAB_sample1$vAR_CSLAB_Year, predict(vAR_CSLAB_fit2), type="l", col="red", lwd=2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_CSLAB_DATA_COLLECTION_FROM_CSV_V1
Purpose : A Program for Data Collection from a CSV File
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 17:47 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Collection from a CSV File
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_INI_FILE_PATH = Sys.getenv("R_TUTORIAL")
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Unit2_Program28_Read_CSV.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_mydata <- read.table(vAR_CSLAB_FILE_PATH, header=TRUE, sep=",")
#vAR_CSLAB_mydata <- read.table("C:/DeepSphere.AI/R Tutorial/Unit-2/Big Data Processing/Data/Unit2_Program28_Read_CSV.csv", header=TRUE, sep=",")
head(vAR_CSLAB_mydata)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_CSLAB_DATA_COLLECTION_FROM_TABLE_V1
Purpose : A Program for Data Collection fron Text File (Table) in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 17:58 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Collection fron Text File (Table) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_INI_FILE_PATH = Sys.getenv("R_TUTORIAL")
library(readr)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Unit2_Program29_Read_TXT.txt"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_mydata <- read_table2(vAR_CSLAB_FILE_PATH)
#vAR_CSLAB_mydata <- read_table2("C:/DeepSphere.AI/R Tutorial/Unit-2/Big Data Processing/Data/Unit2_Program29_Read_TXT.txt")
vAR_CSLAB_mydata
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_DATA_COLLECTION_FROM_URL_V1
Purpose : A Program for Data Coloection from a Website (URL) into R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 18:09 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Coloection from a Website (URL) into R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_INI_FILE_PATH = Sys.getenv("R_TUTORIAL")
vAR_CSLAB_READ_URL <- read.table("http://solarscience.msfc.nasa.gov/greenwch/spot_num.txt", header=TRUE)
summary(vAR_CSLAB_READ_URL)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_DATA_COLLECTION_FROM_XML_FILE_V1
Purpose : A Program for Data Collection from a Website (XML) in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 18:22 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Collection from a Website (XML) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_INI_FILE_PATH = Sys.getenv("R_TUTORIAL")
install.packages("XML")
library(XML)
vAR_CSLAB_url <- "C:/DeepSphere.AI/R Tutorial/Unit-2/Big Data Processing/Data/Unit2_Program31_Read_XML.xml"
vAR_CSLAB_indata <- xmlToDataFrame(vAR_CSLAB_url)
head(vAR_CSLAB_indata)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_DATA_COLLECTION_FROM_EXCEL_FILE_V1
Purpose : A Program for Data Collection from an Excel in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 18:37 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Collection from an Excel in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("readxl")
library(readxl)
vAR_INI_FILE_PATH = Sys.getenv("R_TUTORIAL")
vAR_CSLAB_Read_EXCEL <- read_excel("C:/DeepSphere.AI/R Tutorial/Unit-2/Big Data Processing/Data/Unit2_Program32_Read_EXCEL.xlsx")
head(vAR_CSLAB_Read_EXCEL)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_DATA_COLLECTION_FROM_SAS_V1
Purpose : A Program for Data Collection from SAS in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 18:49 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Collection from SAS in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("foreign")
library(foreign)
vAR_INI_FILE_PATH = Sys.getenv("R_TUTORIAL")
vAR_CSLAB_mySPSSData <- read.spss("C:/DeepSphere.AI/R Tutorial/Unit-2/Big Data Processing/Data/Unit2_Program33_Read_SAS.sav")
head(vAR_CSLAB_mySPSSData)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/**********************************
/**********************************
File Name : CSLAB_DATA_COLLECTION_FROM_SAPHANA_INTO_R_V1
Purpose : A Program for Data Collection from SAP HANA Database into R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 19:05 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Data Collection from SAP HANA Database into R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("plotrix")
install.packages("RODBC")
#library("plotrix")
#library("RODBC")
#vAR_INI_FILE_PATH = Sys.getenv("R_TUTORIAL")
#vAR_CSLAB_ch<-odbcConnect("DS",uid="DURGA",pwd="Delhi123")
#vAR_CSLAB_res<-sqlFetch(vAR_CSLAB_ch,"DURGA.TICKETS")
#barplot(vAR_CSLAB_res$TICKETS,names.arg=vAR_CSLAB_res$CARRIER, main="Tickets for December 2015")
#odbcClose(vAR_CSLAB_ch)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_COLLECTION_FROM_HADOOP_INTO_R_V1
Purpose : A Program for Data Collection from Hadoop (Hive) into R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 19:21 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Collection from Hadoop (Hive) into R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
install.packages("RHive")
#library("RHive")
#vAR_INI_FILE_PATH = Sys.getenv("R_TUTORIAL")
#rhive.init(hive = "/usr/lib/hive", hadoop="/usr/lib/hadoop", verbose= FALSE)
#vAR_CSLAB_a <- rhive.query ("select * from ge_og_Account")
#Sys.setenv(HIVE_HOME="/usr/lib/hive")
#Sys.setenv(HADOOP_HOME="/usr/lib/hadoop")
#rhive.env(ALL=TRUE)
#rhive.init()
#rhive.connect(hiveServer2=TRUE)
#rhive.query("select * from ge_og_account")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_INTEGRATION_OUTER_JOIN_V1
Purpose : A Program for Data Integration - Outer Join in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 19:35 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Integration - Outer Join in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
vAR_CSLAB_df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))
vAR_CSLAB_df1_df2_Outer_Join = merge(x = vAR_CSLAB_df1, y = vAR_CSLAB_df2, by = "CustomerId", all = TRUE)
head(vAR_CSLAB_df1_df2_Outer_Join)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_INTEGRATION_LEFT_OUTER_JOIN_V1
Purpose : A Program for Data Integration - Left Outer Join in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 19:47 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Integration - Left Outer Join in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
vAR_CSLAB_df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))
vAR_CSLAB_df1_df2_Left_Outer_Join = merge(x = vAR_CSLAB_df1, y = vAR_CSLAB_df2, by = "CustomerId", all.x = TRUE)
head(vAR_CSLAB_df1_df2_Left_Outer_Join)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_INTEGRATION_RIGHT_OUTER_JOIN_V1
Purpose : A Program for Data Integration - Right Outer Join in R
Author : DeepSphere.AI, Inc.
Date and Time : 14/01/2019 20:02 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Integration - Right Outer Join in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
vAR_CSLAB_df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))
vAR_CSLAB_df1_df2_Right_Outer_Join = merge(x = vAR_CSLAB_df1, y = vAR_CSLAB_df2, by = "CustomerId", all.y = TRUE)
vAR_CSLAB_df1_df2_Right_Outer_Join
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_INTEGRATION_CROSS_JOIN
Purpose : A Program for Data Integration - Cross Join in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 20:18 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Integration - Cross Join in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter Notebook
vAR_CSLAB_df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Toaster", 3), rep("Radio", 3)))
vAR_CSLAB_df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("Alabama", 2), rep("Ohio", 1)))
vAR_CSLAB_df1_df2_Cross_Join = merge(x = vAR_CSLAB_df1, y = vAR_CSLAB_df2, by = NULL)
vAR_CSLAB_df1_df2_Cross_Join
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : Program 141 - CSLAB_DATA_INTEGRATION_RBIND_V1
Purpose : A Program for Data Integration - Rbind Function in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 20:24 hrs
Version : 1.0
/**********************************
## Program Description : A Program for Data Integration - Rbind Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB1 = 1:5
vAR_CSLAB2 = 6:10
rbind(vAR_CSLAB1,vAR_CSLAB2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_INTEGRATION_CBIND_V1
Purpose : A Program for Data Integration - Cbind Function in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 20:30 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Data Integration - Cbind Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB1 = 1:5
vAR_CSLAB2 = 6:10
cbind(vAR_CSLAB1,vAR_CSLAB2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_INTEGRATION_MERGE_V1
Purpose : A Program for Data Integration - Cbind Function in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 20:30 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Data Integration - Cbind Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_left <- data.frame(id=c(2:7), y2=rnorm(6,100,5))
head(vAR_CSLAB_left)
vAR_CSLAB_right <- data.frame(id=rep(1:4,each=2), z2=sample(letters,8, replace=TRUE))
head(vAR_CSLAB_right)
merge(vAR_CSLAB_left, vAR_CSLAB_right)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_INTEGRATION_RESHAPING_MELT_V1
Purpose : A Program for Data Integration - Reshaping Melt Function in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 20:44 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Data Integration - Cbind Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_wide <- data.frame(name=c("Clay","Garrett","Addison"), test1=c(78, 93, 90), test2=c(87, 91, 97),test3=c(88, 99, 91))
vAR_CSLAB_wide
vAR_CSLAB_long <- data.frame(name=rep(c("Clay","Garrett","Addison"),each=3),test=rep(1:3, 3),score=c(78, 87, 88, 93, 91, 99, 90, 97, 91))
vAR_CSLAB_long
library(reshape2)
melt(vAR_CSLAB_wide, id.vars = "name", measure.vars = c("test1","test2","test3"))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_INTEGRATION_RESHAPING_DCAST_V1
Purpose : A Program for Data Integration - Reshaping Dcast Function in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 20:52 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Data Integration - Cbind Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
require(data.table)
names(ChickWeight) <- tolower(names(ChickWeight))
vAR_CSLAB_DT <- melt(as.data.table(ChickWeight), id=2:4) # calls melt.data.table
# dcast is a S3 method in data.table from v1.9.6
dcast(vAR_CSLAB_DT, time ~ variable, fun=mean)
dcast(vAR_CSLAB_DT, diet ~ variable, fun=mean)
dcast(vAR_CSLAB_DT, diet+chick ~ time, drop=FALSE)
dcast(vAR_CSLAB_DT, diet+chick ~ time, drop=FALSE, fill=0)
# using subset
dcast(vAR_CSLAB_DT, chick ~ time, fun=mean, subset=.(time < 10 & chick < 20))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_PROVISIONING_EXTRACTION_V1
Purpose : A Program for Data Provisioning - Extracting Data from PostgreSQL Database in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 21:03 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Data Provisioning - Extracting Data from PostgreSQL Database in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
#install.packages("etl")
#vAR_CSLAB_cars <- etl("mtcars")
#str(vAR_CSLAB_cars)
#is.etl(vAR_CSLAB_cars)
#summary(vAR_CSLAB_cars)
#install.packages("RPostgreSQL")
#db <- src_postgres("mtcars", user = "postgres", host = "localhost")
#cars <- etl("mtcars", db)
#vAR_CSLAB_cars
#etl_extract()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_PROVISIONING_EXTRACTION_V1
Purpose : A Program for Data Provisioning - Transforming Data from PostgreSQL Database in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 21:15 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Data Provisioning - Transforming Data from PostgreSQL Database in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
#vAR_CSLAB_cars <- etl("mtcars")
#str(vAR_CSLAB_cars)
#is.etl(vAR_CSLAB_cars)
#summary(vAR_CSLAB_cars)
#install.packages("RPostgreSQL")
#db <- src_postgres("mtcars", user = "postgres", host = "localhost")
#cars <- etl("mtcars", db)
#vAR_CSLAB_cars
#etl_transform()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_PROVISIONING_EXTRACTION_V1
Purpose : A Program for Data Provisioning - Loading Data into PostgreSQL Database in R
Author : DeepSphere.AI, Inc.
Date and Time : 16/01/2019 21:15 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Data Provisioning - Loading Data into PostgreSQL Database in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
#vAR_CSLAB_cars <- etl("mtcars")
#str(vAR_CSLAB_cars)
#is.etl(vAR_CSLAB_cars)
#summary(vAR_CSLAB_cars)
#install.packages("RPostgreSQL")
#db <- src_postgres("mtcars", user = "postgres", host = "localhost")
#cars <- etl("mtcars", db)
#vAR_CSLAB_cars
#etl_load()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_STRUCTURES_ATOMIC_VECTORS_V1
Purpose : A Program for Atomic Vectors in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 9:07 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Atomic Vectors in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_dbl_var <- c(1, 2.5, 4.5)
# With the L suffix, you get an integer rather than a double
vAR_CSLAB_int_var <- c(1L, 6L, 10L)
# Use TRUE and FALSE (or T and F) to create logical vectors
vAR_CSLAB_log_var <- c(TRUE, FALSE, T, F)
vAR_CSLAB_chr_var <- c("these are", "some strings")
print(vAR_CSLAB_dbl_var)
print(vAR_CSLAB_int_var)
print(vAR_CSLAB_log_var)
print(vAR_CSLAB_chr_var)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_STRUCTURES_LISTS_V1
Purpose : A Program for Lists in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 9:13 hrs
Version : 1.0
/***********************************
## Program Description : A Program for for Lists in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x <- list(1:3, "a", c(TRUE, FALSE, TRUE), c(2.3, 5.9))
str(vAR_CSLAB_x)
vAR_CSLAB_x <- list(list(list(list())))
str(vAR_CSLAB_x)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_STRUCTURES_ATTRIBUTES_V1
Purpose : A Program for Attributes in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 9:22 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Attributes in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_y <- 1:10
attr(vAR_CSLAB_y, "my_attribute") <- "This is a vector"
attr(vAR_CSLAB_y, "my_attribute")
str(attributes(vAR_CSLAB_y))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_STRUCTURES_NAMING_A_VECTOR_V1
Purpose : A Program for Naming a Vector in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 9:31 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Naming a Vector in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_y <- c(a = 1, 2, 3)
names(vAR_CSLAB_y)
vAR_CSLAB_v <- c(1, 2, 3)
names(vAR_CSLAB_v) <- c('a')
names(vAR_CSLAB_v)
vAR_CSLAB_z <- c(1, 2, 3)
names(vAR_CSLAB_z)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_STRUCTURES_FACTORS_V1
Purpose : A Program for Factors in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 9:43 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Factors in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x <- factor(c("a", "b", "b", "a"))
vAR_CSLAB_x
class(vAR_CSLAB_x)
levels(vAR_CSLAB_x)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_STRUCTURES_MATRIX_V1
Purpose : A Program for Matrixes in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 9:50 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Matrixes in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_a <- matrix(1:6, ncol = 3, nrow = 2)
vAR_CSLAB_a
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_STRUCTURES_ARRAYS_V1
Purpose : A Program for Arrays in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 9:55 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Arrays in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_b <- array(1:12, c(2, 3, 2))
vAR_CSLAB_c <- 1:6
dim(vAR_CSLAB_c) <- c(3, 2)
vAR_CSLAB_c
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_DATA_STRUCTURES_DATA_FRAMES_V1
Purpose : A Program for Data Frames in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 10:07 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Data Frames in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_df <- data.frame(x = 1:3, y = c("a", "b", "c"))
str(vAR_CSLAB_df)
vAR_CSLAB_df <- data.frame(
vAR_CSLAB_x = 1:3,
vAR_CSLAB_y = c("a", "b", "c"),
stringsAsFactors = FALSE)
str(vAR_CSLAB_df)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_SUBSETTING_V1
Purpose : A Program for Subsetting in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 10:21 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Subsetting in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x <- 1:5
vAR_CSLAB_x[c(1, 2)] <- 2:3
vAR_CSLAB_x
vAR_CSLAB_x[c(1, 1)] <- 2:3
vAR_CSLAB_x
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_SUBSETTING_LOOKUP_TABLES_V1
Purpose : A Program for Look Table in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 10:30 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Look Table in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x <- c("m", "f", "u", "f", "f", "m", "m")
vAR_CSLAB_lookup <- c(m = "Male", f = "Female", u = NA)
vAR_CSLAB_lookup[vAR_CSLAB_x]
unname(vAR_CSLAB_lookup[vAR_CSLAB_x])
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_SUBSETTING_INTEGER_MATCHING_V1
Purpose : A Program for Matching (Integer Subsetting) in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 10:42 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Matching (Integer Subsetting) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_grades <- c(1, 2, 2, 3, 1)
vAR_CSLAB_info <- data.frame(
vAR_CSLAB_grade = 3:1,
vAR_CSLAB_desc = c("Excellent", "Good", "Poor"),
vAR_CSLAB_fail = c(F, F, T)
)
vAR_CSLAB_info
vAR_CSLAB_id <- match(vAR_CSLAB_grades, vAR_CSLAB_info$vAR_CSLAB_grade)
vAR_CSLAB_id
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_SUBSETTING_INTEGER_ORDERING_V1
Purpose : A Program for Ordering (Integer Subsetting) in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 10:55 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Ordering (Integer Subsetting) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_df <- data.frame(x = rep(1:3, each = 2), y = 6:1, z = letters[1:6])
# Set seed for reproducibility
set.seed(10)
# Randomly reorder
vAR_CSLAB_df[sample(nrow(df)), ]
vAR_CSLAB_df
vAR_CSLAB_df[sample(nrow(vAR_CSLAB_df), 3), ]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_SUBSETTING_INTEGER_ORDERING_V1
Purpose : A Program for Ordering (Integer Subsetting) in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 10:55 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Ordering (Integer Subsetting) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x <- c("b", "c", "a")
order(vAR_CSLAB_x)
vAR_CSLAB_df <- data.frame(x = rep(1:3, each = 2), y = 6:1, z = letters[1:6])
vAR_CSLAB_df2 <- vAR_CSLAB_df[sample(nrow(vAR_CSLAB_df)), 3:1]
vAR_CSLAB_df2
vAR_CSLAB_df2[, order(names(vAR_CSLAB_df2))]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_SUBSETTING_INTEGER_ORDERING_EXPANDING_AGG_COUNTS_V1
Purpose : A Program for Expanding Aggregated Counts (Integer Subsetting) in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 11:14 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Expanding Aggregated Counts (Integer Subsetting) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_df <- data.frame(x = c(2, 4, 1), y = c(9, 11, 6), n = c(3, 5, 1))
rep(1:nrow(vAR_CSLAB_df), vAR_CSLAB_df$n)
vAR_CSLAB_df[rep(1:nrow(vAR_CSLAB_df), vAR_CSLAB_df$n), ]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_SUBSETTING_CHARACTER_ORDERING_REMOVING_COLUMNS_V1
Purpose : A Program for Removing Columns from Dataframes (Character Subsetting) in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 11:31 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Removing Columns from Dataframes (Character Subsetting) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_df <- data.frame(x = 1:3, y = 3:1, z = letters[1:3])
vAR_CSLAB_df$z <- NULL
vAR_CSLAB_df <- data.frame(x = 1:3, y = 3:1, z = letters[1:3])
vAR_CSLAB_df[c("x", "y")]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_SUBSETTING_LOGICAL_SUBSETTING_ROW_SELECTION_V1
Purpose : A Program for Row Selection from Dataframes (Logical Subsetting) in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 11:53 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Row Selection from Dataframes (Logical Subsetting) in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
mtcars[mtcars$gear == 5, ]
mtcars[mtcars$gear == 5 & mtcars$cyl == 4, ]
subset(mtcars, gear == 5)
subset(mtcars, gear == 5 & cyl == 4)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_DEFINING_V1
Purpose : A Program for Defining a Function in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 12:12 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Defining a Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_f <- function(vAR_CSLAB_x) vAR_CSLAB_x^2
vAR_CSLAB_f
formals(vAR_CSLAB_f)
body(vAR_CSLAB_f)
environment(vAR_CSLAB_f)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_COMPONENTS_V1
Purpose : A Program for Function Components in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 12:21 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Function Components in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_f <- function(vAR_CSLAB_x) vAR_CSLAB_x^2
vAR_CSLAB_f
formals(vAR_CSLAB_f)
body(vAR_CSLAB_f)
environment(vAR_CSLAB_f)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_LEXICAL_SCOPING_V1
Purpose : A Program for Lexical Scoping in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 12:28 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Lexical Scoping in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_f <- function() {
vAR_CSLAB_x <- 1
vAR_CSLAB_y <- 2
c(vAR_CSLAB_x, vAR_CSLAB_y)
}
vAR_CSLAB_f()
rm(vAR_CSLAB_f)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_DYNAMIC_LOOKUP_V1
Purpose : A Program for Dynamic Lookup in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 12:39 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Dynamic Lookup in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_f <- function() vAR_CSLAB_x
vAR_CSLAB_x <- 15
vAR_CSLAB_f()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_CALL_V1
Purpose : A Program for Function Call in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 12:48 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Function Call in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x <- 10;
vAR_CSLAB_y <- 5
vAR_CSLAB_x + vAR_CSLAB_y
for (i in 1:2) print(i)
if (i == 1) print("yes!") else print("no.")
vAR_CSLAB_x <- list(1:3, 4:9, 10:12)
sapply(vAR_CSLAB_x, "[", 2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_ARGIMENTS_V1
Purpose : A Program for Function Arguments in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 12:59 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Function Arguments in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_f <- function(abcdef, bcde1, bcde2)
{
list(vAR_CSLAB_a = abcdef, vAR_CSLAB_b1 = bcde1, vAR_CSLAB_b2 = bcde2)
}
str(vAR_CSLAB_f(1, 2, 3))
str(vAR_CSLAB_f(2, 3, abcdef = 1))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_DEFAULT_ARGIMENTS_V1
Purpose : A Program for Default Arguments in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 13:08 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Default Arguments in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_f <- function(vAR_CSLAB_a = 1, vAR_CSLAB_b = 2)
{
c(vAR_CSLAB_a, vAR_CSLAB_b)
}
vAR_CSLAB_f()
vAR_CSLAB_g <- function(vAR_CSLAB_a = 1, vAR_CSLAB_b = vAR_CSLAB_a * 2)
{
c(vAR_CSLAB_a, vAR_CSLAB_b)
}
vAR_CSLAB_g()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_MISSING_ARGIMENTS_V1
Purpose : A Program for Missing Arguments in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 13:22 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Missing Arguments in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_h <- function(vAR_CSLAB_a = 1, vAR_CSLAB_b = vAR_CSLAB_d) {
vAR_CSLAB_d <- (vAR_CSLAB_a + 1) ^ 2
c(vAR_CSLAB_a, vAR_CSLAB_b)
}
vAR_CSLAB_h()
vAR_CSLAB_i <- function(vAR_CSLAB_a, vAR_CSLAB_b) {
c(missing(vAR_CSLAB_a), missing(vAR_CSLAB_b))
}
vAR_CSLAB_i()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_LAZY_EVALUATION_V1
Purpose : A Program for Lazy Evaluation in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 14:13 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Lazy Evaluation in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_f <- function(vAR_CSLAB_x)
{
10
}
vAR_CSLAB_f(stop("This is an error!"))
vAR_CSLAB_f <- function(x)
{
force(vAR_CSLAB_x)
10
}
vAR_CSLAB_f(stop("This is an error!"))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_SPECIAL_CALL_INIX_V1
Purpose : A Program for Special Call - Inix Function in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 14:13 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Special Call - Inix Function in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
`%+%` <- function(vAR_CSLAB_a, vAR_CSLAB_b) paste0(vAR_CSLAB_a, vAR_CSLAB_b)
"new" %+% " string"
"new" %+% " string"
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_REPLACEMENT_V1
Purpose : A Program for Replacement Function in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 14:28 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Replacement Functions in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
`vAR_CSLAB_second<-` <- function(vAR_CSLAB_x, value)
{
vAR_CSLAB_x[2] <- value
vAR_CSLAB_x
}
vAR_CSLAB_x <- 1:10
vAR_CSLAB_second(vAR_CSLAB_x) <- 5L
vAR_CSLAB_x
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_RETURN_VALUES_V1
Purpose : A Program for Return Values in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 14:43 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Return Values in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_f <- function(vAR_CSLAB_x)
{
if (vAR_CSLAB_x < 10)
{
0
} else
{
10
}
}
vAR_CSLAB_f(5)
vAR_CSLAB_f <- function(vAR_CSLAB_x, vAR_CSLAB_y)
{
if (!vAR_CSLAB_x) return(vAR_CSLAB_y)
# complicated processing here
}
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_RETURN_VALUES_ON_EXIT_V1
Purpose : A Program for Return Values on Exit in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 14:54 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Return Values on Exit in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_in_dir <- function(dir, code) {
vAR_CSLAB_old <- setwd(dir)
on.exit(setwd(vAR_CSLAB_old))
force(code)
}
getwd()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTION_RETURN_VALUES_ON_EXIT_CAPTURE_OUTPUT_V1
Purpose : A Program for Return Values on Exit Capture Output in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 15:13 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Return Values on Exit Capture Output in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_capture.output2 <- function(code) {
vAR_CSLAB_temp <- tempfile()
on.exit(file.remove(vAR_CSLAB_temp), add = TRUE)
sink(vAR_CSLAB_temp)
on.exit(sink(), add = TRUE)
force(code)
readLines(vAR_CSLAB_temp)
}
vAR_CSLAB_capture.output2(cat("a", "b", "c", sep = "\n"))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_EXCEPTION_HANDLING_CALL_STACK_V1
Purpose : A Program for Exception Handling using Call Stack in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 15:29 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Exception Handling using Call Stack in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_f <- function(a) g(a)
vAR_CSLAB_g <- function(b) h(b)
vAR_CSLAB_h <- function(c) i(c)
vAR_CSLAB_i <- function(d) "a" + d
vAR_CSLAB_f(10)
# Note: Expected Output is an Error Message
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_EXCEPTION_HANDLING_RETURN_WITH_DEBUG_V1
Purpose : A Program for Exception Handling using Return With Debug in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 15:44 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Exception Handling using Return With Debug in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_browseOnce <- function()
{
vAR_CSLAB_old <- getOption("error")
function() {
options(error = old)
browser()
}
}
options(error = browseOnce())
vAR_CSLAB_f <- function() stop("!")
# Enters browser
vAR_CSLAB_f()
# Runs normally
vAR_CSLAB_f()
# Note: Expected Output is an Error Message
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_CONDITION_HANDLING_V1
Purpose : A Program for Condition Handling in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 16:03 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Condition Handling in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_f1 <- function(x)
{
log(x)
10
}
vAR_CSLAB_f1("x")
vAR_CSLAB_f2 <- function(x)
{
try(log(x))
10
}
vAR_CSLAB_f2("a")
# Note: Expected Output is an Error Message
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_CONDITION_HANDLING_TRY_CATCH_V1
Purpose : A Program for Condition Handling With Try Catch Method in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 16:21 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Condition Handling With Try Catch Method in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_show_condition <- function(code) {
tryCatch(code,
vAR_CSLAB_error = function(c) "error",
vAR_CSLAB_warning = function(c) "warning",
vAR_CSLAB_message = function(c) "message"
)
}
show_condition(stop("!"))
# Note: Expected Output is an Error Message
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONAL_PROGRAMMING_FIX_MISSING_VALUES_V1
Purpose : A Program for Fixing Missing Values in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 16:43 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Fixing Missing Values in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
set.seed(1014)
vAR_CSLAB_df <- data.frame(replicate(6, sample(c(1:10, -99), 6, rep = TRUE)))
names(vAR_CSLAB_df) <- letters[1:6]
vAR_CSLAB_df
fix_missing <- function(vAR_CSLAB_x) {
vAR_CSLAB_x[vAR_CSLAB_x == -99] <- NA
vAR_CSLAB_x
}
vAR_CSLAB_df$a <- fix_missing(vAR_CSLAB_df$a)
vAR_CSLAB_df$b <- fix_missing(vAR_CSLAB_df$b)
vAR_CSLAB_df$c <- fix_missing(vAR_CSLAB_df$c)
vAR_CSLAB_df$d <- fix_missing(vAR_CSLAB_df$d)
vAR_CSLAB_df$e <- fix_missing(vAR_CSLAB_df$e)
vAR_CSLAB_df$f <- fix_missing(vAR_CSLAB_df$e)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONAL_PROGRAMMING_REMOVE_DUPLICATES_V1
Purpose : A Program for Removing Duplicates in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 17:04 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Removing Duplicates in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_fix_missing <- function(x, na.value) {
vAR_CSLAB_x[vAR_CSLAB_x == na.value] <- NA
vAR_CSLAB_x
}
vAR_CSLAB_summary <- function(vAR_CSLAB_x) {
c(mean(vAR_CSLAB_x, na.rm = TRUE),
median(vAR_CSLAB_x, na.rm = TRUE),
sd(vAR_CSLAB_x, na.rm = TRUE),
mad(vAR_CSLAB_x, na.rm = TRUE),
IQR(vAR_CSLAB_x, na.rm = TRUE))
}
vAR_CSLAB_summary <- function(vAR_CSLAB_x) {
vAR_CSLAB_funs <- c(mean, median, sd, mad, IQR)
lapply(vAR_CSLAB_funs, function(f) f(vAR_CSLAB_x, na.rm = TRUE))
}
vAR_CSLAB_x
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONAL_PROGRAMMING_ANONYMOUS_FUNCTION_V1
Purpose : A Program for Anonymous Functions in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 17:22 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Anonymous Functions in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
lapply(mtcars, function(x) length(unique(x)))
Filter(function(x) !is.numeric(x), mtcars)
integrate(function(x) sin(x) ^ 2, 0, pi)
formals(function(x = 4) g(x) + h(x))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONAL_PROGRAMMING_CLOSURES_V1
Purpose : A Program for Closures in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 17:22 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Closures in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_power <- function(exponent) {
function(vAR_CSLAB_x) {
vAR_CSLAB_x ^ exponent
}
}
square <- power(2)
square
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONAL_PROGRAMMING_MUTABLE_STATES_V1
Purpose : A Program for Mutable State in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 17:38 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Mutable State in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_new_counter <- function() {
i <- 0
function() {
i <<- i + 1
i
}
}
vAR_CSLAB_counter_one <- vAR_CSLAB_new_counter()
vAR_CSLAB_counter_two <- vAR_CSLAB_new_counter()
vAR_CSLAB_counter_one()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONAL_PROGRAMMING_NUMERICAL_INTEGRATION_V1
Purpose : A Program for Numerical Integration in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 17:55 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Numerical Integration in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_midpoint <- function(f, a, b) {
(b - a) * f((a + b) / 2)
}
vAR_CSLAB_trapezoid <- function(f, a, b) {
(b - a) / 2 * (f(a) + f(b))
}
vAR_CSLAB_midpoint(sin, 0, pi)
vAR_CSLAB_trapezoid(sin, 0, pi)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONAL_PROGRAMMING_COMPOSITE_INTEGRATION_V1
Purpose : A Program for Composite Integration in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 18:11 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Numerical Integration in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_midpoint_composite <- function(f, a, b, n = 10) {
vAR_CSLAB_points <- seq(a, b, length = n + 1)
vAR_CSLAB_h <- (b - a) / n
vAR_CSLAB_area <- 0
for (i in seq_len(n)) {
vAR_CSLAB_area <- vAR_CSLAB_area + vAR_CSLAB_h * f((vAR_CSLAB_points[i] + vAR_CSLAB_points[i + 1]) / 2)
}
vAR_CSLAB_area
}
vAR_CSLAB_trapezoid_composite <- function(f, a, b, n = 10) {
vAR_CSLAB_points <- seq(a, b, length = n + 1)
vAR_CSLAB_h <- (b - a) / n
vAR_CSLAB_area <- 0
for (i in seq_len(n)) {
vAR_CSLAB_area <- vAR_CSLAB_area + h / 2 * (f(vAR_CSLAB_points[i]) + f(vAR_CSLAB_points[i + 1]))
}
vAR_CSLAB_area
}
vAR_CSLAB_midpoint_composite(sin, 0, pi, n = 10)
vAR_CSLAB_midpoint_composite(sin, 0, pi, n = 100)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONALS_V1
Purpose : A Program for Functionals in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 18:23 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Functionals in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_randomise <- function(f) f(runif(1e3))
vAR_CSLAB_randomise(mean)
vAR_CSLAB_randomise(sum)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONALS_LAPPLY_V1
Purpose : A Program for Functionals - lapply in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 18:41 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Functionals - lapply in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_l <- replicate(20, runif(sample(1:10, 1)), simplify = FALSE)
# With a for loop
vAR_CSLAB_out <- vector("list", length(vAR_CSLAB_l))
for (i in seq_along(vAR_CSLAB_l)) {
vAR_CSLAB_out[[i]] <- length(vAR_CSLAB_l[[i]])
}
unlist(vAR_CSLAB_out)
unlist(lapply(vAR_CSLAB_l, length))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONALS_LOOPING_PATTERNS_V1
Purpose : A Program for Looping Patterns in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 18:59 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Looping Patterns in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_xs <- runif(1e3)
vAR_CSLAB_res <- c()
for (x in vAR_CSLAB_xs) {
# This is slow!
vAR_CSLAB_res <- c(vAR_CSLAB_res, sqrt(vAR_CSLAB_x))
}
vAR_CSLAB_res <- numeric(length(vAR_CSLAB_xs))
for (i in seq_along(vAR_CSLAB_xs)) {
vAR_CSLAB_res[i] <- sqrt(vAR_CSLAB_xs[i])
}
vAR_CSLAB_res
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONALS_ROLLING_COMPUTATIONS_V1
Purpose : A Program for Rolling Computations in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 19:14 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Rolling Computations in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_rollmean <- function(x, n) {
vAR_CSLAB_out <- rep(NA, length(x))
vAR_CSLAB_offset <- trunc(n / 2)
vAR_CSLAB_out
}
vAR_CSLAB_x <- seq(1, 3, length = 1e2) + runif(1e2)
plot(vAR_CSLAB_x)
lines(vAR_CSLAB_rollmean(x, 5), col = "blue", lwd = 2)
lines(vAR_CSLAB_rollmean(x, 10), col = "red", lwd = 2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONALS_PARALLELISATION_V1
Purpose : A Program for Parallelisation in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 19:29 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Parallelisation in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_lapply3 <- function(x, f, ...) {
vAR_CSLAB_out <- vector("list", length(x))
for (i in sample(seq_along(x))) {
vAR_CSLAB_out[[i]] <- f(x[[i]], ...)
}
vAR_CSLAB_out
}
unlist(lapply(1:10, sqrt))
unlist(vAR_CSLAB_lapply3(1:10, sqrt))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONALS_PREDICATE_FUNCTIONALS_V1
Purpose : A Program for Predicate Functionals in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 19:41 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Predicate Functionals in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_where <- function(f, x) {
vapply(x, f, logical(1))
}
vAR_CSLAB_df <- data.frame(x = 1:3, y = c("a", "b", "c"))
vAR_CSLAB_where(is.factor, vAR_CSLAB_df)
str(Filter(is.factor, vAR_CSLAB_df))
str(Find(is.factor, vAR_CSLAB_df))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONALS_MATHEMATICAL_FUNCTIONALS_V1
Purpose : A Program for Mathematical Functionals in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 19:50 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Mathematical Functionals in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
integrate(sin, 0, pi)
str(uniroot(sin, pi * c(1 / 2, 3 / 2)))
str(optimise(sin, c(0, 2 * pi)))
str(optimise(sin, c(0, pi), maximum = TRUE))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_FUNCTIONALS_RECURSIVE_RELATIONSHIPS_V1
Purpose : A Program for Recursive Relationships in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 20:15 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Recursive Relationships in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_exps <- function(x, alpha) {
vAR_CSLAB_s <- numeric(length(x) + 1)
for (i in seq_along(vAR_CSLAB_s)) {
if (i == 1) {
vAR_CSLAB_s[i] <- x[i]
} else {
vAR_CSLAB_s[i] <- alpha * x[i] + (1 - alpha) * vAR_CSLAB_s[i - 1]
}
}
vAR_CSLAB_s
}
vAR_CSLAB_x <- runif(6)
vAR_CSLAB_exps(vAR_CSLAB_x, 0.5)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NON_STANDARD_EVALUATION_V1
Purpose : A Program for Non Standard Evaluations in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 20:29 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Non Standard Evaluations in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x <- seq(0, 2 * pi, length = 100)
vAR_CSLAB_sinx <- sin(vAR_CSLAB_x)
plot(vAR_CSLAB_x, vAR_CSLAB_sinx, type = "l")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NON_STANDARD_EVALUATION_CAPTURING_EXPRESSIONS_V1
Purpose : A Program for Capturing Expressions in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 20:42 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Capturing Expressions in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_f <- function(x) {
substitute(x)
}
vAR_CSLAB_f(1:10)
vAR_CSLAB_x <- 10
vAR_CSLAB_f(vAR_CSLAB_x)
vAR_CSLAB_y <- 13
vAR_CSLAB_f(vAR_CSLAB_x + vAR_CSLAB_y^2)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NON_STANDARD_EVALUATION_IN_SUBSET_V1
Purpose : A Program for Non Standard Evaluation in Subset in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 21:01 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Non Standard Evaluation in Subset in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_sample_df <- data.frame(a = 1:5, b = 5:1, c = c(5, 3, 1, 4, 1))
subset(vAR_CSLAB_sample_df, a >= 4)
subset(vAR_CSLAB_sample_df, b == c)
quote(1:10)
quote(x)
quote(x + y^2)
eval(quote(x <- 1))
eval(quote(x))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NON_STANDARD_EVALUATION_CALLING_ONE_FUNCTION_FROM_ANOTHER_V1
Purpose : A Program for Calling one function from Another in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 21:17 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Calling one function from Another in Subset in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_sample_df <- data.frame(a = 1:5, b = 5:1, c = c(5, 3, 1, 4, 1))
vAR_CSLAB_subset2 <- function(x, condition) {
vAR_CSLAB_condition_call <- substitute(condition)
r <- eval(vAR_CSLAB_condition_call, x, parent.frame())
x[r, ]
}
vAR_CSLAB_scramble <- function(x) x[sample(nrow(x)), ]
vAR_CSLAB_subscramble <- function(x, condition) {
vAR_CSLAB_scramble(vAR_CSLAB_subset2(x, condition))
}
vAR_CSLAB_subscramble
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_PERFORMANCE_IMPROVEMENT_MICROBENCHMARKING_V1
Purpose : A Program for Performance Improvement using Microbenchmarking in R
Author : DeepSphere.AI, Inc.d
Date and Time : 17/01/2019 21:33 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Performance Improvement using Microbenchmarking in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("microbenchmark")
library(microbenchmark)
vAR_CSLAB_x <- runif(100)
microbenchmark(
sqrt(vAR_CSLAB_x),
vAR_CSLAB_x ^ 0.5
)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_INSTALL_TENSORFLOW_FOR_R_V1
Purpose : A Program for Installing Tensorflow in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 21:48 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Installing Tensorflow in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("devtools")
install.packages("tensorflow")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_INSTALL_TENSORFLOW_BASIC_HELLO_WORLD_V1
Purpose : A Program for Basic Hello World in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 21:55 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Basic Hello World in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_hello <- tf$constant('Hello, World!')
vAR_CSLAB_sess$run(vAR_CSLAB_hello)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_DENSE_TENSORS_V1
Purpose : A Program for Dense Tensors in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 22:16 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Dense Tensors in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorr)
vAR_CSLAB_dims <- c(2,2,2)
vAR_CSLAB_arr <- array(c(10,0,0,0,20,0,0,0), vAR_CSLAB_dims)
vAR_CSLAB_z <- dtensor(vAR_CSLAB_arr)
vAR_CSLAB_z
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_SPARSE_TENSORS_V1
Purpose : A Program for Sparse Tensors in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 22:04 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Sparse Tensors in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("tensorr")
library(tensorr)
vAR_CSLAB_subs <- list(c(1,1,1), c(1,1,2))
vAR_CSLAB_vals <- c(10, 20)
vAR_CSLAB_dims <- c(2,2,2)
vAR_CSLAB_x <- sptensor(vAR_CSLAB_subs, vAR_CSLAB_vals, vAR_CSLAB_dims)
vAR_CSLAB_x
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_STANDARD_INDEXING_V1
Purpose : A Program for Standard Indexing in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 22:47 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Standard Indexing in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x[1,1,1]
vAR_CSLAB_x[1,2,2]
vAR_CSLAB_x[1,,]
vAR_CSLAB_x[1,1:2,1:2]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_LINEAR_INDEXING_V1
Purpose : A Program for Linear Indexing in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 23:02 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Linear Indexing in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
array(1:8, c(2,2,2))
vAR_CSLAB_x[c(1,2,3)]
vAR_CSLAB_x[1:3]
vAR_CSLAB_x[c(1,5)] <- c(-10, -20)
vAR_CSLAB_subs <- list(c(1,1,1), c(1,2,1), c(1,1,2))
vAR_CSLAB_x[vAR_CSLAB_subs]
vAR_CSLAB_x[vAR_CSLAB_subs] <- c(50, 60, 70)
vAR_CSLAB_x
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_NUMERIC_CONSTANTS_V1
Purpose : A Program for Numeric Constants in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 23:17 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Numeric Constants in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
typeof(5)
typeof(5L)
typeof(5i)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_CHARACTER_CONSTANTS_V1
Purpose : A Program for Character Constants in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 23:23 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Character Constants in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
'example'
typeof("5")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_BUILTIN_CONSTANTS_V1
Purpose : A Program for Built-in Constants in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 23:31 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Built-in Constants in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
LETTERS
letters
pi
month.name
month.abb
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_UNFOLDED_TENSORS_V1
Purpose : A Program for Unfolded Tensors in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 22:31 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Unfolded Tensors in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorr)
vAR_CSLAB_subs <- list(c(1,1,1), c(1,1,2))
vAR_CSLAB_vals <- c(10, 20)
vAR_CSLAB_dims <- c(2,2,2)
vAR_CSLAB_x <- sptensor(vAR_CSLAB_subs, vAR_CSLAB_vals, vAR_CSLAB_dims)
unfold(vAR_CSLAB_x, 1)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_VARIABLES_V1
Purpose : A Program for Tensorflow Variables in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 23:48 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Tensorflow Variables in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_x_data <- runif(100, min=0, max=1)
vAR_CSLAB_y_data <- vAR_CSLAB_x_data * 0.1 + 0.3
vAR_CSLAB_W <- tf$Variable(tf$random_uniform(shape(1L), -1.0, 1.0))
vAR_CSLAB_b <- tf$Variable(tf$zeros(shape(1L)))
vAR_CSLAB_y <- vAR_CSLAB_W * vAR_CSLAB_x_data + vAR_CSLAB_b
vAR_CSLAB_y
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_PLACEHOLDERS_V1
Purpose : A Program for Tensorflow Placeholders in R
Author : DeepSphere.AI, Inc.
Date and Time : 17/01/2019 00:05 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Tensorflow Variables in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_X = tf$placeholder(tf$float32, name = "X")
vAR_CSLAB_Y = tf$placeholder(tf$float32, name = "Y")
vAR_CSLAB_X + vAR_CSLAB_Y
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_GRAPHS_V1
Purpose : A Program for Tensorflow Graphs in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 9:09 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Tensorflow Graphs in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
pi = tf$constant(3.14, name="pi")
r = tf$placeholder(tf$float32, name="r")
a = pi * r * r
sess = tf$Session()
sess$run(pi)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_SESSION_V1
Purpose : A Program for Tensorflow Sessions in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 9:21 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Tensorflow Sessions in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x_data <- runif(100, min=0, max=1)
vAR_CSLAB_y_data <- vAR_CSLAB_x_data * 0.1 + 0.3
vAR_CSLAB_W <- tf$Variable(tf$random_uniform(shape(1L), -1.0, 1.0))
vAR_CSLAB_b <- tf$Variable(tf$zeros(shape(1L)))
vAR_CSLAB_y <- vAR_CSLAB_W * vAR_CSLAB_x_data + vAR_CSLAB_b
vAR_CSLAB_loss <- tf$reduce_mean((vAR_CSLAB_y - vAR_CSLAB_y_data) ^ 2)
vAR_CSLAB_optimizer <- tf$train$GradientDescentOptimizer(0.5)
vAR_CSLAB_train <- vAR_CSLAB_optimizer$minimize(vAR_CSLAB_loss)
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(tf$global_variables_initializer())
for (step in 1:201) {
vAR_CSLAB_sess$run(vAR_CSLAB_train)
if (step %% 20 == 0)
cat(step, "-", vAR_CSLAB_sess$run(vAR_CSLAB_W), vAR_CSLAB_sess$run(vAR_CSLAB_b), "\n")
}
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_DATATYPE_V1
Purpose : A Program for Tensorflow Datatype in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 9:28 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Tensorflow Datatype in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_a <- tf$constant(3, dtype = tf$float32, name = "a")
vAR_CSLAB_b <- tf$constant(5, dtype = tf$float32, name = "b")
vAR_CSLAB_c <- list(vAR_CSLAB_a, vAR_CSLAB_b)
vAR_CSLAB_sess = tf$Session()
print(vAR_CSLAB_sess$run(vAR_CSLAB_c))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_ADD_TWO_CONSATNTS_V1
Purpose : A Program for Adding Two Constants in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 9:33 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Adding Two Constants in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_a = tf$constant(3)
vAR_CSLAB_b = tf$constant(5)
vAR_CSLAB_c = vAR_CSLAB_a + vAR_CSLAB_b
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(vAR_CSLAB_c)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_MULTIPLY_TWO_CONSATNTS_V1
Purpose : A Program for Multiplying Two Constants in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 9:45 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Multiplying Two Constants in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_a = tf$constant(3)
vAR_CSLAB_b = tf$constant(5)
vAR_CSLAB_c = vAR_CSLAB_a * vAR_CSLAB_b
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(vAR_CSLAB_c)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_DIVIDE_TWO_CONSATNTS_V1
Purpose : A Program for Dividing Two Constants in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 9:57 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Dividing Two Constants in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_a = tf$constant(10)
vAR_CSLAB_b = tf$constant(5)
vAR_CSLAB_c = vAR_CSLAB_a/vAR_CSLAB_b
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(vAR_CSLAB_c)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_MATRIX_MULTIPLICATION_V1
Purpose : A Program for Matrix Multiplication in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 10:09 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Matrix Multiplication in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_a <- tf$constant(c(1, 2), shape = c(1L, 2L), name = "a")
vAR_CSLAB_b <- tf$constant(c(3, 4), shape = c(2L, 1L), name = "b")
vAR_CSLAB_c <- tf$matmul(vAR_CSLAB_a, vAR_CSLAB_b, name = "mat_mul")
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(vAR_CSLAB_c)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_MATRIX_TRANSPOSE_V1
Purpose : A Program for Transposing a Matrix in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 10:23 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Transposing a Matrix in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_a <- tf$constant(c(1, 2, 3, 4), shape = c(2L, 2L), name = "a")
vAR_CSLAB_b <- tf$transpose(vAR_CSLAB_a, name = "transpose")
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(vAR_CSLAB_b)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_MATRIX_INVERSE_V1
Purpose : A Program for Matrix Inverse in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 10:37 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Matrix Inverse in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_a <- tf$constant(c(1, 2, 3, 4), shape = c(2L, 2L))
vAR_CSLAB_b <- tf$matrix_inverse(vAR_CSLAB_a, name = "inverse")
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(vAR_CSLAB_b)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_MATRIX_DETERMINANT_V1
Purpose : A Program for Matrix Determinant in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 10:50 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Matrix Determinant in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_a <- tf$constant(c(1, 2, 3, 4), shape = c(2L, 2L))
vAR_CSLAB_b <- tf$matrix_determinant(vAR_CSLAB_a, name = "determinant")
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(vAR_CSLAB_b)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_ARRAY_OPERATION_CONCAT_V1
Purpose : A Program for Array Operations Concat in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 11:03 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Array Operations Concat in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_a <- tf$constant(c(1, 2), shape = c(1L, 2L), name = "a")
vAR_CSLAB_b <- tf$constant(c(3, 4), shape = c(1L, 2L), name = "b")
vAR_CSLAB_c <- tf$concat(list(vAR_CSLAB_a,vAR_CSLAB_b), axis = 0L, name = "concat")
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(vAR_CSLAB_c)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_ARRAY_OPERATION_SLICE_FROM_TENSOR_V1
Purpose : A Program for Array Operations Slice from a Tensor in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 11:16 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Array Operations Slice from a Tensor in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_a <- tf$constant(c(1, 2, 3, 4, 5, 6), shape = c(1L, 2L, 3L), name = "a")
vAR_CSLAB_c <- tf$slice(vAR_CSLAB_a, begin = c(0L,0L,0L), size = c(1L,1L,3L), name = "slice")
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(vAR_CSLAB_c)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_ARRAY_OPERATION_SPLIT_A_TENSOR_V1
Purpose : A Program for Array Operations Split a Tensor in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 11:28 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Array Operations Split a Tensor in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_a <- tf$constant(1:20, shape = c(4L, 5L), name = "a")
vAR_CSLAB_l <- tf$split(vAR_CSLAB_a, c(1L, 2L, 2L), axis = 1L)
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(vAR_CSLAB_l)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_ARRAY_OPERATION_SHAPE_OF_TENSOR_V1
Purpose : A Program for Array Operations Shape of a Tensor in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 11:39 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Array Operations Shape of a Tensor in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_a <- tf$constant(1:20, shape = c(4L, 5L), name = "a")
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(tf$shape(vAR_CSLAB_a))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_LINEAR_REGRESSION_V1
Purpose : A Program for Linear Regression in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 11:55 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Linear Regression in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tensorflow)
vAR_CSLAB_p <- 3L
vAR_CSLAB_X <- tf$placeholder("float", shape = shape(NULL, vAR_CSLAB_p), name = "x-data")
vAR_CSLAB_Y <- tf$placeholder("float", name = "y-data")
vAR_CSLAB_W <- tf$Variable(tf$zeros(list(vAR_CSLAB_p, 1L)))
vAR_CSLAB_b <- tf$Variable(tf$zeros(list(1L)))
vAR_CSLAB_Y_hat <- tf$add(tf$matmul(vAR_CSLAB_X, vAR_CSLAB_W), vAR_CSLAB_b)
vAR_CSLAB_cost <- tf$reduce_mean(tf$square(vAR_CSLAB_Y_hat - vAR_CSLAB_Y))
vAR_CSLAB_generator <- tf$train$GradientDescentOptimizer(learning_rate = 0.01)
vAR_CSLAB_optimizer <- vAR_CSLAB_generator$minimize(vAR_CSLAB_cost)
vAR_CSLAB_init <- tf$global_variables_initializer()
vAR_CSLAB_session <- tf$Session()
vAR_CSLAB_session$run(vAR_CSLAB_init)
set.seed(123)
vAR_CSLAB_n <- 250
vAR_CSLAB_x <- matrix(runif(vAR_CSLAB_p * vAR_CSLAB_n), nrow = vAR_CSLAB_n)
vAR_CSLAB_y <- matrix(2 * vAR_CSLAB_x[, 1] + 1 + (rnorm(vAR_CSLAB_n, sd = 0.25)))
vAR_CSLAB_feed_dict <- dict(X = vAR_CSLAB_x, Y = vAR_CSLAB_y)
vAR_CSLAB_epsilon <- .Machine$double.eps
vAR_CSLAB_last_cost <- Inf
vAR_CSLAB_r_model <- lm(vAR_CSLAB_y ~ vAR_CSLAB_x)
vAR_CSLAB_tf_coef <- c(vAR_CSLAB_session$run(vAR_CSLAB_b), vAR_CSLAB_session$run(vAR_CSLAB_W))
vAR_CSLAB_r_coef <- vAR_CSLAB_r_model$coefficients
print(rbind(vAR_CSLAB_tf_coef, vAR_CSLAB_r_coef))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_LOGISTIC_REGRESSION_V1
Purpose : A Program for Logistic Regression in R
Author : DeepSphere.AI, Inc.
Date and Time : 18/01/2019 12:18 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Logistic Regression in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("caret")
library(caret)
library(MASS)
vAR_CSLAB_cat = cats[c("Sex", "Bwt")]
vAR_CSLAB_y = with(vAR_CSLAB_cat, model.matrix(~ Sex + 0))
vAR_CSLAB_x = as.matrix(vAR_CSLAB_cat[,2])
vAR_CSLAB_trainIndex = createDataPartition(vAR_CSLAB_x,
p=0.7, list=FALSE,times=1)
vAR_CSLAB_x_train = as.matrix(vAR_CSLAB_x[vAR_CSLAB_trainIndex,])
vAR_CSLAB_x_test = as.matrix(vAR_CSLAB_x[-vAR_CSLAB_trainIndex,])
vAR_CSLAB_y_train = as.matrix(vAR_CSLAB_y[vAR_CSLAB_trainIndex,])
vAR_CSLAB_y_test = as.matrix(vAR_CSLAB_y[-vAR_CSLAB_trainIndex,])
library(tensorflow)
vAR_CSLAB_X <- tf$placeholder(tf$float32, shape(NULL, 1L))
vAR_CSLAB_Y = tf$placeholder(tf$float32, shape(NULL, 2L), name = "Y")
vAR_CSLAB_W = tf$Variable(tf$random_normal(shape(1L,2L),stddev = 1.0), name = "weghts")
vAR_CSLAB_b = tf$Variable(tf$zeros(shape(2L)), name = "bias")
vAR_CSLAB_logits = tf$add(tf$matmul(vAR_CSLAB_X, vAR_CSLAB_W), vAR_CSLAB_b)
vAR_CSLAB_pred = tf$nn$sigmoid(vAR_CSLAB_logits)
vAR_CSLAB_entropy = tf$nn$sigmoid_cross_entropy_with_logits(labels = vAR_CSLAB_Y, logits = vAR_CSLAB_logits)
vAR_CSLAB_loss = tf$reduce_mean(vAR_CSLAB_entropy)
vAR_CSLAB_optimizer = tf$train$GradientDescentOptimizer(learning_rate = 0.01)$minimize(vAR_CSLAB_loss)
vAR_CSLAB_init_op = tf$global_variables_initializer()
vAR_CSLAB_correct_prediction <- tf$equal(tf$argmax(vAR_CSLAB_pred, 1L), tf$argmax(vAR_CSLAB_Y, 1L))
vAR_CSLAB_accuracy <- tf$reduce_mean(tf$cast(vAR_CSLAB_correct_prediction, tf$float32))
vAR_CSLAB_sess = tf$Session()
vAR_CSLAB_sess$run(vAR_CSLAB_init_op)
print(vAR_CSLAB_accuracy)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_TENSORFLOW_DECISION_TREE_V1
Purpose : A Program for Decision Tree in R
Author : DeepSphere.AI, Inc.
Date and Time : 21/01/2019 9:32 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Logistic Regression in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("caret")
library(caret)
#library(rpart.plot)
vAR_CSLAB_data_url <- c("https://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data")
download.file(url = vAR_CSLAB_data_url, destfile = "car.data")
vAR_CSLAB_car_df <- read.csv("car.data", sep = ',', header = FALSE)
#head(vAR_CSLAB_car_df)
str(vAR_CSLAB_car_df)
set.seed(3033)
vAR_CSLAB_intrain <- createDataPartition(y = vAR_CSLAB_car_df$V7, p= 0.7, list = FALSE)
vAR_CSLAB_training <- vAR_CSLAB_car_df[vAR_CSLAB_intrain,]
vAR_CSLAB_testing <- vAR_CSLAB_car_df[-vAR_CSLAB_intrain,]
dim(vAR_CSLAB_training); dim(vAR_CSLAB_testing)
vAR_CSLAB_trctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 3)
set.seed(3333)
vAR_CSLAB_dtree_fit <- train(V7 ~., data = vAR_CSLAB_training, method = "rpart",parms = list(split = "information"),trControl=vAR_CSLAB_trctrl,tuneLength = 10)
vAR_CSLAB_dtree_fit
#prp(dtree_fit$finalModel, box.palette = "Reds", tweak = 1.2)
vAR_CSLAB_testing[1,]
predict(vAR_CSLAB_dtree_fit, newdata = vAR_CSLAB_testing[1,])
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_INSTALL_PACKAGES_V1
Purpose : A Program for Installing NLP Packages in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 9:32 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Installing NLP Packages in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("rJava")
install.packages(c("NLP", "openNLP", "RWeka", "qdap"))
install.packages("openNLPmodels.en", repos = "http://datacube.wu.ac.at/", type = "source")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_COUNT_WORD_FREQUENCY_V1
Purpose : A Program for Counting Word Frequency using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 9:44 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Counting Word Frequency using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_text <- "Text mining usually involves the process of structuring the input text. The overarching goal is, essentially, to turn text into data for analysis, via application of natural language processing (NLP) and analytical methods."
library(qdap)
vAR_CSLAB_frequent_terms <- freq_terms(vAR_CSLAB_text, 3)
vAR_CSLAB_frequent_terms
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_LOADING_TEXT_DATA_TO_TDM_V1
Purpose : A Program for Loading Textual Data to TDM using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 9:57 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Loading Textual Data to TDM using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(readr)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Tweets/Tweets_1.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_tweets <- read_csv(vAR_CSLAB_FILE_PATH)
vAR_CSLAB_tweets
str(vAR_CSLAB_tweets)
vAR_CSLAB_tweets_text <- vAR_CSLAB_tweets$text
str(vAR_CSLAB_tweets_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_BUILDING_A_CORPUS_V1
Purpose : A Program for Building a Corpus using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 10:12 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Building a Corpus using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(tm)
vAR_CSLAB_tweets_source <- VectorSource(vAR_CSLAB_tweets_text)
# Make a volatile corpus: tweets_corpus
vAR_CSLAB_tweets_corpus <- VCorpus(vAR_CSLAB_tweets_source)
# Print out the tweets_corpus
vAR_CSLAB_tweets_corpus
vAR_CSLAB_tweets_corpus[[15]]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_TEXT_PREPROCESSING_V1
Purpose : A Program for Text Preprocessing using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 10:25 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Text Preprocessing using in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
# Create the object: text
vAR_CSLAB_text <- "<b>She</b> woke up at 6 A.M. It\'s so early! She was only 10% awake and began drinking coffee in front of her computer."
# All lowercase
tolower(vAR_CSLAB_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_TEXT_PREPROCESSING_REMOVE_PUNCTUATION_V1
Purpose : A Program for Removing Punctuation from a Text using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 10:37 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Removing Punctuation from a Text using in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
## Create the object: text
vAR_CSLAB_text <- "<b>She</b> woke up at 6 A.M. It\'s so early! She was only 10% awake and began drinking coffee in front of her computer."
# All lowercase
tolower(vAR_CSLAB_text)
removePunctuation(vAR_CSLAB_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_TEXT_PREPROCESSING_REMOVE_NUMBERS_V1
Purpose : A Program for Removing Numbers from a Text using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 10:42 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Removing Numbers from a Text using in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
# Create the object: text
vAR_CSLAB_text <- "<b>She</b> woke up at 6 A.M. It\'s so early! She was only 10% awake and began drinking coffee in front of her computer."
# All lowercase
tolower(vAR_CSLAB_text)
removePunctuation(vAR_CSLAB_text)
removeNumbers(vAR_CSLAB_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_TEXT_PREPROCESSING_REMOVE_WHITESPACES_V1
Purpose : A Program for Removing Whitespaces from a Text using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 10:48 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Removing Whitespaces from a Text using in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
# Create the object: text
vAR_CSLAB_text <- "<b>She</b> woke up at 6 A.M. It\'s so early! She was only 10% awake and began drinking coffee in front of her computer."
# All lowercase
tolower(vAR_CSLAB_text)
removePunctuation(vAR_CSLAB_text)
removeNumbers(vAR_CSLAB_text)
stripWhitespace(vAR_CSLAB_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_TEXT_PREPROCESSING_REMOVE_WITHIN_BRACKETS_V1
Purpose : A Program for Removing Text Within Brackets using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 10:59 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Removing Text Within Brackets using in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(qdap)
# Create the object: text
vAR_CSLAB_text <- "<b>She</b> woke up at 6 A.M. It\'s so early! She was only 10% awake and began drinking coffee in front of her computer."
# All lowercase
tolower(vAR_CSLAB_text)
removePunctuation(vAR_CSLAB_text)
removeNumbers(vAR_CSLAB_text)
stripWhitespace(vAR_CSLAB_text)
bracketX(vAR_CSLAB_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_TEXT_PREPROCESSING_REPLACE_NUMBERS_V1
Purpose : A Program for Replacing Numbers in Texts using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 11:11 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Replacing Numbers in Texts using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
# Create the object: text
vAR_CSLAB_text <- "<b>She</b> woke up at 6 A.M. It\'s so early! She was only 10% awake and began drinking coffee in front of her computer."
# All lowercase
tolower(vAR_CSLAB_text)
removePunctuation(vAR_CSLAB_text)
removeNumbers(vAR_CSLAB_text)
stripWhitespace(vAR_CSLAB_text)
bracketX(vAR_CSLAB_text)
replace_number(vAR_CSLAB_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_TEXT_PREPROCESSING_REPLACE_ABBREVIATIONS_V1
Purpose : A Program for Replacing Abbreviations in Texts using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 11:24 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Replacing Numbers in Texts using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
# Create the object: text
vAR_CSLAB_text <- "<b>She</b> woke up at 6 A.M. It\'s so early! She was only 10% awake and began drinking coffee in front of her computer."
# All lowercase
tolower(vAR_CSLAB_text)
removePunctuation(vAR_CSLAB_text)
removeNumbers(vAR_CSLAB_text)
stripWhitespace(vAR_CSLAB_text)
bracketX(vAR_CSLAB_text)
replace_number(vAR_CSLAB_text)
replace_abbreviation(vAR_CSLAB_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_TEXT_PREPROCESSING_REPLACE_CONTRACTIONS_V1
Purpose : A Program for Replacing Contractions in Texts using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 11:37 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Replacing Contractions in Texts using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
# Create the object: text
vAR_CSLAB_text <- "<b>She</b> woke up at 6 A.M. It\'s so early! She was only 10% awake and began drinking coffee in front of her computer."
# All lowercase
tolower(vAR_CSLAB_text)
removePunctuation(vAR_CSLAB_text)
removeNumbers(vAR_CSLAB_text)
stripWhitespace(vAR_CSLAB_text)
bracketX(vAR_CSLAB_text)
replace_number(vAR_CSLAB_text)
replace_abbreviation(vAR_CSLAB_text)
replace_contraction(vAR_CSLAB_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_REMOVING_STOP_WORDS_V1
Purpose : A Program for Removing Stop Words from a Text using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 11:52 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Removing Stop Words from a Text using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_all_stops <- c("word1", "word2", stopwords("en"))
stopwords("en")
# Print text without standard stop words
removeWords(vAR_CSLAB_text, stopwords("en"))
# Add "coffee" and "bean" to the list: new_stops
vAR_CSLAB_new_stops <- c("coffee", "bean", stopwords("en"))
# Remove stop words from text
removeWords(vAR_CSLAB_text, vAR_CSLAB_new_stops)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_WORD_STEMMING_ON_TEXTS_V1
Purpose : A Program for Word Stemming on a Text Data using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 12:04 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Removing Stop Words on a Text Data using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("SnowballC")
library(SnowballC)
stemDocument(c("computational", "computers", "computation"))
# Create complicate
vAR_CSLAB_complicate <- c("complicated", "complication", "complicatedly")
# Perform word stemming: stem_doc
vAR_CSLAB_stem_doc <- stemDocument(vAR_CSLAB_complicate)
# Create the completion dictionary: comp_dict
vAR_CSLAB_comp_dict <- ("complicate")
# Perform stem completion: complete_text
vAR_CSLAB_complete_text <- stemCompletion(vAR_CSLAB_stem_doc, vAR_CSLAB_comp_dict)
# Print complete_text
vAR_CSLAB_complete_text
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_WORD_STEMMING_ON_A_DOCUMENT_V1
Purpose : A Program for Word Stemming on a Document using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 12:22 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Word Stemming on a Document using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
stemDocument("In a complicated haste, Tom rushed to fix a new complication, too complicatedly.")
vAR_CSLAB_text_data <- "In a complicated haste, Tom rushed to fix a new complication, too complicatedly."
# Remove punctuation: rm_punc
vAR_CSLAB_rm_punc <- removePunctuation(vAR_CSLAB_text_data)
# Create character vector: n_char_vec
vAR_CSLAB_n_char_vec <- unlist(strsplit(vAR_CSLAB_rm_punc, split = ' '))
# Perform word stemming: stem_doc
vAR_CSLAB_stem_doc <- stemDocument(vAR_CSLAB_n_char_vec)
# Print stem_doc
vAR_CSLAB_stem_doc
# Create the completion dictionary: comp_dict
vAR_CSLAB_comp_dict <- c("In", "a", "complicate", "haste", "Tom", "rush", "to", "fix", "new", "too")
# Re-complete stemmed document: complete_doc
vAR_CSLAB_complete_doc <- stemCompletion(vAR_CSLAB_stem_doc, vAR_CSLAB_comp_dict)
# Print complete_doc
vAR_CSLAB_complete_doc
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_PREPROCESSING_TO_A_CORPUS_V1
Purpose : A Program for Preprocessing to a Corpus using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 12:38 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Preprocessing to a Corpus using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(readr)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Tweets/Tweets_1.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="/")
vAR_CSLAB_tweets <- read_csv(vAR_CSLAB_FILE_PATH)
vAR_CSLAB_tweets_text <- vAR_CSLAB_tweets$text
vAR_CSLAB_tweets_text
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_CREATING_A_DOCUMENT_TERM_MATRIX_V1
Purpose : A Program for Creating a Document Term Matrix using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 12:52 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Creating a Document Term Matrix using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("tm")
library(tm)
library(readr)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Tweets/Tweets_1.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_tweets <- read_csv(vAR_CSLAB_FILE_PATH)
vAR_CSLAB_tweets_text <- vAR_CSLAB_tweets$text
vAR_CSLAB_tweets_source <- VectorSource(vAR_CSLAB_tweets_text)
# Make a volatile corpus: tweets_corpus
vAR_CSLAB_tweets_corpus <- VCorpus(vAR_CSLAB_tweets_source)
vAR_CSLAB_clean_corpus <- function(vAR_CSLAB_corpus){
vAR_CSLAB_corpus <- tm_map(vAR_CSLAB_tweets_corpus, stripWhitespace)
vAR_CSLAB_corpus <- tm_map(vAR_CSLAB_tweets_corpus, removePunctuation)
vAR_CSLAB_corpus <- tm_map(vAR_CSLAB_tweets_corpus, content_transformer(tolower))
vAR_CSLAB_corpus <- tm_map(vAR_CSLAB_tweets_corpus, removeWords, stopwords("en"))
return(vAR_CSLAB_corpus)
}
# Apply your customized function to the tweet_corp: clean_corp
vAR_CSLAB_clean_corp <- vAR_CSLAB_clean_corpus(vAR_CSLAB_corpus)
# Create the dtm from the corpus:
vAR_CSLAB_tweets_dtm <- DocumentTermMatrix(vAR_CSLAB_clean_corp)
# Print out tweets_dtm data
vAR_CSLAB_tweets_dtm
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_CONVERT_DOCUMENT_TERM_TO_A_MATRIX_V1
Purpose : A Program for Converting Document Term to a Matrix using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 13:13 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Converting Document Term to a Matrix using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("tm")
library(tm)
library(readr)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Tweets/Tweets_1.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_tweets <- read_csv(vAR_CSLAB_FILE_PATH)
vAR_CSLAB_tweets_text <- vAR_CSLAB_tweets$text
vAR_CSLAB_tweets_source <- VectorSource(vAR_CSLAB_tweets_text)
# Make a volatile corpus: tweets_corpus
vAR_CSLAB_tweets_corpus <- VCorpus(vAR_CSLAB_tweets_source)
vAR_CSLAB_clean_corpus <- function(corpus){
vAR_CSLAB_corpus <- tm_map(corpus, stripWhitespace)
vAR_CSLAB_corpus <- tm_map(corpus, removePunctuation)
vAR_CSLAB_corpus <- tm_map(corpus, content_transformer(tolower))
vAR_CSLAB_corpus <- tm_map(corpus, removeWords, stopwords("en"))
return(vAR_CSLAB_corpus)
}
# Apply your customized function to the tweet_corp: clean_corp
vAR_CSLAB_clean_corp <- vAR_CSLAB_clean_corpus(vAR_CSLAB_tweets_corpus)
# Create the dtm from the corpus:
vAR_CSLAB_tweets_dtm <- DocumentTermMatrix(vAR_CSLAB_clean_corp)
# Print out tweets_dtm data
vAR_CSLAB_tweets_dtm
# Print the dimensions of tweets_m
dim(vAR_CSLAB_tweets_dtm)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_CREATING_A_DOCUMENT_TERM_TO_A_MATRIX_V1
Purpose : A Program for Creating a Term Document Matrix using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 14:04 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Creating a Term Document Matrix using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("tm")
library(tm)
library(readr)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Tweets/Tweets_1.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_tweets <- read_csv(vAR_CSLAB_FILE_PATH)
vAR_CSLAB_tweets_text <- vAR_CSLAB_tweets$text
vAR_CSLAB_tweets_source <- VectorSource(vAR_CSLAB_tweets_text)
# Make a volatile corpus: tweets_corpus
vAR_CSLAB_tweets_corpus <- VCorpus(vAR_CSLAB_tweets_source)
vAR_CSLAB_clean_corpus <- function(corpus){
vAR_CSLAB_corpus <- tm_map(corpus, stripWhitespace)
vAR_CSLAB_corpus <- tm_map(corpus, removePunctuation)
vAR_CSLAB_corpus <- tm_map(corpus, content_transformer(tolower))
vAR_CSLAB_corpus <- tm_map(corpus, removeWords, stopwords("en"))
return(vAR_CSLAB_corpus)
}
# Apply your customized function to the tweet_corp: clean_corp
vAR_CSLAB_clean_corp <- vAR_CSLAB_clean_corpus(vAR_CSLAB_tweets_corpus)
vAR_CSLAB_clean_corp
vAR_CSLAB_tweets_tdm <- TermDocumentMatrix(vAR_CSLAB_clean_corp)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_CONVERT_TERM_DOCUMENT_TO_A_MATRIX_V1
Purpose : A Program for Converting a Term Document to Matrix using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 14:22 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Converting a Term Document to Matrix using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("tm")
library(tm)
library(readr)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "Tweets/Tweets_1.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_tweets <- read_csv(vAR_CSLAB_FILE_PATH)
vAR_CSLAB_tweets_text <- vAR_CSLAB_tweets$text
vAR_CSLAB_tweets_source <- VectorSource(vAR_CSLAB_tweets_text)
# Make a volatile corpus: tweets_corpus
vAR_CSLAB_tweets_corpus <- VCorpus(vAR_CSLAB_tweets_source)
vAR_CSLAB_clean_corpus <- function(corpus){
vAR_CSLAB_corpus <- tm_map(corpus, stripWhitespace)
vAR_CSLAB_corpus <- tm_map(corpus, removePunctuation)
vAR_CSLAB_corpus <- tm_map(corpus, content_transformer(tolower))
vAR_CSLAB_corpus <- tm_map(corpus, removeWords, stopwords("en"))
return(vAR_CSLAB_corpus)
}
# Apply your customized function to the tweet_corp: clean_corp
vAR_CSLAB_clean_corp <- vAR_CSLAB_clean_corpus(vAR_CSLAB_tweets_corpus)
# Create the tdm from the corpus:
vAR_CSLAB_tweets_tdm <- TermDocumentMatrix(vAR_CSLAB_clean_corp)
# Convert tweets_tdm to a matrix: tweets_m
#vAR_CSLAB_tweets_tweets_m <- as.matrix(vAR_CSLAB_tweets_tdm)
print(dim(vAR_CSLAB_tweets_tdm))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_FUNCTION_TOKENIZER_V1
Purpose : A Program for Tokenizer Function using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 14:38 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Tokenizer Function using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
## A simple text.
vAR_CSLAB_s <- String(" First sentence. Second sentence. ")
## Use a pre-built regexp (span) tokenizer:
wordpunct_tokenizer
wordpunct_tokenizer(vAR_CSLAB_s)
## Turn into a token tokenizer:
vAR_CSLAB_tt <- as.Token_Tokenizer(wordpunct_tokenizer)
vAR_CSLAB_tt(vAR_CSLAB_s)
## Of course, in this case we could simply have done
vAR_CSLAB_s[wordpunct_tokenizer(vAR_CSLAB_s)]
vAR_CSLAB_scan_tokenizer <- function(x)
scan(text = as.character(x), what = "character", quote = "",
quiet = TRUE)
## Create a token tokenizer from this:
vAR_CSLAB_tt <- Token_Tokenizer(vAR_CSLAB_scan_tokenizer)
## Turn into a span tokenizer:
vAR_CSLAB_st <- as.Span_Tokenizer(vAR_CSLAB_tt)
vAR_CSLAB_st(vAR_CSLAB_s)
## Checking tokens from spans:
vAR_CSLAB_s[vAR_CSLAB_st(vAR_CSLAB_s)]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_FUNCTION_DATE-TIME_V1
Purpose : A Program for Date Time Function using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 14:52 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Date Time Function using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_x <- c("1997",
"1997-07",
"1997-07-16",
"1997-07-16T19:20+01:00",
"1997-07-16T19:20:30+01:00",
"1997-07-16T19:20:30.45+01:00",
"1997-07-16T19:20:30.45Z")
vAR_CSLAB_y <- parse_ISO_8601_datetime(vAR_CSLAB_x)
as.Date(vAR_CSLAB_y)
as.POSIXlt(vAR_CSLAB_y)
## Subscripting and extracting components:
head(vAR_CSLAB_y, 3)
vAR_CSLAB_y$mon
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_FUNCTION_ANNOTATION_V1
Purpose : A Program for Annotation Function using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 15:05 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Annotation Function using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_s <- String(" First sentence. Second sentence. ")
## Basic sentence and word token annotations for the text.
vAR_CSLAB_a1s <- Annotation(1 : 2,
rep.int("sentence", 2L),
c( 3L, 20L),
c(17L, 35L))
vAR_CSLAB_a1w <- Annotation(3 : 6,
rep.int("word", 4L),
c( 3L, 9L, 20L, 27L),
c( 7L, 16L, 25L, 34L))
## Use c() to combine these annotations:
vAR_CSLAB_a1 <- c(vAR_CSLAB_a1s, vAR_CSLAB_a1w)
## Subscripting via '[':
vAR_CSLAB_a1[3 : 4]
## Subscripting via '$':
vAR_CSLAB_a1$type
## Subsetting according to slot values, directly:
vAR_CSLAB_a1[vAR_CSLAB_a1$type == "word"]
## or using subset():
subset(vAR_CSLAB_a1, type == "word")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_FUNCTION_NGRAMS_V1
Purpose : A Program for N-grams Function using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 15:17 hrs
Version : 1.0
/***********************************
## Program Description : A Program for N-grams Function using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_s <- "The quick brown fox jumps over the lazy dog"
## Split into words:
vAR_CSLAB_w <- strsplit(vAR_CSLAB_s, " ", fixed = TRUE)[[1L]]
## Word tri-grams:
ngrams(vAR_CSLAB_w, 3L)
## Word tri-grams pasted together:
vapply(ngrams(vAR_CSLAB_w, 3L), paste, "", collapse = " ")
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_FUNCTION_TAGSETS_V1
Purpose : A Program for Tagsets Function using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 15:29 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Tagsets Function using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
## Penn Treebank POS tags
dim(Penn_Treebank_POS_tags)
## Inspect first 20 entries:
write.dcf(head(Penn_Treebank_POS_tags, 20L))
## Brown POS tags
dim(Brown_POS_tags)
## Inspect first 20 entries:
write.dcf(head(Brown_POS_tags, 20L))
## Universal POS tags
Universal_POS_tags
## Available mappings to universal POS tags
names(Universal_POS_tags_map)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_FUNCTION_ANNOTATE_TEXT_STRINGS_V1
Purpose : A Program for Annotating Text Strings using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 15:44 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Annotating Text Strings using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_s <- String(" First sentence. Second sentence. ")
vAR_CSLAB_sent_tokenizer <-function(s) {
vAR_CSLAB_s <- as.String(s)
vAR_CSLAB_m <- gregexpr("[^[:space:]][^.]*\\.", s)[[1L]]
Span(vAR_CSLAB_m, vAR_CSLAB_m + attr(vAR_CSLAB_m, "match.length") - 1L)
}
## A simple sentence token annotator based on the sentence tokenizer.
vAR_CSLAB_sent_token_annotator <- Simple_Sent_Token_Annotator(vAR_CSLAB_sent_tokenizer)
## Annotate sentence tokens.
vAR_CSLAB_a1 <- annotate(vAR_CSLAB_s, vAR_CSLAB_sent_token_annotator)
vAR_CSLAB_a1
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_PART_OF_SPEECH_TAGGING_V1
Purpose : A Program for Part of Speech Tagging using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 16:01 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Part of Speech Tagging using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library("NLP")
library(openNLP)
## Some text.
vAR_CSLAB_s <- paste(c("Pierre Vinken, 61 years old, will join the board as a ",
"nonexecutive director Nov. 29.\n",
"Mr. Vinken is chairman of Elsevier N.V., ",
"the Dutch publishing group."),
collapse = "")
vAR_CSLAB_s <- as.String(vAR_CSLAB_s)
## Chunking needs word token annotations with POS tags.
vAR_CSLAB_sent_token_annotator <- Maxent_Sent_Token_Annotator()
vAR_CSLAB_word_token_annotator <- Maxent_Word_Token_Annotator()
vAR_CSLAB_pos_tag_annotator <- Maxent_POS_Tag_Annotator()
vAR_CSLAB_pos_tag_annotator
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_CHUNKING_V1
Purpose : A Program for Chunking using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 16:14 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Chunking using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library("NLP")
library(openNLP)
## Some text.
vAR_CSLAB_s <- paste(c("Pierre Vinken, 61 years old, will join the board as a ",
"nonexecutive director Nov. 29.\n",
"Mr. Vinken is chairman of Elsevier N.V., ",
"the Dutch publishing group."),
collapse = "")
vAR_CSLAB_s <- as.String(vAR_CSLAB_s)
## Chunking needs word token annotations with POS tags.
vAR_CSLAB_sent_token_annotator <- Maxent_Sent_Token_Annotator()
vAR_CSLAB_word_token_annotator <- Maxent_Word_Token_Annotator()
vAR_CSLAB_pos_tag_annotator <- Maxent_POS_Tag_Annotator()
vAR_CSLAB_a3 <- annotate(vAR_CSLAB_s,list(vAR_CSLAB_sent_token_annotator,vAR_CSLAB_word_token_annotator,vAR_CSLAB_pos_tag_annotator))
vAR_CSLAB_s[vAR_CSLAB_a3]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_SENTENCE_ANNOTATORS_V1
Purpose : A Program for Senetence Annotators using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 16:28 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Senetence Annotators using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
require("NLP")
require("openNLP")
## Some text.
vAR_CSLAB_s <- paste(c("Pierre Vinken, 61 years old, will join the board as a ",
"nonexecutive director Nov. 29.\n",
"Mr. Vinken is chairman of Elsevier N.V., ",
"the Dutch publishing group."),
collapse = "")
vAR_CSLAB_s <- as.String(vAR_CSLAB_s)
vAR_CSLAB_sent_token_annotator <- Maxent_Sent_Token_Annotator()
vAR_CSLAB_sent_token_annotator
vAR_CSLAB_a1 <- annotate(vAR_CSLAB_s, vAR_CSLAB_sent_token_annotator)
## Extract sentences.
vAR_CSLAB_s[vAR_CSLAB_a1]
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_WORD_TOKEN_ANNOTATORS_V1
Purpose : A Program for Word Token Annotators using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 16:44 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Word Token Annotators using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
require("NLP")
## Some text.
vAR_CSLAB_s <- paste(c("Pierre Vinken, 61 years old, will join the board as a ",
"nonexecutive director Nov. 29.\n",
"Mr. Vinken is chairman of Elsevier N.V., ",
"the Dutch publishing group."),
collapse = "")
vAR_CSLAB_s <- as.String(vAR_CSLAB_s)
## Need sentence token annotations.
vAR_CSLAB_sent_token_annotator <- Maxent_Sent_Token_Annotator()
vAR_CSLAB_a1 <- annotate(vAR_CSLAB_s, vAR_CSLAB_sent_token_annotator)
vAR_CSLAB_word_token_annotator <- Maxent_Word_Token_Annotator()
vAR_CSLAB_word_token_annotator
vAR_CSLAB_a2 <- annotate(vAR_CSLAB_s, vAR_CSLAB_word_token_annotator, vAR_CSLAB_a1)
vAR_CSLAB_a2
## Variant with word token probabilities as features.
head(annotate(vAR_CSLAB_s, Maxent_Word_Token_Annotator(probs = TRUE), vAR_CSLAB_a1))
## Can also perform sentence and word token annotations in a pipeline:
vAR_CSLAB_a <- annotate(vAR_CSLAB_s, list(vAR_CSLAB_sent_token_annotator, vAR_CSLAB_word_token_annotator))
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_NAMED_ENTITY_RECOGNITION_V1
Purpose : A Program for Named Entity Recognition using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 17:08 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Named Entity Recognition using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
require("NLP")
## Some text.
vAR_CSLAB_s <- paste(c("Pierre Vinken, 61 years old, will join the board as a ",
"nonexecutive director Nov. 29.\n",
"Mr. Vinken is chairman of Elsevier N.V., ",
"the Dutch publishing group."),
collapse = "")
vAR_CSLAB_s <- as.String(vAR_CSLAB_s)
## Need sentence token annotations.
vAR_CSLAB_sent_token_annotator <- Maxent_Sent_Token_Annotator()
vAR_CSLAB_a1 <- annotate(vAR_CSLAB_s, vAR_CSLAB_sent_token_annotator)
#vAR_CSLAB_word_token_annotator <- Maxent_Word_Token_Annotator()
vAR_CSLAB_word_token_annotator
vAR_CSLAB_a2 <- annotate(vAR_CSLAB_s, vAR_CSLAB_word_token_annotator, vAR_CSLAB_a1)
vAR_CSLAB_a2
## Variant with word token probabilities as features.
head(annotate(vAR_CSLAB_s, Maxent_Word_Token_Annotator(probs = TRUE), vAR_CSLAB_a1))
## Entity recognition for persons.
vAR_CSLAB_entity_annotator <- Maxent_Entity_Annotator()
vAR_CSLAB_entity_annotator
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_STEMMING_V1
Purpose : A Program for Stemming using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 17:21 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Stemming using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("pacman")
require("pacman")
pacman::p_load(textstem, dplyr)
data(presidential_debates_2012)
vAR_CSLAB_dw <- c('driver', 'drive', 'drove', 'driven', 'drives', 'driving')
stem_words(vAR_CSLAB_dw)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_LEMMATIZING_V1
Purpose : A Program for Lemmatizing using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 17:34 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Lemmatizing using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("pacman")
require("pacman")
pacman::p_load(textstem, dplyr)
data(presidential_debates_2012)
vAR_CSLAB_dw <- c('driver', 'drive', 'drove', 'driven', 'drives', 'driving')
stem_words(vAR_CSLAB_dw)
lemmatize_words(vAR_CSLAB_dw)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_BAG_OF_WORDS_V1
Purpose : A Program for Bag of Words using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 17:48 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Bag of Words using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
library(readr)
install.packages("tidyverse")
install.packages("tidytext")
install.packages("SnowballC")
library(tidyverse)
library(tidytext)
library(SnowballC)
vAR_CSLAB_ENV_VARIABLE_PATH = Sys.getenv("R_TUTORIAL_PATH")
vAR_CSLAB_FILE = "womens-ecommerce-clothing-reviews/Womens Clothing E-Commerce Reviews.csv"
vAR_CSLAB_FILE_PATH = paste(vAR_CSLAB_ENV_VARIABLE_PATH,vAR_CSLAB_FILE,sep="")
vAR_CSLAB_df <- read_csv(vAR_CSLAB_FILE_PATH)
#vAR_CSLAB_df <- read_csv("C:/DeepSphere.AI/R Tutorial/Unit-13/R for NLP/Data/womens-ecommerce-clothing-reviews/Womens Clothing E-Commerce Reviews.csv")
vAR_CSLAB_df
glimpse(vAR_CSLAB_df)
vAR_CSLAB_df %>%
select(`Review Text`) %>%
unnest_tokens(word, `Review Text`) %>%
count(word, sort = TRUE)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_ANALYZING_TEXTS_VOCABULARY_BASED_VECTORZATION_V1
Purpose : A Program for Anazying Texts by Voculabary Based Vectorization using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 18:04 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Anazying Texts by Voculabary Based Vectorization using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("text2vec")
install.packages("data.table")
install.packages("magrittr")
library(text2vec)
library(data.table)
library(magrittr)
data("movie_review")
setDT(movie_review)
setkey(movie_review, id)
set.seed(2017L)
vAR_CSLAB_all_ids = movie_review$id
vAR_CSLAB_train_ids = sample(vAR_CSLAB_all_ids, 4000)
vAR_CSLAB_test_ids = setdiff(vAR_CSLAB_all_ids, vAR_CSLAB_train_ids)
vAR_CSLAB_train = movie_review[J(vAR_CSLAB_train_ids)]
vAR_CSLAB_test = movie_review[J(vAR_CSLAB_test_ids)]
vAR_CSLAB_prep_fun = tolower
vAR_CSLAB_tok_fun = word_tokenizer
vAR_CSLAB_it_train = itoken(vAR_CSLAB_train$review,
preprocessor = vAR_CSLAB_prep_fun,
tokenizer = vAR_CSLAB_tok_fun,
ids = vAR_CSLAB_train$id,
progressbar = FALSE)
vAR_CSLAB_vocab = create_vocabulary(vAR_CSLAB_it_train)
vAR_CSLAB_train_tokens = vAR_CSLAB_train$review %>%
vAR_CSLAB_prep_fun %>%
vAR_CSLAB_tok_fun
vAR_CSLAB_it_train = itoken(vAR_CSLAB_train_tokens,
ids = vAR_CSLAB_train$id,
# turn off progressbar because it won't look nice in rmd
progressbar = FALSE)
vAR_CSLAB_vocab = create_vocabulary(vAR_CSLAB_it_train)
vAR_CSLAB_vocab
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_ANALYZING_TEXTS_FEATURE_HASHING_V1
Purpose : A Program for Anazying Texts by Feature Hashing using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 18:22 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Anazying Texts by Feature Hashing using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
install.packages("text2vec")
install.packages("data.table")
install.packages("magrittr")
library(text2vec)
library(data.table)
library(magrittr)
data("movie_review")
setDT(movie_review)
setkey(movie_review, id)
set.seed(2017L)
vAR_CSLAB_all_ids = movie_review$id
vAR_CSLAB_train_ids = sample(vAR_CSLAB_all_ids, 4000)
vAR_CSLAB_test_ids = setdiff(vAR_CSLAB_all_ids, vAR_CSLAB_train_ids)
vAR_CSLAB_train = movie_review[J(vAR_CSLAB_train_ids)]
vAR_CSLAB_test = movie_review[J(vAR_CSLAB_test_ids)]
vAR_CSLAB_prep_fun = tolower
vAR_CSLAB_tok_fun = word_tokenizer
vAR_CSLAB_it_train = itoken(vAR_CSLAB_train$review,
preprocessor = vAR_CSLAB_prep_fun,
tokenizer = vAR_CSLAB_tok_fun,
ids = vAR_CSLAB_train$id,
progressbar = FALSE)
vAR_CSLAB_vocab = create_vocabulary(vAR_CSLAB_it_train)
vAR_CSLAB_train_tokens = vAR_CSLAB_train$review %>%
vAR_CSLAB_prep_fun %>%
vAR_CSLAB_tok_fun
vAR_CSLAB_it_train = itoken(vAR_CSLAB_train_tokens,
ids = vAR_CSLAB_train$id,
# turn off progressbar because it won't look nice in rmd
progressbar = FALSE)
vAR_CSLAB_vocab = create_vocabulary(vAR_CSLAB_it_train)
vAR_CSLAB_h_vectorizer = hash_vectorizer(hash_size = 2 ^ 14, ngram = c(1L, 2L))
vAR_CSLAB_t1 = Sys.time()
vAR_CSLAB_dtm_train = create_dtm(vAR_CSLAB_it_train, vAR_CSLAB_h_vectorizer)
print(difftime(Sys.time(), vAR_CSLAB_t1, units = 'sec'))
vAR_CSLAB_t1 = Sys.time()
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_NOISE_REMOVAL_REMOVE_PUNCTUATIONS_V1
Purpose : A Program for Removing Punctuations from a Text using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 18:31 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Removing Punctuations from a Text using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
# Create the object: text
vAR_CSLAB_text <- "<b>She</b> woke up at 6 A.M. It\'s so early! She was only 10% awake and began drinking coffee in front of her computer."
removePunctuation(vAR_CSLAB_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_NOISE_REMOVAL_REMOVE_NUMBERS_V1
Purpose : A Program for Removing Numbers from a Text using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 18:37 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Removing Numbers from a Text using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_text <- "<b>She</b> woke up at 6 A.M. It\'s so early! She was only 10% awake and began drinking coffee in front of her computer."
removeNumbers(vAR_CSLAB_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************
/**********************************
File Name : CSLAB_NLP_NOISE_REMOVAL_STRIP_WHITESPACES_V1
Purpose : A Program for Stripping Whitespaces from a Text using NLP in R
Author : DeepSphere.AI, Inc.
Date and Time : 22/01/2019 18:44 hrs
Version : 1.0
/***********************************
## Program Description : A Program for Stripping Whitespaces from a Text using NLP in R
## R Development Environment & Runtime - R Studio, Anaconda, Jupyter
vAR_CSLAB_text <- "<b>She</b> woke up at 6 A.M. It\'s so early! She was only 10% awake and began drinking coffee in front of her computer."
stripWhitespace(vAR_CSLAB_text)
/***********************************
Disclaimer:
We are providing this code block strictly for learning and researching. This is not a production-ready code. We assume no liability for this code under any circumstance. By using this code, users assume full risk.
All software, hardware, and other products that are referenced in these materials, belong to the respective vendor who developed or who owns this product.
/***********************************