Quantcast

setting parameters equal in lm

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

setting parameters equal in lm

Dustin Fife
Forgive me if this is a trivial question, but I couldn't find it an answer
in former forums. I'm trying to reproduce some SAS results where they set
two parameters equal. For example:

y = b1X1 + b2X2 + b1X3

Notice that the variables X1 and X3 both have the same slope and the
intercept has been removed. How do I get an estimate of this regression
model? I know how to remove the intercept ("-1" somewhere after the tilde).
But how about setting parameters equal? I have used the car package to set
up linear hypotheses:


X1 = rnorm(20, 10, 5); X2 = rnorm(20, 10, 5); X3 = rnorm(20, 10, 5)
Y = .5*X1 + 3*X2 + .5*X3 + rnorm(20, 0, 15)
data.set = data.frame(cbind(X1, X2, X3, Y))
linMod = lm(Y~X1 + X2 + X3, data=data.set)
require(car)
linearHypothesis(linMod, c("(Intercept)=0", "X1-X3=0"))

(forgive the unconventional use of the equal sign....old habit).
Unfortunately, the linearHypothesis is always compared to a full model
(where the parameters are freely estimated). I want to have an ANOVA
summary table for the reduced model. Any ideas? Thanks in advance for the
help!

--
Dustin Fife
PhD Student
Quantitative Psychology
University of Oklahoma

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: setting parameters equal in lm

Michael Weylandt
I don't know how it ties into the tools car gives you, but one (quick
and dirty) way to do this is to simply regress on

Y ~ aX2 + b(X1+X3)

or in R code something like:

lm(Y ~ X2 + I(X1+X3), data = data.set)

which gives a linear model you can play around with. Note the I()
function [that's the capital letter immediately preceding J] which
tells R to interpret that term "AsIs"

Hope this helps,
Michael

On Mon, May 28, 2012 at 11:14 PM, Dustin Fife <[hidden email]> wrote:

> Forgive me if this is a trivial question, but I couldn't find it an answer
> in former forums. I'm trying to reproduce some SAS results where they set
> two parameters equal. For example:
>
> y = b1X1 + b2X2 + b1X3
>
> Notice that the variables X1 and X3 both have the same slope and the
> intercept has been removed. How do I get an estimate of this regression
> model? I know how to remove the intercept ("-1" somewhere after the tilde).
> But how about setting parameters equal? I have used the car package to set
> up linear hypotheses:
>
>
> X1 = rnorm(20, 10, 5); X2 = rnorm(20, 10, 5); X3 = rnorm(20, 10, 5)
> Y = .5*X1 + 3*X2 + .5*X3 + rnorm(20, 0, 15)
> data.set = data.frame(cbind(X1, X2, X3, Y))
> linMod = lm(Y~X1 + X2 + X3, data=data.set)
> require(car)
> linearHypothesis(linMod, c("(Intercept)=0", "X1-X3=0"))
>
> (forgive the unconventional use of the equal sign....old habit).
> Unfortunately, the linearHypothesis is always compared to a full model
> (where the parameters are freely estimated). I want to have an ANOVA
> summary table for the reduced model. Any ideas? Thanks in advance for the
> help!
>
> --
> Dustin Fife
> PhD Student
> Quantitative Psychology
> University of Oklahoma
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: setting parameters equal in lm

Rui Barradas
In reply to this post by Dustin Fife
Hello,

Your model is equivalent of

y = b1(X1 + X3) + b2X2

(plus error)

So, use I() to add X1 and X3. You don't need to create an extra variable
X13 <- X1 + X3. See the help page for it. The point on function formula.

?I

linMod2 = lm(Y ~ -1 + I(X1  + X3) + X2, data=data.set)
summary(linMod2)
# The same.
linMod3 = lm(Y ~ 0 + I(X1  + X3) + X2, data=data.set)
summary(linMod3)

With set.seed(1) your common slope is

coef(linMod2)
I(X1 + X3)         X2
  0.4237869  3.3626984

Also, I find it better to put 'library', 'require', etc as the first
lines of code.

Hope this helps,

Rui Barradas

Em 29-05-2012 04:14, Dustin Fife escreveu:

> Forgive me if this is a trivial question, but I couldn't find it an answer
> in former forums. I'm trying to reproduce some SAS results where they set
> two parameters equal. For example:
>
> y = b1X1 + b2X2 + b1X3
>
> Notice that the variables X1 and X3 both have the same slope and the
> intercept has been removed. How do I get an estimate of this regression
> model? I know how to remove the intercept ("-1" somewhere after the tilde).
> But how about setting parameters equal? I have used the car package to set
> up linear hypotheses:
>
>
> X1 = rnorm(20, 10, 5); X2 = rnorm(20, 10, 5); X3 = rnorm(20, 10, 5)
> Y = .5*X1 + 3*X2 + .5*X3 + rnorm(20, 0, 15)
> data.set = data.frame(cbind(X1, X2, X3, Y))
> linMod = lm(Y~X1 + X2 + X3, data=data.set)
> require(car)
> linearHypothesis(linMod, c("(Intercept)=0", "X1-X3=0"))
>
> (forgive the unconventional use of the equal sign....old habit).
> Unfortunately, the linearHypothesis is always compared to a full model
> (where the parameters are freely estimated). I want to have an ANOVA
> summary table for the reduced model. Any ideas? Thanks in advance for the
> help!
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: setting parameters equal in lm

Dustin Fife
That did it. Thanks! One more follow-up questions. How do I set a parameter
to a particular value? I tried I(.5*X2), but that didn't do what I
expected.

On Tue, May 29, 2012 at 6:39 AM, Rui Barradas <[hidden email]> wrote:

> Hello,
>
> Your model is equivalent of
>
> y = b1(X1 + X3) + b2X2
>
> (plus error)
>
> So, use I() to add X1 and X3. You don't need to create an extra variable
> X13 <- X1 + X3. See the help page for it. The point on function formula.
>
> ?I
>
> linMod2 = lm(Y ~ -1 + I(X1  + X3) + X2, data=data.set)
> summary(linMod2)
> # The same.
> linMod3 = lm(Y ~ 0 + I(X1  + X3) + X2, data=data.set)
> summary(linMod3)
>
> With set.seed(1) your common slope is
>
> coef(linMod2)
> I(X1 + X3)         X2
>  0.4237869  3.3626984
>
> Also, I find it better to put 'library', 'require', etc as the first lines
> of code.
>
> Hope this helps,
>
> Rui Barradas
>
> Em 29-05-2012 04:14, Dustin Fife escreveu:
>
>  Forgive me if this is a trivial question, but I couldn't find it an answer
>> in former forums. I'm trying to reproduce some SAS results where they set
>> two parameters equal. For example:
>>
>> y = b1X1 + b2X2 + b1X3
>>
>> Notice that the variables X1 and X3 both have the same slope and the
>> intercept has been removed. How do I get an estimate of this regression
>> model? I know how to remove the intercept ("-1" somewhere after the
>> tilde).
>> But how about setting parameters equal? I have used the car package to set
>> up linear hypotheses:
>>
>>
>> X1 = rnorm(20, 10, 5); X2 = rnorm(20, 10, 5); X3 = rnorm(20, 10, 5)
>> Y = .5*X1 + 3*X2 + .5*X3 + rnorm(20, 0, 15)
>> data.set = data.frame(cbind(X1, X2, X3, Y))
>> linMod = lm(Y~X1 + X2 + X3, data=data.set)
>> require(car)
>> linearHypothesis(linMod, c("(Intercept)=0", "X1-X3=0"))
>>
>> (forgive the unconventional use of the equal sign....old habit).
>> Unfortunately, the linearHypothesis is always compared to a full model
>> (where the parameters are freely estimated). I want to have an ANOVA
>> summary table for the reduced model. Any ideas? Thanks in advance for the
>> help!
>>
>>


--
Dustin Fife
PhD Student
Quantitative Psychology
University of Oklahoma

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: setting parameters equal in lm

Rui Barradas
Simple, just make y2 <- y - 0.5*X2

Rui Barradas

Em 29-05-2012 13:55, Dustin Fife escreveu:

> That did it. Thanks! One more follow-up questions. How do I set a parameter
> to a particular value? I tried I(.5*X2), but that didn't do what I
> expected.
>
> On Tue, May 29, 2012 at 6:39 AM, Rui Barradas<[hidden email]>  wrote:
>
>> Hello,
>>
>> Your model is equivalent of
>>
>> y = b1(X1 + X3) + b2X2
>>
>> (plus error)
>>
>> So, use I() to add X1 and X3. You don't need to create an extra variable
>> X13<- X1 + X3. See the help page for it. The point on function formula.
>>
>> ?I
>>
>> linMod2 = lm(Y ~ -1 + I(X1  + X3) + X2, data=data.set)
>> summary(linMod2)
>> # The same.
>> linMod3 = lm(Y ~ 0 + I(X1  + X3) + X2, data=data.set)
>> summary(linMod3)
>>
>> With set.seed(1) your common slope is
>>
>> coef(linMod2)
>> I(X1 + X3)         X2
>>   0.4237869  3.3626984
>>
>> Also, I find it better to put 'library', 'require', etc as the first lines
>> of code.
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Em 29-05-2012 04:14, Dustin Fife escreveu:
>>
>>   Forgive me if this is a trivial question, but I couldn't find it an answer
>>> in former forums. I'm trying to reproduce some SAS results where they set
>>> two parameters equal. For example:
>>>
>>> y = b1X1 + b2X2 + b1X3
>>>
>>> Notice that the variables X1 and X3 both have the same slope and the
>>> intercept has been removed. How do I get an estimate of this regression
>>> model? I know how to remove the intercept ("-1" somewhere after the
>>> tilde).
>>> But how about setting parameters equal? I have used the car package to set
>>> up linear hypotheses:
>>>
>>>
>>> X1 = rnorm(20, 10, 5); X2 = rnorm(20, 10, 5); X3 = rnorm(20, 10, 5)
>>> Y = .5*X1 + 3*X2 + .5*X3 + rnorm(20, 0, 15)
>>> data.set = data.frame(cbind(X1, X2, X3, Y))
>>> linMod = lm(Y~X1 + X2 + X3, data=data.set)
>>> require(car)
>>> linearHypothesis(linMod, c("(Intercept)=0", "X1-X3=0"))
>>>
>>> (forgive the unconventional use of the equal sign....old habit).
>>> Unfortunately, the linearHypothesis is always compared to a full model
>>> (where the parameters are freely estimated). I want to have an ANOVA
>>> summary table for the reduced model. Any ideas? Thanks in advance for the
>>> help!
>>>
>>>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: setting parameters equal in lm

ONKELINX, Thierry
In reply to this post by Dustin Fife
offset() fixes the parameter to 1. So offset(I(.5*X2)) should do the trick.

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
[hidden email]
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey


-----Oorspronkelijk bericht-----
Van: [hidden email] [mailto:[hidden email]] Namens Dustin Fife
Verzonden: dinsdag 29 mei 2012 14:56
Aan: Rui Barradas
CC: r-help
Onderwerp: Re: [R] setting parameters equal in lm

That did it. Thanks! One more follow-up questions. How do I set a parameter to a particular value? I tried I(.5*X2), but that didn't do what I expected.

On Tue, May 29, 2012 at 6:39 AM, Rui Barradas <[hidden email]> wrote:

> Hello,
>
> Your model is equivalent of
>
> y = b1(X1 + X3) + b2X2
>
> (plus error)
>
> So, use I() to add X1 and X3. You don't need to create an extra
> variable
> X13 <- X1 + X3. See the help page for it. The point on function formula.
>
> ?I
>
> linMod2 = lm(Y ~ -1 + I(X1  + X3) + X2, data=data.set)
> summary(linMod2)
> # The same.
> linMod3 = lm(Y ~ 0 + I(X1  + X3) + X2, data=data.set)
> summary(linMod3)
>
> With set.seed(1) your common slope is
>
> coef(linMod2)
> I(X1 + X3)         X2
>  0.4237869  3.3626984
>
> Also, I find it better to put 'library', 'require', etc as the first
> lines of code.
>
> Hope this helps,
>
> Rui Barradas
>
> Em 29-05-2012 04:14, Dustin Fife escreveu:
>
>  Forgive me if this is a trivial question, but I couldn't find it an
> answer
>> in former forums. I'm trying to reproduce some SAS results where they
>> set two parameters equal. For example:
>>
>> y = b1X1 + b2X2 + b1X3
>>
>> Notice that the variables X1 and X3 both have the same slope and the
>> intercept has been removed. How do I get an estimate of this
>> regression model? I know how to remove the intercept ("-1" somewhere
>> after the tilde).
>> But how about setting parameters equal? I have used the car package
>> to set up linear hypotheses:
>>
>>
>> X1 = rnorm(20, 10, 5); X2 = rnorm(20, 10, 5); X3 = rnorm(20, 10, 5) Y
>> = .5*X1 + 3*X2 + .5*X3 + rnorm(20, 0, 15) data.set =
>> data.frame(cbind(X1, X2, X3, Y)) linMod = lm(Y~X1 + X2 + X3,
>> data=data.set)
>> require(car)
>> linearHypothesis(linMod, c("(Intercept)=0", "X1-X3=0"))
>>
>> (forgive the unconventional use of the equal sign....old habit).
>> Unfortunately, the linearHypothesis is always compared to a full
>> model (where the parameters are freely estimated). I want to have an
>> ANOVA summary table for the reduced model. Any ideas? Thanks in
>> advance for the help!
>>
>>


--
Dustin Fife
PhD Student
Quantitative Psychology
University of Oklahoma

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: setting parameters equal in lm

Dustin Fife
Great. Thank you!

On Tue, May 29, 2012 at 8:11 AM, ONKELINX, Thierry <[hidden email]
> wrote:

> offset() fixes the parameter to 1. So offset(I(.5*X2)) should do the trick.
>
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
> Forest
> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
> Kliniekstraat 25
> 1070 Anderlecht
> Belgium
> + 32 2 525 02 51
> + 32 54 43 61 85
> [hidden email]
> www.inbo.be
>
> To call in the statistician after the experiment is done may be no more
> than asking him to perform a post-mortem examination: he may be able to say
> what the experiment died of.
> ~ Sir Ronald Aylmer Fisher
>
> The plural of anecdote is not data.
> ~ Roger Brinner
>
> The combination of some data and an aching desire for an answer does not
> ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
>
> -----Oorspronkelijk bericht-----
> Van: [hidden email] [mailto:[hidden email]]
> Namens Dustin Fife
> Verzonden: dinsdag 29 mei 2012 14:56
> Aan: Rui Barradas
> CC: r-help
> Onderwerp: Re: [R] setting parameters equal in lm
>
> That did it. Thanks! One more follow-up questions. How do I set a
> parameter to a particular value? I tried I(.5*X2), but that didn't do what
> I expected.
>
> On Tue, May 29, 2012 at 6:39 AM, Rui Barradas <[hidden email]>
> wrote:
>
> > Hello,
> >
> > Your model is equivalent of
> >
> > y = b1(X1 + X3) + b2X2
> >
> > (plus error)
> >
> > So, use I() to add X1 and X3. You don't need to create an extra
> > variable
> > X13 <- X1 + X3. See the help page for it. The point on function formula.
> >
> > ?I
> >
> > linMod2 = lm(Y ~ -1 + I(X1  + X3) + X2, data=data.set)
> > summary(linMod2)
> > # The same.
> > linMod3 = lm(Y ~ 0 + I(X1  + X3) + X2, data=data.set)
> > summary(linMod3)
> >
> > With set.seed(1) your common slope is
> >
> > coef(linMod2)
> > I(X1 + X3)         X2
> >  0.4237869  3.3626984
> >
> > Also, I find it better to put 'library', 'require', etc as the first
> > lines of code.
> >
> > Hope this helps,
> >
> > Rui Barradas
> >
> > Em 29-05-2012 04:14, Dustin Fife escreveu:
> >
> >  Forgive me if this is a trivial question, but I couldn't find it an
> > answer
> >> in former forums. I'm trying to reproduce some SAS results where they
> >> set two parameters equal. For example:
> >>
> >> y = b1X1 + b2X2 + b1X3
> >>
> >> Notice that the variables X1 and X3 both have the same slope and the
> >> intercept has been removed. How do I get an estimate of this
> >> regression model? I know how to remove the intercept ("-1" somewhere
> >> after the tilde).
> >> But how about setting parameters equal? I have used the car package
> >> to set up linear hypotheses:
> >>
> >>
> >> X1 = rnorm(20, 10, 5); X2 = rnorm(20, 10, 5); X3 = rnorm(20, 10, 5) Y
> >> = .5*X1 + 3*X2 + .5*X3 + rnorm(20, 0, 15) data.set =
> >> data.frame(cbind(X1, X2, X3, Y)) linMod = lm(Y~X1 + X2 + X3,
> >> data=data.set)
> >> require(car)
> >> linearHypothesis(linMod, c("(Intercept)=0", "X1-X3=0"))
> >>
> >> (forgive the unconventional use of the equal sign....old habit).
> >> Unfortunately, the linearHypothesis is always compared to a full
> >> model (where the parameters are freely estimated). I want to have an
> >> ANOVA summary table for the reduced model. Any ideas? Thanks in
> >> advance for the help!
> >>
> >>
>
>
> --
> Dustin Fife
> PhD Student
> Quantitative Psychology
> University of Oklahoma
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
> * * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
> Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver
> weer en binden het INBO onder geen enkel beding, zolang dit bericht niet
> bevestigd is door een geldig ondertekend document.
> The views expressed in this message and any annex are purely those of the
> writer and may not be regarded as stating an official position of INBO, as
> long as the message is not confirmed by a duly signed document.
>



--
Dustin Fife
PhD Student
Quantitative Psychology
University of Oklahoma

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Loading...