Fitting constrained least square regression in R
Prepared the following for an email inquiry.
Yes. There is something in R for such tasks. This requires a special package called mgcv, which should be installed in standard R configuration. See http://sekhon.berkeley.edu/library/mgcv/html/pcls.html. Especially, check out the option Ain and bin.
Here is an example I wrote:
(copy-paste these lines into R console)
## load the special package first.
## generate some fake data
x.1<-rnorm(100, 0, 1)
x.2<-rnorm(100, 0, 1)
x.3<-rnorm(100, 0, 1)
x.4<-rnorm(100, 0, 1)
y<-1+0.5*x.1-0.2*x.2+0.3*x.3+0.1*x.4+rnorm(100, 0, 0.01)
## make your own design matrix with one column corresponding to the intercept
x.mat<-cbind(rep(1, length(y)), x.1, x.2, x.3, x.4)
## this is the regular least-square regression
ls.print(lsfit(x.mat, y, intercept=FALSE))
## since you already have an X column for intercept, so no need for lsfit to assume another intercept term.
## the penalized constrained least square regression
bin=rep(0, ncol(x.mat)) )