Skip to content

Commit

Permalink
skpr v1.2.1: Rename and export normalize_design()
Browse files Browse the repository at this point in the history
  • Loading branch information
tylermorganwall committed Mar 12, 2023
1 parent 2e111f3 commit 817bf07
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 43 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: skpr
Title: Design of Experiments Suite: Generate and Evaluate Optimal Designs
Date: 2023-01-20
Version: 1.2.0
Version: 1.2.1
Authors@R: c(person("Tyler", "Morgan-Wall", email = "[email protected]", role = c("aut", "cre")),
person("George", "Khoury", email = "[email protected]", role = c("aut")))
Description: Generates and evaluates D, I, A, Alias, E, T, and G optimal designs. Supports generation and evaluation of blocked and split/split-split/.../N-split plot designs. Includes parametric and Monte Carlo power evaluation functions, and supports calculating power for censored responses. Provides a framework to evaluate power using functions provided in other packages or written by the user. Includes a Shiny graphical user interface that displays the underlying code used to create and evaluate the design to improve ease-of-use and make analyses more reproducible. For details, see Morgan-Wall et al. (2021) <doi:10.18637/jss.v099.i01>.
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export(gen_design)
export(get_attribute)
export(get_optimality)
export(get_power_curve_output)
export(normalize_design)
export(plot_correlations)
export(plot_fds)
export(skprGUI)
Expand Down
2 changes: 1 addition & 1 deletion R/eval_design.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ eval_design = function(design, model = NULL, alpha = 0.05,
}

#------Normalize/Center numeric columns ------#
run_matrix_processed = normalize_numeric_runmatrix(run_matrix_processed)
run_matrix_processed = normalize_design(run_matrix_processed)

#-Generate Model Matrix & Anticipated Coefficients-#
#Variables used later: anticoef
Expand Down
2 changes: 1 addition & 1 deletion R/eval_design_custom_mc.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ eval_design_custom_mc = function(design, model = NULL, alpha = 0.05,
model = rearrange_formula_by_order(model, data = run_matrix_processed)

#------Normalize/Center numeric columns ------#
run_matrix_processed = normalize_numeric_runmatrix(run_matrix_processed)
run_matrix_processed = normalize_design(run_matrix_processed)

#Remove skpr-generated REML blocking indicators if present
run_matrix_processed = remove_skpr_blockcols(run_matrix_processed)
Expand Down
2 changes: 1 addition & 1 deletion R/eval_design_mc.R
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ eval_design_mc = function(design, model = NULL, alpha = 0.05,
}

#------Normalize/Center numeric columns ------#
run_matrix_processed = normalize_numeric_runmatrix(run_matrix_processed)
run_matrix_processed = normalize_design(run_matrix_processed)

#---------- Generating model matrix ----------#
#Remove columns from variables not used in the model
Expand Down
2 changes: 1 addition & 1 deletion R/eval_design_survival_mc.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ eval_design_survival_mc = function(design, model = NULL, alpha = 0.05,


#------Normalize/Center numeric columns ------#
run_matrix_processed = normalize_numeric_runmatrix(run_matrix_processed)
run_matrix_processed = normalize_design(run_matrix_processed)

#---------- Generating model matrix ----------#
#remove columns from variables not used in the model
Expand Down
12 changes: 6 additions & 6 deletions R/gen_design.R
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,10 @@ gen_design = function(candidateset, model, trials,
}
if (length(contrastslisttemp) == 0) {
advancedoptions$g_efficiency_samples = model.matrix(model,
normalize_numeric_runmatrix(advancedoptions$g_efficiency_samples))
normalize_design(advancedoptions$g_efficiency_samples))
} else {
advancedoptions$g_efficiency_samples = suppressWarnings(model.matrix(model,
normalize_numeric_runmatrix(advancedoptions$g_efficiency_samples),
normalize_design(advancedoptions$g_efficiency_samples),
contrasts.arg = contrastslisttemp))
}
} else {
Expand Down Expand Up @@ -578,7 +578,7 @@ gen_design = function(candidateset, model, trials,
}
}
#Normalize
augmentnormalized = normalize_numeric_runmatrix(augmentdesign, candidateset)
augmentnormalized = normalize_design(augmentdesign, candidateset)
augmentedrows = nrow(augmentdesign)
} else {
augmentedrows = 0
Expand All @@ -587,11 +587,11 @@ gen_design = function(candidateset, model, trials,
#------Normalize/Center numeric columns ------#
candidatesetnormalized = candidateset

candidatesetnormalized = normalize_numeric_runmatrix(candidateset, augmentdesign)
fullcandidatesetnorm = normalize_numeric_runmatrix(fullcandidateset, augmentdesign)
candidatesetnormalized = normalize_design(candidateset, augmentdesign)
fullcandidatesetnorm = normalize_design(fullcandidateset, augmentdesign)

if (!is.null(splitplotdesign)) {
spdnormalized = normalize_numeric_runmatrix(splitplotdesign)
spdnormalized = normalize_design(splitplotdesign)
}

splitplot = FALSE
Expand Down
46 changes: 46 additions & 0 deletions R/normalize_design.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#'@title Normalize Design
#'
#'@description Normalizes the numeric columns in the design to -1 to 1. This is important to do if your model has interaction or polynomial terms,
#'as these terms can introduce multi-collinearity and standardizing the numeric columns can reduce this problem.
#'
#'@param design The design matrix.
#'@param augmented Default `NULL`. If augmenting an existing design, this should be the pre-existing design. The column types must match design
#'
#'@return Normalized design matrix
#'@export
#'@examples
#'#Normalize a design
#'\dontrun{
#'cand_set = expand.grid(temp = c(100,300,500),
#' altitude = c(10000,20000),
#' offset = seq(-10,-5,by=1),
#' type = c("A","B", "C"))
#'design = gen_design(cand_set, ~., 24)
#'#Un-normalized design
#'design
#'
#'#Normalized design
#'normalize_design(design)
#'}
normalize_design = function(design, augmented = NULL) {
if(!is.null(augmented)) {
all_equal_classes = all(identical(lapply(design,class), unlist(lapply(augmented,class))))
if(!all_equal_classes) {
stop("Design to be augmented and new design must have identical column classes")
}
for (column in 1:ncol(design)) {
if (is.numeric(design[, column])) {
midvalue = mean(c(max(c(design[, column],augmented[,column])), min(c(design[, column],augmented[,column]))))
design[, column] = (design[, column] - midvalue) / (max(c(design[, column],augmented[,column])) - midvalue)
}
}
} else {
for (column in 1:ncol(design)) {
if (is.numeric(design[, column])) {
midvalue = mean(c(max(design[, column]), min(design[, column])))
design[, column] = (design[, column] - midvalue) / (max(design[, column]) - midvalue)
}
}
}
return(design)
}
29 changes: 16 additions & 13 deletions R/normalize_numeric_runmatrix.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
#'@title Normalize Run Matrix
#'@title Normalize Design
#'
#'@description Normalizes the numeric columns in the runmatrix
#'@param RunMatrix The run matrix
#'@description Normalizes the numeric columns in the design to -1 to 1. This is important to do if your model has interaction or polynomial terms,
#'as these terms can introduce multi-collinearity and standardizing the numeric columns can reduce this problem.
#'
#'@param design The design matrix.
#'@param augmented Default `NULL`. If
#'@return Normalized run matrix
#'@keywords internal
normalize_numeric_runmatrix = function(RunMatrix, augmented = NULL) {
normalize_design = function(design, augmented = NULL) {
if(!is.null(augmented)) {
for (column in 1:ncol(RunMatrix)) {
if (is.numeric(RunMatrix[, column])) {
midvalue = mean(c(max(c(RunMatrix[, column],augmented[,column])), min(c(RunMatrix[, column],augmented[,column]))))
RunMatrix[, column] = (RunMatrix[, column] - midvalue) / (max(c(RunMatrix[, column],augmented[,column])) - midvalue)
for (column in 1:ncol(design)) {
if (is.numeric(design[, column])) {
midvalue = mean(c(max(c(design[, column],augmented[,column])), min(c(design[, column],augmented[,column]))))
design[, column] = (design[, column] - midvalue) / (max(c(design[, column],augmented[,column])) - midvalue)
}
}
} else {
for (column in 1:ncol(RunMatrix)) {
if (is.numeric(RunMatrix[, column])) {
midvalue = mean(c(max(RunMatrix[, column]), min(RunMatrix[, column])))
RunMatrix[, column] = (RunMatrix[, column] - midvalue) / (max(RunMatrix[, column]) - midvalue)
for (column in 1:ncol(design)) {
if (is.numeric(design[, column])) {
midvalue = mean(c(max(design[, column]), min(design[, column])))
design[, column] = (design[, column] - midvalue) / (max(design[, column]) - midvalue)
}
}
}
return(RunMatrix)
return(design)
}
2 changes: 1 addition & 1 deletion man/extractPvalues.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions man/normalize_design.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions man/normalize_numeric_runmatrix.Rd

This file was deleted.

0 comments on commit 817bf07

Please sign in to comment.