mosaic (version 0.14.4)

makeFun: Create a function from a formula

Description

Provides an easy mechanism for creating simple "mathematical" functions via a formula interface.

Usage

makeFun(object, ...)

# S3 method for function makeFun(object, ..., strict.declaration = TRUE, use.environment = TRUE, suppress.warnings = FALSE)

# S3 method for formula makeFun(object, ..., strict.declaration = TRUE, use.environment = TRUE, suppress.warnings = FALSE)

# S3 method for lm makeFun(object, ..., transformation = NULL)

# S3 method for glm makeFun(object, ..., type = c("response", "link"), transformation = NULL)

# S3 method for nls makeFun(object, ..., transformation = NULL)

# S3 method for groupwiseModel makeFun(object, ..., transformation = NULL)

Arguments

object

an object from which to create a function. This should generally be specified without naming.

...

additional arguments in the form var = val that set default values for the inputs to the function.

strict.declaration

if TRUE (the default), an error is thrown if default values are given for variables not appearing in the object formula.

use.environment

if TRUE, then variables implicitly defined in the object formula can take default values from the environment at the time makeFun is called. A warning message alerts the user to this situation, unless suppress.warnings is TRUE.

suppress.warnings

A logical indicating whether warnings should be suppressed.

transformation

a function used to transform the response. This can be useful to invert a transformation used on the response when creating the model. If NULL, an attempt will be made to infer the transformation from the model formula. A few simple transformations (log, log2, sqrt) are recognized. For other transformations, transformation should be provided explicitly.

type

one of 'response' (default) or 'link' specifying scale to be used for value of function returned.

Value

a function

Details

The definition of the function is given by the left side of a formula. The right side lists at least one of the inputs to the function. The inputs to the function are all variables appearing on either the left or right sides of the formula. Those appearing in the right side will occur in the order specified. Those not appearing in the right side will appear in an unspecified order.

When creating a function from a model created with lm, glm, or nls, the function produced is a wrapper around the corresponding version of predict. This means that having variables in the model with names that match arguments of predict will lead to potentially ambiguous situations and should be avoided.

Examples

Run this code
# NOT RUN {
f <- makeFun( sin(x^2 * b) ~ x & y & a); f
g <- makeFun( sin(x^2 * b) ~ x & y & a, a=2 ); g
h <- makeFun( a * sin(x^2 * b) ~ b & y, a=2, y=3); h
if (require(mosaicData)) {
model <- lm( log(length) ~ log(width), data=KidsFeet)
f <- makeFun(model, transformation = exp)
f(8.4)
head(KidsFeet,1)
}

model <- lm(wage ~ poly(exper,degree=2), data=CPS85)
fit <- makeFun(model)
xyplot(wage ~ exper, data=CPS85)
plotFun(fit(exper) ~ exper, add=TRUE)
if (require(mosaicData)) {
model <- glm(wage ~ poly(exper,degree=2), data=CPS85, family=gaussian)
fit <- makeFun(model)
xyplot(wage ~ exper, data=CPS85)
plotFun(fit(exper) ~ exper, add=TRUE)
}
if (require(mosaicData)) {
model <- nls( wage ~ A + B * exper + C * exper^2, data=CPS85, start=list(A=1,B=1,C=1) )
fit <- makeFun(model)
xyplot(wage ~ exper, data=CPS85)
plotFun(fit(exper) ~ exper, add=TRUE)
}
mod <- gwm(wage ~ sector, data = CPS85)
modfun <- makeFun(mod)
modfun(sector = "prof")
# }

Run the code above in your browser using DataCamp Workspace