| model_lm | R Documentation |
A wrapper for lm enabling multiple linear models based on a
grouping variable.
model_lm(data, yvar, neighbour = 0, linear.vars, verbose = FALSE, ...)
data |
Training data set on which models will be trained. Must be a data
set of class |
yvar |
Name of the response variable as a character string. |
neighbour |
If multiple models are fitted: Number of neighbours of each
key (i.e. grouping variable) to be considered in model fitting to handle
smoothing over the key. Should be an |
linear.vars |
A character vector of names of the predictor variables. |
verbose |
Logical; controls whether progress messages (model indices) are printed during fitting. Defaults to FALSE. |
... |
Other arguments not currently used. |
An object of class lmFit. This is a tibble with two
columns:
key |
The level of the grouping variable (i.e. key of the training data set). |
fit |
Information of the fitted model
corresponding to the |
Each row of the column fit is
an object of class lm. For details refer stats::lm.
model_smimodel, model_backward,
model_gaim, model_ppr, model_gam
library(dplyr)
library(tibble)
library(tidyr)
library(tsibble)
# Simulate data
n = 1005
set.seed(123)
sim_data <- tibble(x_lag_000 = runif(n)) |>
mutate(
# Add x_lags
x_lag = lag_matrix(x_lag_000, 5)) |>
unpack(x_lag, names_sep = "_") |>
mutate(
# Response variable
y = (0.9*x_lag_000 + 0.6*x_lag_001 + 0.45*x_lag_003)^3 + rnorm(n, sd = 0.1),
# Add an index to the data set
inddd = seq(1, n)) |>
drop_na() |>
select(inddd, y, starts_with("x_lag")) |>
# Make the data set a `tsibble`
as_tsibble(index = inddd)
# Predictor variables
linear.vars <- colnames(sim_data)[3:8]
# Model fitting
lmModel <- model_lm(data = sim_data,
yvar = "y",
linear.vars = linear.vars)
# Fitted model
lmModel$fit[[1]]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.