Re: [R] Constrained OLS regression

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Wed 27 Sep 2006 - 11:22:02 GMT

you could reparameterize, e.g.,

x1 <- runif(100, -4, 4)
x2 <- runif(100, -4, 4)
X <- cbind(1, x1 , x2)
y <- rnorm(100, as.vector(X %*% c(5, -3, 4)), 2)

######################

fn <- function(betas){

    betas <- c(betas, 1 - betas[2])
    crossprod(y - X %*% betas)[1, ]
}

opt <- optim(c(5, -3), fn, method = "BFGS") c(opt$par, 1 - opt$par[2])

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


> Hello R helpers,
>
> I am trying to do a linear OLS regression of y on two variables x1
> and
> x2. I want to constrain the coefficients of x1 and x2 to sum up to
> 1.
> and therefore run a constrained OLS. Can anybody help with this? (I
> have
> seen some answers to similar questions but it was not clear to me
> what I
> need to do) - I have tried the lm function with offset but I must
> not
> have used it properly.
>
> Thanks,
> Spyros
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed Sep 27 21:33:30 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 28 Sep 2006 - 08:30:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.