Help with Implementing missRanger Imputation Module - Resource Limit Error
Posted: Mon Nov 18, 2024 10:19 pm
Hello,
I am just starting to develop modules for Jamovi and have successfully implemented a few so far. However, I am facing an issue with one specific module, and I would greatly appreciate some assistance.
The module I am working on is for data imputation using the missRanger package.
Everything seems to be working fine, but I keep receiving the message:
"This analysis has terminated, likely due to hitting a resource limit."
Below are my code files for your review.
Would anyone have any ideas on what might be causing this issue?
.a.yaml File:
name: missRangerImputation
title: MissRanger Imputation
menuGroup: BSS
menuSubgroup: Data handling
menuSubtitle: Under development...
version: '0.0.1'
jas: '1.2'
options:
- name: data
type: Data
- name: vars
title: Variables to Impute
type: Variables
suggested:
- continuous
- nominal
- ordinal
required: true
permitted:
- numeric
- factor
- name: maxiter
title: Maximum Iterations
type: Integer
default: 10
min: 1
max: 100
- name: num_trees
title: Number of Trees
type: Integer
default: 100
min: 1
max: 500
- name: pmm_k
title: Number of Donors for PMM
type: Integer
default: 5
min: 1
max: 20
- name: saveData
title: Add Imputed Values as New Column
type: Bool
default: false
.r.yaml File:
name: missRangerImputation
title: MissRanger Imputation Results
jrs: '1.1'
items:
- name: text
title: MissRanger Imputation Summary
type: Preformatted
- name: missingSummary
title: Missing Data Summary
type: Table
rows: 0
columns:
- name: variable
title: Variable
type: text
- name: total_missing
title: Missing Values
type: integer
- name: percent_missing
title: Missing (%)
type: number
format: '%.2f'
- name: imputedDataTable
title: Imputed Data Summary
type: Table
rows: 0
columns:
- name: variable
title: Variable
type: text
- name: original_value
title: Original Value
type: text
- name: imputed_value
title: Imputed Value
type: text
- name: row_number
title: Row
type: integer
.b.R File:
missRangerImputationClass <- if (requireNamespace('R6', quietly=TRUE)) R6::R6Class(
"missRangerImputationClass",
inherit = missRangerImputationBase,
public = list(
initialize = function(...) {
super$initialize(...)
},
run = function() {
# Checks if variables are selected
if (length(self$options$vars) == 0) {
self$results$text$setContent("Please select at least one variable for imputation.")
return()
}
# Checks if the 'missRanger' package is available
if (!requireNamespace("missRanger", quietly = TRUE)) {
stop("The 'missRanger' package is not installed.")
}
# Loads the package
library(missRanger)
# Prepares the dataset
df <- self$data
selected_vars <- self$options$vars
# Creates missing data summary
for (var in selected_vars) {
n_missing <- sum(is.na(df[[var]]))
pct_missing <- (n_missing / nrow(df)) * 100
self$results$missingSummary$addRow(list(
variable = var,
total_missing = n_missing,
percent_missing = pct_missing
))
}
# If no missing data, stops execution
missing_summary_df <- self$results$missingSummary$asDF()
if (nrow(missing_summary_df) == 0 || sum(missing_summary_df$total_missing) == 0) {
self$results$text$setContent("None of the selected variables have missing values for imputation.")
return()
}
# Performs imputation
imputed_data <- tryCatch({
message("Starting imputation...")
missRanger(
data = df,
maxiter = self$options$maxiter,
num.trees = self$options$num_trees,
pmm.k = self$options$pmm_k,
verbose = TRUE
)
}, error = function(e) {
self$results$text$setContent(paste("Error during imputation:", e$message))
return(NULL)
})
if (is.null(imputed_data)) return()
# Adds imputed data to results table
for (var in selected_vars) {
na_indices <- which(is.na(df[[var]]))
if (length(na_indices) > 0) {
for (idx in na_indices) {
original_val <- df[[var]][idx]
imputed_val <- imputed_data[[var]][idx]
self$results$imputedDataTable$addRow(list(
variable = var,
original_value = "NA",
imputed_value = as.character(imputed_val),
row_number = idx
))
}
}
}
# Updates text summary
total_imputations <- nrow(self$results$imputedDataTable$asDF())
summary_text <- sprintf(
"Imputation completed!\n\nDetails:\n- Number of trees: %d\n- Max iterations: %d\n- PMM donors: %d\n- Imputed variables: %s\n- Total imputations: %d",
self$options$num_trees,
self$options$maxiter,
self$options$pmm_k,
paste(selected_vars, collapse = ", "),
total_imputations
)
self$results$text$setContent(summary_text)
# Adds imputed data as new columns if requested
if (self$options$saveData) {
for (var in selected_vars) {
new_col_name <- paste0(var, "_imputed")
self$data[[new_col_name]] <- imputed_data[[var]]
}
}
}
)
)
Thanks in advance for any suggestions!
I am just starting to develop modules for Jamovi and have successfully implemented a few so far. However, I am facing an issue with one specific module, and I would greatly appreciate some assistance.
The module I am working on is for data imputation using the missRanger package.
Everything seems to be working fine, but I keep receiving the message:
"This analysis has terminated, likely due to hitting a resource limit."
Below are my code files for your review.
Would anyone have any ideas on what might be causing this issue?
.a.yaml File:
name: missRangerImputation
title: MissRanger Imputation
menuGroup: BSS
menuSubgroup: Data handling
menuSubtitle: Under development...
version: '0.0.1'
jas: '1.2'
options:
- name: data
type: Data
- name: vars
title: Variables to Impute
type: Variables
suggested:
- continuous
- nominal
- ordinal
required: true
permitted:
- numeric
- factor
- name: maxiter
title: Maximum Iterations
type: Integer
default: 10
min: 1
max: 100
- name: num_trees
title: Number of Trees
type: Integer
default: 100
min: 1
max: 500
- name: pmm_k
title: Number of Donors for PMM
type: Integer
default: 5
min: 1
max: 20
- name: saveData
title: Add Imputed Values as New Column
type: Bool
default: false
.r.yaml File:
name: missRangerImputation
title: MissRanger Imputation Results
jrs: '1.1'
items:
- name: text
title: MissRanger Imputation Summary
type: Preformatted
- name: missingSummary
title: Missing Data Summary
type: Table
rows: 0
columns:
- name: variable
title: Variable
type: text
- name: total_missing
title: Missing Values
type: integer
- name: percent_missing
title: Missing (%)
type: number
format: '%.2f'
- name: imputedDataTable
title: Imputed Data Summary
type: Table
rows: 0
columns:
- name: variable
title: Variable
type: text
- name: original_value
title: Original Value
type: text
- name: imputed_value
title: Imputed Value
type: text
- name: row_number
title: Row
type: integer
.b.R File:
missRangerImputationClass <- if (requireNamespace('R6', quietly=TRUE)) R6::R6Class(
"missRangerImputationClass",
inherit = missRangerImputationBase,
public = list(
initialize = function(...) {
super$initialize(...)
},
run = function() {
# Checks if variables are selected
if (length(self$options$vars) == 0) {
self$results$text$setContent("Please select at least one variable for imputation.")
return()
}
# Checks if the 'missRanger' package is available
if (!requireNamespace("missRanger", quietly = TRUE)) {
stop("The 'missRanger' package is not installed.")
}
# Loads the package
library(missRanger)
# Prepares the dataset
df <- self$data
selected_vars <- self$options$vars
# Creates missing data summary
for (var in selected_vars) {
n_missing <- sum(is.na(df[[var]]))
pct_missing <- (n_missing / nrow(df)) * 100
self$results$missingSummary$addRow(list(
variable = var,
total_missing = n_missing,
percent_missing = pct_missing
))
}
# If no missing data, stops execution
missing_summary_df <- self$results$missingSummary$asDF()
if (nrow(missing_summary_df) == 0 || sum(missing_summary_df$total_missing) == 0) {
self$results$text$setContent("None of the selected variables have missing values for imputation.")
return()
}
# Performs imputation
imputed_data <- tryCatch({
message("Starting imputation...")
missRanger(
data = df,
maxiter = self$options$maxiter,
num.trees = self$options$num_trees,
pmm.k = self$options$pmm_k,
verbose = TRUE
)
}, error = function(e) {
self$results$text$setContent(paste("Error during imputation:", e$message))
return(NULL)
})
if (is.null(imputed_data)) return()
# Adds imputed data to results table
for (var in selected_vars) {
na_indices <- which(is.na(df[[var]]))
if (length(na_indices) > 0) {
for (idx in na_indices) {
original_val <- df[[var]][idx]
imputed_val <- imputed_data[[var]][idx]
self$results$imputedDataTable$addRow(list(
variable = var,
original_value = "NA",
imputed_value = as.character(imputed_val),
row_number = idx
))
}
}
}
# Updates text summary
total_imputations <- nrow(self$results$imputedDataTable$asDF())
summary_text <- sprintf(
"Imputation completed!\n\nDetails:\n- Number of trees: %d\n- Max iterations: %d\n- PMM donors: %d\n- Imputed variables: %s\n- Total imputations: %d",
self$options$num_trees,
self$options$maxiter,
self$options$pmm_k,
paste(selected_vars, collapse = ", "),
total_imputations
)
self$results$text$setContent(summary_text)
# Adds imputed data as new columns if requested
if (self$options$saveData) {
for (var in selected_vars) {
new_col_name <- paste0(var, "_imputed")
self$data[[new_col_name]] <- imputed_data[[var]]
}
}
}
)
)
Thanks in advance for any suggestions!