Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
heinonmatti opened this issue May 9, 2018 · 6 comments
Closed

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

heinonmatti opened this issue May 9, 2018 · 6 comments

Comments

@heinonmatti
Copy link

heinonmatti commented May 9, 2018

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
Copy link
Author

heinonmatti commented May 9, 2018

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
Copy link

kamione commented Mar 14, 2019

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants