Skip to content

Commit 0672976

Browse files
feat: model performance
1 parent 9884d89 commit 0672976

File tree

9 files changed

+499
-1
lines changed

9 files changed

+499
-1
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
S3method(ols_coll_diag,default)
44
S3method(ols_correlations,default)
5+
S3method(ols_model_performance,default)
56
S3method(ols_pure_error_anova,default)
67
S3method(ols_regress,default)
78
S3method(ols_regress,lm)
@@ -53,6 +54,7 @@ S3method(plot,ols_step_forward_sbc)
5354
S3method(plot,ols_step_forward_sbic)
5455
S3method(print,ols_coll_diag)
5556
S3method(print,ols_correlations)
57+
S3method(print,ols_model_performance)
5658
S3method(print,ols_pure_error_anova)
5759
S3method(print,ols_regress)
5860
S3method(print,ols_step_all_possible)
@@ -111,6 +113,7 @@ export(ols_launch_app)
111113
export(ols_leverage)
112114
export(ols_mallows_cp)
113115
export(ols_model_info)
116+
export(ols_model_performance)
114117
export(ols_msep)
115118
export(ols_plot_added_variable)
116119
export(ols_plot_comp_plus_resid)

R/ols-model-performance.R

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#' Model Performance
2+
#'
3+
#' @description
4+
#' Evaluate performance of regression models.
5+
#'
6+
#' @param model An object of class \code{lm}.
7+
#' @param ... Other arguments.
8+
#'
9+
#' @return \code{ols_model_performance} returns an object of class
10+
#' \code{"ols_model_performance"}. An object of class \code{"ols_regress"} is a
11+
#' list containing the following components:
12+
#'
13+
#' \item{r}{square root of rsquare, correlation between observed and predicted values of dependent variable}
14+
#' \item{rsq}{coefficient of determination or r-square}
15+
#' \item{adjr}{adjusted rsquare}
16+
#' \item{prsq}{predicted rsquare}
17+
#' \item{aic}{akaike information criteria}
18+
#' \item{sbc}{bayesian information criteria}
19+
#' \item{sbic}{sawa bayesian information criteria}
20+
#' \item{rmse}{root mean squared error}
21+
#'
22+
#' @examples
23+
#' # model
24+
#' model <- lm(mpg ~ disp + hp + wt, data = mtcars)
25+
#'
26+
#' # model performance
27+
#' ols_model_performance(model)
28+
#'
29+
#' @export
30+
#'
31+
ols_model_performance <- function(model, ...) UseMethod("ols_model_performance")
32+
33+
#' @export
34+
#'
35+
ols_model_performance.default <- function(model, ...) {
36+
rsq <- summary(model)$r.squared
37+
r <- sqrt(rsq)
38+
adjr <- summary(model)$adj.r.squared
39+
rmse <- sqrt(mean(model$residuals ^ 2))
40+
aic <- ols_aic(model)
41+
sbc <- ols_sbc(model)
42+
sbic <- ols_sbic(model, model)
43+
prsq <- ols_pred_rsq(model)
44+
45+
result <- list(
46+
r = r,
47+
rsq = rsq,
48+
adjr = adjr,
49+
prsq = prsq,
50+
aic = aic,
51+
sbc = sbc,
52+
sbic = sbic,
53+
rmse = rmse
54+
)
55+
56+
class(result) <- "ols_model_performance"
57+
58+
return(result)
59+
60+
}
61+
62+
#' @export
63+
#'
64+
print.ols_model_performance <- function(x, ...) {
65+
print_model_perf(x)
66+
}

R/output.R

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,4 +784,42 @@ print_step_rsquared <- function(data) {
784784
print_step_output(data, "both")
785785
}
786786

787-
}
787+
}
788+
789+
print_model_perf <- function(data) {
790+
a <- c("R", "R-Squared", "Adj. R-Squared", "Pred R-Squared")
791+
b <- c(
792+
format(round(data$r, 3), nsmall = 3),
793+
format(round(data$rsq, 3), nsmall = 3),
794+
format(round(data$adjr, 3), nsmall = 3),
795+
format(round(data$prsq, 3), nsmall = 3)
796+
)
797+
d <- c("AIC", "SBC", "SBIC", "RMSE")
798+
e <- c(
799+
format(round(data$aic, 3), nsmall = 3),
800+
format(round(data$sbc, 3), nsmall = 3),
801+
format(round(data$sbic, 3), nsmall = 3),
802+
format(round(data$rmse, 3), nsmall = 3)
803+
)
804+
805+
w1 <- max(nchar(a))
806+
w2 <- max(nchar(format(b, nsmall = 3)))
807+
w3 <- max(nchar(d))
808+
w4 <- max(nchar(format(e, nsmall = 3)))
809+
w5 <- sum(w1, w2, w3, w4, 15)
810+
nw <- length(b)
811+
812+
# model summary
813+
cat(fc("Model Performance", w5), "\n")
814+
cat(rep("-", w5), sep = "", "\n")
815+
for (i in seq_len(nw)) {
816+
cat(fl(a[i], w1), fs(), fg(b[i], w2), fs(), fs(), fl(d[i], w3),
817+
fs(), fg(e[i], w4), "\n")
818+
}
819+
cat(rep("-", w5), sep = "", "\n")
820+
cat(" AIC: Akaike Information Criteria", "\n",
821+
" SBC: Schwarz Bayesian Criteria", "\n",
822+
"SBIC: Sawa's Bayesian Criteria", "\n",
823+
"RMSE: Root Mean Square Error", "\n\n")
824+
825+
}

_pkgdown.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ reference:
6464
contents:
6565
- ols_regress
6666

67+
- title: Model Performance
68+
69+
contents:
70+
- ols_model_performance
71+
6772
- title: All Possible Regression
6873

6974
contents:
@@ -172,6 +177,30 @@ reference:
172177
- ols_pure_error_anova
173178
- ols_plot_diagnostics
174179

180+
- title: Model Object Info
181+
182+
contents:
183+
- ols_model_info
184+
- ols_get_formula
185+
- ols_get_interaction_terms
186+
- ols_get_variables
187+
- ols_get_data
188+
- ols_get_df
189+
- ols_get_intercept
190+
- ols_get_model_matrix
191+
- ols_get_predicted
192+
- ols_get_residuals
193+
- ols_get_sigma
194+
- ols_get_vcov
195+
- ols_get_deviance
196+
- ols_get_parameters
197+
- ols_get_predictors
198+
- ols_get_response
199+
- ols_get_call
200+
- ols_count_obs
201+
- ols_count_parameters
202+
- ols_has_intercept
203+
175204
- title: Plot Data
176205

177206
contents:

docs/reference/index.html

Lines changed: 148 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)