Cluster Analysis: Difference between revisions

From RAJ INFO
Jump to navigation Jump to search
mNo edit summary
 
(6 intermediate revisions by the same user not shown)
Line 6: Line 6:
<pre>
<pre>


> mydata <- read.table ("/home/raj/R/data/data_rudresh.csv", header=T, sep="\t")
> mydata <- na.omit(mydata) # listwise deletion of missing
> mydata <- na.omit(mydata) # listwise deletion of missing
> mydata <- scale(mydata) # standardize variables
> mydata <- scale(mydata) # standardize variables


#  (Opyional) Determine number of clusters
#  (Optional) Determine number of clusters
> wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var)) for (i in 2:15)
> wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var)) for (i in 2:15)
> wss[i] <- sum(kmeans(mydata, centers=i)$withinss)
> wss[i] <- sum(kmeans(mydata, centers=i)$withinss)
Line 19: Line 21:
== Kmeans Clustering ==
== Kmeans Clustering ==
<pre>
<pre>
> fit <- kmeans(mydata, 5) # 5 cluster solution
> fit <- kmeans(mydata, 5) # 5 cluster solution
# get cluster means  
# get cluster means  
> aggregate(mydata,by=list(fit$cluster),FUN=mean)
> aggregate(mydata,by=list(fit$cluster),FUN=mean)
# append cluster assignment
# append cluster assignment
> mydata <- data.frame(mydata, fit$cluster)
> mydata <- data.frame(mydata, fit$cluster)
</pre>
</pre>


== Hierarcheal Clustering ==
== Hierarcheal Clustering ==
<pre>
# Ward Hierarchical Clustering
> d <- dist(mydata, method = "euclidean") # distance matrix
> fit <- hclust(d, method="ward")
> plot(fit) # display dendogram
> groups <- cutree(fit, k=5) # cut tree into 5 clusters
# draw dendogram with red borders around the 5 clusters
> rect.hclust(fit, k=5, border="red")
</pre>
==Ward Hierarchical Clustering with Bootstrapped p values ==
<pre>
> library(pvclust)
> fit <- pvclust(mydata, method.hclust="ward", method.dist="euclidean")
> plot(fit) # dendogram with p values
# add rectangles around groups highly supported by the data
> pvrect(fit, alpha=.95)
</pre>

Latest revision as of 05:23, 28 October 2016


Cluster Analysis

Formatting the Data



> mydata <- read.table ("/home/raj/R/data/data_rudresh.csv", header=T, sep="\t")
> mydata <- na.omit(mydata) # listwise deletion of missing
> mydata <- scale(mydata) # standardize variables

#  (Optional) Determine number of clusters
> wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var)) for (i in 2:15)
> wss[i] <- sum(kmeans(mydata, centers=i)$withinss)
> plot(1:15, wss, type="b", xlab="Number of Clusters", ylab="Within groups sum of squares")


Kmeans Clustering


> fit <- kmeans(mydata, 5) # 5 cluster solution

# get cluster means 
> aggregate(mydata,by=list(fit$cluster),FUN=mean)

# append cluster assignment
> mydata <- data.frame(mydata, fit$cluster)

Hierarcheal Clustering


# Ward Hierarchical Clustering
> d <- dist(mydata, method = "euclidean") # distance matrix
> fit <- hclust(d, method="ward") 
> plot(fit) # display dendogram
> groups <- cutree(fit, k=5) # cut tree into 5 clusters

# draw dendogram with red borders around the 5 clusters 
> rect.hclust(fit, k=5, border="red")

Ward Hierarchical Clustering with Bootstrapped p values



> library(pvclust)
> fit <- pvclust(mydata, method.hclust="ward", method.dist="euclidean")
> plot(fit) # dendogram with p values

# add rectangles around groups highly supported by the data
> pvrect(fit, alpha=.95)