Recently Published
Publish Document
Research Paper Revised
Plot prcomp analisis de componentes principales
## signs are random
require(graphics)
## the variances of the variables in the
## USArrests data vary by orders of magnitude, so scaling is appropriate
prcomp(USArrests) # inappropriate
prcomp(USArrests, scale. = TRUE)
prcomp(~ Murder + Assault + Rape, data = USArrests, scale. = TRUE)
plot(prcomp(USArrests))
summary(prcomp(USArrests, scale. = TRUE))
biplot(prcomp(USArrests, scale. = TRUE))
P11
Uji z dan Uji t
Bayu Fachri (52260027)
Plot2 hierarquical clustering
require(graphics)
### Example 1: Violent crime rates by US state
hc <- hclust(dist(USArrests), "ave")
plot(hc)
plot(hc, hang = -1)
## Do the same with centroid clustering and *squared* Euclidean distance,
## cut the tree into ten clusters and reconstruct the upper part of the
## tree from the cluster centers.
hc <- hclust(dist(USArrests)^2, "cen")
memb <- cutree(hc, k = 10)
cent <- matrix(numeric(), 10, 4)
for(k in 1:10)
cent[k,] <- colMeans(USArrests[memb == k, , drop = FALSE])
hc1 <- hclust(dist(cent)^2, method = "cen", members = table(memb))
opar <- par(mfrow = c(1, 2))
plot(hc, labels = FALSE, hang = -1, main = "Original Tree")
plot(hc1, labels = FALSE, hang = -1, main = "Re-start from 10 clusters")
par(opar)
### Example 2: Straight-line distances among 10 US cities
## Compare the results of algorithms "ward.D" and "ward.D2"
mds2 <- -cmdscale(UScitiesD)
plot(mds2, type="n", axes=FALSE, ann=FALSE)
text(mds2, labels=rownames(mds2), xpd = NA)
hcity <- list(
D.o = hclust(UScitiesD, "ward.D"), # "wrong"
D2o = hclust(UScitiesD, "ward.D2"),# similar to next two
D = hclust(1/2* (UScitiesD)^2, "ward.D"), # Ward(1963) ESS based
D2 = hclust(sqrt(1/2)*UScitiesD, "ward.D2")) # sqrt(ESS) based
opar <- par(mfrow = c(2, 2), mgp = c(1.2, .3, 0), mar = .1 + c(3,4,2,1))
plplot(mrg)ot(hcity$D.o, hang=-1, col=2) # different clustering than the others
plot(hcity$D2o, hang=-1) # [1,2]
plot(hcity$D, hang=-1) # [2,1]
plot(hcity$D2, hang=-1) # [2,2] as above, [1,2], just y-axis numbers differ
par(opar)
mrg <- sapply(hcity, `[[`, "merge") # defining cluster tree (apart from height)
ht <- sapply(hcity, `[[`, "height")
stopifnot(exprs = {
any(mrg[,2] != mrg[,1])
mrg[,2] == mrg[,3:4] # all eq., but the first
all.equal(ht[,"D2" ], sqrt(ht[,"D"]))
all.equal(ht[,"D2"], sqrt(1/2)*ht[,"D2o"])
})
plot(ht)
plot(mrg)