0% found this document useful (0 votes)
5 views4 pages

Data Mining Code

The document outlines the process of creating a matrix, calculating a correlation matrix using Spearman's method, and performing hierarchical clustering on the data. It includes steps for plotting dendrograms, reordering them, and visualizing clusters using the mtcars dataset. The document also demonstrates how to extract clustering details and manipulate dendrograms for better visualization.

Uploaded by

sarunspm123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Data Mining Code

The document outlines the process of creating a matrix, calculating a correlation matrix using Spearman's method, and performing hierarchical clustering on the data. It includes steps for plotting dendrograms, reordering them, and visualizing clusters using the mtcars dataset. The document also demonstrates how to extract clustering details and manipulate dendrograms for better visualization.

Uploaded by

sarunspm123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# Create matrix

y <- matrix(rnorm(50), 10, 5,

dimnames = list(paste("g",1:10,sep=""),

paste("t",1:5,sep="")))

# Correlation matrix

c <- cor(t(y), method="spearman")

# Convert to distance

d <- [Link](1-c)

# Hierarchical clustering

hr <- hclust(d, method="complete")

# Plot area

par(mfrow=c(2,2))

# Dendrogram plots

plot(hr, hang=0.1)

plot(hr, hang=-1)

plot([Link](hr),

edgePar=list(col=3,lwd=2),

horiz=TRUE)

# Clustering details

unclass(hr)

str([Link](hr))

hr$labels[hr$order]

# Convert to dendrogram
hrd1 <- [Link](hr)

plot(hrd1)

# Reorder dendrogram

[Link](1)

hrd2 <- reorder(hrd1, sample(1:10))

plot(hrd2)

# Labels

labels(hrd1)

labels(hrd2)

# Example with mtcars dataset

d <- dist([Link](mtcars))

hc <- hclust(d)

plot(hc)

# Draw clusters

[Link](hc, k=3, border="red")

# Generate data

[Link](1)

y <- matrix(rnorm(50),10,5,

dimnames=list(paste("g",1:10,sep=""),

paste("t",1:5,sep="")))

# Correlation

c <- cor(t(y),method="spearman")
# Distance matrix

d <- [Link](1-c)

# Hierarchical clustering

hr <- hclust(d,method="complete")

# Plot layout

par(mfrow=c(2,2))

# Dendrogram plots

plot(hr,hang=0.1)

plot(hr,hang=-1)

plot([Link](hr),

edgePar=list(col=3,lwd=2),

horiz=TRUE)

# Clustering details

unclass(hr)

# Structure

str([Link](hr))

# Order labels

hr$labels[hr$order]

# Convert to dendrogram

hrd1 <- [Link](hr)

plot(hrd1)

# Reorder dendrogram
hrd2 <- reorder(hrd1,sample(1:10))

plot(hrd2)

# Labels

labels(hrd1)

labels(hrd2)

# Example using mtcars dataset

d <- dist([Link](mtcars))

hc <- hclust(d)

plot(hc)

[Link](hc,k=3,border="red")

You might also like