Skip to contents

This is a model fitting function with the usual arguments formula, data and optionally weights, defining a “constant model”. Model predictions will be equal to the result of applying fun() to the response (left hand side variable of formula) for all observations. Fitting such a model thus amounts to evaluating fun(y), where y denotes the model response.

Usage

fm_const(formula, data, fun = mean, weights = NULL, na.action = na.omit, ...)

# S3 method for fm_const
print(x, abbreviate = TRUE, ...)

# S3 method for fm_const
predict(object, newdata, ...)

Arguments

formula

A formula. Only the left hand side relevant.

data

A data.frame.

fun

A function defining a location estimate. Takes a numeric vector as its first argument, optionally an argument weights, and returns a numeric value.

weights

Optional numeric vector, The fitting weights.

na.action

A function which indicates what should happen when the response variable contains NAs.

...

Arguments passed to fun().

x, object

Object of class “fm_const”.

abbreviate

Logical. If TRUE (the default), long formulas and calls are printed in abbreviated mode, such that they usually fit on 4 or fewer output lines; otherwise they are printed entirely, no matter how long they are.

newdata

Data for prediction.

Value

fm_const() returns a list of class “fm_glmnet” with components

  • estimate: the estimated value;

  • formula: the formula;

  • n: the number of rows in data;

  • rownames: the rownames of the data;

  • fun: the fitting function fun();

  • weights: the fitting weights;

  • na.action: the na.action used during data preparation;

  • call: the matched call generating the model.

Methods (by generic)

Examples

fm <- fm_const(Sepal.Width ~ 1, iris)
fm
#> fm_const-model (class ‘fm_knn’)
#>   formula:   Sepal.Width ~ 1
#>   data:      iris
#>   estimate:  3.057333
#>   fun:       function (x, ...)  
unique(predict(fm)) # all equal values
#> [1] 3.057333

# Weighted constant model
wmean <- function(x, weights, ...) weighted.mean(x, w = weights, ...)
w <- runif(150)
fm_const(Sepal.Length ~ 1, iris, weights = w, fun = wmean)
#> fm_const-model (class ‘fm_knn’)
#>   formula:   Sepal.Length ~ 1
#>   data:      iris
#>   estimate:  5.896345
#>   fun:       function (x, weights, ...)  
#>   weights:   numeric [150]