diff --git a/Schrick-Noah_Homework-6.R b/Schrick-Noah_Homework-6.R index 238382c..21270e8 100644 --- a/Schrick-Noah_Homework-6.R +++ b/Schrick-Noah_Homework-6.R @@ -66,10 +66,62 @@ glm_fcn(train.X, train.y, 0) glm_fcn(train.X, train.y, 1) ## b. Repeat comparison using a graph with clusters +if (!require("igraph")) install.packages("igraph") +library(igraph) +if (!require("Matrix")) install.packages("Matrix") +library(Matrix) # bdiag + +npc <-25 # nodes per cluster +n_clust <- 4 # 4 clusters with 25 nodes each + +# no clusters +g0 <- erdos.renyi.game(npc*n_clust, 0.2) +plot(g0) + +matlist = list() +for (i in 1:n_clust){ + matlist[[i]] = get.adjacency(erdos.renyi.game(npc, 0.2)) +} + +# merge clusters into one matrix +mat_clust <- bdiag(matlist) # create block-diagonal matrix + +## the following two things might not be necessary + +# check for loner nodes, connected to nothing, and join them to something +k <- rowSums(mat_clust) +node_vector <- seq(1,npc*n_clust) +for (i in node_vector){ + if (k[i]==0){ # if k=0, connect to something random + j <- sample(node_vector[-i],1) + mat_clust[i,j] <- 1 + mat_clust[j,i] <- 1 + } +} + +node_colors <- c(rep("red",npc), rep("green",npc), rep("blue",npc), rep("orange",npc)) +g1 <- graph_from_adjacency_matrix(mat_clust, mode="undirected", diag=F) +plot(g1, vertex.color=node_colors) ## c. Use npdro and igraph to create knn +my.k <- 3 # larger k, fewer clusters +npdr.nbpairs.idx <- npdro::nearestNeighbors(t(train.X), + # transpose does dist between predictors + # without transpose does dist between samples + #nbd.method="multisurf", k=0, + nbd.method = "relieff", + nbd.metric="manhattan", + k=my.k) +knn.graph <- graph_from_edgelist(as.matrix(npdr.nbpairs.idx), + directed=F) +knn.graph <- simplify(knn.graph) ### Plot network +plot.igraph(knn.graph,layout=layout_with_fr(knn.graph), + vertex.color="red", + vertex.size=3,vertex.label=NA, + main="Manhattan, knn-graph") + ## d. Add Laplace graph penalty