Skip to content

Commit

Permalink
simplify report.compare_performance
Browse files Browse the repository at this point in the history
#118

It's funny 'cos this is the issue that started the whole performance/insight tantrum 😅
  • Loading branch information
DominiqueMakowski committed Jan 16, 2021
1 parent aa98810 commit ad58032
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 115 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ S3method(as.report_table,report)
S3method(as.report_text,default)
S3method(as.report_text,report)
S3method(format,report_table)
S3method(format_model,character)
S3method(format_model,default)
S3method(print,report)
S3method(print,report_effectsize)
S3method(print,report_info)
Expand Down Expand Up @@ -120,7 +122,6 @@ S3method(report_parameters,stanreg)
S3method(report_parameters,survreg)
S3method(report_parameters,zeroinfl)
S3method(report_performance,MixMod)
S3method(report_performance,compare_performance)
S3method(report_performance,default)
S3method(report_performance,glm)
S3method(report_performance,glmmTMB)
Expand Down
28 changes: 28 additions & 0 deletions R/format_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#' @importFrom insight model_info is_nullmodel
#' @export
format_model <- function(x) {
UseMethod("format_model")
}

#' @export
format_model.default <- function(x) {
info <- insight::model_info(x)

if (insight::is_nullmodel(x)) {
Expand Down Expand Up @@ -71,3 +76,26 @@ format_model <- function(x) {

type
}


#' @export
format_model.character <- function(x) {
if(x == "lm"){
type <- "linear model"
} else if(x == "glm"){
type <- "general linear model"
} else if(x == "glm"){
type <- "general linear model"
} else if(x == "lmer"){
type <- "linear mixed model"
} else if(x == "glmer"){
type <- "general linear mixed model"
} else if(x == "gam"){
type <- "general additive model"
} else if(x == "gamm"){
type <- "general additive mixed model"
} else{
"model"
}
type
}
231 changes: 120 additions & 111 deletions R/report.compare_performance.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#' report_table(x)
#' report_statistics(x)
#' report_parameters(x)
#' report_performance(x)
#' }
#'
#' @export
Expand Down Expand Up @@ -105,59 +104,59 @@ report_parameters.compare_performance <- function(x, table = NULL, ...) {

# report_performance ------------------------------------------------------------

#' @rdname report.compare_performance
#' @export
report_performance.compare_performance <- function(x, table = NULL, ...) {
stats <- report_statistics(x, table = table, ...)
table <- attributes(stats)$table

models <- table$Model

text <- ""
text_short <- ""
if("p" %in% names(table)){
p <- effectsize::interpret_p(table$p)[-1]
text <- paste0(
models[-1],
" (",
stats[-1],
") has a ",
p,
"ly different explanatory power from ",
models[1],
" (",
stats[1],
", ",
insight::format_p(table$p)[-1],
")")
text_short <- paste0(
models[-1],
" (",
summary(stats)[-1],
") has a ",
p,
"ly different explanatory power from ",
models[1],
" (",
summary(stats)[1],
", ",
insight::format_p(table$p)[-1],
")")
}

if("BF" %in% names(table)){
bfs <- effectsize::interpret_bf(table$BF, include_value = TRUE, exact = FALSE)[-1]
text_bf <- paste0(bfs,
" the hypothesis that ",
models[-1],
" has a stronger predictive power than ",
models[1])
text <- text_paste(text, text_bf, sep=", and there is ")
text_short <- text_paste(text_short, text_bf, sep=", and there is ")
}

as.report_performance(text, summary = text_short, table = table)
}
# #' @rdname report.compare_performance
# #' @export
# report_performance.compare_performance <- function(x, table = NULL, ...) {
# stats <- report_statistics(x, table = table, ...)
# table <- attributes(stats)$table
#
# models <- table$Model
#
# text <- ""
# text_short <- ""
# # if("p" %in% names(table)){
# # p <- effectsize::interpret_p(table$p)[-1]
# # text <- paste0(
# # models[-1],
# # " (",
# # stats[-1],
# # ") has a ",
# # p,
# # "ly different explanatory power from ",
# # models[1],
# # " (",
# # stats[1],
# # ", ",
# # insight::format_p(table$p)[-1],
# # ")")
# # text_short <- paste0(
# # models[-1],
# # " (",
# # summary(stats)[-1],
# # ") has a ",
# # p,
# # "ly different explanatory power from ",
# # models[1],
# # " (",
# # summary(stats)[1],
# # ", ",
# # insight::format_p(table$p)[-1],
# # ")")
# # }
# #
# # if("BF" %in% names(table)){
# # bfs <- effectsize::interpret_bf(table$BF, include_value = TRUE, exact = FALSE)[-1]
# # text_bf <- paste0(bfs,
# # " the hypothesis that ",
# # models[-1],
# # " has a stronger predictive power than ",
# # models[1])
# # text <- text_paste(text, text_bf, sep=", and there is ")
# # text_short <- text_paste(text_short, text_bf, sep=", and there is ")
# # }
#
# as.report_performance(text, summary = text_short, table = table)
# }



Expand All @@ -168,66 +167,76 @@ report_performance.compare_performance <- function(x, table = NULL, ...) {
#' @export
report_text.compare_performance <- function(x, table=NULL, ...) {

stats <- report_statistics(x, table = table, ...)
stats <- report_statistics(x, table = table)
table <- attributes(stats)$table

# Get indices
models <- table$Model

text <- ""
text_short <- ""
if("p" %in% names(table)){
p <- effectsize::interpret_p(table$p)[-1]
text <- paste0(
p,
"ly different from ",
models[-1],
" (",
stats[-1],
", ",
insight::format_p(table$p)[-1],
")")
text_short <- paste0(
p,
"ly different from ",
models[-1],
" (",
summary(stats)[-1],
", ",
insight::format_p(table$p)[-1],
")")
}


text <- paste0(
"Regarding the explanatory power, ",
models[1],
" (",
stats[1],
") is ",
text_concatenate(text))
text_short <- paste0(
"Regarding the explanatory power, ",
models[1],
" (",
summary(stats)[1],
") is ",
text_concatenate(text_short))


if("BF" %in% names(table)){
bfs <- effectsize::interpret_bf(table$BF, include_value = TRUE, exact = FALSE)[-1]
t <- paste0(bfs,
" the superiority of ",
models[-1],
" compared to ",
models[1])
text_bf <- paste0(
"Regarding the predictive power, there is ",
text_concatenate(t))

text <- text_paste(text, text_bf, sep = ". ")
text_short <- text_paste(text_short, text_bf, sep = ". ")
}
text <- text_concatenate(paste0(models, " (", stats, ")"))
text_short <- text_concatenate(paste0(models, " (", summary(stats), ")"))

# Add intro sentence
text_start <- paste0("We compared ",
insight::format_number(nrow(table)),
" ",
ifelse(length(unique(table$Type)) == 1, format_model(unique(table$Type)), "model"),
"s")
text <- paste0(text_start, "; ", text, ".")
text_short <- paste0(text_start, "; ", text_short, ".")


# if("p" %in% names(table)){
# p <- effectsize::interpret_p(table$p)[-1]
# text <- paste0(
# p,
# "ly different from ",
# models[-1],
# " (",
# stats[-1],
# ", ",
# insight::format_p(table$p)[-1],
# ")")
# text_short <- paste0(
# p,
# "ly different from ",
# models[-1],
# " (",
# summary(stats)[-1],
# ", ",
# insight::format_p(table$p)[-1],
# ")")
# }

# text <- paste0(
# "Regarding the explanatory power, ",
# models[1],
# " (",
# stats[1],
# ") is ",
# text_concatenate(text))
# text_short <- paste0(
# "Regarding the explanatory power, ",
# models[1],
# " (",
# summary(stats)[1],
# ") is ",
# text_concatenate(text_short))


# if("BF" %in% names(table)){
# bfs <- effectsize::interpret_bf(table$BF, include_value = TRUE, exact = FALSE)[-1]
# t <- paste0(bfs,
# " the superiority of ",
# models[-1],
# " compared to ",
# models[1])
# text_bf <- paste0(
# "Regarding the predictive power, there is ",
# text_concatenate(t))
#
# text <- text_paste(text, text_bf, sep = ". ")
# text_short <- text_paste(text_short, text_bf, sep = ". ")
# }

as.report_text(text, summary = text_short)
}
3 changes: 0 additions & 3 deletions man/report.compare_performance.Rd

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

0 comments on commit ad58032

Please sign in to comment.