Learn R Programming

mosaic (version 0.4-1)

linear.algebra: Functions for teaching linear algebra.

Description

These functions provide a formula based interface to the construction of matrices from data and for fitting. You can use them both for numerical vectors and for functions of variables in data frames.

Usage

project(x, u, ...)

singvals(A, data = parent.frame())

dot(u, v)

Arguments

A
a formula. In mat and singvals, only the right-hand side is used. In project, both sides are used, but the left-hand side should be a single quantity
x
a numeric vector, formula, or matrix
u
a numeric vector
data
a data frame from which to pull out numerical values for the variables in the formula
...
additional arguments (currently ignored)
v
a numeric vector

Value

  • mat returns a matrix

    singvals gives singular values for each column in the model matrix

    project returns the projection of x onto u (or its length if u and v are numeric vectors and type == "length")

    dot returns the dot product of u and v

Details

These functions are intended to support teaching basic linear algebra with a particular connection to statistics. project (preferably pronounced "PRO-ject" as in "projection") does either of two related things: (1) Given two vectors as arguments, it will project the first onto the second, returning the point in the subspace of the second that is as close as possible to the first vector. (2) Given a formula as an argument, will work very much like lm(), constructing a model matrix from the right-hand side of the formula and projecting the vector on the left-hand side onto the subspace of that model matrix. In (2), rather than returning the projected vector, project() returns the coefficients on each of the vectors in the model matrix. UNLIKE lm(), the intercept vector is NOT included by default. If you want an intercept vector, include +1 in your formula.

mat returns a model matrix

To demonstrate singularity, use singvals.

See Also

linearModel, which returns a function.

Examples

Run this code
a <- c(1,0,0); b <- c(1,2,3); c <- c(4,5,6); x <- rnorm(3)
dot(b,c)   # dot product
# projection onto the 1 vector gives the mean vector
mean(x)
project(x, 1)
# return the length of the vector, rather than the vector itself
project(x, 1, type='length')
# Formula interface
mat(~a+b)
mat(~a+b+1)
mat(~length+sex, data=KidsFeet)
project(a~b)
project(width~length+sex, data=KidsFeet)
project(log(width) ~ I(length^2)+sin(length)+sex, data=KidsFeet)
singvals(~length*sex*width, data=KidsFeet)

Run the code above in your browser using DataLab