Dear list, In my previous email, the model I'd like to estimate is rea=a*st+b*mod+error, where a+b=1 and a,b>0. My apologies for the misunderstanding. Thanks for all your help, Jorge On Wed, Jul 23, 2008 at 3:35 PM, Jorge Ivan Velez <jorgeivanvelez@gmail.com> wrote:> > Dear list, > > I have a data set which looks like myDF (see below) and I'd like to > estimate the linear model rea=a*rea+b*mod+error, where a+b=1 and a,b>0. I > tried > > mymodel=lm(rea~ st + mod-1, data=myDF) > summary(mymodel) > > but I couldn't get what I'm looking for. Also, I tried > RSiteSearch("constrained coefficients in lm") which produces 20 hints, but > they're not what I need. > > Any help would be appreciated. > > Thanks in advance, > > > Jorge > > > # Data set to estimate the model > myDF=read.table(textConnection("rea st mod > 14482734 14305907 14761000 > 14427969 14502228 14848024 > 14698723 14408264 14259492 > 15741064 14797138 14565303 > 15914249 16162714 14348592 > 16475594 15977623 15592229 > 17124613 16688456 14988151 > 17575281 17383822 15647240 > 17721548 17757635 15624907 > 18178273 17779858 15598802 > 18005810 18364233 16363321 > 18032618 17938963 16536844 > 17737470 18043063 17096089 > 17652014 17624039 17056355"),header=TRUE,sep="") > closeAllConnections() > > >[[alternative HTML version deleted]]
Take a look at the systemfit package. A constraint on a parameter, like the one you described, could be implemented using R*beta0 = q, where R is R.restr and q is q.restr in the function call. I haven't tried it but it's easy to test with some data. Horace -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jorge Ivan Velez Sent: Wednesday, July 23, 2008 12:39 PM To: R mailing list Subject: [R] Constrained coefficients in lm (correction) Dear list, In my previous email, the model I'd like to estimate is rea=a*st+b*mod+error, where a+b=1 and a,b>0. My apologies for the misunderstanding. Thanks for all your help, Jorge On Wed, Jul 23, 2008 at 3:35 PM, Jorge Ivan Velez <jorgeivanvelez at gmail.com> wrote:> > Dear list, > > I have a data set which looks like myDF (see below) and I'd like to > estimate the linear model rea=a*rea+b*mod+error, where a+b=1 and a,b>0. I > tried > > mymodel=lm(rea~ st + mod-1, data=myDF) > summary(mymodel) > > but I couldn't get what I'm looking for. Also, I tried > RSiteSearch("constrained coefficients in lm") which produces 20 hints, but > they're not what I need. > > Any help would be appreciated. > > Thanks in advance, > > > Jorge > > > # Data set to estimate the model > myDF=read.table(textConnection("rea st mod > 14482734 14305907 14761000 > 14427969 14502228 14848024 > 14698723 14408264 14259492 > 15741064 14797138 14565303 > 15914249 16162714 14348592 > 16475594 15977623 15592229 > 17124613 16688456 14988151 > 17575281 17383822 15647240 > 17721548 17757635 15624907 > 18178273 17779858 15598802 > 18005810 18364233 16363321 > 18032618 17938963 16536844 > 17737470 18043063 17096089 > 17652014 17624039 17056355"),header=TRUE,sep="") > closeAllConnections() > > >[[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org 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.
This problem can be easily solved analytically: we want to minimize sum(res(i) -a*st(i) -b*mod(i))^2 subject to a+b=1,a,b>=0, so we want to minimize f(a) = sum((res(i)-mod(i)) - a*(st(i)-mod(i)))^2 for 0<=a<=1 Define Xi = res(i) - mod(i), Yi = st(i) - mod(i), then f(a) = sum(Xi - a*Yi)^2 f(0) = sum(Xi^2), f(1) = sum(Xi-Yi)^2, f'(a) = -2*sum(Yi*(Xi-aYi)), so f'(a) = 0 for a* = sum(Xi*Yi)/sum(Yi^2) So if 0 <= a* <= 1 then a* is the solution, otherwise take a=0 or a=1 depending on whether f(0) < f(1) or not. --- On Thu, 24/7/08, Jorge Ivan Velez <jorgeivanvelez at gmail.com> wrote:> From: Jorge Ivan Velez <jorgeivanvelez at gmail.com> > Subject: [R] Constrained coefficients in lm (correction) > To: "R mailing list" <r-help at r-project.org> > Received: Thursday, 24 July, 2008, 5:39 AM > Dear list, > > In my previous email, the model I'd like to estimate is > rea=a*st+b*mod+error, where a+b=1 and a,b>0. My > apologies for the > misunderstanding. > > Thanks for all your help, > > Jorge > > > On Wed, Jul 23, 2008 at 3:35 PM, Jorge Ivan Velez > <jorgeivanvelez at gmail.com> > wrote: > > > > > Dear list, > > > > I have a data set which looks like myDF (see below) > and I'd like to > > estimate the linear model rea=a*rea+b*mod+error, where > a+b=1 and a,b>0. I > > tried > > > > mymodel=lm(rea~ st + mod-1, data=myDF) > > summary(mymodel) > > > > but I couldn't get what I'm looking for. > Also, I tried > > RSiteSearch("constrained coefficients in > lm") which produces 20 hints, but > > they're not what I need. > > > > Any help would be appreciated. > > > > Thanks in advance, > > > > > > Jorge > > > > > > # Data set to estimate the model > > myDF=read.table(textConnection("rea st > mod > > 14482734 14305907 14761000 > > 14427969 14502228 14848024 > > 14698723 14408264 14259492 > > 15741064 14797138 14565303 > > 15914249 16162714 14348592 > > 16475594 15977623 15592229 > > 17124613 16688456 14988151 > > 17575281 17383822 15647240 > > 17721548 17757635 15624907 > > 18178273 17779858 15598802 > > 18005810 18364233 16363321 > > 18032618 17938963 16536844 > > 17737470 18043063 17096089 > > 17652014 17624039 > 17056355"),header=TRUE,sep="") > > closeAllConnections() > > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.
Hi Jorge, Did you ever figure this out? I want to do the same thing with the additional constraint of the coef for x1 = 2. lm(Y~offset(2*x1)+x2+x3,data=mydata) where b= coeff for x2, c = coeff for x3, b+c=1 and b and c>0. I've loaded the systemfit package, but the suggestion "R*beta0 = q, where R is R.restr and q is q.restr in the function call" makes no sense to me. Cheers, Rosie -- View this message in context: http://r.789695.n4.nabble.com/Constrained-coefficients-in-lm-correction-tp861989p4634321.html Sent from the R help mailing list archive at Nabble.com.