-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphical_model.R
More file actions
137 lines (109 loc) · 4.78 KB
/
Copy pathGraphical_model.R
File metadata and controls
137 lines (109 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# --- Gaussian undirected graphical model ---
rm(list=ls(all=TRUE))
library(huge)
library(MASS)
library(ggplot2)
library(igraph)
library(patchwork)
set.seed(2026)
n <- 1000
p <- 20
traits <- paste0("T", 1:p)
dt <- matrix(rnorm(n * p), nrow = n, ncol = p)
colnames(dt) <- traits
# latentni faktori za generisanje klastera svojstava
F1 <- rnorm(n)
F2 <- rnorm(n)
F3 <- rnorm(n)
# --- klaster 1 (jaka veza)
dt[, 1:5] <- dt[, 1:5] + F1 * 2.5
# --- klaster 2 (umerena veza)
dt[, 6:10] <- dt[, 6:10] + F2 * 1.5
# klaster 3 (slaba veza)
dt[, 11:15] <- dt[, 11:15] + F3 * 0.8
# --- specifične zavisnosti između klastera ---
dt[, 1] <- dt[, 1] + dt[, 6] * 1.5
dt[, 16] <- dt[, 10] * 2.0 + rnorm(n)
dt[, 17] <- dt[, 3] * -1.8 + rnorm(n)
dt[, 18] <- dt[, 11] * 1.2 + rnorm(n)
# --- standardizacija podataka ---
dt <- scale(dt)
# --- Meinshausen-Buhlmann algoritam i selekcija ---
out.mb <- huge(dt, method = "mb")
# --- izbog optimalnog model pomoću StARS kriterijuma ---
out.select.stars <- huge.select(out.mb, criterion = "stars") # c("ric", "stars")
print(out.select.stars)
# --- vizuelizacija ---
# --- matrica susedstva ---
adj_matrix <- as.matrix(out.select.stars$refit)
rownames(adj_matrix) <- traits
colnames(adj_matrix) <- traits
# --- kreiranje "undirected" grafa ---
net <- graph_from_adjacency_matrix(adj_matrix, mode = "undirected", diag = FALSE)
# --- fiksiranje koordinata cvorova (Fruchterman-Reingold layout) ---
set.seed(2026)
layout_coords <- layout_with_fr(net)
# --- podaci o cvorovima ---
nodes_dt <- data.frame(
name = V(net)$name,
x = layout_coords[, 1],
y = layout_coords[, 2])
# --- broj veza ---
nodes_dt$broj_veza <- degree(net)
# --- prepoznavanje klastera (Louvain algoritam) ---
klaster <- cluster_louvain(net)
nodes_dt$louvain_klaster <- as.factor(membership(klaster))
# podaci o granama
edges <- as_data_frame(net, what = "edges")
edges_dt <- merge(edges, nodes_dt, by.x = "from", by.y = "name")
edges_dt <- merge(edges_dt, nodes_dt, by.x = "to", by.y = "name", suffixes = c("_from", "_to"))
# --- graf 1 ---
ggplot() +
geom_segment(data = edges_dt, aes(x = x_from, y = y_from, xend = x_to, yend = y_to), color = "gray60", linewidth = 0.8) +
geom_point(data = nodes_dt, aes(x = x, y = y), size = 12, color = "gold") +
geom_text(data = nodes_dt, aes(x = x, y = y, label = name), fontface = "bold", size = 4, color = "black") +
labs(title = "Osnovni LASSO model") +
theme_void() +
theme(plot.title = element_text(hjust = 0.5, size = 9))
# --- graf 2 ---
ggplot() +
geom_segment(data = edges_dt, aes(x = x_from, y = y_from, xend = x_to, yend = y_to), color = "gray60", linewidth = 0.8) +
geom_point(data = nodes_dt, aes(x = x, y = y, color = broj_veza), size = 12) +
geom_text(data = nodes_dt, aes(x = x, y = y, label = name), fontface = "bold", size = 4, color = "white") +
scale_color_gradient(low = "lightblue", high = "midnightblue") +
labs(title = "Broj veza", color = "Broj veza") +
theme_void() +
theme(plot.title = element_text(hjust = 0.5, size = 9),
legend.title = element_text(size = 9),
legend.text = element_text(size = 8))
# --- graf 3 ---
ggplot() +
geom_segment(data = edges_dt, aes(x = x_from, y = y_from, xend = x_to, yend = y_to), color = "gray60", linewidth = 0.8) +
geom_point(data = nodes_dt, aes(x = x, y = y, color = louvain_klaster), size = 12) +
geom_text(data = nodes_dt, aes(x = x, y = y, label = name), fontface = "bold", size = 4, color = "black") +
scale_color_brewer(palette = "Set1") +
labs(title = "Louvain algoritam", color = "Zajednica") +
theme_void() +
theme(plot.title = element_text(hjust = 0.5, size = 9),
legend.title = element_text(size = 9),
legend.text = element_text(size = 8))
# --- PCA (loadings plot) ---
# --- PCA ---
pca_res <- prcomp(dt, center = TRUE, scale. = TRUE)
# --- koordinata za varijable ---
pca_var <- as.data.frame(pca_res$rotation[, 1:2])
pca_var$name <- rownames(pca_var)
# --- % varijanse ---
var_explained <- pca_res$sdev^2 / sum(pca_res$sdev^2)
pc1_lab <- paste0("PC1 (", round(var_explained[1] * 100, 1), "%)")
pc2_lab <- paste0("PC2 (", round(var_explained[2] * 100, 1), "%)")
# --- graf 4 ---
ggplot(pca_var, aes(x = PC1, y = PC2)) +
geom_hline(yintercept = 0, linetype = "dashed", color = "gray70") +
geom_vline(xintercept = 0, linetype = "dashed", color = "gray70") +
geom_segment(aes(x = 0, y = 0, xend = PC1, yend = PC2),
arrow = arrow(length = unit(0.2, "cm")), color = "gray80", linewidth = 0.4) +
geom_text(aes(label = name), size = 2.5, vjust = -0.5, color = "black") +
labs(title = "PCA (korelacije promenljivih)", x = pc1_lab, y = pc2_lab) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5, size = 9))