SachaEpskamp / qgraph

Developmental version of qgraph
GNU General Public License v2.0
68 stars 21 forks source link

qgraph::flow produces weird edges for an mgm network #13

Closed heinonmatti closed 4 years ago

heinonmatti commented 6 years ago

Ok, so I have this mgm-estimated network:

image

When I plot it with flow instead, I get this: qgraph::flow(BCT_mgm, "PA accelerometer", theme = "colorblind", vsize = 8)

image

For another variable, this: qgraph::flow(BCT_mgm, "PA selfrep", theme = "colorblind", vsize = 8)

image

Any thoughts?

heinonmatti commented 6 years ago

Here's the covariance matrix:

covSacha.zip

and here's what I did with mgm:

bctdf_pa_combinedAgreementBcts <- bctdf_pa_combinedAgreementBcts %>% na.omit()
mgm_pa_variable_types <- c("g", "g", rep("c", 7))
mgm_pa_variable_levels <- c("1", "1", rep("2", 7))

mgm_obj <- mgm::mgm(data = bctdf_pa_combinedAgreementBcts,
  type = mgm_pa_variable_types,
  level = mgm_pa_variable_levels,
  lambdaSel = "CV",
  lambdaFolds = 10,
  pbar = FALSE, 
  binarySign = TRUE)

pred_obj <- predict(object = mgm_obj,
                         data = bctdf_pa_combinedAgreementBcts)

pred_obj$errors

# Take R2 from gaussian, CC from categorical variables 
pie_errors <- c(pred_obj$errors[1, 3],
                pred_obj$errors[2, 3],
                pred_obj$errors[3:nrow(pred_obj$errors), 4])

# PLOT NETWORK 

node_colors <- c(viridis::viridis(4, begin = 0.3, end = 0.8)[1], 
                 viridis::viridis(4, begin = 0.3, end = 0.8)[2],
                 rep(viridis::viridis(4, begin = 0.3, end = 0.8)[3], 7)) 

BCT_mgm <- qgraph::qgraph(mgm_obj$pairwise$wadj, 
            layout = "spring",
            repulsion = 1, # To nudge the network from originally bad visual state
            title = "agreement-measured BCTs and PA",
            edge.color = ifelse(mgm_obj$pairwise$edgecolor == "darkgreen", "blue", mgm_obj$pairwise$edgecolor),
            pie = pie_errors,
            pieColor = viridis::viridis(4, begin = 0.3, end = 0.8)[4],
            color = node_colors,
            labels = names(bctdf_pa_combinedAgreementBcts),
            label.cex = 0.75,
            label.scale = FALSE,
            label.color = c(rep("black", 9)),
            theme = "colorblind")
kamione commented 5 years ago

Same issue happens to me. I am thinking whether it is because the edge color specific in the mgm model does not inherit to the flow function. Any idea?

SachaEpskamp commented 5 years ago

Hi both,

Sorry for the late response. How do I read that cov matrix Matti? And do you possibly have an easier to reproduce example? It is possible indeed that this is due to the edge colors not being supplied. Perhaps try to store the results of flow(...) and then manually change the edge colors in the qgraph object?

Best, Sacha

heinonmatti commented 5 years ago

Apologies for being a bit novice with this. I now tinkered a bit with what little I could simulate, and found a hopefully more reproducible way to come about those grey edges.

If you use 5000 instead of 200 in MASS::mvrnorm(n = 200, mu = mus, Sigma = S), you'll get blue/red edges instead of grey ones, even though the categorical variables have two levels and grey edges don't appear in the "normal" (non-flow) network.

# Make covariance matrix. 
S <- matrix(c(1.0, 0.2, 0.1, 0.35, 0.0,
             0.2, 1.0, 0.0, 0.4, 0.0,
             0.1, 0.0, 1.0, 0.0, 0.4,
             0.35, 0.4, 0.0, 1.0, 0.6,
             0.0, 0.0, 0.4, 0.6, 1.0), ncol = 5)
colnames(S) <- c("Y1", "X1", "X2", "Z1" ,"Z2")
rownames(S) <- colnames(S)

# Make mean vector
mus <- c(1, 2, 3, 4, 5); names(mus) = colnames(S)

# Generate 5347 observations
obs <- MASS::mvrnorm(n = 200, mu = mus, Sigma = S)

obs <- data.frame(obs)
obs$categorical <- ifelse(obs$Y1 > 2, 1, 0)
obs$categorical2 <- ifelse(obs$X1 > 2, 1, 0)

bctdf_pa_combinedAgreementBcts <- obs

mgm_pa_variable_types <- c(rep("g", 5), "c", "c")
mgm_pa_variable_levels <- c(rep("1", 5), "2", "2")

mgm_obj <- mgm::mgm(data = bctdf_pa_combinedAgreementBcts,
                    type = mgm_pa_variable_types,
                    level = mgm_pa_variable_levels,
                    lambdaSel = "CV",
                    lambdaFolds = 10,
                    pbar = FALSE, 
                    binarySign = TRUE)

# PLOT NETWORK 

BCT_mgm <- qgraph::qgraph(mgm_obj$pairwise$wadj, 
                          layout = "spring",
                          repulsion = 1, # To nudge the network from originally bad visual state
                          title = "agreement-measured BCTs and PA",
                          edge.color = ifelse(mgm_obj$pairwise$edgecolor == "darkgreen", "blue", mgm_obj$pairwise$edgecolor),
                          labels = names(bctdf_pa_combinedAgreementBcts),
                          label.cex = 0.75,
                          label.scale = FALSE,
                          label.color = c(rep("black", 9)),
                          theme = "colorblind")

qgraph::flow(BCT_mgm, from = "Y1")
SachaEpskamp commented 5 years ago

Right, I see. Thanks! This may not be trivial for now, I need to open up the function and add that the edge colors are also retained properly. Will put it on the todo list!

SachaEpskamp commented 4 years ago

This is now fixed in the devel version (sorry for the delay).