R version 2.9.2 (2009-08-24)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
>
> # Read in data from UCI website
>
> iris = read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data", sep=',', header=F)
> names(iris) = c("sepal.length", "sepal.width", "petal.length", "petal.width", "iris.type")
>
>
>
>
>
> # A histogram with 10 bins
>
> hist(iris$petal.width, breaks=10)
>
>
>
>
> # A histogram with 20 bins
>
> hist(iris$petal.width, breaks=20)
>
>
>
>
> # A histogram with pre-specified bins
>
> print(seq(0,2.5,length=15))
[1] 0.0000000 0.1785714 0.3571429 0.5357143 0.7142857 0.8928571 1.0714286
[8] 1.2500000 1.4285714 1.6071429 1.7857143 1.9642857 2.1428571 2.3214286
[15] 2.5000000
> hist(iris$petal.width, breaks=seq(0,2.5,length=15), col='blue')
>
>
>
>
> # Density plots
>
> density.setosa = density(iris$petal.length[iris$iris.type == 'Iris-setosa'])
> density.versicolor = density(iris$petal.length[iris$iris.type == 'Iris-versicolor'])
> density.virginica = density(iris$petal.length[iris$iris.type == 'Iris-virginica'])
>
> plot(density.setosa, ylab='f(length)', xlab='length (cm)', col='red', main='Density for petal lengths', xlim=c(0,8), lwd=4)
> lines(density.versicolor, col='blue', lwd=4)
> lines(density.virginica, col='green', lwd=4)
> legend(4,2.5, c('Setosa', 'Virginica', 'Versicolor'), lwd=rep(4,3), col=c('red', 'green', 'blue'))
>
>
>
> # Plot of ECDF (Empirical Cumulative Distribution Function)
> # of petal length in three different types of iris
>
> ecdf.setosa = ecdf(iris$petal.length[iris$iris.type == 'Iris-setosa'])
> ecdf.versicolor = ecdf(iris$petal.length[iris$iris.type == 'Iris-versicolor'])
> ecdf.virginica = ecdf(iris$petal.length[iris$iris.type == 'Iris-virginica'])
>
> plot(ecdf.setosa, ylab='F(length)', xlab='length (cm)', col.h='red', main='ECDF for petal lengths', verticals=T, col.v='red', do.p=F, lwd=4, xlim=c(0,8))
> lines(ecdf.versicolor, col.h='blue', col.v='blue', verticals=T, lwd=4, do.p=F)
> lines(ecdf.virginica, col.h='green', col.v='green', verticals=T, lwd=4, do.p=F)
>
>
>
>
>
> # A boxplot for each attribute in the dataset
>
> boxplot(iris[,-5])
>
>
>
>
> # Make separate boxplots of the petal width for each
> # type of iris
>
> boxplot(iris$petal.width ~ iris$iris.type)
>
>
>
>
> colors = rep("red", nrow(iris))
> colors[iris$iris.type == 'Iris-virginica'] = 'blue'
> colors[iris$iris.type == 'Iris-versicolor'] = 'green'
>
> pairs(as.matrix(iris[,-5]), pch=21, bg=c("red", "green", "blue")[unclass(iris$iris.type)])
>
>
>
>
> # Plot of ECDF (Empirical Cumulative Distribution Function)
> # of petal length in three different types of iris
>
> ps = seq(0,1,length=20)
> quantile.setosa = quantile(iris$petal.length[iris$iris.type == 'Iris-setosa'], probs=ps)
> quantile.versicolor = quantile(iris$petal.length[iris$iris.type == 'Iris-versicolor'], probs=ps)
> quantile.virginica = quantile(iris$petal.length[iris$iris.type == 'Iris-virginica'], probs=ps)
>
> plot(ps, quantile.setosa, xlab='p', ylab='length (cm)', col='red', main='Quantiles for petal lengths', type='l', lwd=4, ylim=c(0,8))
> lines(ps, quantile.versicolor, col='blue', lwd=4)
> lines(ps, quantile.virginica, col='green', lwd=4)
>
>
>
>
>
> proc.time()
user system elapsed
0.728 0.052 0.880
R script








