Feature selection comparison using simulated data from an erdos-renyi graph structure

This commit is contained in:
Noah L. Schrick 2023-04-13 00:14:00 -05:00
parent c8cac4a638
commit 548b426e84
2 changed files with 129 additions and 148 deletions

View File

@ -14,6 +14,7 @@ source("Schrick-Noah_Ridge-LASSO-Regression.R")
source("Schrick-Noah_Simulated-Data.R") source("Schrick-Noah_Simulated-Data.R")
bundled_data <- create_data() bundled_data <- create_data()
run_comparison <- function(bundled_data){
### LASSO ### LASSO
unpen_beta <- unpen_coeff(bundled_data$train.X, bundled_data$train.y) unpen_beta <- unpen_coeff(bundled_data$train.X, bundled_data$train.y)
lasso.df <- data.frame(att=c("intercept", colnames(bundled_data$train.X)), lasso.df <- data.frame(att=c("intercept", colnames(bundled_data$train.X)),
@ -94,76 +95,13 @@ legend(x="topleft",
lty=c(1,1,1,1,1), lty=c(1,1,1,1,1),
col=c("blue", "red", "green", "bisque4", "purple"), col=c("blue", "red", "green", "bisque4", "purple"),
cex=1) cex=1)
}
run_comparison(bundled_data)
## b. Repeat comparison using a graph with clusters ## b. Repeat comparison using a graph with clusters
if (!require("igraph")) install.packages("igraph") source("Schrick-Noah_graphs.R")
library(igraph) bundled_graph_data <- sim_graph_data()
if (!require("Matrix")) install.packages("Matrix") run_comparison(bundled_graph_data)
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)
### Dataset with g1
dataset.graph <- npdro::createSimulation2(num.samples=num.samples,
num.variables=num.variables,
pct.imbalance=0.5,
pct.signals=0.2,
main.bias=0.5,
interaction.bias=1,
hi.cor=0.95,
lo.cor=0.2,
mix.type="main-interactionScalefree",
label="class",
sim.type="mixed",
pct.mixed=0.5,
pct.train=0.5,
pct.holdout=0.5,
pct.validation=0,
plot.graph=F,
graph.structure = g1,
verbose=T)
train.graph <- dataset.graph$train #150x101
test.graph <- dataset.graph$holdout
validation.graph <- dataset.graph$validation
dataset.graph$signal.names
colnames(train.graph)
# separate the class vector from the predictor data matrix
train.graph.X <- train.graph[, -which(colnames(train.graph) == "class")]
train.graph.y <- train.graph[, "class"]
train.graph.y.01 <- as.numeric(train.graph.y)-1
## c. Use npdro and igraph to create knn ## c. Use npdro and igraph to create knn
my.k <- 3 # larger k, fewer clusters my.k <- 3 # larger k, fewer clusters

43
Schrick-Noah_graphs.R Normal file
View File

@ -0,0 +1,43 @@
source("Schrick-Noah_Simulated-Data.R")
if (!require("igraph")) install.packages("igraph")
library(igraph)
if (!require("Matrix")) install.packages("Matrix")
library(Matrix) # bdiag
sim_graph_data <- function(){
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)
### Dataset with g1
bundled_graph_data <- create_data(graph.structure=g1)
return(bundled_graph_data)
}