cjvanlissa / tidySEM

54 stars 7 forks source link

Combine fields into an edge label #11

Closed mattansb closed 3 years ago

mattansb commented 3 years ago
library(lavaan)
#> This is lavaan 0.6-7
#> lavaan is BETA software! Please report any bugs.
library(tidySEM)

fit <- sem("mpg ~ cyl\nmpg ~ am", data = mtcars)

set.seed(4)

graph_sem(fit, label = "est_sig_std")


graph_sem(fit, label = "confint_std")

would be nice if this could be done:

graph_sem(fit, label = c("est_sig_std","confint_std"))

or even, this would allow for "est_sig" to be:

graph_sem(fit, label = c("est","sig"))

Created on 2020-10-14 by the reprex package (v0.3.0)

cjvanlissa commented 3 years ago

I don't think this is within scope. The proposed interface introduces an implicit syntax, where label = c("est_sig_std","confint_std") means that the columns are pasted together. It seems more fitting of the tidySEM interface to edit the nodes() and edges() objects directly, for complete freedom over how the labels are rendered.

mattansb commented 3 years ago

By pasting together pieces into a label? That does sound like a good idea - but currently get_edges(fit) only gives the processed label, not the other possible pieces, so it is not possible.

cjvanlissa commented 3 years ago

I'm working on a possible solution, what do you think would be an intuitive way to solve this?

cjvanlissa commented 3 years ago

What I'm doing now is allowing users to pass the arguments of table_results() to get_edges(), so that it is possible to request additional columns. These can then be used in a label, e.g. with tidyr::unite()

mattansb commented 3 years ago

That sounds like a great idea! Yes!

cjvanlissa commented 3 years ago

I've also implemented support for expressions in the label argument, as you requested. This needs to be tested before it can go to CRAN, though, as it involves a major rewrite of several functions.

mattansb commented 3 years ago

I would be happy to test this out in the morning, if you can point me in the right direction ..?

cjvanlissa commented 3 years ago

No, because not all rows in table_results() are edges; some are means or thresholds etc.

cjvanlissa commented 3 years ago

I'm writing some help files for the new get_edges() and get_nodes(), to help you get started!

cjvanlissa commented 3 years ago

Alright - please install dev version 0.1.4, it has new help files with examples. Could you check whether this does approximately what you wanted, and see whether they work in your code?

mattansb commented 3 years ago

This is 🔥🔥🔥🔥🔥🔥

library(lavaan)
#> This is lavaan 0.6-7
#> lavaan is BETA software! Please report any bugs.
library(tidySEM)
#> Registered S3 methods overwritten by 'tidySEM':
#>   method              from           
#>   print.mplus.model   MplusAutomation
#>   print.mplusObject   MplusAutomation
#>   summary.mplus.model MplusAutomation
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

fit <- sem("mpg ~ cyl\nmpg ~ am", data = mtcars)

lay <- get_layout(NA, NA, "am", 
                  "mpg", NA, NA,
                  NA, NA, "cyl", rows = 3)

edg <- get_edges(fit, columns = c("est_std", "est_sig_std", "confint_std", "pval_std"), digits = 2) %>% 
  mutate(label = paste0(est_std, " ", confint_std),
         color = "black",
         color = replace(color, as.numeric(pval_std) < 0.05, "green"))

nod <- get_nodes(fit) %>% 
  mutate(color = "black",
         color = replace(color, name=="mpg", "red"))

graph_data <- prepare_graph(model = fit, layout = lay, edges = edg, nodes = nod)

plot(graph_data)

Created on 2021-02-19 by the reprex package (v1.0.0)

cjvanlissa commented 3 years ago

Thank you for the suggestion and helping me debug @mattansb , version 0.1.6 is on CRAN now :)

mattansb commented 3 years ago

My pleasure! Thanks for a pkg for easy pretty plots!