Learn R Programming

mosaic (version 0.3-13)

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 = NULL)

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

mat returns a model matrix from a formula while project carries out the operation of least-squares fitting using a singular value method. This means that even when the matrix is singular, a solution, either exact or least-squares, will be found. To demonstrate singularity, use singvals. NOTE: unlike the standard formula expansion in lm, these linear algebra functions do NOT include an intercept by default. If you want an intercept, put +1 as a term in your formula. (See the examples.)

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)
project(x, 1, type='length')
mat(~a+b)
mat(~a+b+1)
kids = fetchData("KidsFeet.csv")
mat(~length+sex,data=kids)
project(a~b)
project(width~length+sex,data=kids)
project(log(width)~I(length^2)+sin(length)+sex,data=kids)
singvals(~length*sex*width,data=kids)

Run the code above in your browser using DataLab