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)

6 comments:

Half Stat said...

Hi,

Something related to this topic. I need to run a regression with constraints for coefficients,
Xi>0 and sum(Xi)=1,
Xi is coefficient, i=1,2...

How can I do it in R?

Thanks for any help.

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

Wildan Asrori said...

http://www.inginhidupsehat.com

Wildan Asrori said...

http://www.inginhidupsehat.com

http://idearumahidaman.com

ezy emon 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