Open rempsyc opened 1 year ago
Thank you for reporting! I think it was down to the new code for flipping curved edges to avoid crowding, which divided by zero if nodes were exactly aligned. Could you try the update I just pushed?
That works very well, thanks so much!
However, it seems that the sign of curvature
is now ignored in some contexts. Is it a feature or bug? Reprex below:
suppressWarnings(suppressPackageStartupMessages(library(lavaan)))
suppressWarnings(suppressPackageStartupMessages(library(tidySEM)))
suppressWarnings(suppressPackageStartupMessages(library(dplyr)))
packageVersion("tidySEM")
#> [1] '0.2.5.2'
data <- HolzingerSwineford1939
data$visual <- rowMeans(data[paste0("x", 1:3)])
data$textual <- rowMeans(data[paste0("x", 4:6)])
data$speed <- rowMeans(data[paste0("x", 7:9)])
model <- "
textual ~ visual
speed ~ grade
"
fit <- sem(model, data)
layout <- data.frame(
IV = c("grade", "", ""),
M = c("", "visual", ""),
DV = c("textual", "", "speed"))
p <- prepare_graph(fit, layout = layout)
# This works
p$edges <- p$edges %>%
mutate(curvature = replace(.$curvature, from == "grade" & to == "speed", -40))
plot(p)
# Does this flip the curve?
p$edges <- p$edges %>%
mutate(curvature = replace(.$curvature, from == "grade" & to == "speed", 40))
plot(p)
Created on 2023-05-12 with reprex v2.0.2
suppressWarnings(suppressPackageStartupMessages(library(lavaan)))
suppressWarnings(suppressPackageStartupMessages(library(tidySEM)))
suppressWarnings(suppressPackageStartupMessages(library(dplyr)))
packageVersion("tidySEM")
#> [1] '0.2.3'
data <- HolzingerSwineford1939
data$visual <- rowMeans(data[paste0("x", 1:3)])
data$textual <- rowMeans(data[paste0("x", 4:6)])
data$speed <- rowMeans(data[paste0("x", 7:9)])
model <- "
textual ~ visual
speed ~ grade
"
fit <- sem(model, data)
layout <- data.frame(
IV = c("grade", "", ""),
M = c("", "visual", ""),
DV = c("textual", "", "speed"))
p <- prepare_graph(fit, layout = layout)
# This works
p$edges <- p$edges %>%
mutate(curvature = replace(.$curvature, from == "grade" & to == "speed", -40))
plot(p)
# Does this flip the curve?
p$edges <- p$edges %>%
mutate(curvature = replace(.$curvature, from == "grade" & to == "speed", 40))
plot(p)
Created on 2023-05-12 with reprex v2.0.2
It's trying to move the curve away from other objects, but I see how this could overwrite user defaults as you illustrate. I have to think more about what the priority should be.. do you have opinions about it?
Well, let's think about it this way. If I don't like the old default placement, I can still change it to fit my needs. Whereas, if I don't like the new default placement, is seems like I can't do anything about it because my user choice is being overwritten. So I think user choice should take priority.
In the example above, it is clear that the option that does not overlap is better. But that was a simplified example. In the current model I am working with, which is much larger, I prefer having the curves opposite to the new default.
In the middle of the graph, this is how I want the curved lines (old tidySEM version):
Whereas, this is what I am getting with the new version:
All in all, I think I also prefer the old line behaviour in general, even though starting the curved edges from the sides takes a bit more space than from top/bottom, but I suppose that's a bit more subjective.
@rempsyc I think I've fixed this, but would you please have a look?
I need to implement integration tests for the figures :( But how to go about that...
Works!
suppressWarnings(suppressPackageStartupMessages(library(lavaan)))
suppressWarnings(suppressPackageStartupMessages(library(tidySEM)))
suppressWarnings(suppressPackageStartupMessages(library(dplyr)))
packageVersion("tidySEM")
#> [1] '0.2.5.3'
data <- HolzingerSwineford1939
data$visual <- rowMeans(data[paste0("x", 1:3)])
data$textual <- rowMeans(data[paste0("x", 4:6)])
data$speed <- rowMeans(data[paste0("x", 7:9)])
model <- "
textual ~ visual
speed ~ grade
"
fit <- sem(model, data)
layout <- data.frame(
IV = c("grade", "", ""),
M = c("", "visual", ""),
DV = c("textual", "", "speed"))
p <- prepare_graph(fit, layout = layout)
# This works
p$edges <- p$edges %>%
mutate(curvature = replace(.$curvature, from == "grade" & to == "speed", -40))
plot(p)
# Does this flip the curve?
p$edges <- p$edges %>%
mutate(curvature = replace(.$curvature, from == "grade" & to == "speed", 40))
plot(p)
Created on 2023-06-17 with reprex v2.0.2
And:
I need to implement integration tests for the figures :( But how to go about that...
There is, in fact, a way to integrate tests for figures (though I've never used it myself yet):
what happens if you want to snapshot an image?
expect_snapshot_file()
provides an alternative workflow that generates one snapshot per expectation, rather than one file per test. Assuming you’re in test-burger.R then the snapshot created byexpect_snapshot_file(code_that_returns_path_to_file(), "toppings.png")
would be saved intests/testthat/_snaps/burger/toppings.png
. If a future change in the code creates a different file it will be saved intests/testthat/_snaps/burger/toppings.new.png
.Unlike
expect_snapshot()
and friends,expect_snapshot_file()
can’t provide an automatic diff when the test fails. Instead you’ll need to callsnapshot_review()
. This launches a Shiny app that allows you to visually review each change and approve it if it’s deliberate.
From: https://testthat.r-lib.org/articles/snapshotting.html#whole-file-snapshotting
Yes, I've used this once.. the difficult thing is that there are changes we could make to figures that are not breaking but improve functionality, and this still would trigger a breaking test.
Maybe instead I should make a gallery of figures and compare them manually..
At easystats
we rely on snapshots a lot. And we often make minor non-breaking changes and that triggers the snapshots tests, but we simply review them and accept the changes (when things work as expected). It is part of our workflow now. I think it is not much different for image snapshots. And it is very similar to comparing them manually for similarity like you suggest except you only have to review the differences when there has been a chance instead of all the time.
In
tidySEM
’s latest CRAN version (0.2.4), it seems thatggplot2
warnings appeared that were not in the previous version. Additionally, the covariance edges (paths) betweentextual
andspeed
in the reprex below has disappeared.New:
Created on 2023-05-11 with reprex v2.0.2
Old
Created on 2023-05-11 with reprex v2.0.2