Twitter

Thursday, July 20, 2006

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.
library(mgcv)
options(digits=3)

## 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

M<-list(y=y,
w=rep(1, length(y)),
X=x.mat,
C=matrix(0,0,0),
p=rep(1, ncol(x.mat)),
off=array(0,0),
S=list(),
sp=array(0,0),
Ain=diag(ncol(x.mat)),
bin=rep(0, ncol(x.mat)) )

pcls(M)

4 comments:

Tian Zheng said...

Using the same R function, it can be done by specify the options of PCLS. You can follow the link in this post to read the help file of this function.

NutRajan said...

Tian,
Need to run a regression (y~ x) (just one independent variable) with constraints on the coefficient of x. y > x.

Constraint such that coefficient of x is integer between 1 and 10 (say). How can I do it in R ?. Any help greatly appreciated.

Thanks

Unknown said...

Ilike it, this article is very good
blog women blog blog business blog business business blog business blog business blog business blog business totorial word full version blog business Online business blog online bisnis online gratis bisnis online gratis jual jilbab murah jual jilbab murah jilbab murah bisnis online gratis cara berbisnis

Unknown said...

I am performing a linear regression and what I need is (1) to constrain the sum of the regression coefficients to 1, and (2) to constrain the sum of regression coefficients to 1 AND each regression coefficient to be non-negative. For each case, I need the standard error, t-stat and p values for each regression coefficient as it appears in an unconstrained linear regression.

I have tried the solve.QP command and I get correct results but I do not get the t-stat and p-values as in a linear regression