Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

getwd()
## [1] "C:/Users/alviy/OneDrive/RPractice"
?setwd()
## starting httpd help server ... done
setwd("C:/Users/alviy/OneDrive/Documents")
filename <- "iris.csv"
# load the CSV file from the local directory
dataset <- read.csv(filename, header=FALSE)
# preview the first 5 rows
head(dataset)
##    V1  V2  V3  V4          V5
## 1 5.1 3.5 1.4 0.2 Iris-setosa
## 2 4.9 3.0 1.4 0.2 Iris-setosa
## 3 4.7 3.2 1.3 0.2 Iris-setosa
## 4 4.6 3.1 1.5 0.2 Iris-setosa
## 5 5.0 3.6 1.4 0.2 Iris-setosa
## 6 5.4 3.9 1.7 0.4 Iris-setosa
dataset
##      V1  V2  V3  V4              V5
## 1   5.1 3.5 1.4 0.2     Iris-setosa
## 2   4.9 3.0 1.4 0.2     Iris-setosa
## 3   4.7 3.2 1.3 0.2     Iris-setosa
## 4   4.6 3.1 1.5 0.2     Iris-setosa
## 5   5.0 3.6 1.4 0.2     Iris-setosa
## 6   5.4 3.9 1.7 0.4     Iris-setosa
## 7   4.6 3.4 1.4 0.3     Iris-setosa
## 8   5.0 3.4 1.5 0.2     Iris-setosa
## 9   4.4 2.9 1.4 0.2     Iris-setosa
## 10  4.9 3.1 1.5 0.1     Iris-setosa
## 11  5.4 3.7 1.5 0.2     Iris-setosa
## 12  4.8 3.4 1.6 0.2     Iris-setosa
## 13  4.8 3.0 1.4 0.1     Iris-setosa
## 14  4.3 3.0 1.1 0.1     Iris-setosa
## 15  5.8 4.0 1.2 0.2     Iris-setosa
## 16  5.7 4.4 1.5 0.4     Iris-setosa
## 17  5.4 3.9 1.3 0.4     Iris-setosa
## 18  5.1 3.5 1.4 0.3     Iris-setosa
## 19  5.7 3.8 1.7 0.3     Iris-setosa
## 20  5.1 3.8 1.5 0.3     Iris-setosa
## 21  5.4 3.4 1.7 0.2     Iris-setosa
## 22  5.1 3.7 1.5 0.4     Iris-setosa
## 23  4.6 3.6 1.0 0.2     Iris-setosa
## 24  5.1 3.3 1.7 0.5     Iris-setosa
## 25  4.8 3.4 1.9 0.2     Iris-setosa
## 26  5.0 3.0 1.6 0.2     Iris-setosa
## 27  5.0 3.4 1.6 0.4     Iris-setosa
## 28  5.2 3.5 1.5 0.2     Iris-setosa
## 29  5.2 3.4 1.4 0.2     Iris-setosa
## 30  4.7 3.2 1.6 0.2     Iris-setosa
## 31  4.8 3.1 1.6 0.2     Iris-setosa
## 32  5.4 3.4 1.5 0.4     Iris-setosa
## 33  5.2 4.1 1.5 0.1     Iris-setosa
## 34  5.5 4.2 1.4 0.2     Iris-setosa
## 35  4.9 3.1 1.5 0.1     Iris-setosa
## 36  5.0 3.2 1.2 0.2     Iris-setosa
## 37  5.5 3.5 1.3 0.2     Iris-setosa
## 38  4.9 3.1 1.5 0.1     Iris-setosa
## 39  4.4 3.0 1.3 0.2     Iris-setosa
## 40  5.1 3.4 1.5 0.2     Iris-setosa
## 41  5.0 3.5 1.3 0.3     Iris-setosa
## 42  4.5 2.3 1.3 0.3     Iris-setosa
## 43  4.4 3.2 1.3 0.2     Iris-setosa
## 44  5.0 3.5 1.6 0.6     Iris-setosa
## 45  5.1 3.8 1.9 0.4     Iris-setosa
## 46  4.8 3.0 1.4 0.3     Iris-setosa
## 47  5.1 3.8 1.6 0.2     Iris-setosa
## 48  4.6 3.2 1.4 0.2     Iris-setosa
## 49  5.3 3.7 1.5 0.2     Iris-setosa
## 50  5.0 3.3 1.4 0.2     Iris-setosa
## 51  7.0 3.2 4.7 1.4 Iris-versicolor
## 52  6.4 3.2 4.5 1.5 Iris-versicolor
## 53  6.9 3.1 4.9 1.5 Iris-versicolor
## 54  5.5 2.3 4.0 1.3 Iris-versicolor
## 55  6.5 2.8 4.6 1.5 Iris-versicolor
## 56  5.7 2.8 4.5 1.3 Iris-versicolor
## 57  6.3 3.3 4.7 1.6 Iris-versicolor
## 58  4.9 2.4 3.3 1.0 Iris-versicolor
## 59  6.6 2.9 4.6 1.3 Iris-versicolor
## 60  5.2 2.7 3.9 1.4 Iris-versicolor
## 61  5.0 2.0 3.5 1.0 Iris-versicolor
## 62  5.9 3.0 4.2 1.5 Iris-versicolor
## 63  6.0 2.2 4.0 1.0 Iris-versicolor
## 64  6.1 2.9 4.7 1.4 Iris-versicolor
## 65  5.6 2.9 3.6 1.3 Iris-versicolor
## 66  6.7 3.1 4.4 1.4 Iris-versicolor
## 67  5.6 3.0 4.5 1.5 Iris-versicolor
## 68  5.8 2.7 4.1 1.0 Iris-versicolor
## 69  6.2 2.2 4.5 1.5 Iris-versicolor
## 70  5.6 2.5 3.9 1.1 Iris-versicolor
## 71  5.9 3.2 4.8 1.8 Iris-versicolor
## 72  6.1 2.8 4.0 1.3 Iris-versicolor
## 73  6.3 2.5 4.9 1.5 Iris-versicolor
## 74  6.1 2.8 4.7 1.2 Iris-versicolor
## 75  6.4 2.9 4.3 1.3 Iris-versicolor
## 76  6.6 3.0 4.4 1.4 Iris-versicolor
## 77  6.8 2.8 4.8 1.4 Iris-versicolor
## 78  6.7 3.0 5.0 1.7 Iris-versicolor
## 79  6.0 2.9 4.5 1.5 Iris-versicolor
## 80  5.7 2.6 3.5 1.0 Iris-versicolor
## 81  5.5 2.4 3.8 1.1 Iris-versicolor
## 82  5.5 2.4 3.7 1.0 Iris-versicolor
## 83  5.8 2.7 3.9 1.2 Iris-versicolor
## 84  6.0 2.7 5.1 1.6 Iris-versicolor
## 85  5.4 3.0 4.5 1.5 Iris-versicolor
## 86  6.0 3.4 4.5 1.6 Iris-versicolor
## 87  6.7 3.1 4.7 1.5 Iris-versicolor
## 88  6.3 2.3 4.4 1.3 Iris-versicolor
## 89  5.6 3.0 4.1 1.3 Iris-versicolor
## 90  5.5 2.5 4.0 1.3 Iris-versicolor
## 91  5.5 2.6 4.4 1.2 Iris-versicolor
## 92  6.1 3.0 4.6 1.4 Iris-versicolor
## 93  5.8 2.6 4.0 1.2 Iris-versicolor
## 94  5.0 2.3 3.3 1.0 Iris-versicolor
## 95  5.6 2.7 4.2 1.3 Iris-versicolor
## 96  5.7 3.0 4.2 1.2 Iris-versicolor
## 97  5.7 2.9 4.2 1.3 Iris-versicolor
## 98  6.2 2.9 4.3 1.3 Iris-versicolor
## 99  5.1 2.5 3.0 1.1 Iris-versicolor
## 100 5.7 2.8 4.1 1.3 Iris-versicolor
## 101 6.3 3.3 6.0 2.5  Iris-virginica
## 102 5.8 2.7 5.1 1.9  Iris-virginica
## 103 7.1 3.0 5.9 2.1  Iris-virginica
## 104 6.3 2.9 5.6 1.8  Iris-virginica
## 105 6.5 3.0 5.8 2.2  Iris-virginica
## 106 7.6 3.0 6.6 2.1  Iris-virginica
## 107 4.9 2.5 4.5 1.7  Iris-virginica
## 108 7.3 2.9 6.3 1.8  Iris-virginica
## 109 6.7 2.5 5.8 1.8  Iris-virginica
## 110 7.2 3.6 6.1 2.5  Iris-virginica
## 111 6.5 3.2 5.1 2.0  Iris-virginica
## 112 6.4 2.7 5.3 1.9  Iris-virginica
## 113 6.8 3.0 5.5 2.1  Iris-virginica
## 114 5.7 2.5 5.0 2.0  Iris-virginica
## 115 5.8 2.8 5.1 2.4  Iris-virginica
## 116 6.4 3.2 5.3 2.3  Iris-virginica
## 117 6.5 3.0 5.5 1.8  Iris-virginica
## 118 7.7 3.8 6.7 2.2  Iris-virginica
## 119 7.7 2.6 6.9 2.3  Iris-virginica
## 120 6.0 2.2 5.0 1.5  Iris-virginica
## 121 6.9 3.2 5.7 2.3  Iris-virginica
## 122 5.6 2.8 4.9 2.0  Iris-virginica
## 123 7.7 2.8 6.7 2.0  Iris-virginica
## 124 6.3 2.7 4.9 1.8  Iris-virginica
## 125 6.7 3.3 5.7 2.1  Iris-virginica
## 126 7.2 3.2 6.0 1.8  Iris-virginica
## 127 6.2 2.8 4.8 1.8  Iris-virginica
## 128 6.1 3.0 4.9 1.8  Iris-virginica
## 129 6.4 2.8 5.6 2.1  Iris-virginica
## 130 7.2 3.0 5.8 1.6  Iris-virginica
## 131 7.4 2.8 6.1 1.9  Iris-virginica
## 132 7.9 3.8 6.4 2.0  Iris-virginica
## 133 6.4 2.8 5.6 2.2  Iris-virginica
## 134 6.3 2.8 5.1 1.5  Iris-virginica
## 135 6.1 2.6 5.6 1.4  Iris-virginica
## 136 7.7 3.0 6.1 2.3  Iris-virginica
## 137 6.3 3.4 5.6 2.4  Iris-virginica
## 138 6.4 3.1 5.5 1.8  Iris-virginica
## 139 6.0 3.0 4.8 1.8  Iris-virginica
## 140 6.9 3.1 5.4 2.1  Iris-virginica
## 141 6.7 3.1 5.6 2.4  Iris-virginica
## 142 6.9 3.1 5.1 2.3  Iris-virginica
## 143 5.8 2.7 5.1 1.9  Iris-virginica
## 144 6.8 3.2 5.9 2.3  Iris-virginica
## 145 6.7 3.3 5.7 2.5  Iris-virginica
## 146 6.7 3.0 5.2 2.3  Iris-virginica
## 147 6.3 2.5 5.0 1.9  Iris-virginica
## 148 6.5 3.0 5.2 2.0  Iris-virginica
## 149 6.2 3.4 5.4 2.3  Iris-virginica
## 150 5.9 3.0 5.1 1.8  Iris-virginica
# load the package
library(RCurl)
# specify the URL for the Iris data CSV
urlfile <-'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
# download the file
downloaded <- getURL(urlfile, ssl.verifypeer=FALSE)
# treat the text data as a steam so we can read from it
connection <- textConnection(downloaded)
# parse the downloaded data as CSV
dataset <- read.csv(connection, header=FALSE)
# preview the first 5 rows
head(dataset)
##    V1  V2  V3  V4          V5
## 1 5.1 3.5 1.4 0.2 Iris-setosa
## 2 4.9 3.0 1.4 0.2 Iris-setosa
## 3 4.7 3.2 1.3 0.2 Iris-setosa
## 4 4.6 3.1 1.5 0.2 Iris-setosa
## 5 5.0 3.6 1.4 0.2 Iris-setosa
## 6 5.4 3.9 1.7 0.4 Iris-setosa
# load the package
library(mlbench)
# load the dataset
data(PimaIndiansDiabetes)
# display first 20 rows of data
head(PimaIndiansDiabetes, n=20)
##    pregnant glucose pressure triceps insulin mass pedigree age diabetes
## 1         6     148       72      35       0 33.6    0.627  50      pos
## 2         1      85       66      29       0 26.6    0.351  31      neg
## 3         8     183       64       0       0 23.3    0.672  32      pos
## 4         1      89       66      23      94 28.1    0.167  21      neg
## 5         0     137       40      35     168 43.1    2.288  33      pos
## 6         5     116       74       0       0 25.6    0.201  30      neg
## 7         3      78       50      32      88 31.0    0.248  26      pos
## 8        10     115        0       0       0 35.3    0.134  29      neg
## 9         2     197       70      45     543 30.5    0.158  53      pos
## 10        8     125       96       0       0  0.0    0.232  54      pos
## 11        4     110       92       0       0 37.6    0.191  30      neg
## 12       10     168       74       0       0 38.0    0.537  34      pos
## 13       10     139       80       0       0 27.1    1.441  57      neg
## 14        1     189       60      23     846 30.1    0.398  59      pos
## 15        5     166       72      19     175 25.8    0.587  51      pos
## 16        7     100        0       0       0 30.0    0.484  32      pos
## 17        0     118       84      47     230 45.8    0.551  31      pos
## 18        7     107       74       0       0 29.6    0.254  31      pos
## 19        1     103       30      38      83 43.3    0.183  33      neg
## 20        1     115       70      30      96 34.6    0.529  32      pos
# display the dimensions of the dataset
dim(PimaIndiansDiabetes)
## [1] 768   9
# load dataset
data(BostonHousing)
# list types for each attribute
sapply(BostonHousing, class)
##      crim        zn     indus      chas       nox        rm       age       dis 
## "numeric" "numeric" "numeric"  "factor" "numeric" "numeric" "numeric" "numeric" 
##       rad       tax   ptratio         b     lstat      medv 
## "numeric" "numeric" "numeric" "numeric" "numeric" "numeric"
# load the dataset
data(PimaIndiansDiabetes)
# distribution of class variable
y <- PimaIndiansDiabetes$diabetes
cbind(freq=table(y), percentage=prop.table(table(y))*100)
##     freq percentage
## neg  500   65.10417
## pos  268   34.89583
# load the iris dataset
data(iris)
# summarize the dataset
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  
##                 
##                 
## 
?summary()
# load the dataset
data(PimaIndiansDiabetes)
# calculate standard deviation for all attributes
sapply(PimaIndiansDiabetes[,1:8], sd)
##    pregnant     glucose    pressure     triceps     insulin        mass 
##   3.3695781  31.9726182  19.3558072  15.9522176 115.2440024   7.8841603 
##    pedigree         age 
##   0.3313286  11.7602315
# load packages
library(mlbench)
library(e1071)
# load the dataset
data(PimaIndiansDiabetes)
# calculate skewness for each variable
skew <- apply(PimaIndiansDiabetes[,1:8], 2, skewness)
# display skewness, larger/smaller deviations from 0 show more skew
print(skew)
##   pregnant    glucose   pressure    triceps    insulin       mass   pedigree 
##  0.8981549  0.1730754 -1.8364126  0.1089456  2.2633826 -0.4273073  1.9124179 
##        age 
##  1.1251880
# calculate a correlation matrix for numeric variables
correlations <- cor(PimaIndiansDiabetes[,1:8])
# display the correlation matrix
print(correlations)
##             pregnant    glucose   pressure     triceps     insulin       mass
## pregnant  1.00000000 0.12945867 0.14128198 -0.08167177 -0.07353461 0.01768309
## glucose   0.12945867 1.00000000 0.15258959  0.05732789  0.33135711 0.22107107
## pressure  0.14128198 0.15258959 1.00000000  0.20737054  0.08893338 0.28180529
## triceps  -0.08167177 0.05732789 0.20737054  1.00000000  0.43678257 0.39257320
## insulin  -0.07353461 0.33135711 0.08893338  0.43678257  1.00000000 0.19785906
## mass      0.01768309 0.22107107 0.28180529  0.39257320  0.19785906 1.00000000
## pedigree -0.03352267 0.13733730 0.04126495  0.18392757  0.18507093 0.14064695
## age       0.54434123 0.26351432 0.23952795 -0.11397026 -0.04216295 0.03624187
##             pedigree         age
## pregnant -0.03352267  0.54434123
## glucose   0.13733730  0.26351432
## pressure  0.04126495  0.23952795
## triceps   0.18392757 -0.11397026
## insulin   0.18507093 -0.04216295
## mass      0.14064695  0.03624187
## pedigree  1.00000000  0.03356131
## age       0.03356131  1.00000000
###NOTE###
# As it says "Values above 0.75 or below-0.75 are perhaps more interesting as they show a high correlation or high negative correlation.Values of 1 and -1 show full positive or negative correlation."
#Row 1, Col 1 represents its a full positive correlation
#There is no representation of a full negative correlation obtained from this data
#Row 2, Col 1 represents its a high positive correlation
#Row 1, Col 4 represents its a high negative correlation

# load the data
data(iris)
# create histograms for each attribute
par(mfrow=c(1,4))
for(i in 1:4) {
hist(iris[,i], main=names(iris)[i])
}
# load packages
library(lattice)

# create a panel of simpler density plots by attribute
par(mfrow=c(1,4))
for(i in 1:4) {
plot(density(iris[,i]), main=names(iris)[i])
}

# Create separate boxplots for each attribute
par(mfrow=c(1,4))
for(i in 1:4) {
boxplot(iris[,i], main=names(iris)[i])
}

# load the package
library(mlbench)
# load the dataset
data(BreastCancer)
?BreastCancer
# create a bar plot of each categorical attribute
par(mfrow=c(2,4))
for(i in 2:9) {
counts <- table(BreastCancer[,i])
name <- names(BreastCancer)[i]
barplot(counts, main=name)
}

# load packages
library(Amelia)
## Loading required package: Rcpp
## ## 
## ## Amelia II: Multiple Imputation
## ## (Version 1.8.1, built: 2022-11-18)
## ## Copyright (C) 2005-2024 James Honaker, Gary King and Matthew Blackwell
## ## Refer to http://gking.harvard.edu/amelia/ for more information
## ##
library(mlbench)
# load dataset
data(Soybean)
# create a missing map
missmap(Soybean, col=c("black", "grey"), legend=FALSE)
# load package
library(corrplot)
## corrplot 0.92 loaded
# load the data
data(iris)
# calculate correlations
correlations <- cor(iris[,1:4])
# create correlation plot
corrplot(correlations, method="circle")
# pair-wise scatterplots of all 4 attributes
pairs(iris)

# pair-wise scatterplots colored by class
pairs(Species~., data=iris, col=iris$Species)

# load the package
library(caret)
## Loading required package: ggplot2
# load the data
data(iris)
# density plots for each attribute by class value
x <- iris[,1:4]
y <- iris[,5]
scales <- list(x=list(relation="free"), y=list(relation="free"))
featurePlot(x=x, y=y, plot="density", scales=scales)

# box and whisker plots for each attribute by class value
x <- iris[,1:4]
y <- iris[,5]
featurePlot(x=x, y=y, plot="box")

###Note###
# Histograms:-
# Most of the attributes show Gaussian or multi-modal Gaussian distributions.
# Petal width and length columns indicate the presence of measurements from very small flowers.
# Density Plots:-
# Double Gaussian distribution is observed in petal measurements.
# Sepal width possibly follows an exponential distribution.
# Box and Whisker Plots:-
# Data exhibits a similar range across attributes, all measured in centimeters.
# Sepal width may contain a few outlier values.
# Bar Plots (BreastCancer dataset):-
# Some attributes show a good mixed distribution, while others have a few labels dominating the instances.
# Missing Plot:-
# Some instances have a significant amount of missing data across attributes.
# Correlation Plot:-
# Some attributes are highly correlated, as seen in the correlation matrix.
# Perfect positive correlation exists for diagonal attributes (each attribute with itself).
# Scatterplot Matrix:-
# Linear relationship observed between petal length and width.
# Matrix symmetry aids in viewing data from multiple perspectives.
# Scatterplot Matrix By Class:-
# Clear separation of points by class label observed in most pair-wise plots.
# Density Plots By Class:-
# Some classes show non-overlapping density distributions, while others have considerable overlap.
# Box and Whisker Plots By Class:-
# Some attributes exhibit good separation between attribute-class groups, while others show overlap.
# load packages
library(caret)
# load the dataset
data(iris)
# summarize data
summary(iris[,1:4])
##   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
# calculate the pre-process parameters from the dataset
preprocessParams <- preProcess(iris[,1:4], method=c("scale"))
# summarize transform parameters
print(preprocessParams)
## Created from 150 samples and 4 variables
## 
## Pre-processing:
##   - ignored (0)
##   - scaled (4)
# transform the dataset using the parameters
transformed <- predict(preprocessParams, iris[,1:4])
# summarize the transformed dataset
summary(transformed)
##   Sepal.Length    Sepal.Width      Petal.Length     Petal.Width    
##  Min.   :5.193   Min.   : 4.589   Min.   :0.5665   Min.   :0.1312  
##  1st Qu.:6.159   1st Qu.: 6.424   1st Qu.:0.9064   1st Qu.:0.3936  
##  Median :7.004   Median : 6.883   Median :2.4642   Median :1.7055  
##  Mean   :7.057   Mean   : 7.014   Mean   :2.1288   Mean   :1.5734  
##  3rd Qu.:7.729   3rd Qu.: 7.571   3rd Qu.:2.8890   3rd Qu.:2.3615  
##  Max.   :9.540   Max.   :10.095   Max.   :3.9087   Max.   :3.2798
# calculate the pre-process parameters from the dataset
preprocessParams <- preProcess(iris[,1:4], method=c("center"))
# summarize transform parameters
print(preprocessParams)
## Created from 150 samples and 4 variables
## 
## Pre-processing:
##   - centered (4)
##   - ignored (0)
# transform the dataset using the parameters
transformed <- predict(preprocessParams, iris[,1:4])
# summarize the transformed dataset
summary(transformed)
##   Sepal.Length       Sepal.Width        Petal.Length     Petal.Width     
##  Min.   :-1.54333   Min.   :-1.05733   Min.   :-2.758   Min.   :-1.0993  
##  1st Qu.:-0.74333   1st Qu.:-0.25733   1st Qu.:-2.158   1st Qu.:-0.8993  
##  Median :-0.04333   Median :-0.05733   Median : 0.592   Median : 0.1007  
##  Mean   : 0.00000   Mean   : 0.00000   Mean   : 0.000   Mean   : 0.0000  
##  3rd Qu.: 0.55667   3rd Qu.: 0.24267   3rd Qu.: 1.342   3rd Qu.: 0.6007  
##  Max.   : 2.05667   Max.   : 1.34267   Max.   : 3.142   Max.   : 1.3007
# calculate the pre-process parameters from the dataset
preprocessParams <- preProcess(iris[,1:4], method=c("center", "scale"))
# summarize transform parameters
print(preprocessParams)
## Created from 150 samples and 4 variables
## 
## Pre-processing:
##   - centered (4)
##   - ignored (0)
##   - scaled (4)
# transform the dataset using the parameters
transformed <- predict(preprocessParams, iris[,1:4])
# summarize the transformed dataset
summary(transformed)
##   Sepal.Length       Sepal.Width       Petal.Length      Petal.Width     
##  Min.   :-1.86378   Min.   :-2.4258   Min.   :-1.5623   Min.   :-1.4422  
##  1st Qu.:-0.89767   1st Qu.:-0.5904   1st Qu.:-1.2225   1st Qu.:-1.1799  
##  Median :-0.05233   Median :-0.1315   Median : 0.3354   Median : 0.1321  
##  Mean   : 0.00000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.: 0.67225   3rd Qu.: 0.5567   3rd Qu.: 0.7602   3rd Qu.: 0.7880  
##  Max.   : 2.48370   Max.   : 3.0805   Max.   : 1.7799   Max.   : 1.7064
# calculate the pre-process parameters from the dataset
preprocessParams <- preProcess(iris[,1:4], method=c("range"))
# summarize transform parameters
print(preprocessParams)
## Created from 150 samples and 4 variables
## 
## Pre-processing:
##   - ignored (0)
##   - re-scaling to [0, 1] (4)
# transform the dataset using the parameters
transformed <- predict(preprocessParams, iris[,1:4])
# summarize the transformed dataset
summary(transformed)
##   Sepal.Length     Sepal.Width      Petal.Length     Petal.Width     
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :0.00000  
##  1st Qu.:0.2222   1st Qu.:0.3333   1st Qu.:0.1017   1st Qu.:0.08333  
##  Median :0.4167   Median :0.4167   Median :0.5678   Median :0.50000  
##  Mean   :0.4287   Mean   :0.4406   Mean   :0.4675   Mean   :0.45806  
##  3rd Qu.:0.5833   3rd Qu.:0.5417   3rd Qu.:0.6949   3rd Qu.:0.70833  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.0000   Max.   :1.00000
# load packages
library(mlbench)
library(caret)
# load the dataset
data(PimaIndiansDiabetes)
# summarize pedigree and age
summary(PimaIndiansDiabetes[,7:8])
##     pedigree           age       
##  Min.   :0.0780   Min.   :21.00  
##  1st Qu.:0.2437   1st Qu.:24.00  
##  Median :0.3725   Median :29.00  
##  Mean   :0.4719   Mean   :33.24  
##  3rd Qu.:0.6262   3rd Qu.:41.00  
##  Max.   :2.4200   Max.   :81.00
# calculate the pre-process parameters from the dataset
preprocessParams <- preProcess(PimaIndiansDiabetes[,7:8], method=c("BoxCox"))
# summarize transform parameters
print(preprocessParams)
## Created from 768 samples and 2 variables
## 
## Pre-processing:
##   - Box-Cox transformation (2)
##   - ignored (0)
## 
## Lambda estimates for Box-Cox transformation:
## -0.1, -1.1
# transform the dataset using the parameters
transformed <- predict(preprocessParams, PimaIndiansDiabetes[,7:8])
# summarize the transformed dataset (note pedigree and age)
summary(transformed)
##     pedigree            age        
##  Min.   :-2.5510   Min.   :0.8772  
##  1st Qu.:-1.4116   1st Qu.:0.8815  
##  Median :-0.9875   Median :0.8867  
##  Mean   :-0.9599   Mean   :0.8874  
##  3rd Qu.:-0.4680   3rd Qu.:0.8938  
##  Max.   : 0.8838   Max.   :0.9019
# calculate the pre-process parameters from the dataset
preprocessParams <- preProcess(PimaIndiansDiabetes[,7:8], method=c("YeoJohnson"))
# summarize transform parameters
print(preprocessParams)
## Created from 768 samples and 2 variables
## 
## Pre-processing:
##   - ignored (0)
##   - Yeo-Johnson transformation (2)
## 
## Lambda estimates for Yeo-Johnson transformation:
## -2.25, -1.15
# transform the dataset using the parameters
transformed <- predict(preprocessParams, PimaIndiansDiabetes[,7:8])
# summarize the transformed dataset (note pedigree and age)
summary(transformed)
##     pedigree           age        
##  Min.   :0.0691   Min.   :0.8450  
##  1st Qu.:0.1724   1st Qu.:0.8484  
##  Median :0.2265   Median :0.8524  
##  Mean   :0.2317   Mean   :0.8530  
##  3rd Qu.:0.2956   3rd Qu.:0.8580  
##  Max.   :0.4164   Max.   :0.8644
# load the packages
library(mlbench)
# load the dataset
data(iris)
# summarize dataset
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  
##                 
##                 
## 
# calculate the pre-process parameters from the dataset
preprocessParams <- preProcess(iris, method=c("center", "scale", "pca"))
# summarize transform parameters
print(preprocessParams)
## Created from 150 samples and 5 variables
## 
## Pre-processing:
##   - centered (4)
##   - ignored (1)
##   - principal component signal extraction (4)
##   - scaled (4)
## 
## PCA needed 2 components to capture 95 percent of the variance
# transform the dataset using the parameters
transformed <- predict(preprocessParams, iris)
# summarize the transformed dataset
summary(transformed)
##        Species        PC1               PC2          
##  setosa    :50   Min.   :-2.7651   Min.   :-2.67732  
##  versicolor:50   1st Qu.:-2.0957   1st Qu.:-0.59205  
##  virginica :50   Median : 0.4169   Median :-0.01744  
##                  Mean   : 0.0000   Mean   : 0.00000  
##                  3rd Qu.: 1.3385   3rd Qu.: 0.59649  
##                  Max.   : 3.2996   Max.   : 2.64521
# load packages
library(mlbench)
library(caret)
# load the dataset
data(PimaIndiansDiabetes)
# summarize dataset
summary(PimaIndiansDiabetes[,1:8])
##     pregnant         glucose         pressure         triceps     
##  Min.   : 0.000   Min.   :  0.0   Min.   :  0.00   Min.   : 0.00  
##  1st Qu.: 1.000   1st Qu.: 99.0   1st Qu.: 62.00   1st Qu.: 0.00  
##  Median : 3.000   Median :117.0   Median : 72.00   Median :23.00  
##  Mean   : 3.845   Mean   :120.9   Mean   : 69.11   Mean   :20.54  
##  3rd Qu.: 6.000   3rd Qu.:140.2   3rd Qu.: 80.00   3rd Qu.:32.00  
##  Max.   :17.000   Max.   :199.0   Max.   :122.00   Max.   :99.00  
##     insulin           mass          pedigree           age       
##  Min.   :  0.0   Min.   : 0.00   Min.   :0.0780   Min.   :21.00  
##  1st Qu.:  0.0   1st Qu.:27.30   1st Qu.:0.2437   1st Qu.:24.00  
##  Median : 30.5   Median :32.00   Median :0.3725   Median :29.00  
##  Mean   : 79.8   Mean   :31.99   Mean   :0.4719   Mean   :33.24  
##  3rd Qu.:127.2   3rd Qu.:36.60   3rd Qu.:0.6262   3rd Qu.:41.00  
##  Max.   :846.0   Max.   :67.10   Max.   :2.4200   Max.   :81.00
# calculate the pre-process parameters from the dataset
preprocessParams <- preProcess(PimaIndiansDiabetes[,1:8], method=c("center", "scale", "ica"), n.comp=5)
# summarize transform parameters
print(preprocessParams)
## Created from 768 samples and 8 variables
## 
## Pre-processing:
##   - centered (8)
##   - independent component signal extraction (8)
##   - ignored (0)
##   - scaled (8)
## 
## ICA used 5 components
# transform the dataset using the parameters
transformed <- predict(preprocessParams, PimaIndiansDiabetes[,1:8])
# summarize the transformed dataset
summary(transformed)
##       ICA1                ICA2              ICA3               ICA4        
##  Min.   :-2.573321   Min.   :-5.7206   Min.   :-4.25503   Min.   :-6.0276  
##  1st Qu.:-0.643787   1st Qu.:-0.4903   1st Qu.:-0.56632   1st Qu.:-0.4674  
##  Median : 0.004947   Median : 0.1831   Median :-0.04965   Median : 0.2986  
##  Mean   : 0.000000   Mean   : 0.0000   Mean   : 0.00000   Mean   : 0.0000  
##  3rd Qu.: 0.633449   3rd Qu.: 0.6845   3rd Qu.: 0.48085   3rd Qu.: 0.6953  
##  Max.   : 3.760677   Max.   : 2.1749   Max.   : 4.89640   Max.   : 1.3647  
##       ICA5        
##  Min.   :-2.9688  
##  1st Qu.:-0.7065  
##  Median : 0.2401  
##  Mean   : 0.0000  
##  3rd Qu.: 0.8256  
##  Max.   : 1.8970