R Programming

R Markdown presentation

We will be using R Markdown for our outline

R is optimized for vector operations and as well, it is an open source software

CRAN :- Comprehensive R Archive Network

loading the data sets

library(datasets)
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

To see the summary of the data set “iris”

summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 

Sometimes we can even get to visualize the data directly

plot(iris)

We can install one packages that carries it many packages we would need

install.packages("pacman")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
require("pacman")
## Loading required package: pacman

There are two types of packages in R

  • Base Packages - download with r (inbuilt) but not loaded o the environment

  • Contributed packages - they must be downloaded

To load the entire collection

pacman::p_load(pacman, dplyr, GGally, ggplot2, ggthemes, ggris, lubridate, plotly, rio, rmarkdown, shiny, stringr, tidyr)
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
## Warning: package 'ggris' is not available for this version of R
## 
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## Warning: 'BiocManager' not available.  Could not check Bioconductor.
## 
## Please use `install.packages('BiocManager')` and then retry.
## Warning in p_install(package, character.only = TRUE, ...):
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'ggris'
## Warning in pacman::p_load(pacman, dplyr, GGally, ggplot2, ggthemes, ggris, : Failed to install/load:
## ggris

Basic Graphics

we can plot categorical data

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
plot(iris$Species)

  • quantitative variable
plot(iris$Petal.Length)

Or else plot both quantitative data

plot(iris$Sepal.Length, iris$Sepal.Width)

categorical vs quantitative

plot(iris$Species, iris$Petal.Width, main="Species vs Petal Width")

plot(iris$Species, iris$Petal.Length)

coloring the graphs

plot(iris$Sepal.Length, iris$Sepal.Width,
     col = "#cc0000",
     pch =19, #shape of the pointers for this case is circles
     main = "Sepa Length vs Sepal Width",
     xlab = "Sepal length fro iris dataset",
     ylab = "sepal width from iris dataset")

Bar charts

we use mtcars as our datset

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000

Number of rows

paste(nrow(mtcars), "rows")
## [1] "32 rows"

Number of columns

paste(ncol(mtcars), "Columns")
## [1] "11 Columns"
str(mtcars)
## 'data.frame':    32 obs. of  11 variables:
##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
##  $ disp: num  160 160 108 258 360 ...
##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
##  $ qsec: num  16.5 17 18.6 19.4 17 ...
##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

Factoring the vs column

mtcars$vs <- factor(mtcars$vs)
#This is more of like counting distinct to create a table 
cylinders <- table(mtcars$cyl)

barplot(cylinders)

Histogram

hist(iris$Sepal.Length)

hist(iris$Sepal.Width,
     main="Sepal Width Histogram")

Iris data

summary(iris$Species)
##     setosa versicolor  virginica 
##         50         50         50

We can arrange the graphs into different rows and columns

par(mfrow = c(3,1)) #3 rows and 1 columns
#plot histogram for each species

hist(iris$Petal.Width [iris$Species=="setosa"], 
     xlim = c(0,3),
     breaks = 9,
     main = "Petal Width for Setosa",
     xlab = "Petal Width",
     col ="green")

hist(iris$Petal.Width [iris$Species=="versicolor"], 
     xlim = c(0,3), #xlim?
     breaks = 9,
     main = "Petal Width for Versicolor",
     xlab = "Petal Width",
     col = "red")

hist(iris$Petal.Width [iris$Species=="virginica"], 
     xlim = c(0,3),
     breaks = 9,
     main = "Petal Width for Virginica",
     xlab = "Petal Width",
     col = "blue")

Restoring the graphics parameters

par(mfrow=c(1,1))

Scatter plot

This case you are plotting between two quantitative variables

plot(mtcars$wt, mtcars$mpg,
     main = "MTCARS",
     xlab ="Weights",
     ylab ="mpg")

adding some more plots

plot(mtcars$wt, mtcars$mpg,
     pch =19,
     cex = 1.5, #circumference of the circle
     col = "violet",
     main = "MTCARS",
     xlab ="Weights",
     ylab ="mpg")

Overlaying

hist(lynx)

Basic Statistics

These are statistical summaries of a dataset

summary(iris$Species)
##     setosa versicolor  virginica 
##         50         50         50
summary(iris$Sepal.Length)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   4.300   5.100   5.800   5.843   6.400   7.900
summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 

To be able to use describe() we need to load psych package

p_load(psych)
describe(iris$Sepal.Length)
##    vars   n mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 150 5.84 0.83    5.8    5.81 1.04 4.3 7.9   3.6 0.31    -0.61 0.07

From the above output, we can see that describe function gives more information about a variable.

describe(iris)
##              vars   n mean   sd median trimmed  mad min max range  skew
## Sepal.Length    1 150 5.84 0.83   5.80    5.81 1.04 4.3 7.9   3.6  0.31
## Sepal.Width     2 150 3.06 0.44   3.00    3.04 0.44 2.0 4.4   2.4  0.31
## Petal.Length    3 150 3.76 1.77   4.35    3.76 1.85 1.0 6.9   5.9 -0.27
## Petal.Width     4 150 1.20 0.76   1.30    1.18 1.04 0.1 2.5   2.4 -0.10
## Species*        5 150 2.00 0.82   2.00    2.00 1.48 1.0 3.0   2.0  0.00
##              kurtosis   se
## Sepal.Length    -0.61 0.07
## Sepal.Width      0.14 0.04
## Petal.Length    -1.42 0.14
## Petal.Width     -1.36 0.06
## Species*        -1.52 0.07

Selecting Cases

library(datasets)

hist(iris$Petal.Length,
     main="Petal Length",
     col = "blue")

summary(iris$Petal.Length)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   1.600   4.350   3.758   5.100   6.900
Histogram: Selection by Category
hist(iris$Petal.Length[iris$Species=="versicolor"],
     main="Petal Length of Versicolor Speies",
     xlab = "Petal Length")

Selecting by value

hist(iris$Petal.Length[iris$Petal.Length<2],
     main = "Petal Lengths less than 2",
     xlab = "Petal Length")

Multiselection cases

hist(iris$Petal.Length[iris$Species=="virginica" & iris$Petal.Length<5.5],
     main="Virginica speccies and Petal Length> 5.5",
     xlab = "Petal Length")

Subset

we can create sub-dataframe by slicing out the main dataframe

#we will be slicing the species setosa
i.setosa <- iris[iris$Species =="setosa", ]  #we put a comma to species it returns all the columns
head(i.setosa)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

Accessing data

data type - level of measurement of a variable

they include;

  • integer

  • single & double

  • character

  • logical

  • complex

Vector - 1D array, all of the same data type

Matrix - 2D data, same length, same class

Data frame - Can have vectors of multiple types

List - most flexible, ordered collection of elements

n1 <- 15
typeof(n1)
## [1] "double"
c1 <- "c"
typeof(c1)
## [1] "character"
l1 <- TRUE
typeof(l1)
## [1] "logical"
l2 <- F
typeof(l2)
## [1] "logical"

Vector

v1 <- c(1, 2, 3, 4, 5)
is.vector(v1)
## [1] TRUE

Matrix

m1 <- matrix(c(T, T, T, F, F, F), nrow=2)
is.matrix(m1)
## [1] TRUE

Array

a1 <- array(c(1:24), c(4, 3, 2))

# 4 - number of rows
# 3 - number of columns
# 2 - number of tables

is.array(a1)
## [1] TRUE

dataframe

n1 <- c(1, 2, 3, 4)
c1 <- c("a", "b", "c", "d")
l1 <- c(T, F, T, F)

df <- cbind(n1, c1, l1) # This returns a matrix because one has used column_bind function only to concatenate the three arrays
df1 <- as.data.frame(cbind(n1, c1, l1))
df1
##   n1 c1    l1
## 1  1  a  TRUE
## 2  2  b FALSE
## 3  3  c  TRUE
## 4  4  d FALSE

Another way of creating a data franme is directly from a matrix.

df2 <- as.data.frame(matrix(1:9, nrow=3))
df2
##   V1 V2 V3
## 1  1  4  7
## 2  2  5  8
## 3  3  6  9

Factors

categories and names

lets create data

x1 <- 1:3
y1 <- 1:9
#combine the variables
df3 <- cbind.data.frame(x1, y1)
typeof(df3$x1)
## [1] "integer"
str(df3)
## 'data.frame':    9 obs. of  2 variables:
##  $ x1: int  1 2 3 1 2 3 1 2 3
##  $ y1: int  1 2 3 4 5 6 7 8 9

Factoring

x2 <- as.factor(c(1:3))
df4 <- as.data.frame(x2)
str(df4)
## 'data.frame':    3 obs. of  1 variable:
##  $ x2: Factor w/ 3 levels "1","2","3": 1 2 3

Restructuring the levels

df4$x2 <- factor(df4$x2, levels = c(1, 3, 2))
str(df4)
## 'data.frame':    3 obs. of  1 variable:
##  $ x2: Factor w/ 3 levels "1","3","2": 1 3 2

Changing levels to labels

#using df3
df4 <- df3 
df4
##   x1 y1
## 1  1  1
## 2  2  2
## 3  3  3
## 4  1  4
## 5  2  5
## 6  3  6
## 7  1  7
## 8  2  8
## 9  3  9

we can give labels to categorial variables

df4$x1 <- factor(df4$x1, levels = c(1, 2, 3), labels = c("macOS", "Windows", "Linux"))
typeof(df4$x1)
## [1] "integer"
str(df4)
## 'data.frame':    9 obs. of  2 variables:
##  $ x1: Factor w/ 3 levels "macOS","Windows",..: 1 2 3 1 2 3 1 2 3
##  $ y1: int  1 2 3 4 5 6 7 8 9
df4
##        x1 y1
## 1   macOS  1
## 2 Windows  2
## 3   Linux  3
## 4   macOS  4
## 5 Windows  5
## 6   Linux  6
## 7   macOS  7
## 8 Windows  8
## 9   Linux  9

Its possible to re-order

Entering data

We can use different methods:

  • colon

  • c for combine or concatenate

  • seq

  • rep

NB: <- is the assignment operator in R

Colon

x1 <- 0:10
x2 <- 10:0
x1
##  [1]  0  1  2  3  4  5  6  7  8  9 10
x2
##  [1] 10  9  8  7  6  5  4  3  2  1  0

seq

x3 <- seq(10)
x3
##  [1]  1  2  3  4  5  6  7  8  9 10
x4 <- seq(30,0, by=-3) #by is for steps
x4
##  [1] 30 27 24 21 18 15 12  9  6  3  0

scan

x6 <- scan() #check the console to input the figures, every entry click enter ,  to stop inputting double click enter
x6
## numeric(0)

rep

x7 <- rep(TRUE, 5)
x7
## [1] TRUE TRUE TRUE TRUE TRUE
x8 <- rep(c(TRUE, FALSE), 5)
x8
##  [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
x9 <- rep(c(TRUE, FALSE), each=5)
x9
##  [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE

Import data

rio (r input output)

data <- import("/cloud/project/200601.csv")
data
##     Year Month ProductKey  OrderDate   ShipDate CustomerKey PromotionKey
## 1   2006     1        310 2006-01-01 2006-01-08       21768            1
## 2   2006     1        346 2006-01-01 2006-01-08       28389            1
## 3   2006     1        346 2006-01-01 2006-01-08       25863            1
## 4   2006     1        336 2006-01-01 2006-01-08       14501            1
## 5   2006     1        346 2006-01-01 2006-01-08       11003            1
## 6   2006     1        311 2006-01-02 2006-01-09       27645            1
## 7   2006     1        310 2006-01-02 2006-01-09       16624            1
## 8   2006     1        351 2006-01-02 2006-01-09       11005            1
## 9   2006     1        344 2006-01-02 2006-01-09       11011            1
## 10  2006     1        312 2006-01-03 2006-01-10       27621            1
## 11  2006     1        312 2006-01-03 2006-01-10       27616            1
## 12  2006     1        330 2006-01-03 2006-01-10       20042            1
## 13  2006     1        313 2006-01-03 2006-01-10       16351            1
## 14  2006     1        314 2006-01-03 2006-01-10       16517            1
## 15  2006     1        314 2006-01-04 2006-01-11       27606            1
## 16  2006     1        311 2006-01-04 2006-01-11       13513            1
## 17  2006     1        310 2006-01-05 2006-01-12       27601            1
## 18  2006     1        311 2006-01-05 2006-01-12       13591            1
## 19  2006     1        314 2006-01-05 2006-01-12       16483            1
## 20  2006     1        311 2006-01-05 2006-01-12       16529            1
## 21  2006     1        336 2006-01-05 2006-01-12       25249            1
## 22  2006     1        311 2006-01-06 2006-01-13       27668            1
## 23  2006     1        312 2006-01-06 2006-01-13       27612            1
## 24  2006     1        311 2006-01-06 2006-01-13       13264            1
## 25  2006     1        310 2006-01-06 2006-01-13       13590            1
## 26  2006     1        312 2006-01-07 2006-01-14       13581            1
## 27  2006     1        332 2006-01-07 2006-01-14       14520            1
## 28  2006     1        311 2006-01-07 2006-01-14       16520            1
## 29  2006     1        311 2006-01-08 2006-01-15       13258            1
## 30  2006     1        332 2006-01-08 2006-01-15       14560            1
## 31  2006     1        311 2006-01-08 2006-01-15       16607            1
## 32  2006     1        313 2006-01-09 2006-01-16       27666            1
## 33  2006     1        346 2006-01-09 2006-01-16       11238            1
## 34  2006     1        346 2006-01-09 2006-01-16       25861            1
## 35  2006     1        314 2006-01-09 2006-01-16       16629            1
## 36  2006     1        350 2006-01-09 2006-01-16       11025            1
## 37  2006     1        312 2006-01-09 2006-01-16       27577            1
## 38  2006     1        312 2006-01-10 2006-01-17       27604            1
## 39  2006     1        310 2006-01-10 2006-01-17       16522            1
## 40  2006     1        346 2006-01-10 2006-01-17       11002            1
## 41  2006     1        311 2006-01-11 2006-01-18       13261            1
## 42  2006     1        312 2006-01-11 2006-01-18       11606            1
## 43  2006     1        310 2006-01-11 2006-01-18       13563            1
## 44  2006     1        312 2006-01-11 2006-01-18       16527            1
## 45  2006     1        310 2006-01-12 2006-01-19       27671            1
## 46  2006     1        314 2006-01-12 2006-01-19       13576            1
## 47  2006     1        347 2006-01-12 2006-01-19       11007            1
## 48  2006     1        313 2006-01-12 2006-01-19       16631            1
## 49  2006     1        311 2006-01-13 2006-01-20       16514            1
## 50  2006     1        312 2006-01-13 2006-01-20       16616            1
## 51  2006     1        314 2006-01-14 2006-01-21       27623            1
## 52  2006     1        313 2006-01-14 2006-01-21       27625            1
## 53  2006     1        314 2006-01-14 2006-01-21       27636            1
## 54  2006     1        311 2006-01-14 2006-01-21       11591            1
## 55  2006     1        313 2006-01-14 2006-01-21       11592            1
## 56  2006     1        311 2006-01-14 2006-01-21       13265            1
## 57  2006     1        310 2006-01-14 2006-01-21       16482            1
## 58  2006     1        312 2006-01-15 2006-01-22       27617            1
## 59  2006     1        311 2006-01-15 2006-01-22       27670            1
## 60  2006     1        332 2006-01-15 2006-01-22       19941            1
## 61  2006     1        350 2006-01-15 2006-01-22       11017            1
## 62  2006     1        310 2006-01-16 2006-01-23       27646            1
## 63  2006     1        310 2006-01-16 2006-01-23       13257            1
## 64  2006     1        314 2006-01-16 2006-01-23       16352            1
## 65  2006     1        310 2006-01-16 2006-01-23       16493            1
## 66  2006     1        314 2006-01-17 2006-01-24       27578            1
## 67  2006     1        313 2006-01-17 2006-01-24       16525            1
## 68  2006     1        310 2006-01-17 2006-01-24       16612            1
## 69  2006     1        346 2006-01-17 2006-01-24       11010            1
## 70  2006     1        313 2006-01-18 2006-01-25       16518            1
## 71  2006     1        350 2006-01-18 2006-01-25       11001            1
## 72  2006     1        312 2006-01-19 2006-01-26       27649            1
## 73  2006     1        312 2006-01-19 2006-01-26       21659            1
## 74  2006     1        312 2006-01-19 2006-01-26       27614            1
## 75  2006     1        330 2006-01-19 2006-01-26       14559            1
## 76  2006     1        312 2006-01-19 2006-01-26       16524            1
## 77  2006     1        314 2006-01-19 2006-01-26       16526            1
## 78  2006     1        313 2006-01-19 2006-01-26       16348            1
## 79  2006     1        347 2006-01-19 2006-01-26       11027            1
## 80  2006     1        314 2006-01-20 2006-01-27       13260            1
## 81  2006     1        311 2006-01-20 2006-01-27       16515            1
## 82  2006     1        345 2006-01-20 2006-01-27       11018            1
## 83  2006     1        326 2006-01-20 2006-01-27       25252            1
## 84  2006     1        311 2006-01-21 2006-01-28       11599            1
## 85  2006     1        312 2006-01-21 2006-01-28       16495            1
## 86  2006     1        312 2006-01-21 2006-01-28       16516            1
## 87  2006     1        312 2006-01-21 2006-01-28       16620            1
## 88  2006     1        310 2006-01-22 2006-01-29       27667            1
## 89  2006     1        311 2006-01-22 2006-01-29       11601            1
## 90  2006     1        311 2006-01-22 2006-01-29       11607            1
## 91  2006     1        344 2006-01-22 2006-01-29       29385            1
## 92  2006     1        351 2006-01-22 2006-01-29       11239            1
## 93  2006     1        351 2006-01-22 2006-01-29       25865            1
## 94  2006     1        344 2006-01-22 2006-01-29       25857            1
## 95  2006     1        314 2006-01-22 2006-01-29       16484            1
## 96  2006     1        312 2006-01-22 2006-01-29       16623            1
## 97  2006     1        344 2006-01-22 2006-01-29       11000            1
## 98  2006     1        347 2006-01-22 2006-01-29       11029            1
## 99  2006     1        312 2006-01-23 2006-01-30       27615            1
## 100 2006     1        342 2006-01-23 2006-01-30       17956            1
## 101 2006     1        312 2006-01-23 2006-01-30       16345            1
## 102 2006     1        310 2006-01-23 2006-01-30       16521            1
## 103 2006     1        312 2006-01-23 2006-01-30       16636            1
## 104 2006     1        311 2006-01-24 2006-01-31       21710            1
## 105 2006     1        311 2006-01-24 2006-01-31       13583            1
## 106 2006     1        312 2006-01-24 2006-01-31       11593            1
## 107 2006     1        314 2006-01-25 2006-02-01       21741            1
## 108 2006     1        310 2006-01-25 2006-02-01       13585            1
## 109 2006     1        328 2006-01-25 2006-02-01       14510            1
## 110 2006     1        312 2006-01-25 2006-02-01       16523            1
## 111 2006     1        312 2006-01-26 2006-02-02       11611            1
## 112 2006     1        313 2006-01-26 2006-02-02       13259            1
## 113 2006     1        310 2006-01-26 2006-02-02       16350            1
## 114 2006     1        345 2006-01-26 2006-02-02       11004            1
## 115 2006     1        346 2006-01-26 2006-02-02       11026            1
## 116 2006     1        312 2006-01-27 2006-02-03       27662            1
## 117 2006     1        350 2006-01-27 2006-02-03       28390            1
## 118 2006     1        350 2006-01-27 2006-02-03       25866            1
## 119 2006     1        311 2006-01-27 2006-02-03       16494            1
## 120 2006     1        314 2006-01-27 2006-02-03       16496            1
## 121 2006     1        310 2006-01-27 2006-02-03       16519            1
## 122 2006     1        312 2006-01-27 2006-02-03       16528            1
## 123 2006     1        346 2006-01-27 2006-02-03       11006            1
## 124 2006     1        312 2006-01-28 2006-02-04       27651            1
## 125 2006     1        310 2006-01-28 2006-02-04       27663            1
## 126 2006     1        313 2006-01-28 2006-02-04       27669            1
## 127 2006     1        342 2006-01-28 2006-02-04       19942            1
## 128 2006     1        338 2006-01-28 2006-02-04       14558            1
## 129 2006     1        313 2006-01-28 2006-02-04       16349            1
## 130 2006     1        348 2006-01-28 2006-02-04       11008            1
## 131 2006     1        338 2006-01-28 2006-02-04       25250            1
## 132 2006     1        310 2006-01-29 2006-02-05       27605            1
## 133 2006     1        310 2006-01-29 2006-02-05       27611            1
## 134 2006     1        311 2006-01-29 2006-02-05       16347            1
## 135 2006     1        349 2006-01-29 2006-02-05       11028            1
## 136 2006     1        310 2006-01-29 2006-02-05       13584            1
## 137 2006     1        310 2006-01-30 2006-02-06       21727            1
## 138 2006     1        348 2006-01-30 2006-02-06       28393            1
## 139 2006     1        348 2006-01-30 2006-02-06       19802            1
## 140 2006     1        324 2006-01-30 2006-02-06       26620            1
## 141 2006     1        350 2006-01-30 2006-02-06       11009            1
## 142 2006     1        313 2006-01-31 2006-02-07       27641            1
## 143 2006     1        311 2006-01-31 2006-02-07       27673            1
## 144 2006     1        326 2006-01-31 2006-02-07       17957            1
## 145 2006     1        314 2006-01-31 2006-02-07       13406            1
## 146 2006     1        312 2006-01-31 2006-02-07       16630            1
##     SalesTerritoryKey SalesOrderNumber SalesOrderLineNumber OrderQuantity
## 1                   6          SO43697                    1             2
## 2                   7          SO43698                    1             2
## 3                   1          SO43699                    1             2
## 4                   4          SO43700                    1             2
## 5                   9          SO43701                    1             2
## 6                   4          SO43702                    1             4
## 7                   9          SO43703                    1             4
## 8                   9          SO43704                    1             4
## 9                   9          SO43705                    1             4
## 10                  4          SO43706                    1             1
## 11                  4          SO43707                    1             1
## 12                 10          SO43708                    1             1
## 13                  9          SO43709                    1             1
## 14                  9          SO43710                    1             1
## 15                  1          SO43711                    1             1
## 16                  8          SO43712                    1             1
## 17                  4          SO43713                    1             1
## 18                 10          SO43714                    1             1
## 19                  9          SO43715                    1             1
## 20                  9          SO43716                    1             1
## 21                  9          SO43717                    1             1
## 22                  1          SO43718                    1             1
## 23                  4          SO43719                    1             1
## 24                  8          SO43720                    1             1
## 25                 10          SO43721                    1             1
## 26                 10          SO43722                    1             1
## 27                  1          SO43723                    1             1
## 28                  9          SO43724                    1             1
## 29                  8          SO43725                    1             2
## 30                  4          SO43726                    1             2
## 31                  9          SO43727                    1             2
## 32                  4          SO43728                    1             4
## 33                 10          SO43729                    1             4
## 34                  4          SO43730                    1             4
## 35                  9          SO43731                    1             4
## 36                  9          SO43732                    1             4
## 37                  1          SO43733                    1             4
## 38                  1          SO43734                    1             1
## 39                  9          SO43735                    1             1
## 40                  9          SO43736                    1             1
## 41                  8          SO43737                    1             1
## 42                  7          SO43738                    1             1
## 43                 10          SO43739                    1             1
## 44                  9          SO43740                    1             1
## 45                  1          SO43741                    1             1
## 46                 10          SO43742                    1             1
## 47                  9          SO43743                    1             1
## 48                  9          SO43744                    1             1
## 49                  9          SO43745                    1             1
## 50                  9          SO43746                    1             1
## 51                  4          SO43747                    1             1
## 52                  1          SO43748                    1             1
## 53                  1          SO43749                    1             1
## 54                  7          SO43750                    1             1
## 55                  7          SO43751                    1             1
## 56                  8          SO43752                    1             1
## 57                  9          SO43753                    1             1
## 58                  1          SO43754                    1             2
## 59                  4          SO43755                    1             2
## 60                 10          SO43756                    1             2
## 61                  9          SO43757                    1             2
## 62                  4          SO43758                    1             4
## 63                  8          SO43759                    1             4
## 64                  9          SO43760                    1             4
## 65                  9          SO43761                    1             4
## 66                  4          SO43762                    1             1
## 67                  9          SO43763                    1             1
## 68                  9          SO43764                    1             1
## 69                  9          SO43765                    1             1
## 70                  9          SO43766                    1             1
## 71                  9          SO43767                    1             1
## 72                  1          SO43768                    1             1
## 73                  6          SO43769                    1             1
## 74                  1          SO43770                    1             1
## 75                  4          SO43771                    1             1
## 76                  9          SO43772                    1             1
## 77                  9          SO43773                    1             1
## 78                  9          SO43774                    1             1
## 79                  9          SO43775                    1             1
## 80                  8          SO43776                    1             1
## 81                  9          SO43777                    1             1
## 82                  9          SO43778                    1             1
## 83                  9          SO43779                    1             1
## 84                  7          SO43780                    1             1
## 85                  9          SO43781                    1             1
## 86                  9          SO43782                    1             1
## 87                  9          SO43783                    1             1
## 88                  4          SO43784                    1             2
## 89                  7          SO43785                    1             2
## 90                  7          SO43786                    1             2
## 91                  8          SO43787                    1             2
## 92                 10          SO43788                    1             2
## 93                  1          SO43789                    1             2
## 94                  1          SO43790                    1             2
## 95                  9          SO43791                    1             2
## 96                  9          SO43792                    1             2
## 97                  9          SO43793                    1             2
## 98                  9          SO43794                    1             2
## 99                  4          SO43795                    1             4
## 100                 7          SO43796                    1             4
## 101                 9          SO43797                    1             4
## 102                 9          SO43798                    1             4
## 103                 9          SO43799                    1             4
## 104                 6          SO43800                    1             1
## 105                10          SO43801                    1             1
## 106                 7          SO43802                    1             1
## 107                 6          SO43803                    1             1
## 108                10          SO43804                    1             1
## 109                 1          SO43805                    1             1
## 110                 9          SO43806                    1             1
## 111                 7          SO43807                    1             1
## 112                 8          SO43808                    1             1
## 113                 9          SO43809                    1             1
## 114                 9          SO43810                    1             1
## 115                 9          SO43811                    1             1
## 116                 4          SO43812                    1             1
## 117                 7          SO43813                    1             1
## 118                 1          SO43814                    1             1
## 119                 9          SO43815                    1             1
## 120                 9          SO43816                    1             1
## 121                 9          SO43817                    1             1
## 122                 9          SO43818                    1             1
## 123                 9          SO43819                    1             1
## 124                 4          SO43820                    1             1
## 125                 4          SO43821                    1             1
## 126                 4          SO43822                    1             1
## 127                10          SO43823                    1             1
## 128                 1          SO43824                    1             1
## 129                 9          SO43825                    1             1
## 130                 9          SO43826                    1             1
## 131                 9          SO43827                    1             1
## 132                 1          SO43828                    1             2
## 133                 4          SO43829                    1             2
## 134                 9          SO43830                    1             2
## 135                 9          SO43831                    1             2
## 136                10          SO43832                    1             2
## 137                 6          SO43833                    1             4
## 138                 7          SO43834                    1             4
## 139                 6          SO43835                    1             4
## 140                 6          SO43836                    1             4
## 141                 9          SO43837                    1             4
## 142                 4          SO43838                    1             1
## 143                 1          SO43839                    1             1
## 144                 7          SO43840                    1             1
## 145                 8          SO43841                    1             1
## 146                 9          SO43842                    1             1
##     UnitPrice TotalProductCost SalesAmount   TaxAmt
## 1   1789.1350        2171.2942   3578.2700 286.2616
## 2   1699.9950        1912.1544   3399.9900 271.9992
## 3   1699.9950        1912.1544   3399.9900 271.9992
## 4    349.5491         413.1463    699.0982  55.9279
## 5   1699.9950        1912.1544   3399.9900 271.9992
## 6    894.5675        2171.2942   3578.2700 286.2616
## 7    894.5675        2171.2942   3578.2700 286.2616
## 8    843.7475        1898.0944   3374.9900 269.9992
## 9    849.9975        1912.1544   3399.9900 271.9992
## 10  3578.2700        2171.2942   3578.2700 286.2616
## 11  3578.2700        2171.2942   3578.2700 286.2616
## 12   699.0982         413.1463    699.0982  55.9279
## 13  3578.2700        2171.2942   3578.2700 286.2616
## 14  3578.2700        2171.2942   3578.2700 286.2616
## 15  3578.2700        2171.2942   3578.2700 286.2616
## 16  3578.2700        2171.2942   3578.2700 286.2616
## 17  3578.2700        2171.2942   3578.2700 286.2616
## 18  3578.2700        2171.2942   3578.2700 286.2616
## 19  3578.2700        2171.2942   3578.2700 286.2616
## 20  3578.2700        2171.2942   3578.2700 286.2616
## 21   699.0982         413.1463    699.0982  55.9279
## 22  3578.2700        2171.2942   3578.2700 286.2616
## 23  3578.2700        2171.2942   3578.2700 286.2616
## 24  3578.2700        2171.2942   3578.2700 286.2616
## 25  3578.2700        2171.2942   3578.2700 286.2616
## 26  3578.2700        2171.2942   3578.2700 286.2616
## 27   699.0982         413.1463    699.0982  55.9279
## 28  3578.2700        2171.2942   3578.2700 286.2616
## 29  1789.1350        2171.2942   3578.2700 286.2616
## 30   349.5491         413.1463    699.0982  55.9279
## 31  1789.1350        2171.2942   3578.2700 286.2616
## 32   894.5675        2171.2942   3578.2700 286.2616
## 33   849.9975        1912.1544   3399.9900 271.9992
## 34   849.9975        1912.1544   3399.9900 271.9992
## 35   894.5675        2171.2942   3578.2700 286.2616
## 36   843.7475        1898.0944   3374.9900 269.9992
## 37   894.5675        2171.2942   3578.2700 286.2616
## 38  3578.2700        2171.2942   3578.2700 286.2616
## 39  3578.2700        2171.2942   3578.2700 286.2616
## 40  3399.9900        1912.1544   3399.9900 271.9992
## 41  3578.2700        2171.2942   3578.2700 286.2616
## 42  3578.2700        2171.2942   3578.2700 286.2616
## 43  3578.2700        2171.2942   3578.2700 286.2616
## 44  3578.2700        2171.2942   3578.2700 286.2616
## 45  3578.2700        2171.2942   3578.2700 286.2616
## 46  3578.2700        2171.2942   3578.2700 286.2616
## 47  3399.9900        1912.1544   3399.9900 271.9992
## 48  3578.2700        2171.2942   3578.2700 286.2616
## 49  3578.2700        2171.2942   3578.2700 286.2616
## 50  3578.2700        2171.2942   3578.2700 286.2616
## 51  3578.2700        2171.2942   3578.2700 286.2616
## 52  3578.2700        2171.2942   3578.2700 286.2616
## 53  3578.2700        2171.2942   3578.2700 286.2616
## 54  3578.2700        2171.2942   3578.2700 286.2616
## 55  3578.2700        2171.2942   3578.2700 286.2616
## 56  3578.2700        2171.2942   3578.2700 286.2616
## 57  3578.2700        2171.2942   3578.2700 286.2616
## 58  1789.1350        2171.2942   3578.2700 286.2616
## 59  1789.1350        2171.2942   3578.2700 286.2616
## 60   349.5491         413.1463    699.0982  55.9279
## 61  1687.4950        1898.0944   3374.9900 269.9992
## 62   894.5675        2171.2942   3578.2700 286.2616
## 63   894.5675        2171.2942   3578.2700 286.2616
## 64   894.5675        2171.2942   3578.2700 286.2616
## 65   894.5675        2171.2942   3578.2700 286.2616
## 66  3578.2700        2171.2942   3578.2700 286.2616
## 67  3578.2700        2171.2942   3578.2700 286.2616
## 68  3578.2700        2171.2942   3578.2700 286.2616
## 69  3399.9900        1912.1544   3399.9900 271.9992
## 70  3578.2700        2171.2942   3578.2700 286.2616
## 71  3374.9900        1898.0944   3374.9900 269.9992
## 72  3578.2700        2171.2942   3578.2700 286.2616
## 73  3578.2700        2171.2942   3578.2700 286.2616
## 74  3578.2700        2171.2942   3578.2700 286.2616
## 75   699.0982         413.1463    699.0982  55.9279
## 76  3578.2700        2171.2942   3578.2700 286.2616
## 77  3578.2700        2171.2942   3578.2700 286.2616
## 78  3578.2700        2171.2942   3578.2700 286.2616
## 79  3399.9900        1912.1544   3399.9900 271.9992
## 80  3578.2700        2171.2942   3578.2700 286.2616
## 81  3578.2700        2171.2942   3578.2700 286.2616
## 82  3399.9900        1912.1544   3399.9900 271.9992
## 83   699.0982         413.1463    699.0982  55.9279
## 84  3578.2700        2171.2942   3578.2700 286.2616
## 85  3578.2700        2171.2942   3578.2700 286.2616
## 86  3578.2700        2171.2942   3578.2700 286.2616
## 87  3578.2700        2171.2942   3578.2700 286.2616
## 88  1789.1350        2171.2942   3578.2700 286.2616
## 89  1789.1350        2171.2942   3578.2700 286.2616
## 90  1789.1350        2171.2942   3578.2700 286.2616
## 91  1699.9950        1912.1544   3399.9900 271.9992
## 92  1687.4950        1898.0944   3374.9900 269.9992
## 93  1687.4950        1898.0944   3374.9900 269.9992
## 94  1699.9950        1912.1544   3399.9900 271.9992
## 95  1789.1350        2171.2942   3578.2700 286.2616
## 96  1789.1350        2171.2942   3578.2700 286.2616
## 97  1699.9950        1912.1544   3399.9900 271.9992
## 98  1699.9950        1912.1544   3399.9900 271.9992
## 99   894.5675        2171.2942   3578.2700 286.2616
## 100  174.7745         413.1463    699.0982  55.9279
## 101  894.5675        2171.2942   3578.2700 286.2616
## 102  894.5675        2171.2942   3578.2700 286.2616
## 103  894.5675        2171.2942   3578.2700 286.2616
## 104 3578.2700        2171.2942   3578.2700 286.2616
## 105 3578.2700        2171.2942   3578.2700 286.2616
## 106 3578.2700        2171.2942   3578.2700 286.2616
## 107 3578.2700        2171.2942   3578.2700 286.2616
## 108 3578.2700        2171.2942   3578.2700 286.2616
## 109  699.0982         413.1463    699.0982  55.9279
## 110 3578.2700        2171.2942   3578.2700 286.2616
## 111 3578.2700        2171.2942   3578.2700 286.2616
## 112 3578.2700        2171.2942   3578.2700 286.2616
## 113 3578.2700        2171.2942   3578.2700 286.2616
## 114 3399.9900        1912.1544   3399.9900 271.9992
## 115 3399.9900        1912.1544   3399.9900 271.9992
## 116 3578.2700        2171.2942   3578.2700 286.2616
## 117 3374.9900        1898.0944   3374.9900 269.9992
## 118 3374.9900        1898.0944   3374.9900 269.9992
## 119 3578.2700        2171.2942   3578.2700 286.2616
## 120 3578.2700        2171.2942   3578.2700 286.2616
## 121 3578.2700        2171.2942   3578.2700 286.2616
## 122 3578.2700        2171.2942   3578.2700 286.2616
## 123 3399.9900        1912.1544   3399.9900 271.9992
## 124 3578.2700        2171.2942   3578.2700 286.2616
## 125 3578.2700        2171.2942   3578.2700 286.2616
## 126 3578.2700        2171.2942   3578.2700 286.2616
## 127  699.0982         413.1463    699.0982  55.9279
## 128  699.0982         413.1463    699.0982  55.9279
## 129 3578.2700        2171.2942   3578.2700 286.2616
## 130 3374.9900        1898.0944   3374.9900 269.9992
## 131  699.0982         413.1463    699.0982  55.9279
## 132 1789.1350        2171.2942   3578.2700 286.2616
## 133 1789.1350        2171.2942   3578.2700 286.2616
## 134 1789.1350        2171.2942   3578.2700 286.2616
## 135 1687.4950        1898.0944   3374.9900 269.9992
## 136 1789.1350        2171.2942   3578.2700 286.2616
## 137  894.5675        2171.2942   3578.2700 286.2616
## 138  843.7475        1898.0944   3374.9900 269.9992
## 139  843.7475        1898.0944   3374.9900 269.9992
## 140  174.7745         413.1463    699.0982  55.9279
## 141  843.7475        1898.0944   3374.9900 269.9992
## 142 3578.2700        2171.2942   3578.2700 286.2616
## 143 3578.2700        2171.2942   3578.2700 286.2616
## 144  699.0982         413.1463    699.0982  55.9279
## 145 3578.2700        2171.2942   3578.2700 286.2616
## 146 3578.2700        2171.2942   3578.2700 286.2616

Modelling Data

Hierachical clustering

technique is measuring the euclidean distance

we use inbuild data set

#select specific columns
cars <- mtcars[, c(1:4, 6:7, 9:11)]

Compute and plot clusters

hc <- cars %>% #data
  dist %>% #compute distance
  hclust

plotting the cluster dendrogram

plot(hc)

If we would want to draw boxes

#rect.hclust(hc, k=2, border = "gray")
#rect.hclust(hc, k=2, border = "blue")
#rect.hclust(hc, k=2, border = "red")
#rect.hclust(hc, k=2, border = "green")

Principal Component

Dimensionality Reduction

PCA

This is the aspect of reducing dimensionality and maintaining the meaning.

#using cars subset
# we can compute PC
pc <- prcomp(cars, 
             center = TRUE, 
             scale = TRUE)
summary(pc)
## Importance of components:
##                           PC1    PC2     PC3     PC4     PC5     PC6     PC7
## Standard deviation     2.3391 1.5299 0.71836 0.46491 0.38903 0.35099 0.31714
## Proportion of Variance 0.6079 0.2601 0.05734 0.02402 0.01682 0.01369 0.01118
## Cumulative Proportion  0.6079 0.8680 0.92537 0.94939 0.96620 0.97989 0.99107
##                            PC8    PC9
## Standard deviation     0.24070 0.1499
## Proportion of Variance 0.00644 0.0025
## Cumulative Proportion  0.99750 1.0000
plot(pc)

predict(pc) %>%
  round(2)
##                       PC1   PC2   PC3   PC4   PC5   PC6   PC7   PC8   PC9
## Mazda RX4           -0.82  1.46 -0.21  0.32 -0.85 -0.01  0.25  0.07 -0.18
## Mazda RX4 Wag       -0.79  1.26  0.05  0.12 -0.89 -0.08  0.25  0.16 -0.09
## Datsun 710          -2.49  0.03 -0.32 -0.40 -0.37  0.54 -0.50 -0.03  0.11
## Hornet 4 Drive      -0.29 -1.93 -0.32 -0.07  0.21 -0.05 -0.01  0.01 -0.12
## Hornet Sportabout    1.56 -0.81 -1.04  0.05  0.38 -0.14  0.15  0.08 -0.15
## Valiant             -0.21 -2.19  0.14 -0.07 -0.08  0.27 -0.26  0.28  0.02
## Duster 360           2.73  0.29 -0.58  0.53  0.20 -0.21 -0.40 -0.36 -0.17
## Merc 240D           -1.80 -1.27  1.03  0.14  0.40  0.22  0.54 -0.33  0.03
## Merc 230            -1.90 -1.93  1.96 -0.26  0.61 -0.08 -0.39  0.34 -0.19
## Merc 280             0.02 -0.06  1.06  0.74  0.14  0.10  0.43  0.00  0.11
## Merc 280C            0.04 -0.23  1.29  0.68  0.08  0.19  0.25  0.17  0.01
## Merc 450SE           1.82 -0.68 -0.19  0.30 -0.14 -0.18  0.06  0.14  0.40
## Merc 450SL           1.60 -0.68 -0.27  0.40 -0.01 -0.31 -0.03  0.22  0.20
## Merc 450SLC          1.71 -0.80 -0.07  0.37 -0.12 -0.11 -0.21  0.35  0.14
## Cadillac Fleetwood   3.54 -0.79  0.62 -0.84 -0.35  0.14  0.17 -0.10 -0.26
## Lincoln Continental  3.65 -0.73  0.64 -0.87 -0.36  0.12  0.14 -0.17 -0.04
## Chrysler Imperial    3.39 -0.52  0.40 -0.82 -0.07 -0.39  0.26 -0.36  0.22
## Fiat 128            -3.53 -0.24 -0.33 -0.52 -0.03 -0.62  0.11  0.03  0.21
## Honda Civic         -3.44  0.33 -0.42  0.17 -0.28 -0.46  0.16 -0.09 -0.30
## Toyota Corolla      -3.85 -0.29 -0.35 -0.41  0.13 -0.85  0.00  0.16  0.02
## Toyota Corona       -1.64 -1.98  0.10  0.62  0.05  0.14 -0.69 -0.46 -0.07
## Dodge Challenger     1.55 -0.87 -0.91  0.33 -0.03  0.35  0.19  0.20 -0.03
## AMC Javelin          1.44 -0.96 -0.77  0.37 -0.04  0.33  0.05  0.33 -0.06
## Camaro Z28           2.92  0.37 -0.57  0.53  0.06 -0.04 -0.31 -0.47  0.07
## Pontiac Firebird     1.81 -0.90 -0.96 -0.31  0.39 -0.19  0.38 -0.04 -0.12
## Fiat X1-9           -3.22 -0.06 -0.45 -0.20 -0.25  0.06 -0.19  0.06  0.01
## Porsche 914-2       -2.66  1.53 -0.28 -0.21  0.32  0.69  0.41 -0.25  0.09
## Lotus Europa        -3.19  1.69 -0.52  0.01  0.78  0.06  0.16 -0.22  0.02
## Ford Pantera L       1.60  3.10 -0.61 -0.69  0.69  0.60 -0.18  0.42  0.00
## Ferrari Dino        -0.25  3.18  0.73  0.51 -0.24  0.06  0.22 -0.02 -0.01
## Maserati Bora        2.63  4.40  0.97 -0.01  0.27 -0.57 -0.55  0.07 -0.04
## Volvo 142E          -1.94  0.28  0.19 -0.46 -0.58  0.40 -0.48 -0.19  0.14
biplot(pc)

Regression

# we have already loaded our pacman
#Load dataset
library(datasets)
data <-  USJudgeRatings

#define variable groups
x <- as.matrix(data[-12])
y <- data[, 12]
data
##                 CONT INTG DMNR DILG CFMG DECI PREP FAMI ORAL WRIT PHYS RTEN
## AARONSON,L.H.    5.7  7.9  7.7  7.3  7.1  7.4  7.1  7.1  7.1  7.0  8.3  7.8
## ALEXANDER,J.M.   6.8  8.9  8.8  8.5  7.8  8.1  8.0  8.0  7.8  7.9  8.5  8.7
## ARMENTANO,A.J.   7.2  8.1  7.8  7.8  7.5  7.6  7.5  7.5  7.3  7.4  7.9  7.8
## BERDON,R.I.      6.8  8.8  8.5  8.8  8.3  8.5  8.7  8.7  8.4  8.5  8.8  8.7
## BRACKEN,J.J.     7.3  6.4  4.3  6.5  6.0  6.2  5.7  5.7  5.1  5.3  5.5  4.8
## BURNS,E.B.       6.2  8.8  8.7  8.5  7.9  8.0  8.1  8.0  8.0  8.0  8.6  8.6
## CALLAHAN,R.J.   10.6  9.0  8.9  8.7  8.5  8.5  8.5  8.5  8.6  8.4  9.1  9.0
## COHEN,S.S.       7.0  5.9  4.9  5.1  5.4  5.9  4.8  5.1  4.7  4.9  6.8  5.0
## DALY,J.J.        7.3  8.9  8.9  8.7  8.6  8.5  8.4  8.4  8.4  8.5  8.8  8.8
## DANNEHY,J.F.     8.2  7.9  6.7  8.1  7.9  8.0  7.9  8.1  7.7  7.8  8.5  7.9
## DEAN,H.H.        7.0  8.0  7.6  7.4  7.3  7.5  7.1  7.2  7.1  7.2  8.4  7.7
## DEVITA,H.J.      6.5  8.0  7.6  7.2  7.0  7.1  6.9  7.0  7.0  7.1  6.9  7.2
## DRISCOLL,P.J.    6.7  8.6  8.2  6.8  6.9  6.6  7.1  7.3  7.2  7.2  8.1  7.7
## GRILLO,A.E.      7.0  7.5  6.4  6.8  6.5  7.0  6.6  6.8  6.3  6.6  6.2  6.5
## HADDEN,W.L.JR.   6.5  8.1  8.0  8.0  7.9  8.0  7.9  7.8  7.8  7.8  8.4  8.0
## HAMILL,E.C.      7.3  8.0  7.4  7.7  7.3  7.3  7.3  7.2  7.1  7.2  8.0  7.6
## HEALEY.A.H.      8.0  7.6  6.6  7.2  6.5  6.5  6.8  6.7  6.4  6.5  6.9  6.7
## HULL,T.C.        7.7  7.7  6.7  7.5  7.4  7.5  7.1  7.3  7.1  7.3  8.1  7.4
## LEVINE,I.        8.3  8.2  7.4  7.8  7.7  7.7  7.7  7.8  7.5  7.6  8.0  8.0
## LEVISTER,R.L.    9.6  6.9  5.7  6.6  6.9  6.6  6.2  6.0  5.8  5.8  7.2  6.0
## MARTIN,L.F.      7.1  8.2  7.7  7.1  6.6  6.6  6.7  6.7  6.8  6.8  7.5  7.3
## MCGRATH,J.F.     7.6  7.3  6.9  6.8  6.7  6.8  6.4  6.3  6.3  6.3  7.4  6.6
## MIGNONE,A.F.     6.6  7.4  6.2  6.2  5.4  5.7  5.8  5.9  5.2  5.8  4.7  5.2
## MISSAL,H.M.      6.2  8.3  8.1  7.7  7.4  7.3  7.3  7.3  7.2  7.3  7.8  7.6
## MULVEY,H.M.      7.5  8.7  8.5  8.6  8.5  8.4  8.5  8.5  8.4  8.4  8.7  8.7
## NARUK,H.J.       7.8  8.9  8.7  8.9  8.7  8.8  8.9  9.0  8.8  8.9  9.0  9.0
## O'BRIEN,F.J.     7.1  8.5  8.3  8.0  7.9  7.9  7.8  7.8  7.8  7.7  8.3  8.2
## O'SULLIVAN,T.J.  7.5  9.0  8.9  8.7  8.4  8.5  8.4  8.3  8.3  8.3  8.8  8.7
## PASKEY,L.        7.5  8.1  7.7  8.2  8.0  8.1  8.2  8.4  8.0  8.1  8.4  8.1
## RUBINOW,J.E.     7.1  9.2  9.0  9.0  8.4  8.6  9.1  9.1  8.9  9.0  8.9  9.2
## SADEN.G.A.       6.6  7.4  6.9  8.4  8.0  7.9  8.2  8.4  7.7  7.9  8.4  7.5
## SATANIELLO,A.G.  8.4  8.0  7.9  7.9  7.8  7.8  7.6  7.4  7.4  7.4  8.1  7.9
## SHEA,D.M.        6.9  8.5  7.8  8.5  8.1  8.2  8.4  8.5  8.1  8.3  8.7  8.3
## SHEA,J.F.JR.     7.3  8.9  8.8  8.7  8.4  8.5  8.5  8.5  8.4  8.4  8.8  8.8
## SIDOR,W.J.       7.7  6.2  5.1  5.6  5.6  5.9  5.6  5.6  5.3  5.5  6.3  5.3
## SPEZIALE,J.A.    8.5  8.3  8.1  8.3  8.4  8.2  8.2  8.1  7.9  8.0  8.0  8.2
## SPONZO,M.J.      6.9  8.3  8.0  8.1  7.9  7.9  7.9  7.7  7.6  7.7  8.1  8.0
## STAPLETON,J.F.   6.5  8.2  7.7  7.8  7.6  7.7  7.7  7.7  7.5  7.6  8.5  7.7
## TESTO,R.J.       8.3  7.3  7.0  6.8  7.0  7.1  6.7  6.7  6.7  6.7  8.0  7.0
## TIERNEY,W.L.JR.  8.3  8.2  7.8  8.3  8.4  8.3  7.7  7.6  7.5  7.7  8.1  7.9
## WALL,R.A.        9.0  7.0  5.9  7.0  7.0  7.2  6.9  6.9  6.5  6.6  7.6  6.6
## WRIGHT,D.B.      7.1  8.4  8.4  7.7  7.5  7.7  7.8  8.2  8.0  8.1  8.3  8.1
## ZARRILLI,K.J.    8.6  7.4  7.0  7.5  7.5  7.7  7.4  7.2  6.9  7.0  7.8  7.1

Regression with simultaneous entry

reg <- lm(y~x)
reg
## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)        xCONT        xINTG        xDMNR        xDILG        xCFMG  
##    -2.11943      0.01280      0.36484      0.12540      0.06669     -0.19453  
##       xDECI        xPREP        xFAMI        xORAL        xWRIT        xPHYS  
##     0.27829     -0.00196     -0.13579      0.54782     -0.06806      0.26881
summary(reg)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.22123 -0.06155 -0.01055  0.05045  0.26079 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -2.11943    0.51904  -4.083 0.000290 ***
## xCONT        0.01280    0.02586   0.495 0.624272    
## xINTG        0.36484    0.12936   2.820 0.008291 ** 
## xDMNR        0.12540    0.08971   1.398 0.172102    
## xDILG        0.06669    0.14303   0.466 0.644293    
## xCFMG       -0.19453    0.14779  -1.316 0.197735    
## xDECI        0.27829    0.13826   2.013 0.052883 .  
## xPREP       -0.00196    0.24001  -0.008 0.993536    
## xFAMI       -0.13579    0.26725  -0.508 0.614972    
## xORAL        0.54782    0.27725   1.976 0.057121 .  
## xWRIT       -0.06806    0.31485  -0.216 0.830269    
## xPHYS        0.26881    0.06213   4.326 0.000146 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1174 on 31 degrees of freedom
## Multiple R-squared:  0.9916, Adjusted R-squared:  0.9886 
## F-statistic: 332.9 on 11 and 31 DF,  p-value: < 2.2e-16
hist(residuals(reg))

More Models

p_load(lars, caret)
# lars - least angle regression
# caret - classification and regression training

stepwise <- lars(x,y, type="stepwise")
stepwise
## 
## Call:
## lars(x = x, y = y, type = "stepwise")
## R-squared: 0.992 
## Sequence of Forward Stepwise moves:
##      ORAL DMNR PHYS CONT INTG DECI CFMG FAMI DILG WRIT PREP
## Var     9    3   11    1    2    6    5    8    4   10    7
## Step    1    2    3    4    5    6    7    8    9   10   11
forward <- lars(x,y, type="forward.stagewise")
forward
## 
## Call:
## lars(x = x, y = y, type = "forward.stagewise")
## R-squared: 0.992 
## Sequence of Forward Stagewise moves:
##      ORAL DMNR PHYS ORAL INTG DMNR CONT FAMI DECI CONT WRIT FAMI CFMG DILG CONT
## Var     9    3   11   -9    2   -3    1    8    6   -1   10   -8    5    4    1
## Step    1    2    3    3    4    4    5    6    7    7    8    8    9   10   11
##      DMNR ORAL FAMI WRIT PREP FAMI WRIT FAMI PREP PREP DILG DILG
## Var     3    9    8  -10    7   -8   10    8   -7    7   -4    4
## Step   12   13   14   14   15   15   16   17   17   18   18   19
lasso <- lars(x,y, type="lasso")
lasso
## 
## Call:
## lars(x = x, y = y, type = "lasso")
## R-squared: 0.992 
## Sequence of LASSO moves:
##      ORAL DMNR PHYS INTG DECI CONT CFMG DILG FAMI WRIT PREP
## Var     9    3   11    2    6    1    5    4    8   10    7
## Step    1    2    3    4    5    6    7    8    9   10   11
lar <- lars(x,y, type="lar")
lar
## 
## Call:
## lars(x = x, y = y, type = "lar")
## R-squared: 0.992 
## Sequence of LAR moves:
##      ORAL DMNR PHYS INTG DECI CONT CFMG DILG FAMI WRIT PREP
## Var     9    3   11    2    6    1    5    4    8   10    7
## Step    1    2    3    4    5    6    7    8    9   10   11
r2comp <- c(stepwise$R2[6], forward$R2[6],
            lar$R2[6], lasso$R2[6] %>% round(2))

r2comp
##         5         5         5         5 
## 0.9898984 0.9886482 0.9908669 0.9900000
names(r2comp) <- c("stepwise", "forward", "lar", "lasso")

Converting data type

x <- 400
y <- "100"

print(x + strtoi(y))
## [1] 500