Grouped regression

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Grouped regression

SKrishna

Hi,

I am a very occasional user of R, and will be grateful for some help  in  
constructing a regression across groups.

Here is an example:

library(MASS)
attach(cats)
Sex[120:144]<-factor(TG) #Renaming some males to transgender, to create 3  
groups, male, female and transgender

out<-lm(Bwt~Sex/Hwt) #Gives me 3 separate linear regressions for groups M,  
F and TG

What I now want to do is to examine subsets of this, where groups F and TG  
have the same slope (and/or intercept). I want to do this in such a way  
that future calls to anova() for model comparison will recognize the  
subset structure: so I do not want to just reset the levels with F to TG  
and then examine the regression.

Thanks much,

Suresh

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

Re: Grouped regression

David Winsemius

On Jul 8, 2012, at 4:11 AM, Suresh Krishna wrote:

>
> Hi,
>
> I am a very occasional user of R, and will be grateful for some  
> help  in constructing a regression across groups.
>
> Here is an example:
>
> library(MASS)
> attach(cats)
> Sex[120:144]<-factor(TG) #Renaming some males to transgender, to  
> create 3 groups, male, female and transgender

That throws an error for me. (and I expected it to do so since I was  
guessing the "TG" was not in the levels of cats$Sex.) The error  
actually had nothing to do with the lack of a "TG"  level, but was  
instead due to the lack of a TG object in my workspace. Didn't you get  
an error?

If a level does not exist then you need to add it before you make  
assignments to a facotr variable:

 > levels(Sex) <- c(levels(Sex), "TG")
 > Sex[120:144]<-"TG"
 > table(Sex)
Sex
  F  M TG
47 72 25

>
> out<-lm(Bwt~Sex/Hwt) #Gives me 3 separate linear regressions for  
> groups M, F and TG
>
> What I now want to do is to examine subsets of this, where groups F  
> and TG have the same slope (and/or intercept). I want to do this in  
> such a way that future calls to anova() for model comparison will  
> recognize the subset structure: so I do not want to just reset the  
> levels with F to TG and then examine the regression.

I'm getting the sense that this is homework. You offer no information  
about you business or academic affiliations and appear not to have  
read the Posting Guide. Also setting up regressions is covered in the  
"Introduction to R" which you are requested to have reviewed before  
asking questions on Rhelp. Section 11 would be particularly relevant  
here.
>

--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

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

Re: Grouped regression

SKrishna

Hi, Thanks for the reply.

> Sex[120:144]<-factor(TG) #Renaming some males to transgender, to  
> create 3 groups, male, female and transgender

Sorry, that should have been

Sex[120:144]<-factor('TG')

The original line did not have the quotes.

>I'm getting the sense that this is homework. You offer no information  
>about you business or academic affiliations and appear not to have  
>read the Posting Guide. Also setting up regressions is covered in the  
>"Introduction to R" which you are requested to have reviewed before  
>asking questions on Rhelp. Section 11 would be particularly relevant  
>here.

No, it is not homework, I just used the cats dataset to produce a working example, as the Posting Guide suggests. I did not get the information I was looking for (how to create a subset regression where the slope and/or intercept for some factors were constrained to be the same) from the "Introduction to R" which I did look at (along with searching the archives of R-help and doing  a general Google search and going through the first 100 or so results and looking at Julian Faraway's book on Regression Analysis as well as some material from John Fox...

Very best, Suresh
Reply | Threaded
Open this post in threaded view
|

Re: Grouped regression

David Winsemius

On Jul 8, 2012, at 7:55 AM, SKrishna wrote:

>
> Hi, Thanks for the reply.
>
>> Sex[120:144]<-factor(TG) #Renaming some males to transgender, to
>> create 3 groups, male, female and transgender
>
> Sorry, that should have been
>
> Sex[120:144]<-factor('TG')
>
> The original line did not have the quotes.

I showed you how to fix that in the reply which you have not copied.  
Is this further evidence that you have not read the Posting Guide?

>
>> I'm getting the sense that this is homework. You offer no information
>> about you business or academic affiliations and appear not to have
>> read the Posting Guide. Also setting up regressions is covered in the
>> "Introduction to R" which you are requested to have reviewed before
>> asking questions on Rhelp. Section 11 would be particularly relevant
>> here.
>
> No, it is not homework, I just used the cats dataset to produce a  
> working
> example, as the Posting Guide suggests. I did not get the  
> information I was
> looking for (how to create a subset regression where the slope and/or
> intercept for some factors were constrained to be the same) from the
> "Introduction to R" which I did look at (along with searching the  
> archives
> of R-help and doing  a general Google search and going through the  
> first 100
> or so results and looking at Julian Faraway's book on Regression  
> Analysis as
> well as some material from John Fox...

The term "subset regression" is not one I recognize but it sounded  
from your description that you wanted what you would get with:

 > out<-lm(Bwt~Sex+Hwt)
 > out

Call:
lm(formula = Bwt ~ Sex + Hwt)

Coefficients:
(Intercept)         SexM        SexTG          Hwt
      1.4716       0.2144       0.6854       0.0965

> --
> View this message in context: http://r.789695.n4.nabble.com/Grouped-regression-tp4635761p4635771.html
> Sent from the R help mailing list archive at Nabble.com.

It is unfortunate that you continue (along with so many users of  
Nabble) to fail to include context.

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

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

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

Re: Grouped regression

SKrishna

Dear David,

Thanks for the tip about naming the levels first with levels before assigning them.

However, your answer does not solve the original question that I requested help on.  I will post a simplified version as a separate query, partly because the subject line on the present one seems to match one of the spam filters in place for the list (thereby necessitating moderation).

Best, Suresh

David Winsemius wrote
On Jul 8, 2012, at 7:55 AM, SKrishna wrote:

>
> Hi, Thanks for the reply.
>
>> Sex[120:144]<-factor(TG) #Renaming some males to transgender, to
>> create 3 groups, male, female and transgender
>
> Sorry, that should have been
>
> Sex[120:144]<-factor('TG')
>
> The original line did not have the quotes.

I showed you how to fix that in the reply which you have not copied.  
Is this further evidence that you have not read the Posting Guide?

>
>> I'm getting the sense that this is homework. You offer no information
>> about you business or academic affiliations and appear not to have
>> read the Posting Guide. Also setting up regressions is covered in the
>> "Introduction to R" which you are requested to have reviewed before
>> asking questions on Rhelp. Section 11 would be particularly relevant
>> here.
>
> No, it is not homework, I just used the cats dataset to produce a  
> working
> example, as the Posting Guide suggests. I did not get the  
> information I was
> looking for (how to create a subset regression where the slope and/or
> intercept for some factors were constrained to be the same) from the
> "Introduction to R" which I did look at (along with searching the  
> archives
> of R-help and doing  a general Google search and going through the  
> first 100
> or so results and looking at Julian Faraway's book on Regression  
> Analysis as
> well as some material from John Fox...

The term "subset regression" is not one I recognize but it sounded  
from your description that you wanted what you would get with:

 > out<-lm(Bwt~Sex+Hwt)
 > out

Call:
lm(formula = Bwt ~ Sex + Hwt)

Coefficients:
(Intercept)         SexM        SexTG          Hwt
      1.4716       0.2144       0.6854       0.0965

> --
> View this message in context: http://r.789695.n4.nabble.com/Grouped-regression-tp4635761p4635771.html
> Sent from the R help mailing list archive at Nabble.com.

It is unfortunate that you continue (along with so many users of  
Nabble) to fail to include context.

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

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

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