Learn R Programming

mosaic (version 1.9.1)

FunctionsFromData: Create function from data

Description

These functions create mathematical functions from data, by smoothing, splining, or linear combination (fitting). Each of them takes a formula and a data frame as an argument

Usage

spliner(formula, data = NULL, method = "fmm", monotonic = FALSE)

connector(formula, data = NULL, method = "linear")

smoother(formula, data, span = 0.5, degree = 2, ...)

linearModel(formula, data, ...)

Arguments

formula

a formula. Only one quantity is allowed on the left-hand side, the output quantity

data

a data frame

method

a method for splining. See spline().

monotonic

a TRUE/FALSE flag specifying whether the spline should respect monotonicity in the data

span

parameter to smoother. How smooth it should be.

degree

parameter to smoother. 1 is locally linear, 2 is locally quadratic.

...

additional arguments to stats::loess() or stats::lm()

Details

These functions use data to create a mathematical, single-valued function of the inputs. All return a function whose arguments are the variables used on the right-hand side of the formula. If the formula involves a transformation, e.g. sqrt(age) or log(income), only the variable itself, e.g. age or income, is an argument to the function.

linearModel takes a linear combination of the vectors specified on the right-hand side. It differs from project in that linearModel returns a function whereas project returns the coefficients. NOTE: An intercept term is not included unless that is explicitly part of the formula with +1. This conflicts with the standard usage of formulas as found in lm. Another option for creating such functions is to combine lm() and makeFun().

spliner and connector currently work for only one input variable.

See Also

project() method for formulas

Examples

Run this code
if (require(mosaicData)) {
data(CPS85)
f <- smoother(wage ~ age, span=.9, data=CPS85)
f(40)
g <- linearModel(log(wage) ~ age + educ + 1, data=CPS85)
g(age=40, educ=12)
# an alternative way to define g (Note: + 1 is the default for lm().)
g2 <- makeFun(lm(log(wage) ~ age + educ, data=CPS85))
g2(age=40, educ=12)
x<-1:5; y=c(1, 2, 4, 8, 8.2)
f1 <- spliner(y ~ x)
f1(x=8:10)
f2 <- connector(x~y)
}

Run the code above in your browser using DataLab