Loading data
data(iris)
# ?iris
Taking a look at the data we have
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
Creating a data without the Species' labels
iris2 <- iris[, -5]
A linear model of petal lengh and width
fit <- lm(Petal.Length ~ Petal.Width, data = iris2)
summary(fit)
##
## Call:
## lm(formula = Petal.Length ~ Petal.Width, data = iris2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.3354 -0.3035 -0.0295 0.2578 1.3945
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.0836 0.0730 14.8 <2e-16 ***
## Petal.Width 2.2299 0.0514 43.4 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.478 on 148 degrees of freedom
## Multiple R-squared: 0.927, Adjusted R-squared: 0.927
## F-statistic: 1.88e+03 on 1 and 148 DF, p-value: <2e-16
with(iris2, plot(Petal.Length ~ Petal.Width))
abline(fit, col = "darkblue", lwd = 3, lty = 4)
A scatterplot matrix of the 4 variables in our data
Species <- iris[, 5]
col_Species <- c("#7DB0DD", "#86B875", "#E495A5")
pairs(iris2, lower.panel = NULL, col = col_Species[as.numeric(Species)], pch = 19,
cex = 1.4)
par(xpd = TRUE)
legend(x = 0, y = 0.4, cex = 2, legend = as.character(levels(Species)), fill = col_Species)
par(xpd = NA)
# fix legend:
# https://stat.ethz.ch/pipermail/r-help/2001-September/015374.html
Create a heirarchical clustering of the flowers
hc <- hclust(dist(scale(iris2)), method = "complete")
# Turning the hclust object into a dendrogram object for plotting
dend <- as.dendrogram(hc)
# install.packages('dendextend')
require(dendextend)
col_clust <- c("burlywood4", "black", "darkgrey")
dend <- color_branches(dend, k = 3, col = col_clust, groupLabels = 3:1)
dend <- color_labels(dend, col = col_Species[as.numeric(Species[order.dendrogram(dend)])])
dend <- hang.dendrogram(dend)
Viewing our dendrogram with colors per each cluster (cutting for 3 clusters)
plot(dend)
legend("topright", cex = 1.5, legend = as.character(levels(Species)), fill = col_Species)
library(knitr)
knit2html("output_07.rmd")