Business Intelligence (California State University, Fullerton)
Question 9.1
> #Tran Tran
> #ISYE 6501
> #18 Feb 2025
>
> #Question 9.1
>
> #First we need to read the data in the directory
> data = read.table("/Users/joeytran/Downloads/hw5/uscrime.txt", header = TRUE)
> head(data)
M So Ed Po1 Po2 LF M.F Pop NW U1 U2 Wealth
1 15.1 1 9.1 5.8 5.6 0.510 95.0 33 30.1 0.108 4.1 3940
2 14.3 0 11.3 10.3 9.5 0.583 101.2 13 10.2 0.096 3.6 5570
3 14.2 1 8.9 4.5 4.4 0.533 96.9 18 21.9 0.094 3.3 3180
4 13.6 0 12.1 14.9 14.1 0.577 99.4 157 8.0 0.102 3.9 6730
5 14.1 0 12.1 10.9 10.1 0.591 98.5 18 3.0 0.091 2.0 5780
6 12.1 0 11.0 11.8 11.5 0.547 96.4 25 4.4 0.084 2.9 6890
Ineq Prob Time Crime
1 26.1 0.084602 26.2011 791
2 19.4 0.029599 25.2999 1635
3 25.0 0.083401 24.3006 578
4 16.7 0.015801 29.9012 1969
5 17.4 0.041399 21.2998 1234
6 12.6 0.034201 20.9995 682
>
> #Start running PCA
> pca = prcomp(data[,1:15], scale = TRUE)
> summary(pca)
Importance of components:
PC1 PC2 PC3 PC4 PC5
Standard deviation 2.4534 1.6739 1.4160 1.07806 0.97893
,Proportion of Variance 0.4013 0.1868 0.1337 0.07748 0.06389
Cumulative Proportion 0.4013 0.5880 0.7217 0.79920 0.86308
PC6 PC7 PC8 PC9 PC10
Standard deviation 0.74377 0.56729 0.55444 0.48493 0.44708
Proportion of Variance 0.03688 0.02145 0.02049 0.01568 0.01333
Cumulative Proportion 0.89996 0.92142 0.94191 0.95759 0.97091
PC11 PC12 PC13 PC14 PC15
Standard deviation 0.41915 0.35804 0.26333 0.2418 0.06793
Proportion of Variance 0.01171 0.00855 0.00462 0.0039 0.00031
Cumulative Proportion 0.98263 0.99117 0.99579 0.9997 1.00000
>
> #then we proceed to find the principal components
> pc = pca$x[,1:4]
> attributes(pca$x)
$dim
[1] 47 15
$dimnames
$dimnames[[1]]
NULL
$dimnames[[2]]
[1] "PC1" "PC2" "PC3" "PC4" "PC5" "PC6" "PC7" "PC8"
[9] "PC9" "PC10" "PC11" "PC12" "PC13" "PC14" "PC15"
> pca$x
PC1 PC2 PC3 PC4
[1,] -4.1992835 -1.09383120 -1.11907395 0.67178115
[2,] 1.1726630 0.67701360 -0.05244634 -0.08350709
[3,] -4.1737248 0.27677501 -0.37107658 0.37793995
[4,] 3.8349617 -2.57690596 0.22793998 0.38262331 [5,]
1.8392999 1.33098564 1.27882805 0.71814305
[6,] 2.9072336 -0.33054213 0.53288181 1.22140635
[7,] 0.2457752 -0.07362562 -0.90742064 1.13685873
[8,] -0.1301330 -1.35985577 0.59753132 1.44045387
, [9,] -3.6103169 -0.68621008 1.28372246 0.55171150
[10,] 1.1672376 3.03207033 0.37984502 -0.28887026 [11,]
2.5384879 -2.66771358 1.54424656 -0.87671210
[12,] 1.0065920 -0.06044849 1.18861346 -1.31261964
[13,] 0.5161143 0.97485189 1.83351610 -1.59117618
[14,] 0.4265556 1.85044812 1.02893477 -0.07789173
[15,] -3.3435299 0.05182823 -1.01358113 0.08840211
[16,] -3.0310689 -2.10295524 -1.82993161 0.52347187
[17,] -0.2262961 1.44939774 -1.37565975 0.28960865
[18,] -0.1127499 -0.39407030 -0.38836278 3.97985093
[19,] 2.9195668 -1.58646124 0.97612613 0.78629766
[20,] 2.2998485 -1.73396487 -2.82423222 -0.23281758
[21,] 1.1501667 0.13531015 0.28506743 -2.19770548
[22,] -5.6594827 -1.09730404 0.10043541 -0.05245484
[23,] -0.1011749 -0.57911362 0.71128354 -0.44394773
[24,] 1.3836281 1.95052341 -2.98485490 -0.35942784
[25,] 0.2727756 2.63013778 1.83189535 0.05207518
[26,] 4.0565577 1.17534729 -0.81690756 1.66990720
[27,] 0.8929694 0.79236692 1.26822542 -0.57575615
[28,] 0.1514495 1.44873320 0.10857670 -0.51040146
[29,] 3.5592481 -4.76202163 0.75080576 0.64692974 [30,]
-4.1184576 -0.38073981 1.43463965 0.63330834
[31,] -0.6811731 1.66926027 -2.88645794 -1.30977099
1.7157269 -1.30836339 -0.55971313 -0.70557980
-1.8860627 0.59058174 1.43570145 0.18239089
1.9526349 0.52395429 -0.75642216 0.44289927
1.5888864 -3.12998571 -1.73107199 -1.68604766
1.0709414 -1.65628271 0.79436888 -1.85172698 -
4.1101715 0.15766712 2.36296974 -0.56868399
-0.7254706 2.89263339 -0.36348376 -0.50612576
-3.3451254 -0.95045293 0.19551398 -0.27716645
-1.0644466 -1.05265304 0.82886286 -0.12042931
[41,] 1.4933989 1.86712106 1.81853582 -1.06112429
[42,] -0.6789284 1.83156328 -1.65435992 0.95121379 [43,]
-2.4164258 -0.46701087 1.42808323 0.41149015
[44,] 2.2978729 0.41865689 -0.64422929 -0.63462770