Learn R Programming

OptimModel (version 2.0-1)

linear_model: Linear model, gradient, and starting values

Description

Linear model, gradient, and starting values.

Usage

linear_model(theta, x)

Value

Let N = nrow(x). Then

  • linear_model(theta, x) returns a numeric vector of length N.

  • attr(linear_model, "gradient")(theta, x) returns x.

  • attr(linear_model, "start")(x, y) returns solve(t(x) * x) * t(x) * y

Arguments

theta

Vector of model parameters intercept and slope. See details.

x

Matrix, possibly from model.matrix().

Author

Steven Novick

Details

The linear model is given by:

$$y = x * \theta \text{, where}$$ x is a matrix, possibly generated from model.matrix() \(\theta\) is a vector of linear parameters

See Also

optim_fit, rout_fitter

Examples

Run this code
set.seed(123)
d = data.frame( Group=factor(rep(LETTERS[1:3], each=5)), age=rnorm(15, mean=20, sd=3) )
d$y = c(80, 100, 120)[unclass(d$Group)] - 3*d$age + rnorm(nrow(d), mean=0, sd=5)

X = model.matrix(~Group+age, data=d)  ## This is the "x" in linear.model()
theta = c(80, 20, 40, -3)   ## Intercept, effect for B, effect for C, slope for age
linear_model(theta, x=X)
attr(linear_model, "gradient")(theta, x=X)
attr(linear_model, "start")(x=X, y=d$y)

Run the code above in your browser using DataLab