Skip to content

Commit

Permalink
multiple bug fixes in mgm (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
vandenman authored Nov 27, 2024
1 parent 2d78470 commit b3c6afc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:

unit-tests:

if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
uses: jasp-stats/jasp-actions/.github/workflows/unittests.yml@master
with:
needs_JAGS: false
Expand Down
36 changes: 28 additions & 8 deletions R/networkanalysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ NetworkAnalysis <- function(jaspResults, dataset, options) {

.networkAnalysisCentralityTable (mainContainer, network, options)
.networkAnalysisClusteringTable (mainContainer, network, options)
.networkAnalysisPlotContainer (mainContainer, network, options)
.networkAnalysisPlotContainer (mainContainer, network, options, dataset)
.networkAnalysisWeightMatrixTable(mainContainer, network, options)

# done last so that all other results are shown already
Expand Down Expand Up @@ -177,12 +177,13 @@ NetworkAnalysis <- function(jaspResults, dataset, options) {
if (is.null(mainContainer)) {
mainContainer <- createJaspContainer(dependencies = c(
# data
"variables", "groupingVariable", "mgmVariableType",
"variables", "groupingVariable",
# what kind of network is estimated
"estimator",
# arguments for the estimator
"correlationMethod", "tuningParameter", "criterion", "isingEstimator",
"nFolds", "split", "rule", "sampleSize", "thresholdBox", "thresholdString", "thresholdValue",
"mgmContinuousVariables", "mgmCategoricalVariables", "mgmCountVariables",
# general arguments
"weightedNetwork", "signedNetwork", "missingValues"
))
Expand All @@ -199,7 +200,9 @@ NetworkAnalysis <- function(jaspResults, dataset, options) {
if (is.null(mainContainer[["generalTable"]])) {
tb <- createJaspTable(gettext("Summary of Network"), position = 1, dependencies = c(
# These are dependencies because specifying them incorrectly is communicated as footnotes on this table
"computedLayoutX", "computedLayoutY", "bootstrap", "bootstrapSamples", "minEdgeStrength"
"computedLayoutX", "computedLayoutY", "bootstrap", "bootstrapSamples", "minEdgeStrength",
# these trigger recomputation in addition to footnotes
"mgmContinuousVariables", "mgmCategoricalVariables", "mgmCountVariables"
))
if (length(dataset) > 1L)
tb$addColumnInfo(name = "info", title = gettext("Network"), type = "string")
Expand Down Expand Up @@ -462,7 +465,7 @@ NetworkAnalysis <- function(jaspResults, dataset, options) {
}

# plots ----
.networkAnalysisPlotContainer <- function(mainContainer, network, options) {
.networkAnalysisPlotContainer <- function(mainContainer, network, options, dataset) {

plotContainer <- mainContainer[["plotContainer"]]
if (is.null(plotContainer)) {
Expand All @@ -471,7 +474,7 @@ NetworkAnalysis <- function(jaspResults, dataset, options) {
mainContainer[["plotContainer"]] <- plotContainer
}

.networkAnalysisNetworkPlot (plotContainer, network, options)
.networkAnalysisNetworkPlot (plotContainer, network, options, dataset = dataset)
.networkAnalysisCentralityPlot(plotContainer, network, options)
.networkAnalysisClusteringPlot(plotContainer, network, options)
}
Expand Down Expand Up @@ -669,7 +672,7 @@ NetworkAnalysis <- function(jaspResults, dataset, options) {
))
}

.networkAnalysisNetworkPlot <- function(plotContainer, network, options, method = "frequentist") {
.networkAnalysisNetworkPlot <- function(plotContainer, network, options, method = "frequentist", dataset = NULL) {

if (!is.null(plotContainer[["networkPlotContainer"]]) || !options[["networkPlot"]])
return()
Expand Down Expand Up @@ -755,7 +758,10 @@ NetworkAnalysis <- function(jaspResults, dataset, options) {
idx[options[["variables"]] %in% options[[nms[[i]]]]] <- i
# idx[i] is 1 if variable[i] %in% mgmContinuousVariables, 2 if in mgmCategoricalVariables, etc.

ll <- lengths(options[c("mgmContinuousVariables", "mgmCategoricalVariables", "mgmCountVariables")])
# order of variables need not match dataset, and thus the order of types may be wrong
newOrder <- match(colnames(dataset[[1L]]), options[["variables"]])
# now we have variables[newOrder] == colnames(dataset[[1L]])
idx <- idx[newOrder]

if (options[["mgmVariableTypeShown"]] == "nodeShape") {
# gaussian, categorical, poisson
Expand Down Expand Up @@ -1002,6 +1008,11 @@ NetworkAnalysis <- function(jaspResults, dataset, options) {
}
}

# order of variables need not match dataset, and thus the order of types may be wrong
newOrder <- match(colnames(dataset[[1L]]), variables)
# now we have variables[newOrder] == colnames(dataset[[1L]])
type <- type[newOrder]

if (any(type == "")) {
message <- gettext("Please drag all variables to a particular type under \"Analysis options\".")
e <- structure(class = c("mgmError", "error", "condition"), list(message = message, call = sys.call(-1)))
Expand Down Expand Up @@ -1104,10 +1115,19 @@ NetworkAnalysis <- function(jaspResults, dataset, options) {
# for every dataset do the analysis
for (nw in seq_along(dataset)) {

data <- dataset[[nw]]

# mgm requires integer instead of factor
if (options[["estimator"]] == "mgm") {
for (i in seq_along(data))
if (!is.numeric(data[[i]]))
data[[i]] <- as.integer(data[[i]])
}

jaspBase::.suppressGrDevice(
msg <- capture.output(
network <- bootnet::estimateNetwork(
data = dataset[[nw]],
data = data,
default = .networkAnalysisJaspToBootnetEstimator(options[["estimator"]]),
.dots = .dots
)
Expand Down
8 changes: 4 additions & 4 deletions inst/qml/NetworkAnalysis.qml
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ Form
source: ["variables"]
}

AssignedVariablesList { name: "mgmContinuousVariables"; title: qsTr("Continuous Variables"); allowedColumns: ["scale"]}
AssignedVariablesList { name: "mgmCategoricalVariables"; title: qsTr("Categorical Variables"); allowedColumns: ["ordinal"]}
AssignedVariablesList { name: "mgmCountVariables"; title: qsTr("Count Variables"); allowedColumns: ["nominal"]}
AssignedVariablesList { name: "mgmContinuousVariables"; title: qsTr("Continuous Variables"); }
AssignedVariablesList { name: "mgmCategoricalVariables"; title: qsTr("Categorical Variables"); }
AssignedVariablesList { name: "mgmCountVariables"; title: qsTr("Count Variables"); }
}
}

Expand Down Expand Up @@ -383,7 +383,7 @@ Form
{
name: "mgmVariableTypeShown";
title: qsTr("Show Variable Type")
visible: [7].includes(estimator.currentIndex)
visible: [6].includes(estimator.currentIndex)
RadioButton { value: "hide"; label: qsTr("Don't show") }
RadioButton { value: "nodeColor"; label: qsTr("Using node color") }
RadioButton { value: "nodeShape"; label: qsTr("Using node shape"); checked: true }
Expand Down

0 comments on commit b3c6afc

Please sign in to comment.