Multiple regression: Difference between revisions

Content added Content deleted
Line 2,176: Line 2,176:


## create design matrix
## create design matrix
X <- model.matrix(attr(mf, "terms"), mf)
X <- model.matrix(mf)


## create dependent variable
## create dependent variable
Line 2,203: Line 2,203:
A numerically more stable way is to use the QR decomposition of the design matrix:
A numerically more stable way is to use the QR decomposition of the design matrix:


<lang R>qr.coef(qr(X), y)</lang>
<lang rsplus>lm.impl <- function(formula) {
mf <- model.frame(formula)
X <- model.matrix(mf)
Y <- model.response(mf)
qr.coef(qr(X), Y)
}


lm(y ~ x + I(x^2))

# Call:
# lm(formula = y ~ x + I(x^2))
#
# Coefficients:
# (Intercept) x I(x^2)
# 128.81 -143.16 61.96

lm.impl(y ~ x + I(x^2))

# (Intercept) x I(x^2)
# 128.81280 -143.16202 61.96033</lang>


=={{header|Racket}}==
=={{header|Racket}}==