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

w=rep(1, length(y)),
p=rep(1, ncol(x.mat)),
bin=rep(0, ncol(x.mat)) )



Half Stat said...


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

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.


Wildan Asrori said...

Wildan Asrori said...

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