{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r} # define the filename filename <- “iris.csv” # load the CSV file from the local directory dataset <- read.csv(filename, header=FALSE) # preview the first 5 rows head(dataset)
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)
library(mlbench) # load the dataset data(PimaIndiansDiabetes) # display first 20 rows of data head(PimaIndiansDiabetes, n=20)
library(mlbench) # load the dataset data(PimaIndiansDiabetes) # display the dimensions of the dataset dim(PimaIndiansDiabetes)
library(mlbench) # load the dataset data(PimaIndiansDiabetes) # distribution of class variable y <- PimaIndiansDiabetes$diabetes cbind(freq=table(y), percentage=prop.table(table(y))*100)
data(iris) # summarize the dataset summary(iris)
library(mlbench) # load the dataset data(PimaIndiansDiabetes) # calculate standard deviation for all attributes sapply(PimaIndiansDiabetes[,1:8], sd)
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)
library(mlbench) # load the dataset data(PimaIndiansDiabetes) # calculate a correlation matrix for numeric variables correlations <- cor(PimaIndiansDiabetes[,1:8]) # display the correlation matrix print(correlations)
data(iris) # create histograms for each attribute par(mfrow=c(1,4)) for(i in 1:4) { hist(iris[,i], main=names(iris)[i]) }
library(lattice) # load dataset data(iris) # 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]) }
data(iris) # Create separate boxplots for each attribute par(mfrow=c(1,4)) for(i in 1:4) { boxplot(iris[,i], main=names(iris)[i]) }
library(mlbench) # load the dataset data(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) }
library(Amelia) library(mlbench) # load dataset data(Soybean) # create a missing map missmap(Soybean, col=c(“black”, “grey”), legend=FALSE)
library(corrplot) # load the data data(iris) # calculate correlations correlations <- cor(iris[,1:4]) # create correlation plot corrplot(correlations, method=“circle”)
data(iris) # pair-wise scatterplots of all 4 attributes pairs(iris)
data(iris) # pair-wise scatterplots colored by class pairs(Species~., data=iris, col=iris$Species)
library(caret) # 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)
library(caret) # load the iris dataset data(iris) # box and whisker plots for each attribute by class value x <- iris[,1:4] y <- iris[,5] featurePlot(x=x, y=y, plot=“box”)
library(caret) # load the dataset data(iris) # summarize data summary(iris[,1:4]) # calculate the pre-process parameters from the dataset preprocessParams <- preProcess(iris[,1:4], method=c(“scale”)) # summarize transform parameters print(preprocessParams) # transform the dataset using the parameters transformed <- predict(preprocessParams, iris[,1:4]) # summarize the transformed dataset summary(transformed)
library(caret) # load the dataset data(iris) # summarize data summary(iris[,1:4]) # calculate the pre-process parameters from the dataset preprocessParams <- preProcess(iris[,1:4], method=c(“center”, “scale”)) # summarize transform parameters print(preprocessParams) # transform the dataset using the parameters transformed <- predict(preprocessParams, iris[,1:4]) # summarize the transformed dataset summary(transformed)
library(caret) # load the dataset data(iris) # summarize data summary(iris[,1:4]) # calculate the pre-process parameters from the dataset preprocessParams <- preProcess(iris[,1:4], method=c(“range”)) # summarize transform parameters print(preprocessParams) # transform the dataset using the parameters transformed <- predict(preprocessParams, iris[,1:4]) # summarize the transformed dataset summary(transformed)
library(mlbench) library(caret) # load the dataset data(PimaIndiansDiabetes) # summarize pedigree and age summary(PimaIndiansDiabetes[,7:8]) # calculate the pre-process parameters from the dataset preprocessParams <- preProcess(PimaIndiansDiabetes[,7:8], method=c(“BoxCox”)) # summarize transform parameters print(preprocessParams) # transform the dataset using the parameters transformed <- predict(preprocessParams, PimaIndiansDiabetes[,7:8]) # summarize the transformed dataset (note pedigree and age) summary(transformed)
library(mlbench) library(caret) # load the dataset data(PimaIndiansDiabetes) # summarize pedigree and age summary(PimaIndiansDiabetes[,7:8]) # calculate the pre-process parameters from the dataset preprocessParams <- preProcess(PimaIndiansDiabetes[,7:8], method=c(“YeoJohnson”)) # summarize transform parameters print(preprocessParams) # transform the dataset using the parameters transformed <- predict(preprocessParams, PimaIndiansDiabetes[,7:8]) # summarize the transformed dataset (note pedigree and age) summary(transformed)
library(mlbench) # load the dataset data(iris) # summarize dataset summary(iris) # calculate the pre-process parameters from the dataset preprocessParams <- preProcess(iris, method=c(“center”, “scale”, “pca”)) # summarize transform parameters print(preprocessParams) # transform the dataset using the parameters transformed <- predict(preprocessParams, iris) # summarize the transformed dataset summary(transformed)
library(mlbench) library(caret) # load the dataset data(PimaIndiansDiabetes) # summarize dataset summary(PimaIndiansDiabetes[,1:8]) # 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) # transform the dataset using the parameters transformed <- predict(preprocessParams, PimaIndiansDiabetes[,1:8]) # summarize the transformed dataset summary(transformed) ```