Created
April 9, 2018 15:48
-
-
Save syanle/f1182f098869b81144382c0de6cff574 to your computer and use it in GitHub Desktop.
wy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wdata = read_excel("w.xlsx") | |
fit <- hclust(dist(model$x[,1:3]), method="complete") # 1:3 -> based on 3 components | |
groups <- cutree(fit, k=5) | |
library(rgl) | |
plotPCA <- function(x, nGroup) { | |
n <- ncol(x) | |
if(!(n %in% c(2,3))) { # check if 2d or 3d | |
stop("x must have either 2 or 3 columns") | |
} | |
fit <- hclust(dist(x), method="complete") # cluster | |
groups <- cutree(fit, k=nGroup) | |
if(n == 3) { # 3d plot | |
plot3d(x, col=groups, type="s", size=1, axes=F) | |
axes3d(edges=c("x--", "y--", "z"), lwd=3, axes.len=2, labels=FALSE) | |
grid3d("x") | |
grid3d("y") | |
grid3d("z") | |
} else { # 2d plot | |
maxes <- apply(abs(x), 2, max) | |
rangeX <- c(-maxes[1], maxes[1]) | |
rangeY <- c(-maxes[2], maxes[2]) | |
plot(x, col=groups, pch=19, xlab=colnames(x)[1], ylab=colnames(x)[2], xlim=rangeX, ylim=rangeY) | |
lines(c(0,0), rangeX*2) | |
lines(rangeY*2, c(0,0)) | |
} | |
} | |
plotPCA(model$x[,1:2], 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment