solve.QP can pass arbitrary linear constraints, so it can, of course, be used to model the constraints a+c >= 0 and c >= 0 .
First, we can add a column from 1 to X to capture the interception term, and then we can replicate the standard linear regression using solve.QP :
X2 <- cbind(X, 1) library(quadprog) solve.QP(t(X2) %*% X2, t(Y) %*% X2, matrix(0, 3, 0), c())$solution
With sample data from a question, no constraint is satisfied using standard linear regression.
bvec modifying the Amat and bvec , we can add our two limitations:
solve.QP(t(X2) %*% X2, t(Y) %*% X2, cbind(c(1, 0, 1), c(0, 0, 1)), c(0, 0))$solution
In accordance with these restrictions, square residuals are minimized by setting the coefficients a and c equal to 0.
You can handle missing values ββin Y or X2 in the same way that the lm function does, by removing offensive observations. As a preprocessing step, you can do something like the following:
has.missing <- rowSums(is.na(cbind(Y, X2))) > 0 Y <- Y[!has.missing] X2 <- X2[!has.missing,]