PCA

865 days ago by bigdata2016

Price <- c(6,7,6,5,7,6,5,6,3,1,2,5,2,3,1,2) Software <- c(5,3,4,7,7,4,7,5,5,3,6,7,4,5,6,3) Aesthetics <- c(3,2,4,1,5,2,2,4,6,7,6,7,5,6,5,7) Brand <- c(4,2,5,3,5,3,1,4,7,5,7,6,6,5,5,7) data <- data.frame(Price, Software, Aesthetics, Brand) 
       
data 
       
   Price Software Aesthetics Brand
1      6        5          3     4
2      7        3          2     2
3      6        4          4     5
4      5        7          1     3
5      7        7          5     5
6      6        4          2     3
7      5        7          2     1
8      6        5          4     4
9      3        5          6     7
10     1        3          7     5
11     2        6          6     7
12     5        7          7     6
13     2        4          5     6
14     3        5          6     5
15     1        6          5     5
16     2        3          7     7
   Price Software Aesthetics Brand
1      6        5          3     4
2      7        3          2     2
3      6        4          4     5
4      5        7          1     3
5      7        7          5     5
6      6        4          2     3
7      5        7          2     1
8      6        5          4     4
9      3        5          6     7
10     1        3          7     5
11     2        6          6     7
12     5        7          7     6
13     2        4          5     6
14     3        5          6     5
15     1        6          5     5
16     2        3          7     7
mydata <- scale(data, center=T, scale=T) mydata 
       
           Price    Software Aesthetics      Brand
 [1,]  0.8485483 -0.04217745      -0.75 -0.3865961
 [2,]  1.3167129 -1.39185589      -1.25 -1.5112392
 [3,]  0.8485483 -0.71701667      -0.25  0.1757255
 [4,]  0.3803837  1.30750098      -1.75 -0.9489176
 [5,]  1.3167129  1.30750098       0.25  0.1757255
 [6,]  0.8485483 -0.71701667      -1.25 -0.9489176
 [7,]  0.3803837  1.30750098      -1.25 -2.0735607
 [8,]  0.8485483 -0.04217745      -0.25 -0.3865961
 [9,] -0.5559454 -0.04217745       0.75  1.3003686
[10,] -1.4922746 -1.39185589       1.25  0.1757255
[11,] -1.0241100  0.63266177       0.75  1.3003686
[12,]  0.3803837  1.30750098       1.25  0.7380470
[13,] -1.0241100 -0.71701667       0.25  0.7380470
[14,] -0.5559454 -0.04217745       0.75  0.1757255
[15,] -1.4922746  0.63266177       0.25  0.1757255
[16,] -1.0241100 -1.39185589       1.25  1.3003686
attr(,"scaled:center")
     Price   Software Aesthetics      Brand 
    4.1875     5.0625     4.5000     4.6875 
attr(,"scaled:scale")
     Price   Software Aesthetics      Brand 
  2.136001   1.481834   2.000000   1.778342 
           Price    Software Aesthetics      Brand
 [1,]  0.8485483 -0.04217745      -0.75 -0.3865961
 [2,]  1.3167129 -1.39185589      -1.25 -1.5112392
 [3,]  0.8485483 -0.71701667      -0.25  0.1757255
 [4,]  0.3803837  1.30750098      -1.75 -0.9489176
 [5,]  1.3167129  1.30750098       0.25  0.1757255
 [6,]  0.8485483 -0.71701667      -1.25 -0.9489176
 [7,]  0.3803837  1.30750098      -1.25 -2.0735607
 [8,]  0.8485483 -0.04217745      -0.25 -0.3865961
 [9,] -0.5559454 -0.04217745       0.75  1.3003686
[10,] -1.4922746 -1.39185589       1.25  0.1757255
[11,] -1.0241100  0.63266177       0.75  1.3003686
[12,]  0.3803837  1.30750098       1.25  0.7380470
[13,] -1.0241100 -0.71701667       0.25  0.7380470
[14,] -0.5559454 -0.04217745       0.75  0.1757255
[15,] -1.4922746  0.63266177       0.25  0.1757255
[16,] -1.0241100 -1.39185589       1.25  1.3003686
attr(,"scaled:center")
     Price   Software Aesthetics      Brand 
    4.1875     5.0625     4.5000     4.6875 
attr(,"scaled:scale")
     Price   Software Aesthetics      Brand 
  2.136001   1.481834   2.000000   1.778342 
mypca <- prcomp(mydata, center=F, scale=F,retx=T) summary(mypca) 
       
Importance of components:
                          PC1    PC2    PC3     PC4
Standard deviation     1.5589 0.9804 0.6817 0.37926
Proportion of Variance 0.6076 0.2403 0.1162 0.03596
Cumulative Proportion  0.6076 0.8479 0.9640 1.00000
Importance of components:
                          PC1    PC2    PC3     PC4
Standard deviation     1.5589 0.9804 0.6817 0.37926
Proportion of Variance 0.6076 0.2403 0.1162 0.03596
Cumulative Proportion  0.6076 0.8479 0.9640 1.00000
mypca$rotation 
       
                  PC1        PC2        PC3         PC4
Price      -0.5229138 0.00807487 -0.8483525  0.08242604
Software   -0.1771390 0.97675554  0.1198660  0.01423081
Aesthetics  0.5965260 0.13369503 -0.2950727  0.73431229
Brand       0.5825287 0.16735905 -0.4229212 -0.67363855
                  PC1        PC2        PC3         PC4
Price      -0.5229138 0.00807487 -0.8483525  0.08242604
Software   -0.1771390 0.97675554  0.1198660  0.01423081
Aesthetics  0.5965260 0.13369503 -0.2950727  0.73431229
Brand       0.5825287 0.16735905 -0.4229212 -0.67363855
mypca$x[,1:3] 
       
             PC1         PC2         PC3
 [1,] -1.1088441 -0.19931676 -0.34011951
 [2,] -2.0679730 -1.76890900 -0.27589687
 [3,] -0.3634723 -0.69751259 -0.80636384
 [4,] -2.0272096  0.88740412  0.75172002
 [5,] -0.6686402  1.35057412 -1.10839793
 [6,] -1.6151352 -1.01942683 -0.03565573
 [7,] -2.3840835  0.76603244  1.07981912
 [8,] -0.8105812 -0.13246925 -0.48765585
 [9,]  1.5030793  0.27221348 -0.30467593
[10,]  1.8749056 -1.17502483  0.65597978
[11,]  1.6283487  0.92758606  0.17338293
[12,]  0.7450737  1.57081802 -0.84695116
[13,]  1.2415980 -0.55167695  0.39695643
[14,]  0.8479424  0.08399428  0.17095950
[15,]  0.9197585  0.66873897  1.19372328
[16,]  2.2852328 -0.98302526 -0.21682424
             PC1         PC2         PC3
 [1,] -1.1088441 -0.19931676 -0.34011951
 [2,] -2.0679730 -1.76890900 -0.27589687
 [3,] -0.3634723 -0.69751259 -0.80636384
 [4,] -2.0272096  0.88740412  0.75172002
 [5,] -0.6686402  1.35057412 -1.10839793
 [6,] -1.6151352 -1.01942683 -0.03565573
 [7,] -2.3840835  0.76603244  1.07981912
 [8,] -0.8105812 -0.13246925 -0.48765585
 [9,]  1.5030793  0.27221348 -0.30467593
[10,]  1.8749056 -1.17502483  0.65597978
[11,]  1.6283487  0.92758606  0.17338293
[12,]  0.7450737  1.57081802 -0.84695116
[13,]  1.2415980 -0.55167695  0.39695643
[14,]  0.8479424  0.08399428  0.17095950
[15,]  0.9197585  0.66873897  1.19372328
[16,]  2.2852328 -0.98302526 -0.21682424
PC1=mypca$x[,1] PC2=mypca$x[,2] myclust <- kmeans(mypca$x[,1:2], centers=4,nstart=10) plot(PC1, PC2, xlab="PC 1", ylab="PC 2", type="p", col=myclust$cluster) dev.off() 
       
null device 
          1 
null device 
          1