Skip to content

Commit d977b01

Browse files
committed
Update analyse.R
1 parent fa00367 commit d977b01

1 file changed

Lines changed: 40 additions & 10 deletions

File tree

R/analyse.R

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,13 @@ ModelArray.gam <- function(formula, data, phenotypes, scalar,
462462
}
463463
}
464464

465+
# Changed.rsq setup ----
465466
# Changed.rsq setup ----
466467
var.model.orig <- var.model
467468
if (!is.null(changed.rsq.term.index)) {
468469

469-
# check if the term index is valid:
470-
if (min(changed.rsq.term.index) <= 0) {
470+
# Validation checks
471+
if (min(unlist(changed.rsq.term.index)) <= 0) {
471472
stop(
472473
"There is element(s) in changed.rsq.term.index <= 0. ",
473474
"It should be a (list of) positive integer!"
@@ -476,7 +477,7 @@ ModelArray.gam <- function(formula, data, phenotypes, scalar,
476477

477478
terms.full.formula <- stats::terms(formula, keep.order = TRUE)
478479

479-
if (max(changed.rsq.term.index) > length(labels(terms.full.formula))) {
480+
if (max(unlist(changed.rsq.term.index)) > length(labels(terms.full.formula))) {
480481
stop(
481482
"Largest index in changed.rsq.term.index is more than the term number on the ",
482483
"right hand side of formula!"
@@ -491,14 +492,35 @@ ModelArray.gam <- function(formula, data, phenotypes, scalar,
491492
}
492493

493494
changed.rsq.term.fullFormat.list <- labels(terms.full.formula)[unlist(changed.rsq.term.index)]
495+
496+
# Compute short format names: s(age) -> s_age, ti(x,z) -> ti_x_z, etc.
494497
changed.rsq.term.shortFormat.list <- list()
495-
for (idx in seq_along(changed.rsq.term.index)) {
496-
tidx <- changed.rsq.term.index[[idx]]
497-
tidy_names_smooth <- character(0)
498-
tidy_names_param <- character(0)
499-
changed.rsq.term.shortFormat.list[[idx]] <- c(tidy_names_smooth, tidy_names_param)
498+
for (changed.rsq.term.fullFormat in changed.rsq.term.fullFormat.list) {
499+
temp <- strsplit(changed.rsq.term.fullFormat, "[(]")[[1]]
500+
if (length(temp) == 1) {
501+
# Not a smooth term — no parentheses
502+
str_valid <- changed.rsq.term.fullFormat
503+
} else {
504+
smooth.class <- temp[1]
505+
theEval <- eval(parse(text = changed.rsq.term.fullFormat))
506+
str_valid <- paste0(
507+
smooth.class, "_",
508+
paste(theEval$term, collapse = "_")
509+
)
510+
if (theEval$by != "NA") {
511+
str_valid <- paste0(str_valid, "_BY", theEval$by)
512+
}
513+
}
514+
changed.rsq.term.shortFormat.list <- append(
515+
changed.rsq.term.shortFormat.list, str_valid
516+
)
500517
}
501-
message("will get changed R-squared (delta.adj.rsq and partial.rsq) so the execution time will be longer.")
518+
519+
message(
520+
"will get changed R-squared (delta.adj.rsq and partial.rsq) ",
521+
"so the execution time will be longer."
522+
)
523+
502524
if (!("adj.r.squared" %in% var.model)) {
503525
var.model <- c(var.model, "adj.r.squared")
504526
}
@@ -616,7 +638,7 @@ ModelArray.gam <- function(formula, data, phenotypes, scalar,
616638

617639
# Compute changed R-squared ----
618640
if (need_changed_rsq) {
619-
terms.full.formula <- stats::terms(formula)
641+
terms.full.formula <- stats::terms(formula, keep.order = TRUE)
620642

621643
for (i.changed.rsq.term in seq_along(changed.rsq.term.index)) {
622644
idx.changed.rsq.term <- changed.rsq.term.index[[i.changed.rsq.term]]
@@ -682,6 +704,14 @@ ModelArray.gam <- function(formula, data, phenotypes, scalar,
682704
reduced_sse <- reduced.model.df_out[["model.sse"]]
683705
df_out[[partial_col]] <- (reduced_sse - full_sse) / reduced_sse
684706
}
707+
708+
# if adjusted r sq is not requested (see var.model.orig), remove it:
709+
if (!("adj.r.squared" %in% var.model.orig)) {
710+
df_out <- df_out[, colnames(df_out) != "model.adj.r.squared", drop = FALSE]
711+
}
712+
713+
# remove full model's sse (model.sse):
714+
df_out <- df_out[, colnames(df_out) != "model.sse", drop = FALSE]
685715
}
686716

687717
# Rewrite if corrections applied and streaming was used ----

0 commit comments

Comments
 (0)