From c81ec1e128df82f7ec7278c3debc2fec9a8d8726 Mon Sep 17 00:00:00 2001 From: Johnny Date: Wed, 27 Mar 2024 17:38:13 +0100 Subject: [PATCH 1/2] add mahalanobis --- R/generalizedlinearmodel.R | 16 ++++++++++------ inst/qml/common/OutlierComponent.qml | 8 +++++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/R/generalizedlinearmodel.R b/R/generalizedlinearmodel.R index 42227980..3bc01f04 100644 --- a/R/generalizedlinearmodel.R +++ b/R/generalizedlinearmodel.R @@ -908,14 +908,14 @@ GeneralizedLinearModelInternal <- function(jaspResults, dataset = NULL, options, tableOptionsOn <- c(options[["dfbetas"]], options[["dffits"]], options[["covarianceRatio"]], - # options[["cooksDistance"]], - options[["leverage"]]) + options[["leverage"]], + options[["mahalanobis"]]) if (!ready | !options[["residualCasewiseDiagnostic"]]) return() - tableOptions <- c("dfbetas", "dffits", "covarianceRatio", "leverage") + tableOptions <- c("dfbetas", "dffits", "covarianceRatio", "leverage", "mahalanobis") tableOptionsClicked <- tableOptions[tableOptionsOn] tableOptionsClicked <- c("cooksDistance", tableOptionsClicked) @@ -936,7 +936,8 @@ GeneralizedLinearModelInternal <- function(jaspResults, dataset = NULL, options, "dffits" = "DFFITS", "covarianceRatio" = "Covariance Ratio", "cooksDistance" = "Cook's Distance", - "leverage" = "Leverage") + "leverage" = "Leverage", + "mahalanobis" = "Mahalanobis") } if (is.null(model)) { @@ -1003,7 +1004,10 @@ GeneralizedLinearModelInternal <- function(jaspResults, dataset = NULL, options, influenceResData[["dependent"]] <- model.frame(model)[[options$dependent]] influenceResData[["predicted"]] <- model$fitted.values influenceResData[["residual"]] <- model$residual - +# browser() + modelMatrix <- as.data.frame(model.matrix(model)) + modelMatrix <- modelMatrix[colnames(modelMatrix) != "(Intercept)"] + influenceResData[["mahalanobis"]] <- mahalanobis(modelMatrix, center = colMeans(modelMatrix), cov = cov(modelMatrix)) if (options$residualCasewiseDiagnosticType == "cooksDistance") index <- which(abs(influenceResData[["cook.d"]]) > options$residualCasewiseDiagnosticCooksDistanceThreshold) @@ -1017,7 +1021,7 @@ GeneralizedLinearModelInternal <- function(jaspResults, dataset = NULL, options, colnames(influenceResSig) <- colNames[1:length(colInd)] influenceResData <- influenceResData[index, ] - colnames(influenceResData) <- colNames + colnames(influenceResData)[1:length(colInd)] <- colNames[1:length(colInd)] if (length(index) == 0) influenceTable$addFootnote(gettext("No influential cases found.")) diff --git a/inst/qml/common/OutlierComponent.qml b/inst/qml/common/OutlierComponent.qml index 1c666778..560318e8 100644 --- a/inst/qml/common/OutlierComponent.qml +++ b/inst/qml/common/OutlierComponent.qml @@ -56,10 +56,12 @@ Group Group { - CheckBox { name: "dfbetas"; label: qsTr("DFBETAS") } - CheckBox { name: "dffits"; label: qsTr("DFFITS") } + CheckBox { name: "dfbetas"; label: qsTr("DFBETAS") } + CheckBox { name: "dffits"; label: qsTr("DFFITS") } CheckBox { name: "covarianceRatio"; label: qsTr("Cov ratio") } - CheckBox { name: "leverage"; label: qsTr("Leverage") } + CheckBox { name: "leverage"; label: qsTr("Leverage") } + CheckBox { name: "mahalanobis"; label: qsTr("Mahalanobis") } + } } From 58f9858d79300776bb06c6b974c7403932cd324c Mon Sep 17 00:00:00 2001 From: Johnny Date: Thu, 28 Mar 2024 16:49:49 +0100 Subject: [PATCH 2/2] bug cook --- R/generalizedlinearmodel.R | 5 +++-- inst/qml/RegressionLinear.qml | 21 ++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/R/generalizedlinearmodel.R b/R/generalizedlinearmodel.R index 3bc01f04..735cea6b 100644 --- a/R/generalizedlinearmodel.R +++ b/R/generalizedlinearmodel.R @@ -999,6 +999,8 @@ GeneralizedLinearModelInternal <- function(jaspResults, dataset = NULL, options, } influenceResData <- as.data.frame(influenceRes[["infmat"]][, colInd]) + colnames(influenceResData)[1:length(colInd)] <- colNames[1:length(colInd)] + influenceResData[["caseN"]] <- seq.int(nrow(influenceResData)) influenceResData[["stdResidual"]] <- rstandard(model) influenceResData[["dependent"]] <- model.frame(model)[[options$dependent]] @@ -1010,7 +1012,7 @@ GeneralizedLinearModelInternal <- function(jaspResults, dataset = NULL, options, influenceResData[["mahalanobis"]] <- mahalanobis(modelMatrix, center = colMeans(modelMatrix), cov = cov(modelMatrix)) if (options$residualCasewiseDiagnosticType == "cooksDistance") - index <- which(abs(influenceResData[["cook.d"]]) > options$residualCasewiseDiagnosticCooksDistanceThreshold) + index <- which(abs(influenceResData[["cooksDistance"]]) > options$residualCasewiseDiagnosticCooksDistanceThreshold) else if (options$residualCasewiseDiagnosticType == "outliersOutside") index <- which(abs(influenceResData[["stdResidual"]]) > options$residualCasewiseDiagnosticZThreshold) else # all @@ -1021,7 +1023,6 @@ GeneralizedLinearModelInternal <- function(jaspResults, dataset = NULL, options, colnames(influenceResSig) <- colNames[1:length(colInd)] influenceResData <- influenceResData[index, ] - colnames(influenceResData)[1:length(colInd)] <- colNames[1:length(colInd)] if (length(index) == 0) influenceTable$addFootnote(gettext("No influential cases found.")) diff --git a/inst/qml/RegressionLinear.qml b/inst/qml/RegressionLinear.qml index be76ab75..33bc9704 100644 --- a/inst/qml/RegressionLinear.qml +++ b/inst/qml/RegressionLinear.qml @@ -80,6 +80,16 @@ Form title: qsTr("Statistics") columns: 2 + Group + { + title: qsTr("Model Summary") + CheckBox { name: "rSquaredChange"; label: qsTr("R squared change") } + CheckBox { name: "fChange"; label: qsTr("F change") } + CheckBox { name: "modelAICBIC"; label: qsTr("AIC and BIC") } + CheckBox { name: "residualDurbinWatson"; label: qsTr("Durbin-Watson") } + + } + Group { title: qsTr("Coefficients") @@ -115,17 +125,6 @@ Form CheckBox { name: "collinearityStatistic"; label: qsTr("Tolerance and VIF") } CheckBox { name: "vovkSellke"; label: qsTr("Vovk-Sellke maximum p-ratio") } } - - - Group - { - title: qsTr("Model Summary") - CheckBox { name: "rSquaredChange"; label: qsTr("R squared change") } - CheckBox { name: "fChange"; label: qsTr("F change") } - CheckBox { name: "modelAICBIC"; label: qsTr("AIC and BIC") } - CheckBox { name: "residualDurbinWatson"; label: qsTr("Durbin-Watson") } - - } Group {