86 lines
3.3 KiB
R
86 lines
3.3 KiB
R
head(USArrests)
|
||
pairs(USArrests)
|
||
cor.test(USArrests$Murder,USArrests$UrbanPop, method="pearson")
|
||
cor.test(USArrests$Rape,USArrests$UrbanPop, method="pearson")
|
||
(pca_1 <- prcomp(~ Murder + Assault + Rape, data = USArrests, scale = TRUE))
|
||
summary(pca_1)
|
||
head(pca_1$x)
|
||
cat("...")
|
||
pca_1$rotation
|
||
par(mfrow = c(1, 2))
|
||
matplot(pca_1$rotation, type = 'l', lty = 1, lwd = 2,
|
||
xlab = 'zmienne', ylab = '<EFBFBD>adunki', ylim = c(-0.9, 1.05),
|
||
xaxt = "n")
|
||
axis(1, at = 1:3, labels = rownames(pca_1$rotation))
|
||
legend('topleft', legend = c('PC1', 'PC2', 'PC3'), ncol = 3, col = 1:3, lwd = 2)
|
||
text(rep(1, 3), pca_1$rotation[1, ], round(pca_1$rotation[1, ], 2), pos = 4)
|
||
text(rep(2, 3), pca_1$rotation[2, ], round(pca_1$rotation[2, ], 2), pos = 1)
|
||
text(rep(3, 3), pca_1$rotation[3, ], round(pca_1$rotation[3, ], 2), pos = 2)
|
||
matplot(abs(pca_1$rotation), type = 'l', lty = 1, lwd = 2,
|
||
xlab = 'zmienne', ylab = '|<7C>adunki|', ylim = c(0, 1.05),
|
||
xaxt = "n")
|
||
axis(1, at = 1:3, labels = rownames(pca_1$rotation))
|
||
text(rep(1, 3), abs(pca_1$rotation)[1, ], abs(round(pca_1$rotation[1, ], 2)), pos = 4)
|
||
text(rep(2, 3), abs(pca_1$rotation)[2, ], abs(round(pca_1$rotation[2, ], 2)), pos = 1)
|
||
text(rep(3, 3), abs(pca_1$rotation)[3, ], abs(round(pca_1$rotation[3, ], 2)), pos = 2)
|
||
par(mfrow = c(1, 1))
|
||
plot(pca_1)
|
||
# wartości własne = wariancje
|
||
pca_1$sdev^2
|
||
mean(pca_1$sdev^2)
|
||
biplot(pca_1)
|
||
library(ape)
|
||
install.packages("ape")
|
||
library(ape)
|
||
plot(mst(dist(scale(USArrests[, -3]))), x1 = pca$x[, 1], x2 = pca$x[, 2])
|
||
plot(mst(dist(scale(USArrests[, -3]))), x1 = pca_1$x[, 1], x2 = pca_1$x[, 2])
|
||
mtcars_sel <- mtcars[, c(1, 3:7)]
|
||
(pca_2 <- prcomp(mtcars_sel, scale = TRUE))
|
||
summary(pca_2)
|
||
head(pca_2$x)
|
||
cat("...")
|
||
pca_2$rotation
|
||
par(mfrow = c(2, 1))
|
||
matplot(pca_2$rotation, type = 'l', lty = 1, lwd = 2,
|
||
xlab = 'zmienne', ylab = 'ładunki', ylim = c(-0.9, 1.05),
|
||
xaxt = "n")
|
||
axis(1, at = 1:6, labels = rownames(pca_2$rotation))
|
||
legend('topleft', legend = c('PC1', 'PC2', 'PC3', 'PC4', 'PC5', 'PC6'), ncol = 6, col = 1:6, lwd = 2)
|
||
matplot(abs(pca_2$rotation), type = 'l', lty = 1, lwd = 2,
|
||
xlab = 'zmienne', ylab = '|ładunki|', ylim = c(0, 1.05),
|
||
xaxt = "n")
|
||
axis(1, at = 1:6, labels = rownames(pca_2$rotation))
|
||
legend('topleft', legend = c('PC1', 'PC2', 'PC3', 'PC4', 'PC5', 'PC6'), ncol = 6, col = 1:6, lwd = 2)
|
||
par(mfrow = c(1, 1))
|
||
plot(pca_2)
|
||
# trzecie podejście
|
||
# wartości własne = wariancje
|
||
pca_2$sdev^2
|
||
mean(pca_2$sdev^2)
|
||
## 1, tak musi być przy skalowaniu
|
||
# Pomijamy te składowe główne, których wartości własne są mniejsze od średniej.
|
||
# Zatem wybieramy dwie.
|
||
biplot(pca_2)
|
||
plot(mst(dist(mtcars_sel)), x1 = pca_2$x[, 1], x2 = pca_2$x[, 2])
|
||
(pca_3 <- prcomp(mtcars_sel, scale = FALSE, center = FALSE))
|
||
summary(pca_3)
|
||
head(pca_3$x)
|
||
cat("...")
|
||
pca_3$rotation
|
||
par(mfrow = c(2, 1))
|
||
matplot(pca_3$rotation, type = 'l', lty = 1, lwd = 2,
|
||
xlab = 'zmienne', ylab = '<EFBFBD>adunki', ylim = c(-0.9, 1.15),
|
||
xaxt = "n")
|
||
axis(1, at = 1:6, labels = rownames(pca_3$rotation))
|
||
legend('topleft', legend = c('PC1', 'PC2', 'PC3', 'PC4', 'PC5', 'PC6'), ncol = 6, col = 1:6, lwd = 2)
|
||
matplot(abs(pca_3$rotation), type = 'l', lty = 1, lwd = 2,
|
||
xlab = 'zmienne', ylab = '|<7C>adunki|', ylim = c(0, 1.1),
|
||
xaxt = "n")
|
||
axis(1, at = 1:6, labels = rownames(pca_3$rotation))
|
||
legend('topleft', legend = c('PC1', 'PC2', 'PC3', 'PC4', 'PC5', 'PC6'), ncol = 6, col = 1:6, lwd = 2)
|
||
par(mfrow = c(1, 1))
|
||
plot(pca_3)
|
||
#1
|
||
pca_3$sdev^2
|
||
mean(pca_3$sdev^2)
|