sna_and_clustering
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| sna_and_clustering [2024/11/21 02:47] – created hkimscil | sna_and_clustering [2024/11/24 23:45] (current) – [output] hkimscil | ||
|---|---|---|---|
| Line 65: | Line 65: | ||
| degree(net) | degree(net) | ||
| closeness(net) | closeness(net) | ||
| - | sort(closeness(net)) | + | hits_scores(net)$hub |
| - | hub_score(net) | + | hits_scores(net)$authority |
| + | |||
| + | hs <- hits_scores(net)$hub # outlinks | ||
| + | as <- hits_scores(net)$authority | ||
| - | hits_scores(net) | ||
| - | hs <- hits_scores(net)$hub | ||
| - | as <- hits_scores(net)$authority # inlinks | ||
| - | hs | ||
| - | as | ||
| set.seed(123) | set.seed(123) | ||
| Line 101: | Line 99: | ||
| # cluster_spinglass() | # cluster_spinglass() | ||
| # cluster.distribution() | # cluster.distribution() | ||
| - | # components(net) | + | # components(net, " |
| infomap <- cluster_infomap(net) | infomap <- cluster_infomap(net) | ||
| - | c.infomap <- cluster_infomap(net) | + | # c.infomap <- cluster_infomap(net) |
| plot(cnet, | plot(cnet, | ||
| Line 117: | Line 114: | ||
| | | ||
| - | plot(infomap, | + | ############################################################ |
| - | | + | |
| - | | + | |
| - | | + | |
| - | + | ||
| # clustering in R | # clustering in R | ||
| # clusterdata.csv | # clusterdata.csv | ||
| Line 140: | Line 132: | ||
| means <- apply(z, 2, mean) | means <- apply(z, 2, mean) | ||
| sds <- apply(z, 2, sd) | sds <- apply(z, 2, sd) | ||
| + | means | ||
| + | sds | ||
| + | |||
| nor <- scale(z, center=means, | nor <- scale(z, center=means, | ||
| + | nor | ||
| distance <- dist(nor) | distance <- dist(nor) | ||
| Line 161: | Line 157: | ||
| | | ||
| | | ||
| + | |||
| + | plot(orgs.hclust.average, | ||
| + | | ||
| + | | ||
| + | rect.hclust(orgs.hclust.average, | ||
| member.by.3 <- cutree(orgs.hclust, | member.by.3 <- cutree(orgs.hclust, | ||
| table(member.by.3) | table(member.by.3) | ||
| + | member.by.3 | ||
| member.by.5 <- cutree(orgs.hclust, | member.by.5 <- cutree(orgs.hclust, | ||
| table(member.by.5) | table(member.by.5) | ||
| + | member.by.5 | ||
| + | |||
| aggregate(nor, | aggregate(nor, | ||
| Line 185: | Line 189: | ||
| kc | kc | ||
| - | install.packages(" | + | # install.packages(" |
| library(cluster) | library(cluster) | ||
| Line 199: | Line 203: | ||
| | | ||
| | | ||
| + | |||
| + | # kmeans | ||
| + | # install.packages(" | ||
| + | library(factoextra) | ||
| + | k2 <- kmeans(nor, centers = 3, nstart = 25) | ||
| + | str(k2) | ||
| + | |||
| + | fviz_cluster(k2, | ||
| + | fviz_nbclust(nor, | ||
| + | fviz_nbclust(nor, | ||
| + | |||
| + | gap_stat <- clusGap(nor, | ||
| + | nstart = 25, K.max = 10, B = 50) | ||
| + | fviz_gap_stat(gap_stat) | ||
| + | |||
| + | |||
| </ | </ | ||
| + | ====== output ====== | ||
| + | < | ||
| + | > | ||
| + | > | ||
| + | > # | ||
| + | > library(igraph) | ||
| + | > data <- read.csv(" | ||
| + | > head(data) | ||
| + | first second grade spec | ||
| + | 1 AA | ||
| + | 2 AB | ||
| + | 3 AF | ||
| + | 4 DD | ||
| + | 5 CD | ||
| + | 6 DD | ||
| + | > str(data) | ||
| + | ' | ||
| + | $ first : chr " | ||
| + | $ second: chr " | ||
| + | $ grade : int 6 6 6 6 6 6 6 6 6 6 ... | ||
| + | $ spec : chr " | ||
| + | > | ||
| + | > y <- data.frame(data$first, | ||
| + | > head(y) | ||
| + | data.first data.second | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | 4 | ||
| + | 5 | ||
| + | 6 | ||
| + | > | ||
| + | > # net <- graph.data.frame(y, | ||
| + | > net <- graph_from_data_frame(y, | ||
| + | > head(net) | ||
| + | 6 x 52 sparse Matrix of class " | ||
| + | [[ suppressing 52 column names ‘AA’, ‘AB’, ‘AF’ ... ]] | ||
| + | |||
| + | AA . . . 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . | ||
| + | AB . . . 1 . 4 . . . . . . . . . . . . . . . . . . . . . . . . . | ||
| + | AF . . . . . 3 . . 2 . . . . 1 . . . . . . . . . . . . . . . . . | ||
| + | DD . . . . 2 . . . . . . . . . . . . . . . . . . 2 . . . . . . . | ||
| + | CD . . . 10 . . . 4 . 2 . 2 . . 1 . . . . . . . . . . . . . . . . | ||
| + | BA . 2 5 . . . . . 1 1 . . . . . 1 . . . . . . . . 1 . . . . . . | ||
| + | | ||
| + | AA . . . . . . . . . 1 1 . . . . . . . . . . | ||
| + | AB . . 1 . . . . . . . . . . . . . . . . . . | ||
| + | AF . . . . . . . . . . . . . . . . . . . 1 . | ||
| + | DD . . . . 4 . . . . . . . . . . . . . . . . | ||
| + | CD . . . . . 3 1 . . . . . . . . 1 . 1 . . . | ||
| + | BA . . . . . . . . . . . . . . . . . . 1 . 1 | ||
| + | > net | ||
| + | IGRAPH 612e768 DN-- 52 290 -- | ||
| + | + attr: name (v/c) | ||
| + | + edges from 612e768 (vertex names): | ||
| + | [1] AA->DD AB->DD AF->BA DD->DA CD->EC DD->CE CD->FA CD->CC BA->AF | ||
| + | [10] CB->CA CC->CA CD->CA BC->CA DD->DA ED->AD AE->AC AB->BA CD->EC | ||
| + | [19] CA->CC EB->CC BF->CE BB->CD AC->AE CC->FB DC->BB BD->CF DB->DA | ||
| + | [28] DD->DA DB->DD BC->AF CF->DE DF->BF CB->CA BE->CA EA->CA CB->CA | ||
| + | [37] CB->CA CC->CA CD->CA BC->CA BF->CA CE->CA AC->AD BD->BE AE->DF | ||
| + | [46] CB->DF AC->DF AA->DD AA->DD AA->DD CD->DD AA->DD EE->DD CD->DD | ||
| + | [55] DB->AA AA->FC BE->CC EF->FD CF->FE BB->DD CD->DD BA->AB CD->EC | ||
| + | [64] BE->EE CE->CC CD->CC ED->CC BB->CC BE->CE DD->CE AC->CD ED->CD | ||
| + | + ... omitted several edges | ||
| + | > | ||
| + | > V(net) # vertex in net data | ||
| + | + 52/52 vertices, named, from 612e768: | ||
| + | [1] AA AB AF DD CD BA CB CC BC ED AE CA EB BF BB AC DC BD DB CF DF | ||
| + | [22] BE EA CE EE EF FF FD GB GC GD AD KA KF LC DA EC FA FB DE FC FE | ||
| + | [43] GA GE KB KC KD KE LB LA LD LE | ||
| + | > who.net <- V(net) # 52/52 vertices | ||
| + | > data.frame(who.net) | ||
| + | | ||
| + | AA 1 | ||
| + | AB 2 | ||
| + | AF 3 | ||
| + | DD 4 | ||
| + | CD 5 | ||
| + | BA 6 | ||
| + | CB 7 | ||
| + | CC 8 | ||
| + | BC 9 | ||
| + | ED 10 | ||
| + | AE 11 | ||
| + | CA 12 | ||
| + | EB 13 | ||
| + | BF 14 | ||
| + | BB 15 | ||
| + | AC 16 | ||
| + | DC 17 | ||
| + | BD 18 | ||
| + | DB 19 | ||
| + | CF 20 | ||
| + | DF 21 | ||
| + | BE 22 | ||
| + | EA 23 | ||
| + | CE 24 | ||
| + | EE 25 | ||
| + | EF 26 | ||
| + | FF 27 | ||
| + | FD 28 | ||
| + | GB 29 | ||
| + | GC 30 | ||
| + | GD 31 | ||
| + | AD 32 | ||
| + | KA 33 | ||
| + | KF 34 | ||
| + | LC 35 | ||
| + | DA 36 | ||
| + | EC 37 | ||
| + | FA 38 | ||
| + | FB 39 | ||
| + | DE 40 | ||
| + | FC 41 | ||
| + | FE 42 | ||
| + | GA 43 | ||
| + | GE 44 | ||
| + | KB 45 | ||
| + | KC 46 | ||
| + | KD 47 | ||
| + | KE 48 | ||
| + | LB 49 | ||
| + | LA 50 | ||
| + | LD 51 | ||
| + | LE 52 | ||
| + | > | ||
| + | > E(net) # edge info in net data | ||
| + | + 290/290 edges from 612e768 (vertex names): | ||
| + | [1] AA->DD AB->DD AF->BA DD->DA CD->EC DD->CE CD->FA CD->CC BA->AF | ||
| + | [10] CB->CA CC->CA CD->CA BC->CA DD->DA ED->AD AE->AC AB->BA CD->EC | ||
| + | [19] CA->CC EB->CC BF->CE BB->CD AC->AE CC->FB DC->BB BD->CF DB->DA | ||
| + | [28] DD->DA DB->DD BC->AF CF->DE DF->BF CB->CA BE->CA EA->CA CB->CA | ||
| + | [37] CB->CA CC->CA CD->CA BC->CA BF->CA CE->CA AC->AD BD->BE AE->DF | ||
| + | [46] CB->DF AC->DF AA->DD AA->DD AA->DD CD->DD AA->DD EE->DD CD->DD | ||
| + | [55] DB->AA AA->FC BE->CC EF->FD CF->FE BB->DD CD->DD BA->AB CD->EC | ||
| + | [64] BE->EE CE->CC CD->CC ED->CC BB->CC BE->CE DD->CE AC->CD ED->CD | ||
| + | [73] FF->CD AC->CD DD->CD DD->CD AE->GA AE->GA AE->GA AE->GA BA->ED | ||
| + | [82] BE->ED EB->ED CD->ED FD->EF FD->EF CD->BB BF->BB BC->BB BB->CF | ||
| + | + ... omitted several edges | ||
| + | > rel.net <- E(net) | ||
| + | > rel.net # output 290/290 edges | ||
| + | + 290/290 edges from 612e768 (vertex names): | ||
| + | [1] AA->DD AB->DD AF->BA DD->DA CD->EC DD->CE CD->FA CD->CC BA->AF | ||
| + | [10] CB->CA CC->CA CD->CA BC->CA DD->DA ED->AD AE->AC AB->BA CD->EC | ||
| + | [19] CA->CC EB->CC BF->CE BB->CD AC->AE CC->FB DC->BB BD->CF DB->DA | ||
| + | [28] DD->DA DB->DD BC->AF CF->DE DF->BF CB->CA BE->CA EA->CA CB->CA | ||
| + | [37] CB->CA CC->CA CD->CA BC->CA BF->CA CE->CA AC->AD BD->BE AE->DF | ||
| + | [46] CB->DF AC->DF AA->DD AA->DD AA->DD CD->DD AA->DD EE->DD CD->DD | ||
| + | [55] DB->AA AA->FC BE->CC EF->FD CF->FE BB->DD CD->DD BA->AB CD->EC | ||
| + | [64] BE->EE CE->CC CD->CC ED->CC BB->CC BE->CE DD->CE AC->CD ED->CD | ||
| + | [73] FF->CD AC->CD DD->CD DD->CD AE->GA AE->GA AE->GA AE->GA BA->ED | ||
| + | [82] BE->ED EB->ED CD->ED FD->EF FD->EF CD->BB BF->BB BC->BB BB->CF | ||
| + | + ... omitted several edges | ||
| + | > | ||
| + | > # 52 by 290 data set | ||
| + | > | ||
| + | > V(net)$degree <- degree(net) | ||
| + | > degree.net <- data.frame(degree(net)) | ||
| + | > V(net)$degree | ||
| + | [1] 18 9 23 36 40 26 24 50 21 27 15 62 7 12 23 27 2 4 8 12 23 | ||
| + | [22] 20 8 10 6 8 1 8 1 1 1 9 3 3 1 7 3 1 1 2 1 2 | ||
| + | [43] 5 1 1 1 1 1 1 1 1 1 | ||
| + | > | ||
| + | > | ||
| + | > str(who.net) | ||
| + | ' | ||
| + | - attr(*, " | ||
| + | - attr(*, " | ||
| + | - attr(*, " | ||
| + | - attr(*, " | ||
| + | > who.net$name | ||
| + | [1] " | ||
| + | [13] " | ||
| + | [25] " | ||
| + | [37] " | ||
| + | [49] " | ||
| + | > | ||
| + | > hist(V(net)$degree) | ||
| + | > | ||
| + | > set.seed(222) | ||
| + | > plot(net, | ||
| + | + vertex.color = ' | ||
| + | + vertext.size = 2, | ||
| + | + edge.arrow.size = 0.1, | ||
| + | + vertex.label.cex = 0.8) | ||
| + | > | ||
| + | > plot(net, | ||
| + | + vertex.color = rainbow(52), | ||
| + | + vertex.size = V(net)$degree*0.3, | ||
| + | + edge.arrow.size = 0.1, | ||
| + | + layout=layout.fruchterman.reingold) | ||
| + | > # layout layout.fruchterman.reingold | ||
| + | > # layout.graphopt | ||
| + | > plot(net, | ||
| + | + vertex.color = rainbow(52), | ||
| + | + vertex.size = V(net)$degree*0.8, | ||
| + | + edge.arrow.size = 0.1, | ||
| + | + layout=layout.graphopt) | ||
| + | > | ||
| + | > plot(net, | ||
| + | + vertex.color = rainbow(52), | ||
| + | + vertex.size = V(net)$degree*0.4, | ||
| + | + edge.arrow.size = 0.1, | ||
| + | + layout=layout.kamada.kawai) | ||
| + | > | ||
| + | > betweenness(net) | ||
| + | AA | ||
| + | | ||
| + | CB | ||
| + | | ||
| + | EB | ||
| + | 0.000000 | ||
| + | DB | ||
| + | 0.000000 181.814414 | ||
| + | EE | ||
| + | 4.002137 | ||
| + | GD | ||
| + | 0.000000 | ||
| + | EC | ||
| + | 0.000000 | ||
| + | GA | ||
| + | 0.000000 | ||
| + | LB | ||
| + | 0.000000 | ||
| + | > degree(net) | ||
| + | AA AB AF DD CD BA CB CC BC ED AE CA EB BF BB AC DC BD DB CF DF BE | ||
| + | 18 9 23 36 40 26 24 50 21 27 15 62 7 12 23 27 2 4 8 12 23 20 | ||
| + | EA CE EE EF FF FD GB GC GD AD KA KF LC DA EC FA FB DE FC FE GA GE | ||
| + | 8 10 6 8 1 8 1 1 1 9 3 3 1 7 3 1 1 2 1 2 5 1 | ||
| + | KB KC KD KE LB LA LD LE | ||
| + | | ||
| + | > closeness(net) | ||
| + | | ||
| + | 0.005988024 0.007874016 0.008196721 0.007352941 0.010101010 | ||
| + | | ||
| + | 0.009900990 0.010101010 0.008771930 0.008695652 0.012048193 | ||
| + | | ||
| + | 0.010000000 0.008264463 0.009009009 0.009090909 0.010869565 | ||
| + | | ||
| + | 0.011111111 0.008547009 0.008771930 0.005917160 0.009433962 | ||
| + | | ||
| + | 0.010869565 0.009433962 0.006711409 0.006849315 0.007751938 | ||
| + | | ||
| + | 0.005319149 0.007042254 0.005319149 0.006329114 0.004761905 | ||
| + | | ||
| + | 0.006369427 0.009174312 0.008620690 0.005347594 0.006097561 | ||
| + | | ||
| + | NaN | ||
| + | | ||
| + | NaN | ||
| + | | ||
| + | NaN | ||
| + | | ||
| + | NaN | ||
| + | > hits_scores(net)$hub | ||
| + | AA | ||
| + | 8.042807e-02 1.300950e-02 4.731440e-03 1.255496e-02 2.350165e-01 | ||
| + | BA | ||
| + | 3.239013e-02 7.145888e-01 1.000000e+00 2.321828e-01 4.858357e-02 | ||
| + | AE | ||
| + | 7.949708e-02 4.494956e-02 1.870058e-01 1.272314e-01 1.150808e-01 | ||
| + | AC | ||
| + | 2.093518e-01 7.455515e-03 1.305960e-02 1.738176e-02 7.102816e-03 | ||
| + | DF | ||
| + | 2.505787e-02 2.195271e-01 5.693409e-02 6.319085e-02 1.273824e-02 | ||
| + | EF | ||
| + | 1.119098e-03 4.485458e-03 4.563724e-04 3.594258e-04 2.507046e-04 | ||
| + | GD | ||
| + | 6.374463e-03 9.643170e-03 6.276463e-02 4.418478e-04 5.681639e-02 | ||
| + | DA | ||
| + | 1.458925e-19 1.458925e-19 1.458925e-19 1.458925e-19 1.458925e-19 | ||
| + | FC | ||
| + | 1.458925e-19 1.458925e-19 1.458925e-19 1.458925e-19 1.458925e-19 | ||
| + | KC | ||
| + | 1.458925e-19 1.458925e-19 1.458925e-19 1.458925e-19 1.458925e-19 | ||
| + | LD | ||
| + | 1.458925e-19 1.458925e-19 | ||
| + | > hits_scores(net)$authority | ||
| + | AA | ||
| + | 4.412541e-03 3.968841e-03 8.375004e-02 1.409698e-01 7.894656e-02 | ||
| + | BA | ||
| + | 2.146204e-02 3.660444e-03 1.121941e-01 6.326094e-03 4.799095e-02 | ||
| + | AE | ||
| + | 1.524499e-02 1.000000e+00 0.000000e+00 6.072030e-03 8.323026e-02 | ||
| + | AC | ||
| + | 6.262068e-02 0.000000e+00 2.486679e-04 0.000000e+00 8.146610e-03 | ||
| + | DF | ||
| + | 1.925650e-01 1.095155e-01 7.776767e-03 2.437300e-02 2.419075e-02 | ||
| + | EF | ||
| + | 6.390996e-05 0.000000e+00 2.071608e-03 0.000000e+00 0.000000e+00 | ||
| + | GD | ||
| + | 0.000000e+00 1.806051e-02 0.000000e+00 2.156359e-03 0.000000e+00 | ||
| + | DA | ||
| + | 3.583779e-03 2.468362e-02 8.227873e-03 3.500976e-02 4.973358e-04 | ||
| + | FC | ||
| + | 2.815768e-03 3.064436e-03 1.391587e-02 3.500976e-02 3.500976e-02 | ||
| + | KC | ||
| + | 2.486679e-04 8.227873e-03 2.501759e-02 8.227873e-03 1.133971e-03 | ||
| + | LD | ||
| + | 1.656466e-04 1.133971e-03 | ||
| + | > | ||
| + | > hs <- hits_scores(net)$hub | ||
| + | > as <- hits_scores(net)$authority | ||
| + | > | ||
| + | > | ||
| + | > | ||
| + | > set.seed(123) | ||
| + | > plot(net, | ||
| + | + vertex.size=hs*30, | ||
| + | + main = ' | ||
| + | + vertex.color = rainbow(52), | ||
| + | + edge.arrow.size=0.1, | ||
| + | + layout = layout.kamada.kawai) | ||
| + | > | ||
| + | > set.seed(123) | ||
| + | > plot(net, | ||
| + | + vertex.size=as*30, | ||
| + | + main = ' | ||
| + | + vertex.color = rainbow(52), | ||
| + | + edge.arrow.size=0.1, | ||
| + | + layout = layout.kamada.kawai) | ||
| + | > | ||
| + | > | ||
| + | > net <- graph_from_data_frame(y, | ||
| + | > cnet <- cluster_edge_betweenness(net) | ||
| + | > | ||
| + | > # cluster_fast_greedy(net) | ||
| + | > # cluster_fluid_communities(net, | ||
| + | > # cluster_infomap(net) | ||
| + | > # cluster_label_prop() | ||
| + | > # cluster_spinglass() | ||
| + | > # cluster.distribution() | ||
| + | > # components(net, | ||
| + | > | ||
| + | > infomap <- cluster_infomap(net) | ||
| + | > # c.infomap <- cluster_infomap(net) | ||
| + | > | ||
| + | > plot(cnet, | ||
| + | + net, | ||
| + | + vertex.size = 10, | ||
| + | + vertex.label.cex = 0.8) | ||
| + | > | ||
| + | > plot(infomap, | ||
| + | + net, | ||
| + | + vertex.size = 10, | ||
| + | + vertex.label.cex = 0.8) | ||
| + | > | ||
| + | > ############################################################ | ||
| + | > # clustering in R | ||
| + | > # clusterdata.csv | ||
| + | > orgs <- read.csv(" | ||
| + | > str(orgs) | ||
| + | ' | ||
| + | $ Company | ||
| + | $ Fixed_charge: | ||
| + | $ RoR : num 9.2 10.3 15.4 11.2 8.8 13.5 12.2 9.2 13 12.4 ... | ||
| + | $ Cost : int 151 202 113 168 192 111 175 245 168 197 ... | ||
| + | $ Load : num 54.4 57.9 53 56 51.2 60 67.6 57 60.4 53 ... | ||
| + | $ D.Demand | ||
| + | $ Sales : int 9077 5088 9212 6423 3300 11127 7642 13082 8406 6455 ... | ||
| + | $ Nuclear | ||
| + | $ Fuel_Cost | ||
| + | > head(orgs) | ||
| + | | ||
| + | 1 | ||
| + | 2 Boston | ||
| + | 3 | ||
| + | 4 Commonwealth | ||
| + | 5 Con Ed NY | ||
| + | 6 | ||
| + | Fuel_Cost | ||
| + | 1 0.628 | ||
| + | 2 1.555 | ||
| + | 3 1.058 | ||
| + | 4 0.700 | ||
| + | 5 2.044 | ||
| + | 6 1.241 | ||
| + | > | ||
| + | > pairs(orgs[2: | ||
| + | > | ||
| + | > plot(Fuel_Cost ~ Sales, data = orgs) | ||
| + | > with(orgs, text(Fuel_Cost ~ Sales, | ||
| + | + | ||
| + | + | ||
| + | > | ||
| + | > # normalization (standardization) | ||
| + | > z <- orgs[, | ||
| + | > means <- apply(z, 2, mean) | ||
| + | > sds <- apply(z, 2, sd) | ||
| + | > means | ||
| + | Fixed_charge | ||
| + | 1.114091 | ||
| + | | ||
| + | | ||
| + | > sds | ||
| + | Fixed_charge | ||
| + | | ||
| + | | ||
| + | 3549.9840305 | ||
| + | > | ||
| + | > nor <- scale(z, center=means, | ||
| + | > nor | ||
| + | Fixed_charge | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | [10,] | ||
| + | [11,] -1.97327298 -1.44219805 | ||
| + | [12,] | ||
| + | [13,] | ||
| + | [14,] -0.13056613 | ||
| + | [15,] -0.83513051 -1.39763576 -0.101521757 | ||
| + | [16,] | ||
| + | [17,] -1.91907572 -1.93238335 -0.781276132 | ||
| + | [18,] -0.34735517 | ||
| + | [19,] | ||
| + | [20,] | ||
| + | [21,] -0.40155243 -0.95201276 | ||
| + | [22,] -0.23896065 -0.64007666 | ||
| + | | ||
| + | [1,] -0.52622751 | ||
| + | [2,] -0.33381191 -1.07776413 | ||
| + | | ||
| + | [4,] -0.94312798 -0.70170610 | ||
| + | [5,] -0.71864311 -1.58142837 | ||
| + | [6,] -1.74485965 | ||
| + | [7,] -0.33381191 -0.35832428 -0.7146294 | ||
| + | | ||
| + | | ||
| + | [10,] -0.17346558 -0.69269198 | ||
| + | [11,] 1.04516655 | ||
| + | [12,] 0.14722709 -0.77748109 -0.7146294 | ||
| + | [13,] 1.01309729 -0.48874740 | ||
| + | [14,] -0.59036605 | ||
| + | [15,] -1.07140505 -0.68902999 -0.6610322 | ||
| + | [16,] 1.91103676 | ||
| + | [17,] 1.84689822 -0.90142531 -0.2203441 | ||
| + | [18,] -0.17346558 | ||
| + | [19,] -1.71279038 | ||
| + | [20,] 0.08308855 -0.45832473 | ||
| + | [21,] 0.08308855 -0.63776215 -0.7146294 | ||
| + | [22,] 0.85275095 | ||
| + | attr(," | ||
| + | Fixed_charge | ||
| + | 1.114091 | ||
| + | | ||
| + | | ||
| + | attr(," | ||
| + | Fixed_charge | ||
| + | | ||
| + | | ||
| + | 3549.9840305 | ||
| + | > | ||
| + | > distance <- dist(nor) | ||
| + | > distance | ||
| + | 1 2 3 4 5 6 7 | ||
| + | 2 3.096154 | ||
| + | 3 3.679230 4.916465 | ||
| + | 4 2.462149 2.164213 4.107079 | ||
| + | 5 4.123129 3.852850 4.468735 4.127368 | ||
| + | 6 3.606269 4.218804 2.992760 3.201836 4.600183 | ||
| + | 7 3.901898 3.448346 4.217769 3.969367 4.596261 3.352919 | ||
| + | 8 2.737407 3.892524 4.990876 3.692949 5.155516 4.913953 4.364509 | ||
| + | 9 3.253851 3.957125 2.752623 3.753627 4.489900 3.730814 2.796298 | ||
| + | 10 3.099116 2.705330 3.934935 1.491427 4.045276 3.829058 4.506512 | ||
| + | 11 3.491163 4.792640 5.902882 4.864730 6.460986 6.004557 5.995814 | ||
| + | 12 3.223138 2.432568 4.031434 3.498769 3.603863 3.738824 1.660047 | ||
| + | 13 3.959637 3.434878 4.385973 2.577003 4.758059 4.554909 5.010221 | ||
| + | 14 2.113490 4.323825 2.742000 3.230069 4.818803 3.469268 4.914949 | ||
| + | 15 2.593481 2.501195 5.156977 3.190250 4.255251 4.065764 2.930142 | ||
| + | 16 4.033051 4.837051 5.264442 4.967244 5.816715 5.842268 5.042444 | ||
| + | 17 4.396680 3.623588 6.356548 4.893679 5.628591 6.099456 4.577294 | ||
| + | 18 1.877248 2.904409 2.723954 2.651532 4.338150 2.853942 2.949006 | ||
| + | 19 2.410434 4.634878 3.179392 3.464171 5.133791 2.581208 4.515428 | ||
| + | 20 3.174488 2.997481 3.733274 1.816465 4.385852 2.912401 3.541931 | ||
| + | 21 3.453407 2.318451 5.088018 3.884260 3.644137 4.628341 2.675404 | ||
| + | 22 2.509287 2.421916 4.109321 2.578463 3.771757 4.026935 4.000096 | ||
| + | 8 9 | ||
| + | 2 | ||
| + | 3 | ||
| + | 4 | ||
| + | 5 | ||
| + | 6 | ||
| + | 7 | ||
| + | 8 | ||
| + | 9 3.594824 | ||
| + | 10 3.673884 3.572023 | ||
| + | 11 3.462587 5.175240 5.081469 | ||
| + | 12 4.059770 2.735861 3.942171 5.208504 | ||
| + | 13 4.140607 3.658647 1.407032 5.309741 4.496249 | ||
| + | 14 4.335241 3.816443 3.610272 4.315584 4.335484 4.385649 | ||
| + | 15 3.849872 4.113606 4.264133 4.735659 2.328833 5.103646 4.239522 | ||
| + | 16 2.201457 3.627307 4.531420 3.429962 4.617791 4.406173 5.169314 | ||
| + | 17 5.426511 4.901037 5.484537 4.751387 3.497555 5.606577 5.558002 | ||
| + | 18 3.237409 2.428533 3.070750 3.945595 2.451935 3.780942 2.301050 | ||
| + | 19 4.107966 4.109049 4.130120 4.522319 4.414578 5.010864 1.876051 | ||
| + | 20 4.094283 2.948021 2.054393 5.352136 3.430937 2.226493 3.744430 | ||
| + | 21 3.977130 3.742680 4.361961 4.883977 1.384124 4.937119 4.926966 | ||
| + | 22 3.239374 3.208932 2.559945 3.436927 2.995066 2.739910 3.512207 | ||
| + | | ||
| + | 2 | ||
| + | 3 | ||
| + | 4 | ||
| + | 5 | ||
| + | 6 | ||
| + | 7 | ||
| + | 8 | ||
| + | 9 | ||
| + | 10 | ||
| + | 11 | ||
| + | 12 | ||
| + | 13 | ||
| + | 14 | ||
| + | 15 | ||
| + | 16 5.175157 | ||
| + | 17 3.399659 5.559320 | ||
| + | 18 2.998784 3.973815 4.426129 | ||
| + | 19 4.030721 5.232256 6.089597 2.473696 | ||
| + | 20 3.782111 4.823711 4.866540 2.922392 3.903723 | ||
| + | 21 2.097150 4.568885 3.095002 3.185250 4.972551 4.145222 | ||
| + | 22 3.352644 3.457129 3.628061 2.548060 3.967618 2.618050 3.012264 | ||
| + | > | ||
| + | > orgs.hclust = hclust(distance) | ||
| + | > | ||
| + | > plot(orgs.hclust) | ||
| + | > plot(orgs.hclust, | ||
| + | + labels = orgs$Company, | ||
| + | + main=' | ||
| + | > plot(orgs.hclust, | ||
| + | + hang=-1, | ||
| + | + labels=orgs$Company, | ||
| + | + main=' | ||
| + | > | ||
| + | > | ||
| + | > orgs.hclust.average< | ||
| + | > plot(orgs.hclust.average, | ||
| + | + labels=orgs$Company, | ||
| + | + main=' | ||
| + | > | ||
| + | > plot(orgs.hclust.average, | ||
| + | + labels=orgs$Company, | ||
| + | + main=' | ||
| + | > rect.hclust(orgs.hclust.average, | ||
| + | > | ||
| + | > member.by.3 <- cutree(orgs.hclust, | ||
| + | > table(member.by.3) | ||
| + | member.by.3 | ||
| + | | ||
| + | 14 5 3 | ||
| + | > member.by.3 | ||
| + | [1] 1 1 1 1 1 1 2 3 1 1 3 2 1 1 2 3 2 1 1 1 2 1 | ||
| + | > | ||
| + | > member.by.5 <- cutree(orgs.hclust, | ||
| + | > table(member.by.5) | ||
| + | member.by.5 | ||
| + | 1 2 3 4 5 | ||
| + | 7 6 1 5 3 | ||
| + | > member.by.5 | ||
| + | [1] 1 2 1 2 3 1 4 5 1 2 5 4 2 1 4 5 4 1 1 2 4 2 | ||
| + | > | ||
| + | > | ||
| + | > aggregate(nor, | ||
| + | Group.1 Fixed_charge | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | D.Demand | ||
| + | 1 -0.2605107 -0.1575387 | ||
| + | 2 0.1343994 -0.6728046 -0.6050529 | ||
| + | 3 0.9917178 | ||
| + | > aggregate(nor, | ||
| + | Group.1 Fixed_charge | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | Sales | ||
| + | 1 0.7392081 1.0933974 0.7974146 | ||
| + | 2 0.2022460 0.2163074 0.4969827 | ||
| + | 3 0.6253048 0.0000000 0.4212819 | ||
| + | > | ||
| + | > wss <- (nrow(nor)-1)*sum(apply(nor, | ||
| + | > for (i in 2:20) | ||
| + | + | ||
| + | > plot(1:20, wss, | ||
| + | + type=" | ||
| + | + xlab=" | ||
| + | + ylab=" | ||
| + | > | ||
| + | > | ||
| + | > # kmeans clustering | ||
| + | > set.seed(123) | ||
| + | > kc< | ||
| + | > kc | ||
| + | K-means clustering with 3 clusters of sizes 7, 5, 10 | ||
| + | |||
| + | Cluster means: | ||
| + | Fixed_charge | ||
| + | 1 -0.23896065 -0.65917479 | ||
| + | 2 | ||
| + | 3 -0.09262805 -0.05185431 | ||
| + | | ||
| + | 1 -0.8604593 -0.2884040 | ||
| + | 2 0.5120458 -0.4466434 -0.3174391 | ||
| + | 3 0.3462986 | ||
| + | |||
| + | Clustering vector: | ||
| + | [1] 3 1 2 3 1 2 1 3 3 3 3 1 3 2 1 3 1 2 2 3 1 3 | ||
| + | |||
| + | Within cluster sum of squares by cluster: | ||
| + | [1] 34.16481 15.15613 57.53424 | ||
| + | | ||
| + | |||
| + | Available components: | ||
| + | |||
| + | [1] " | ||
| + | [5] " | ||
| + | [9] " | ||
| + | > | ||
| + | > # install.packages(" | ||
| + | > library(cluster) | ||
| + | > | ||
| + | > ot<-nor | ||
| + | > datadistshortset <- dist(ot, method = " | ||
| + | > hc1 <- hclust(datadistshortset, | ||
| + | > pamvshortset <- pam(datadistshortset, | ||
| + | > clusplot(pamvshortset, | ||
| + | + labels=2, | ||
| + | + col.clus=" | ||
| + | + col.p=" | ||
| + | + span=FALSE, | ||
| + | + main=" | ||
| + | + cex=1.2) | ||
| + | > | ||
| + | > # kmeans | ||
| + | > # install.packages(" | ||
| + | > library(factoextra) | ||
| + | > k2 <- kmeans(nor, centers = 3, nstart = 25) | ||
| + | > str(k2) | ||
| + | List of 9 | ||
| + | $ cluster | ||
| + | $ centers | ||
| + | ..- attr(*, " | ||
| + | .. ..$ : chr [1:3] " | ||
| + | .. ..$ : chr [1:8] " | ||
| + | $ totss : num 168 | ||
| + | $ withinss | ||
| + | $ tot.withinss: | ||
| + | $ betweenss | ||
| + | $ size : int [1:3] 3 7 12 | ||
| + | $ iter : int 2 | ||
| + | $ ifault | ||
| + | - attr(*, " | ||
| + | > | ||
| + | > fviz_cluster(k2, | ||
| + | > fviz_nbclust(nor, | ||
| + | > fviz_nbclust(nor, | ||
| + | > | ||
| + | > gap_stat <- clusGap(nor, | ||
| + | + | ||
| + | Clustering k = 1,2,..., K.max (= 10): .. done | ||
| + | Bootstrapping, | ||
| + | .................................................. 50 | ||
| + | > fviz_gap_stat(gap_stat) | ||
| + | > | ||
| + | </ | ||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | |||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | ---- | ||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | {{: | ||
| + | {{: | ||
| + | {{: | ||
| + | |||
| + | |||
sna_and_clustering.1732157267.txt.gz · Last modified: by hkimscil
