R/place_variances.R

if(FALSE){
df_nodes = structure(list(name = c("speed", "textual", "visual", "x1", "x2",
                        "x3", "x4", "x5", "x6", "x7", "x8", "x9"), shape = c("oval",
                                                                             "oval", "oval", "rect", "rect", "rect", "rect", "rect", "rect",
                                                                             "rect", "rect", "rect"), label = c("speed", "textual", "visual",
                                                                                                                "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9"), y = c(4,
                                                                                                                                                                             4, 6, 4, 4, 4, 2, 2, 2, 2, 2, 2), x = c(10, 4, 12, 12, 14, 16,
                                                                                                                                                                                                                     2, 4, 6, 8, 10, 12), node_xmin = c(9.5, 3.5, 11.5, 11.4, 13.4,
                                                                                                                                                                                                                                                        15.4, 1.4, 3.4, 5.4, 7.4, 9.4, 11.4), node_xmax = c(10.5, 4.5,
                                                                                                                                                                                                                                                                                                            12.5, 12.6, 14.6, 16.6, 2.6, 4.6, 6.6, 8.6, 10.6, 12.6), node_ymin = c(3.5,
                                                                                                                                                                                                                                                                                                                                                                                   3.5, 5.5, 3.6, 3.6, 3.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6), node_ymax = c(4.5,
                                                                                                                                                                                                                                                                                                                                                                                                                                                         4.5, 6.5, 4.4, 4.4, 4.4, 2.4, 2.4, 2.4, 2.4, 2.4, 2.4)), row.names = c(NA,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                -12L), class = "data.frame")


df_edges = structure(list(from = c("visual", "visual", "visual", "textual",
                        "textual", "textual", "speed", "speed", "speed", "x1", "x2",
                        "x3", "x4", "x5", "x6", "x7", "x8", "x9", "visual", "textual",
                        "speed", "visual", "visual", "textual"), to = c("x1", "x2", "x3",
                                                                        "x4", "x5", "x6", "x7", "x8", "x9", "x1", "x2", "x3", "x4", "x5",
                                                                        "x6", "x7", "x8", "x9", "visual", "textual", "speed", "textual",
                                                                        "speed", "speed"), arrow = c("last", "last", "last", "last",
                                                                                                     "last", "last", "last", "last", "last", "both", "both", "both",
                                                                                                     "both", "both", "both", "both", "both", "both", "both", "both",
                                                                                                     "both", "none", "none", "none"), label = c("1.00", "0.55***",
                                                                                                                                                "0.73***", "1.00", "1.11***", "0.93***", "1.00", "1.18***", "1.08***",
                                                                                                                                                "0.55***", "1.13***", "0.84***", "0.37***", "0.45***", "0.36***",
                                                                                                                                                "0.80***", "0.49***", "0.57***", "0.81***", "0.98***", "0.38***",
                                                                                                                                                "0.41***", "0.26***", "0.17***"), connect_from = c(NA, NA, NA,
                                                                                                                                                                                                   NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                                                                                                                                                                                                   NA, NA, NA, NA, NA), connect_to = c(NA, NA, NA, NA, NA, NA, NA,
                                                                                                                                                                                                                                       NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                                                                                                                                                                                                                                       NA), curvature = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                                                                                                                                                                                                                                                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 60, 60, 60), lhs = c("visual",
                                                                                                                                                                                                                                                                                                                       "visual", "visual", "textual", "textual", "textual", "speed",
                                                                                                                                                                                                                                                                                                                       "speed", "speed", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8",
                                                                                                                                                                                                                                                                                                                       "x9", "visual", "textual", "speed", "visual", "visual", "textual"
                                                                                                                                                                                                                                                          ), op = c("=~", "=~", "=~", "=~", "=~", "=~", "=~", "=~", "=~",
                                                                                                                                                                                                                                                                    "~~", "~~", "~~", "~~", "~~", "~~", "~~", "~~", "~~", "~~", "~~",
                                                                                                                                                                                                                                                                    "~~", "~~", "~~", "~~"), rhs = c("x1", "x2", "x3", "x4", "x5",
                                                                                                                                                                                                                                                                                                     "x6", "x7", "x8", "x9", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
                                                                                                                                                                                                                                                                                                     "x8", "x9", "visual", "textual", "speed", "textual", "speed",
                                                                                                                                                                                                                                                                                                     "speed"), est = c("1.00", "0.55", "0.73", "1.00", "1.11", "0.93",
                                                                                                                                                                                                                                                                                                                       "1.00", "1.18", "1.08", "0.55", "1.13", "0.84", "0.37", "0.45",
                                                                                                                                                                                                                                                                                                                       "0.36", "0.80", "0.49", "0.57", "0.81", "0.98", "0.38", "0.41",
                                                                                                                                                                                                                                                                                                                       "0.26", "0.17"), se = c("0.00", "0.10", "0.11", "0.00", "0.07",
                                                                                                                                                                                                                                                                                                                                               "0.06", "0.00", "0.16", "0.15", "0.11", "0.10", "0.09", "0.05",
                                                                                                                                                                                                                                                                                                                                               "0.06", "0.04", "0.08", "0.07", "0.07", "0.15", "0.11", "0.09",
                                                                                                                                                                                                                                                                                                                                               "0.07", "0.06", "0.05"), pval = c(NA, "0.00", "0.00", NA, "0.00",
                                                                                                                                                                                                                                                                                                                                                                                 "0.00", NA, "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
                                                                                                                                                                                                                                                                                                                                                                                 "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
                                                                                                                                                                                                                                                                                                                                                                                 "0.00", "0.00"), confint = c("[1.00, 1.00]", "[0.36, 0.75]",
                                                                                                                                                                                                                                                                                                                                                                                                              "[0.52, 0.94]", "[1.00, 1.00]", "[0.98, 1.24]", "[0.82, 1.03]",
                                                                                                                                                                                                                                                                                                                                                                                                              "[1.00, 1.00]", "[0.86, 1.50]", "[0.79, 1.38]", "[0.33, 0.77]",
                                                                                                                                                                                                                                                                                                                                                                                                              "[0.93, 1.33]", "[0.67, 1.02]", "[0.28, 0.46]", "[0.33, 0.56]",
                                                                                                                                                                                                                                                                                                                                                                                                              "[0.27, 0.44]", "[0.64, 0.96]", "[0.34, 0.63]", "[0.43, 0.70]",
                                                                                                                                                                                                                                                                                                                                                                                                              "[0.52, 1.09]", "[0.76, 1.20]", "[0.21, 0.55]", "[0.26, 0.55]",
                                                                                                                                                                                                                                                                                                                                                                                                              "[0.15, 0.37]", "[0.08, 0.27]"), est_sig = c("1.00", "0.55***",
                                                                                                                                                                                                                                                                                                                                                                                                                                                           "0.73***", "1.00", "1.11***", "0.93***", "1.00", "1.18***", "1.08***",
                                                                                                                                                                                                                                                                                                                                                                                                                                                           "0.55***", "1.13***", "0.84***", "0.37***", "0.45***", "0.36***",
                                                                                                                                                                                                                                                                                                                                                                                                                                                           "0.80***", "0.49***", "0.57***", "0.81***", "0.98***", "0.38***",
                                                                                                                                                                                                                                                                                                                                                                                                                                                           "0.41***", "0.26***", "0.17***"), est_std = c("0.77", "0.42",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "0.58", "0.85", "0.86", "0.84", "0.57", "0.72", "0.67", "0.40",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "0.82", "0.66", "0.27", "0.27", "0.30", "0.68", "0.48", "0.56",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "1.00", "1.00", "1.00", "0.46", "0.47", "0.28"), se_std = c("0.05",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "0.06", "0.06", "0.02", "0.02", "0.02", "0.05", "0.05", "0.05",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "0.08", "0.05", "0.06", "0.04", "0.04", "0.04", "0.06", "0.07",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     "0.07", "0.00", "0.00", "0.00", "0.06", "0.07", "0.07"), pval_std = c("0.00",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           "0.00", NA, NA, NA, "0.00", "0.00", "0.00"), confint_std = c("[0.66, 0.88]",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "[0.31, 0.54]", "[0.47, 0.69]", "[0.81, 0.90]", "[0.81, 0.90]",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "[0.79, 0.88]", "[0.47, 0.67]", "[0.62, 0.82]", "[0.56, 0.77]",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "[0.24, 0.57]", "[0.72, 0.92]", "[0.54, 0.79]", "[0.20, 0.35]",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "[0.19, 0.34]", "[0.22, 0.37]", "[0.56, 0.79]", "[0.33, 0.62]",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "[0.42, 0.69]", "[1.00, 1.00]", "[1.00, 1.00]", "[1.00, 1.00]",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "[0.33, 0.58]", "[0.33, 0.61]", "[0.15, 0.42]"), est_sig_std = c("0.77***",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "0.42***", "0.58***", "0.85***", "0.86***", "0.84***", "0.57***",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "0.72***", "0.67***", "0.40***", "0.82***", "0.66***", "0.27***",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "0.27***", "0.30***", "0.68***", "0.48***", "0.56***", "1.00",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "1.00", "1.00", "0.46***", "0.47***", "0.28***"), label_results = c("visual.BY.x1",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "visual.BY.x2", "visual.BY.x3", "textual.BY.x4", "textual.BY.x5",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "textual.BY.x6", "speed.BY.x7", "speed.BY.x8", "speed.BY.x9",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "Variances.x1", "Variances.x2", "Variances.x3", "Variances.x4",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "Variances.x5", "Variances.x6", "Variances.x7", "Variances.x8",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "Variances.x9", "Variances.visual", "Variances.textual", "Variances.speed",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             "visual.WITH.textual", "visual.WITH.speed", "textual.WITH.speed"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ), show = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     TRUE, TRUE, TRUE, TRUE)), class = c("tidy_edges", "tidy_results",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         "data.frame"), row.names = c(NA, -24L), which_label = "est_sig")


connect_from = structure(c("bottom", "top", "bottom", "left", "right", "left",
            "bottom", "right", "bottom", "top", "bottom", "left", "bottom",
            "right", "bottom", "top", "bottom", "left", "left", "right",
            "bottom", "top", "right", "left"), dim = c(2L, 12L), dimnames = list(
              NULL, c("1", "2", "3", "4", "5", "6", "7", "8", "9", "22",
                      "23", "24")))
is_variance <- df_edges$from == df_edges$to

graphmid <- c(median(df_nodes$x), median(df_nodes$y))

varvariables <- df_edges$from[which(is_variance)]
vars <- matrix(c(df_nodes$x[match(varvariables, df_nodes$name)], df_nodes$y[match(varvariables, df_nodes$name)]), ncol = 2)

ggplot(as.data.frame(vars), aes(x=V1, y=V2))+geom_point() +
  geom_point(data = as.data.frame(t(graphmid)), color = "red")

tmp <- vars - matrix(rep(graphmid, each = nrow(vars)), ncol = 2)
angls <- (apply(tmp, 1, function(i){do.call(atan2, as.list(i))}) *180/pi) %% 360
# position <- replicate(length(tmp), c("top", "right", "left", "bottom"), simplify = FALSE)
position <- rep("top", length(tmp))
position[angls > 46 & angls < 135] <- "right"
position[angls >= 135 & angls < 226] <- "bottom"
position[angls >= 226 & angls < 315] <- "left"

position <- replicate(length(varvariables), c("top", "right", "left", "bottom"), simplify = FALSE)
position[angls > 46 & angls < 135] <- list(c("right", "bottom", "top", "left"))
position[angls >= 135 & angls < 226] <- list(c("bottom", "left", "right", "top"))
position[angls >= 226 & angls < 315] <- list(c("left", "top", "bottom", "right"))
names(position) <- varvariables
# thisvariable = varvariables[1]
# thisvariable = "visual"
for(thisvariable in varvariables){
  fromthisnode <- df_edges$from[!is_variance] == thisvariable
  if(!any(fromthisnode)){
    position[[thisvariable]] <- position[[thisvariable]][1]
  } else {
    ordr <- sort(table(c(connect_from[1, which(fromthisnode)], c("top", "right", "left", "bottom"))) - 1)
    ordr <- ordr[ordr == min(ordr)]
    position[[thisvariable]] <- names(ordr)[order(match(names(ordr), position[[thisvariable]]))][1]
  }
}

}

Try the tidySEM package in your browser

Any scripts or data that you put into this service are public.

tidySEM documentation built on Oct. 25, 2023, 1:06 a.m.