The recommended way to do OLS in R
is to use the lm
function. The following example can be found in Dobson (1990) dand the manual-page of lm.
c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
ctl <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
trt <- gl(2, 10, 20, labels = c("Ctl","Trt"))
group <- c(ctl, trt)
weight <- lm(weight ~ group)
lm.D9 <- lm.D9
##
## Call:
## lm(formula = weight ~ group)
##
## Coefficients:
## (Intercept) groupTrt
## 5.032 -0.371
This shows how ROI could be used to solve the ordinary least squares problem. It is well know that OLS solves the following optimization problem. \[\underset{\beta}{\text{minimize}} ~ || y - X \beta ||_2^2\] Therefore we can easily solve this quadratic optimization problem by making use of ROI.
Sys.setenv(ROI_LOAD_PLUGINS = FALSE)
suppressMessages(library(ROI))
library(ROI.plugin.qpoases)
model.matrix(lm.D9)
X <- weight
y <-
2 * t(X) %*% X
Q <- -2 * t(y) %*% X
L <- OP(objective = Q_objective(Q = Q, L = L),
op <-bounds = V_bound(ld = -Inf, nobj = ncol(X)))
ROI_solve(op)) (sol <-
## Optimal solution found.
## The objective value is: -4.704595e+02
solution(sol)) (beta <-
## [1] 5.032 -0.371