Skip to content

Commit 048886c

Browse files
feat: model anova
1 parent c6cbdbe commit 048886c

File tree

9 files changed

+365
-0
lines changed

9 files changed

+365
-0
lines changed

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(ols_anova,default)
34
S3method(ols_coll_diag,default)
45
S3method(ols_correlations,default)
56
S3method(ols_model_performance,default)
@@ -52,6 +53,7 @@ S3method(plot,ols_step_forward_p)
5253
S3method(plot,ols_step_forward_r2)
5354
S3method(plot,ols_step_forward_sbc)
5455
S3method(plot,ols_step_forward_sbic)
56+
S3method(print,ols_anova)
5557
S3method(print,ols_coll_diag)
5658
S3method(print,ols_correlations)
5759
S3method(print,ols_model_performance)
@@ -83,6 +85,7 @@ S3method(print,ols_test_f)
8385
S3method(print,ols_test_normality)
8486
S3method(print,ols_test_score)
8587
export(ols_aic)
88+
export(ols_anova)
8689
export(ols_apc)
8790
export(ols_coll_diag)
8891
export(ols_correlations)

R/ols-anova.R

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#' ANOVA
2+
#'
3+
#' ANOVA for regression model.
4+
#'
5+
#' @param model An object of class \code{lm}.
6+
#' @param ... Other arguments.
7+
#'
8+
#' @return \code{ols_anova} returns an object of class \code{"ols_anova"}.
9+
#' An object of class \code{"ols_anova"} is a list containing the following
10+
#' components:
11+
#'
12+
#' \item{error_df}{residual degrees of freedom}
13+
#' \item{model_df}{regression degrees of freedom}
14+
#' \item{total_df}{total degrees of freedom}
15+
#' \item{ess}{error sum of squares}
16+
#' \item{rss}{regression sum of squares}
17+
#' \item{tss}{total sum of squares}
18+
#' \item{rms}{regression mean square}
19+
#' \item{ems}{error mean square}
20+
#' \item{f}{f statistis}
21+
#' \item{p}{p-value for \code{f}}
22+
#'
23+
#' @examples
24+
#' # model
25+
#' model <- lm(mpg ~ disp + hp + wt, data = mtcars)
26+
#'
27+
#' # anova
28+
#' ols_anova(model)
29+
#'
30+
#' @export
31+
#'
32+
ols_anova <- function(model, ...) UseMethod("ols_anova")
33+
34+
#' @export
35+
#'
36+
ols_anova.default <- function(model, ...) {
37+
anovam <- anova(model)
38+
n <- length(anovam$Df)
39+
error_df <- anovam$Df[n]
40+
model_df <- sum(anovam$Df) - error_df
41+
total_df <- sum(anovam$Df)
42+
ess <- anovam$`Sum Sq`[n]
43+
tss <- sum(anovam$`Sum Sq`)
44+
rss <- tss - ess
45+
rms <- rss / model_df
46+
ems <- ess / error_df
47+
f <- rms / ems
48+
p <- pf(f, model_df, error_df, lower.tail = F)
49+
50+
result <-
51+
list(
52+
error_df = error_df,
53+
model_df = model_df,
54+
total_df = total_df,
55+
ess = ess,
56+
rss = rss,
57+
tss = tss,
58+
rms = rms,
59+
ems = ems,
60+
f = f,
61+
p = p
62+
)
63+
64+
class(result) <- "ols_anova"
65+
66+
return(result)
67+
}
68+
69+
#' @export
70+
#'
71+
print.ols_anova <- function(x, ...) {
72+
print_anova(x)
73+
}

R/output.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,65 @@ print_model_perf <- function(data) {
823823
"RMSE: Root Mean Square Error", "\n\n")
824824

825825
}
826+
827+
print_anova <- function(data) {
828+
w7 <- nchar("Regression")
829+
830+
w8 <- max(
831+
nchar("Squares"), nchar(format(round(data$rss, 3), nsmall = 3)),
832+
nchar(format(round(data$ess, 3), nsmall = 3)),
833+
nchar(format(round(data$tss, 3), nsmall = 3))
834+
)
835+
836+
w9 <- max(
837+
nchar("DF"), nchar(format(round(data$model_df, 3), nsmall = 3)),
838+
nchar(format(round(data$error_df, 3), nsmall = 3)),
839+
nchar(format(round(data$total_df, 3), nsmall = 3))
840+
)
841+
842+
w10 <- max(
843+
nchar("Mean Square"),
844+
nchar(format(round(data$rms, 3), nsmall = 3)),
845+
nchar(format(round(data$ems, 3), nsmall = 3))
846+
)
847+
848+
w11 <- max(nchar("F"), nchar(format(round(data$f, 3), nsmall = 3)))
849+
w12 <- max(nchar("Sig."), nchar(format(round(data$p, 3), nsmall = 4)))
850+
w <- sum(w7, w8, w9, w10, w11, w12, 21)
851+
852+
p <- format(round(data$p, 4), nsmall = 4)
853+
854+
cat(fc("ANOVA", w), "\n")
855+
cat(rep("-", w), sep = "", "\n")
856+
857+
cat(fg("", w7), fs(), fg("Sum of", w8), fs(), fg("", w9), fs(), fg("", w10),
858+
fs(), fg("", w11), fs(), fg("", w12), "\n")
859+
860+
cat(fg("", w7), fs(), fg("Squares", w8), fs(), fg("DF", w9), fs(),
861+
fg("Mean Square", w10), fs(), fc("F", w11), fs(), fg("Sig.", w12), "\n")
862+
863+
cat(rep("-", w), sep = "", "\n")
864+
865+
cat(
866+
fl("Regression", w7), fs(), fg(format(round(data$rss, 3), nsmall = 3), w8),
867+
fs(), fg(round(data$model_df, 3), w9), fs(),
868+
fg(format(round(data$rms, 3), nsmall = 3), w10), fs(),
869+
fg(round(data$f, 3), w11), fs(), fg(p, w12), "\n"
870+
)
871+
872+
cat(
873+
fl("Residual", w7), fs(), fg(format(round(data$ess, 3), nsmall = 3), w8),
874+
fs(), fg(round(data$error_df, 3), w9), fs(),
875+
fg(format(round(data$ems, 3), nsmall = 3), w10), fs(), fg("", w11), fs(),
876+
fg("", w12), "\n"
877+
)
878+
879+
cat(
880+
fl("Total", w7), fs(), fg(format(round(data$tss, 3), nsmall = 3), w8), fs(),
881+
fg(round(data$total_df, 3), w9), fs(), fg("", w10), fs(),
882+
fg("", w11), fs(), fg("", w12), "\n"
883+
)
884+
885+
cat(rep("-", w), sep = "", "\n\n")
886+
887+
}

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ reference:
6363

6464
contents:
6565
- ols_regress
66+
- ols_anova
6667

6768
- title: Model Performance
6869

docs/reference/index.html

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

docs/reference/ols_anova.html

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

man/ols_anova.Rd

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

tests/testthat/_snaps/anova.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# ols_anova prints the expected output
2+
3+
Code
4+
ols_anova(model)
5+
Output
6+
ANOVA
7+
--------------------------------------------------------------------
8+
Sum of
9+
Squares DF Mean Square F Sig.
10+
--------------------------------------------------------------------
11+
Regression 931.057 3 310.352 44.566 0.0000
12+
Residual 194.991 28 6.964
13+
Total 1126.047 31
14+
--------------------------------------------------------------------
15+
16+

0 commit comments

Comments
 (0)