Learn R Programming

mosaic (version 0.9-0)

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 class 'formula': makeFun(object, ..., strict.declaration = TRUE, use.environment = TRUE, suppress.warnings = FALSE)

## S3 method for class 'lm': makeFun(object, ..., transform = identity)

## S3 method for class 'glm': makeFun(object, ..., type = c("response", "link"), transform = identity)

## S3 method for class 'nls': makeFun(object, ..., transform = identity)

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
suppress.warnings
A logical indicating whether warnings should be suppressed.
transform
a function used to transform the response. This can be useful to invert a transformation used on the response when creating the model.
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.

Examples

Run this code
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
model <- lm( log(length) ~ log(width), data=KidsFeet)
f <- makeFun(model, transform=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)
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)
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)

Run the code above in your browser using DataLab