Re: [R] Constrained OLS regression

2006-09-28 Thread vito muggeo
In addition to Dimitris's approach, probably the following is more 
straightforward..(the idea is the same, but implementation is simpler; 
you do not need starting values, for instance..)

Given the linear predictor lp:
b0+b1X1+b2X2
as b2=1-b1 the lp becomes:
b0+b1X1+(1-b1)X2 = b0+b1(X1-X2)+offset(X2)

Hence for a generic GLM you can type
glm(y~1+I(x1-x2)+offset(x2))

Hope this helps,
vito


Dimitris Rizopoulos wrote:
 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
 
 
 - Original Message - 
 From: Mesomeris, Spyros [CIR] [EMAIL PROTECTED]
 To: r-help@stat.math.ethz.ch
 Sent: Wednesday, September 27, 2006 12:51 PM
 Subject: [R] Constrained OLS regression
 
 
 
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.
 
 

-- 

Vito M.R. Muggeo
Dip.to Sc Statist e Matem `Vianelli'
Università di Palermo
viale delle Scienze, edificio 13
90128 Palermo - ITALY
tel: 091 6626240
fax: 091 485726/485612

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


[R] Constrained OLS regression

2006-09-27 Thread Mesomeris, Spyros [CIR]
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.


Re: [R] Constrained OLS regression

2006-09-27 Thread David Barron
Have a look at the linear.hypothesis function in the car package.  For example:

 mod.duncan - lm(prestige ~ income + education, data=Duncan)

 linear.hypothesis(mod.duncan, income + education = 1)
Linear hypothesis test

Hypothesis:
income + education = 1

Model 1: prestige ~ income + education
Model 2: restricted model

  Res.DfRSS Df Sum of Sq  F  Pr(F)
1 42 7506.7
2 43 8045.2 -1-538.5 3.0129 0.08994 .
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1


On 27/09/06, Mesomeris, Spyros [CIR] [EMAIL PROTECTED] wrote:
 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.




-- 
=
David Barron
Said Business School
University of Oxford
Park End Street
Oxford OX1 1HP

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


Re: [R] Constrained OLS regression

2006-09-27 Thread Dimitris Rizopoulos
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


- Original Message - 
From: Mesomeris, Spyros [CIR] [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Sent: Wednesday, September 27, 2006 12:51 PM
Subject: [R] Constrained OLS regression


 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.