Skip to content

Commit dd8cfe4

Browse files
feat: inteaction terms
1 parent 696c53f commit dd8cfe4

File tree

5 files changed

+131
-2
lines changed

5 files changed

+131
-2
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export(ols_correlations)
8787
export(ols_eigen_cindex)
8888
export(ols_fpe)
8989
export(ols_get_formula)
90+
export(ols_get_interaction_terms)
9091
export(ols_hadi)
9192
export(ols_hsp)
9293
export(ols_launch_app)

R/ols-model-info.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,28 @@
1818
ols_get_formula <- function(model) {
1919
formula(model)
2020
}
21+
22+
#' Interaction terms
23+
#'
24+
#' Returns interaction terms present in the model.
25+
#'
26+
#' @param model An object of class \code{lm}.
27+
#'
28+
#' @return \code{Character} vector or \code{NULL}.
29+
#'
30+
#' @examples
31+
#' model <- lm(mpg ~ wt + cyl * hp * disp + gear * drat, data = mtcars)
32+
#' ols_get_interaction_terms(model)
33+
#'
34+
#' @export
35+
#'
36+
ols_get_interaction_terms <- function(model) {
37+
terms <- model$terms
38+
i <- attr(terms, 'order')
39+
if (any(i > 1)) {
40+
iterms <- i > 1
41+
return(attr(terms, 'term.labels')[iterms])
42+
} else {
43+
return(NULL)
44+
}
45+
}

R/utils.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,71 @@ max_nchar <- function(char, val, rn = 3, ns = 3) {
9090
max(nchar(char), nchar(format(round(val, rn), nsmall = ns)))
9191
}
9292

93+
ols_get_terms <- function(model) {
94+
attr(model$terms, 'term.label')
95+
}
96+
97+
ols_get_variables <- function(model) {
98+
vars <- names(model$model)
99+
n <- length(vars)
100+
resp <- vars[1]
101+
preds <- vars[2:n]
102+
list(response = resp, predictors = preds)
103+
}
104+
105+
ols_get_data <- function(model) {
106+
model$model
107+
}
108+
109+
ols_get_df <- function(model) {
110+
model$df.residual
111+
}
112+
113+
ols_get_intercept <- function(model) {
114+
model$coefficients[[1]]
115+
}
116+
117+
ols_get_model_matrix <- function(model) {
118+
model.matrix(model)
119+
}
120+
121+
ols_get_predicted <- function(model) {
122+
as.numeric(model$fitted.values)
123+
}
124+
125+
ols_get_sigma <- function(model) {
126+
summary(model)$sigma
127+
}
93128

129+
ols_get_residuals <- function(model) {
130+
model$residuals
131+
}
132+
133+
ols_get_deviance <- function(model) {
134+
deviance(model)
135+
}
136+
137+
ols_get_parameters <- function(model) {
138+
params <- model$coefficients[, 1]
139+
data.frame(Parameter = names(params), Estimate = unname(params))
140+
}
141+
142+
ols_get_predictors <- function(model) {
143+
model$model[, -1]
144+
}
145+
146+
ols_get_response <- function(model) {
147+
model$model[, 1]
148+
}
149+
150+
ols_get_call <- function(model) {
151+
model$call
152+
}
153+
154+
ols_get_obs <- function(model) {
155+
nrow(model$model)
156+
}
157+
158+
ols_has_intercept <- function(model) {
159+
as.logical(attr(model$terms, "intercept"))
160+
}

man/ols_get_interaction_terms.Rd

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

tests/testthat/test-model-info.R

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
test_that("the model forumula is returned", {
22
model <- lm(mpg ~ disp + hp + wt, data = mtcars)
3-
want <- as.formula(mpg ~ disp + hp + wt)
4-
got <- ols_get_formula(model)
3+
want <- as.formula(mpg ~ disp + hp + wt)
4+
got <- ols_get_formula(model)
55
expect_equal(want, got)
66
})
7+
8+
test_that("interaction terms are returned", {
9+
model <- lm(mpg ~ wt * cyl + vs * hp * gear + carb, data = mtcars)
10+
want <- c("wt:cyl", "vs:hp", "vs:gear", "hp:gear", "vs:hp:gear")
11+
got <- ols_get_interaction_terms(model)
12+
expect_equal(want, got)
13+
})
14+
15+
test_that("NULL is returned in absence of interaction terms", {
16+
model <- lm(mpg ~ disp + hp + wt, data = mtcars)
17+
want <- NULL
18+
got <- ols_get_interaction_terms(model)
19+
expect_null(want)
20+
})

0 commit comments

Comments
 (0)