Showing posts with label DatascienceR. Show all posts
Showing posts with label DatascienceR. Show all posts

Machine Learning | Logistic Regression in R

The Logistic Regression is a regression model in which the response variable (dependent variable) has categorical values such as True/False or 0/1. It actually measures the probability of a binary response as the value of response variable based on the mathematical equation relating it with the predictor variables.

Lets load the remission dataset and understand the logistic regression.
Lets understand the dataset by summarizing 
str function used to view the datatypes of each feature in the dataset and dim function returns the observation and feature counts as shown below.
Logistic reg equation :
LOG P /1-P = B0+B1X1+B2X2+B3X3+BNXN:
B0 : intercept
B1: Slope for x1 : First independent variable in your data (cell)
X1 : First independent variable, cell
B2 : Slope for x2 : Second independent variable (smear)
X2 : Second independent variable, smear

Now lets built the model. In order to create a logistic regression model glm() function is used as shown below.

In the summary, as the p-value in the for most variable is more than 0.05 and we consider them to be insignificant in contributing to the value of the variable "Remiss". Only LI impacts the "Remiss" value in this regression model.

Now lets predict for observations and try to plot graph as shown below.


Machine Learning | Linear Regression in R

Lets us understand the linear regression model with a example.we would load the real estate dataset from a csv file. 
One of the most common modeling approaches in statistical learning is linear regression. In R, use the lm function to generate these models. The general form of a linear regression model is
 Y = C+M1x1+M2x2+M3x3+Mnxn+ Error Term 
Equation of mlr : 
 Y = Dependent variable is Selling Price 
 C= Constant 
 X1 = 1st INdependent variable , X2 = 2nd Independent variable ,  X3 = 3rd  Independent variable 
 M1= Slope For X1 : First Ind variable In Your Dat A( Local Selling Price ) 
 M2 = Slope For X2 : Second Ind variable In Your Data ( No Of Bathroom)
 M3 = Slope For X3 : Third Ind variable ( Area Of Site) and so on


Now lets built the model. In order to create a linear regression model lm() function is used as shown below.
Summary shows size of the living space is most significant variable.
In the summary, as the p-value in the for most variable is more than 0.05 and we consider them to be insignificant in contributing to the value of the variable "Selling Price". Only Size of living space impacts the "Selling Price" value in this regression model.
Now lets us try to predict the selling price and plot the graph to compare the results.

Basic R Programming | Data Visualization in R with ggplot2

In data analysis more than anything, a picture really is worth a thousand words. When you start analyzing data in R, your first step shouldn't be to run a complex statistical test: first, you should visualize your data in a graph. This lets you understand the basic nature of the data, so that you know what tests you can perform, and where you should focus your analysis.

ggplot2 is a third party package and to install it separately; use the following command:
install.packages("ggplot2")
To load it into the memory; use the following command:
library(
ggplot2)

Bar Chart: It is either horizontal or vertical bars to show comparisons between categorical values
Pie Charts: It is a type of graph in which a circle is divided into sectors, each representing a proportion of the whole.
Histograms are graphical representations of a probability distribution. A histogram is a kind of a bar chart. They display the number of values within a specified interval. They are suitable for large datasets as they can be grouped within the intervals.
Line Chart :Generally used to visualize data that changes over time.
Box Plot: Displays the distribution of data that is based on the five-number summary: Minimum,First quartile,Median,Third quartile,Maximum

Basic R Programming | Data Manipulation in R with dplyr


dplyr is a powerful R-package to transform and summarize tabular data with rows and columns. dplyr is not a part of the default package of R.

To install it separately; use the following command:
install.packages("dplyr")
To load it into the memory; use the following command:
library(dplyr)

The package contains a set of functions (or “verbs”) that perform common data manipulation operations as below, we would use the mtcars dataset available in dplyr package itself.
Select allows you to select specific columns from large data sets. 
Filter your data to select specific rows based on certain condition, this enables easy filtering, zoom in, and zoom out of relevant data.
Arrange the rows of our data into an order by sorting the data in ascending or descending order based on a column
Mutate helps add new variables to an existing data set

Summarise chunks of you data in some way. This summarizes multiple values to a single value in a dataset.

Load dataset in R:
R allows data import from a Comma Separated Values (CSV), Excel and Tables format as well.  
Lets load a dataframe from a csv file and view few records to understand the data in a DataFrame

 Understanding the remission dataset
General Functions
 Statistical Functions

Basic R Programming | Apply Functions in R


The apply functions are used to perform a specific change to each column or row of R objects.

Apply Function: It helps apply a function to a matrix row or column and returns a vector, array, or list.
Syntax: apply(x, byrow/bycolumn, function)

Lapply Function: It takes a list as an argument and works by looping through each element in the list. The output of the this function is a list.
Syntax : lapply(list, function)
Sapply Function: Its similar to lapply(), except that it simplifies the result so that
If the result is a list and every element in the list is of size 1, then a vector is returned.
If the result is a list and every element in the list is of the same size (>1), then a matrix is returned. Otherwise, the result is returned as a list itself.
Syntax : sapply(list, function)
Tapply Funtion: To define it, you need three arguments: vector, factor of vector, and function.
Syntax : tapply(x, factor, fun)
Vapply Function: It works like sapply except that you need to  specify type of return value,which can be integer,double or a character
Mapply Function: It is used for repetition
Syntax: mapply(function,.....)

Basic R Programming | Data Structures in R


There are different types of data structures in R, lets us explore one by one by storing and retrieving data from these data structures,

Vectors : One-dimensional arrays.
Matrices: Two-dimensional data structures, elements in a matrix must be of the same type, whether a number, character, or Boolean. Matrices byrow is an optional parameter used by matrices.
Arrays : Similar to matrices;these can have more than two dimensions.
Lists: A list may contain a combination of vectors, matrices, data frames, and even other lists.
Data frames:A data frame is similar to a general matrix, but its columns can contain different modes of data, such as a number and character.
Factors: These are categorical variables,

Basic R Programming | Control Flow Statements in R

Control flow is where the rubber really meets the road in programming. Without it, a program is simply a list of statements that are sequentially executed. With control flow, you can execute certain code blocks conditionally and/or repeatedly, these basic building blocks can be combined to create surprisingly sophisticated programs!


Conditional Statements: 
R supports two types of conditional statements

  • If…else : In if...else statements, when the test expression is True, the code in the “if” block executes; otherwise, the code in the “else” block executes.
  • Nested if…else : In nested if...else statements, only one statement executes, depending on the test expressions in the “if” blocks.
  • Ifelse() : This is a vector equivalent form of if…else.
  • Switch Function : This is similar to a controlled branch of if…else statements

For Loops
Loops in Rare a way to repeatedly execute some code statement. So, for example, if we'd like to print each of the items in a list, we can use a for loop:
While loops
The argument of the while loop is evaluated as a boolean statement, and the loop is executed until the statement evaluates to False.
Repeat loops
A repeat loop iterates a code multiple times. Since there is no conditional check to exit the loop, you must specify it inside the body of the loop.
next and break: Fine-Tuning Your Loops
There are two useful statements that can be used within loops to fine-tune how they are executed:
  • The next statement skips the remainder of the current loop, and goes to the next iteration
  • The break statement breaks-out of the loop entirely
These can be used in both for and while loops.

Introduction and Installation of R


R is:
  • A programing language developed at AT&T Bell Laboratories by Robert Gentleman and Ross Ihaka
  • An alternative to S language
  • A free, open source language, with highly active community members
  • Available across all platforms (Linux, Mac, Windows)
  • The RStudio program can run on a desktop or through a web browser.
Due to its underlying philosophy and design; R is useful for statistical computation and graphic visualization. R has a worldwide repository system—Comprehensive R Archive Network (CRAN). It can be accessed at http://cran.r-project.org. As of 2011, there were more than 3,000 such packages hosted on CRAN and many more on other websites.

You can install R from the following two sources:
CRAN Website: http://cran.r-project.org/
RStudio: https://www.rstudio.com/products/rstudio/download/

R Installation: 
First, you will learn how to install R on Windows from the CRAN website. You can download the package from: https://cran.r-project.org/bin/windows/base/.
  1. Download R 3.X.X for Windows executable file (.exe).
  2. Click “Next” in the Setup Wizard. (You may leave the setting as default.)
  3. Finally, open R console (RGUI from your desktop).
Now, you will learn how to install R on Windows from the RStudio website.
  1. Download RStudio from https://www.rstudio.com/products/rstudio/download/.
  2. Run the installation file.
  3. Open RStudio.
The different areas of RStudio are depicted below:
In RStudio, you can set the workspace by clicking Tools -> Global Options.
  • getwd() -return working directory
  • setwd() -set working directory
Many data scientist programmers and statisticians use R to design tools for analyzing data and to contribute their codes as pre-assembled collections of functions and objects called packages. You can install an R package by clicking GUI RStudio ->Tools -> Install Packages or directly from packages tab in File/Package Panel
Getting Started : Quick Tour!!

R has many operators to perform different mathematical and logical operations. These can be categorized as follows:
Arithmetic Operators: used for mathematical operations like addition and multiplication, look at the table below:
Operator
Description
+
Addition
-
Subtraction
*
Multiplication
/
Division
^
Exponent
%%
Modulus
%/%
Integer Division
Logical Operators: These operators are used to perform Boolean operations such as “AND” and “OR.”, look at the table below:
Operator
Description
!
Logical NOT
&
Element-wise logical AND
&&
Logical AND
|
Element-wise logical OR
||
Logical OR
Relational Operators: used to compare two values, look at the table below:
Operator
Description
< 
Less than
> 
Greater than
<=
Less than or equal to
>=
Greater than or equal to
==
Equal to
!=
Not equal to
Assignment Operator: These operators are used to assign values to variables. Variables are assigned using “<-”, although “=” also works.

Lets do a simply hello world program and execute as below.

Data Science | Data Analytics

Data by itself is just an information source. But unless you can understand it, you will not be able to use it effectively.

Data Science,

  • Includes processes, principles, and methods to understand phenomena through automated data analysis
  • Allows Data-Driven Decision Making (DDD), which determines the productivity of an organization

Data Scientists collect data and explore, analyze, and visualize it. They apply mathematical and statistical models to find patterns and solutions in the data.

A Data Scientist should be able to
  • Ask the right questions
  • Understand data structure
  • Interpret and wrangle data
  • Apply statistical and mathematical methods
  • Visualize data and communicate with stakeholders
  • Work as a team player
Data analysis can be:
  • Descriptive: Study a dataset to decipher the details
  • Predictive: Create a model based on existing information to predict outcome and behavior
  • Prescriptive: Suggest actions for a given situation using the collected information
Data analysis that uses only technology and domain knowledge without mathematical and statistical knowledge often leads to incorrect patterns and wrong interpretations. This can cause serious damage to businesses.

Data Analytics is a combination of processes to extract information from datasets.


Business Problem : Business problems trigger the need to analyze data and find answers,the process of analytics begins with questions or business problems of stakeholders.
Data Acquisition : Collect data from various sources for analysis to answer the question raised in step 1.
Data Wrangling : Data wrangling is the most challenging phase and takes up 70% of the data scientist’s time.

  • Data cleansing
  • Data manipulation

Data Exploration (Model Selection)
  • Data discovery
  • Data pattern
Model selection
  • Based on the overall data analysis process
  • Should be accurate to avoid iterations
  • Depends on pattern identification and algorithms
  • Depends on hypothesis building and testing
  • Leads to building mathematical statistical functions
EDA: Studies the data to recommend suitable models that best fit the data. The focus is on data; its structure, outliers, and models suggested by the data. EDA techniques make minimal or no assumptions. They present and show all the underlying data without any data loss.
  • Quantitative: Provides numeric outputs for the inputted data
  • Graphical: Uses statistical functions for graphical output
Prediction 
Involves heavy use of mathematical and statistical functions,requires model selection, training and testing to help in forecasting, it is called “machine learning” as data analysis is fully or semi-automated with minimal or no human intervention.