Coding An
Coding An
6/4/2021
head(dataku)
## # A tibble: 6 x 9
## ph Hardness Solids Chloramines Sulfate Conductivity Organic_carbon
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 8.32 214. 22018. 8.06 357. 363. 18.4
## 2 9.09 181. 17979. 6.55 310. 398. 11.6
## 3 5.58 188. 28749. 7.54 327. 280. 8.40
## 4 10.2 248. 28750. 7.51 394. 284. 13.8
## 5 8.64 203. 13672. 4.56 303. 475. 12.4
## 6 11.2 227. 25485. 9.08 404. 564. 17.9
## # ... with 2 more variables: Trihalomethanes <dbl>, Turbidity <dbl>
str(dataku)
summary(dataku)
library(corrplot)
crx <-cor(dataku)
crx
library(lattice)
library(ggplot2)
## [1] "Sulfate"
## # A tibble: 2 x 9
## ph Hardness Solids Chloramines Sulfate Conductivity Organic_carbon
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 7.81 100. 12014. 5.21 247. 605. 9.61
## 2 10.2 300. 12652. 6.66 399. 487. 9.35
## # ... with 2 more variables: Trihalomethanes <dbl>, Turbidity <dbl>
## [1] 273.8138
## # A tibble: 1 x 9
## ph Hardness Solids Chloramines Sulfate Conductivity Organic_carbon
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 9.18 274. 24041. 6.90 398. 478. 13.4
## # ... with 2 more variables: Trihalomethanes <dbl>, Turbidity <dbl>
head(dataku)
## # A tibble: 6 x 9
## ph Hardness Solids Chloramines Sulfate Conductivity Organic_carbon
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 8.32 214. 22018. 8.06 357. 363. 18.4
## 2 9.09 181. 17979. 6.55 310. 398. 11.6
## 3 5.58 188. 28749. 7.54 327. 280. 8.40
## 4 10.2 248. 28750. 7.51 394. 284. 13.8
## 5 8.64 203. 13672. 4.56 303. 475. 12.4
## 6 11.2 227. 25485. 9.08 404. 564. 17.9
## # ... with 2 more variables: Trihalomethanes <dbl>, Turbidity <dbl>
str(dataku)
summary(dataku)
library(corrplot)
correlation_matrix <-cor(dataku)
correlation_matrix
## Importance of components:
## Comp.1 Comp.2 Comp.3 Comp.4 Comp.5
## Standard deviation 1.542230 1.2865755 1.1905802 1.0413163 0.9585635
## Proportion of Variance 0.264275 0.1839196 0.1574979 0.1204822 0.1020938
## Cumulative Proportion 0.264275 0.4481946 0.6056925 0.7261747 0.8282684
## Comp.6 Comp.7 Comp.8 Comp.9
## Standard deviation 0.8016643 0.72607634 0.55639248 0.257213679
## Proportion of Variance 0.0714073 0.05857632 0.03439696 0.007350986
## Cumulative Proportion 0.8996757 0.95825206 0.99264901 1.000000000
##
## Loadings:
## Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8
Comp.9
## ph 0.647 0.156 0.688 0.234
0.154
## Hardness 0.425 0.290 -0.351 -0.220 -0.132 0.411 -0.340
0.507
## Solids 0.421 -0.391 0.273 -0.153 -0.383 0.152 0.586
0.233
## Chloramines 0.490 -0.157 0.318 -0.186 0.143 -0.232 0.231 -0.624
0.288
## Sulfate 0.613 0.177
-0.759
## Conductivity -0.168 0.493 0.385 -0.509 0.557
str(pca)
## List of 7
## $ sdev : Named num [1:9] 1.542 1.287 1.191 1.041 0.959 ...
## ..- attr(*, "names")= chr [1:9] "Comp.1" "Comp.2" "Comp.3" "Comp.4" ...
## $ loadings: 'loadings' num [1:9, 1:9] 0.00255 0.42501 0.42149 0.49009
0.61305 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : chr [1:9] "ph" "Hardness" "Solids" "Chloramines" ...
## .. ..$ : chr [1:9] "Comp.1" "Comp.2" "Comp.3" "Comp.4" ...
## $ center : Named num [1:9] 7.11 192.9 23521.59 6.81 331.89 ...
## ..- attr(*, "names")= chr [1:9] "ph" "Hardness" "Solids"
"Chloramines" ...
## $ scale : Named num [1:9] 2 33.66 7650.66 1.54 33.29 ...
## ..- attr(*, "names")= chr [1:9] "ph" "Hardness" "Solids"
"Chloramines" ...
## $ n.obs : int 47
## $ scores : num [1:47, 1:9] 0.835 -0.734 0.182 2.01 -1.584 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr [1:9] "Comp.1" "Comp.2" "Comp.3" "Comp.4" ...
## $ call : language princomp(x = dataku, cor = TRUE, scores = TRUE)
## - attr(*, "class")= chr "princomp"
pca$scores
## Comp.1 Comp.2 Comp.3 Comp.4 Comp.5
Comp.6
## [1,] 0.83537697 1.459207088 0.83328767 -1.87713760 -0.83711976
-1.305154144
## [2,] -0.73368175 0.689620847 -0.01808034 0.79579704 1.74579905
0.490445980
## [3,] 0.18182662 -1.105247619 -2.51539852 0.19553031 0.75440820
-1.008353084
## [4,] 2.00999656 1.962542178 -2.24314592 -0.89884430 -0.85086815
-1.088067028
## [5,] -1.58420090 0.980378893 0.23267778 -0.12477974 0.10601668
1.360302281
## [6,] 2.70559338 2.092424982 1.81588643 0.02274932 -1.12646024
0.337759522
## [7,] 0.17557736 -0.312320922 0.34876183 0.27018560 -0.80356994
-1.569307523
## [8,] -2.83586662 0.620859338 -0.77917218 0.14764064 -0.51445622
-1.110968219
## [9,] 2.38352909 -1.727336230 1.08085334 -0.06576437 -0.69531580
-0.627311827
## [10,] -0.36849969 0.427483989 -1.78728888 1.20670052 -1.23646571
0.554642386
## [11,] -0.59346021 -0.212291202 0.81754537 1.05773370 -1.57146813
-0.906640733
## [12,] 0.09132290 -1.396843646 1.91854363 -0.45624587 1.20399045
-0.123693706
## [13,] 0.36749487 -0.065481054 1.46751397 -1.50514356 0.28595422
-0.485783020
## [14,] 2.59087705 -1.297940064 -0.02359614 1.09552260 1.01670547
-0.236064824
## [15,] -0.46587506 0.060789475 -0.30476304 -2.00903699 1.24615680
1.005791442
## [16,] -0.97062834 0.174380102 2.19051202 -0.38260610 1.22203962
-0.996373415
## [17,] 2.10308155 0.072711157 -1.74759037 0.81812003 0.19072866
0.478483764
## [18,] -0.52505073 -0.667410745 -0.49552160 0.40544275 0.57056958
0.711384894
## [19,] -1.42115764 -1.958825548 -1.00067309 0.03859002 -0.61775485
0.344277344
## [20,] 0.12027186 1.393758836 0.28351674 1.25486261 1.35688146
-0.172615151
## [21,] -0.28411415 -0.631577362 -0.27281403 -1.15078432 1.41504880
-0.002154818
## [22,] 1.85365524 0.261005723 -0.36075398 -1.53613813 0.96962674
0.220267644
## [23,] -0.47052963 3.433083829 1.29680664 -0.75977132 0.28454905
-1.340317411
## [24,] -1.79944338 1.060642312 -1.36271687 -0.80065954 -1.03010688
0.416520003
## [25,] -1.71788086 1.158407696 -0.50490671 -1.15011829 0.58882473
-0.122349319
## [26,] -0.79770261 1.077199079 -0.73337190 0.87377995 0.42354572
0.262447104
## [27,] -2.21143926 -0.917195710 0.48505411 0.69094918 -0.79298055
0.648842623
## [28,] 0.09828236 2.072451971 0.96111006 0.98296578 -0.16833507
0.785450529
## [29,] 0.24562663 1.697046576 0.78294831 1.48858170 0.88643952
-0.033755433
## [30,] 2.40188613 -0.325936173 -0.68538202 0.23641566 0.90726808
1.775685302
## [31,] -0.26398252 -1.573087540 -0.14200601 -2.03623827 -0.53220012
-0.007085688
## [32,] 1.19931822 -1.195168113 -2.07183785 -0.98281135 0.77557452
-0.616557054
## [33,] -0.13107457 0.574634295 -0.99829126 1.40344865 1.81878838
0.510330924
## [34,] 2.83938809 -1.366925461 2.02485127 -0.20345891 -1.57399949
1.276730397
## [35,] 0.08126472 0.627190918 0.35371310 1.55735205 -0.47542742
-0.161136502
## [36,] -3.22787465 0.316428527 -0.59562096 -1.21998698 -0.56780720
1.150236429
## [37,] -0.85433398 -2.493674687 0.88696516 1.33505375 0.67114624
-0.335475903
## [38,] -1.26034081 -0.975593451 2.70009760 0.38769758 0.52438028
0.274293235
## [39,] -1.53899514 -1.905853598 -0.99861024 -1.02670277 0.41253834
-1.158650427
## [40,] 0.39939077 -1.859421174 -0.06167952 1.64279259 -0.23804273
-1.078003866
## [41,] -0.82101562 0.180761939 -0.98688213 1.93770354 -1.19392487
-0.598639565
## [42,] 2.46303992 -0.740992341 -0.59259982 -0.56026464 -0.15587938
-0.117490689
## [43,] -1.18791755 0.479241076 0.13455350 -0.77238019 -0.80321638
1.276026909
## [44,] 2.10655163 -0.009092158 -1.15993082 -0.26483347 -1.96724509
0.807079867
## [45,] -2.59584035 -1.740273092 1.13325879 -0.12920269 -1.48485401
0.407608573
## [46,] 1.47449313 0.030589662 0.78867771 -0.60809855 0.08580463
0.408030798
## [47,] -0.06693902 1.575647403 -0.09450085 0.67539236 -0.22528723
-0.300688601
## Comp.7 Comp.8 Comp.9
## [1,] -0.49955243 0.072140378 -0.019598764
## [2,] 0.58678736 -0.130597329 0.330011004
## [3,] 0.72491435 -0.787045119 0.167140746
## [4,] 1.09390449 0.035743435 -0.128529295
## [5,] -0.17798408 0.557459615 0.212975591
## [6,] 0.71817481 -0.079304148 -0.259829623
## [7,] 0.45018884 0.168640767 0.126731333
## [8,] -0.44214871 0.558632331 -0.184278655
## [9,] 0.75496100 0.357838034 0.131330785
## [10,] 0.12432910 0.313567879 -0.115332232
## [11,] -0.41742979 0.578682768 0.165801940
## [12,] 0.21951394 -1.628166963 -0.410533452
## [13,] -0.67528439 -0.216041412 0.482449830
## [14,] -1.31061800 -0.402150126 -0.454939756
## [15,] -0.11862205 0.594927581 -0.404287250
## [16,] 1.68997292 0.242620053 -0.115214845
## [17,] 0.41685849 0.348282520 -0.298236432
## [18,] -0.65080119 -0.311672057 -0.220335724
## [19,] -0.47529904 -0.095707158 0.365639994
## [20,] -0.26675413 0.702400636 0.390341399
## [21,] -0.45734191 -0.837674867 0.209874344
## [22,] -0.54834102 1.400620864 -0.432390500
## [23,] -1.25896693 -0.348742610 -0.054541361
## [24,] -0.51094083 -0.408336961 -0.297188300
## [25,] 1.49001792 0.455995081 -0.030711736
## [26,] 0.04359672 0.551672905 0.198791866
## [27,] 0.44435773 0.092778607 -0.239340471
## [28,] -0.40908103 -1.105325563 0.131160536
## [29,] 0.61270045 -0.005327059 0.035269600
## [30,] 1.25564031 0.075177539 0.091362881
## [31,] 0.07180436 0.139542919 -0.118371347
## [32,] -0.92503335 0.450769962 0.207726793
## [33,] -1.14623164 0.190523065 0.141388535
## [34,] -0.84064824 0.409048604 0.326761704
## [35,] -0.48296540 0.130075486 -0.603953357
## [36,] -0.05529957 -0.048074643 -0.101786496
## [37,] 0.95675212 0.343096578 -0.114422493
## [38,] -0.33539619 -0.015791436 -0.287573588
## [39,] -0.36930270 -0.502217185 0.112149872
## [40,] -0.79826151 0.399575379 -0.095551065
## [41,] -0.11273598 -0.764219573 0.002449963
## [42,] 1.21616556 -0.679284732 0.116621529
## [43,] 0.06630628 -1.009207155 0.133451816
## [44,] -0.24432055 -0.591491432 -0.050757889
## [45,] 0.72647562 0.601690174 0.123687627
## [46,] -0.72412898 0.432640668 0.468794504
## [47,] 0.59006726 -0.237766299 0.365790438
biplot(pca)
pca <- princomp(dataku, cor = TRUE, scores = TRUE)
summary(pca, loadings = TRUE)
## Importance of components:
## Comp.1 Comp.2 Comp.3 Comp.4 Comp.5
## Standard deviation 1.542230 1.2865755 1.1905802 1.0413163 0.9585635
## Proportion of Variance 0.264275 0.1839196 0.1574979 0.1204822 0.1020938
## Cumulative Proportion 0.264275 0.4481946 0.6056925 0.7261747 0.8282684
## Comp.6 Comp.7 Comp.8 Comp.9
## Standard deviation 0.8016643 0.72607634 0.55639248 0.257213679
## Proportion of Variance 0.0714073 0.05857632 0.03439696 0.007350986
## Cumulative Proportion 0.8996757 0.95825206 0.99264901 1.000000000
##
## Loadings:
## Comp.1 Comp.2 Comp.3 Comp.4 Comp.5 Comp.6 Comp.7 Comp.8
Comp.9
## ph 0.647 0.156 0.688 0.234
0.154
## Hardness 0.425 0.290 -0.351 -0.220 -0.132 0.411 -0.340
0.507
## Solids 0.421 -0.391 0.273 -0.153 -0.383 0.152 0.586
0.233
## Chloramines 0.490 -0.157 0.318 -0.186 0.143 -0.232 0.231 -0.624
0.288
## Sulfate 0.613 0.177
-0.759
## Conductivity -0.168 0.493 0.385 -0.509 0.557
## Organic_carbon 0.504 0.322 0.280 -0.233 -0.500 -0.503
plot(pca)
##
## Call:
## factanal(x = dataku, factors = 3, scores = "regression", rotation =
"varimax")
##
## Uniquenesses:
## ph Hardness Solids Chloramines
Sulfate
## 0.107 0.005 0.429 0.416
0.005
## Conductivity Organic_carbon Trihalomethanes Turbidity
## 0.930 0.798 0.950 0.944
##
## Loadings:
## Factor1 Factor2 Factor3
## ph 0.943
## Hardness 0.464 0.878
## Solids 0.660 -0.320 -0.180
## Chloramines 0.742 -0.183
## Sulfate 0.911 0.200 0.355
## Conductivity 0.113 -0.238
## Organic_carbon 0.445
## Trihalomethanes -0.192
## Turbidity -0.215
##
## Factor1 Factor2 Factor3
## SS loadings 2.091 1.247 1.079
## Proportion Var 0.232 0.139 0.120
## Cumulative Var 0.232 0.371 0.491
##
## Test of the hypothesis that 3 factors are sufficient.
## The chi square statistic is 15 on 12 degrees of freedom.
## The p-value is 0.242
pca.fa1$scores
plot(pca.fa1$scores)
load <- pca.fa1$loadings[,1:2]
plot(load, type="p", pch=20, cex=2, col="red", xlim=c(-1.0, 1.0),
ylim=c(-1.0, 1.4))
text(load, labels=names(dataku), pos=2, cex=0.75)