[R] Obtaining values of estimates from a regression; How do I get values from a list?

Fox, John j|ox @end|ng |rom mcm@@ter@c@
Fri Feb 22 14:56:53 CET 2019


Dear John,

This seems to be more complicated than it needs to be. One normally uses coef() to extract coefficients from a model object. Thus

> coef(fitchange)
(Intercept)         pre 
-54.1010158   0.6557661 
> coef(fitchange)[2]
      pre 
0.6557661 

Best,
 John

  -------------------------------------------------
  John Fox, Professor Emeritus
  McMaster University
  Hamilton, Ontario, Canada
  Web: http::/socserv.mcmaster.ca/jfox

> On Feb 22, 2019, at 3:26 AM, Sorkin, John <jsorkin using som.umaryland.edu> wrote:
> 
> Problem solved:
> 
> summary(fitchange)$coefficients[2,1]
> 
> 
> 
> John David Sorkin M.D., Ph.D.
> Professor of Medicine
> Chief, Biostatistics and Informatics
> University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
> Baltimore VA Medical Center
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> (Phone) 410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
> 
> 
> ________________________________
> From: R-help <r-help-bounces using r-project.org> on behalf of Ivan Calandra <calandra using rgzm.de>
> Sent: Friday, February 22, 2019 2:58:47 AM
> To: r-help using r-project.org
> Subject: Re: [R] Obtaining values of estimates from a regression; How do I get values from a list?
> 
> I find that the str() function is really helpful to understand how an object is
> structured, and therefore how to extract part(s) of it.
> 
> Try for example:
> str(zz)
> and it might help you understand why zz$coefficients[2,1] is what you were
> looking for.
> 
> HTH
> Ivan
> 
> --
> Dr. Ivan Calandra
> TraCEr, laboratory for Traceology and Controlled Experiments
> MONREPOS Archaeological Research Centre and
> Museum for Human Behavioural Evolution
> Schloss Monrepos
> 56567 Neuwied, Germany
> +49 (0) 2631 9772-243
> https://www.researchgate.net/profile/Ivan_Calandra
> 
> On February 22, 2019 at 8:50 AM Eric Berger <ericjberger using gmail.com> wrote:
>> You have some choices
>> 
>> fitchange$coefficients[2]
>> 
>> zz$coefficients[2,1]
>> 
>> Note that class(zz$coefficients) shows that it is a matrix.
>> 
>> HTH,
>> Eric
>> 
>> 
>> On Fri, Feb 22, 2019 at 9:45 AM Sorkin, John <jsorkin using som.umaryland.edu>
>> wrote:
>> 
>>> I am trying to obtain the coefficients from a regression (performed using
>>> lm). I would like to get the value for the slope (i.e. estimate) for pre
>>> from the following regression:
>>> 
>>> 
>>> fitchange <- lm(post-pre~pre,data=mydata2)
>>> 
>>> 
>>> I have tried the following without any success:
>>> 
>>> 
>>> zz <- summary(fitchange)["coefficients"]
>>> class(zz)
>>> print(zz)
>>> zz[[2,1]]
>>> zz[2,1]
>>> zz["pre","Estimate"]
>>> 
>>> 
>>> I clearly don't know how to select elements from the list returned by the
>>> summary function.
>>> 
>>> 
>>> A reproducible version of my code follows:
>>> 
>>> 
>>> mydata2 <-structure(list(pre = c(71.3302299440613, 86.2703384845455,
>>> 120.941698468568,
>>> 80.9020778388552, 84.9927752038908,
>>> 77.9108032451793, 111.007107108483,
>>> 93.288442414475, 126.097826796255,
>>> 111.63734644637),
>>> post = c(45.9294556667686,
>>> 114.661937978585, 138.501558726477,
>>> 55.355775963925, 97.7906200355594,
>>> 71.1008233796004, 149.308274695789,
>>> 122.828428213951, 143.690814568562,
>>> 116.607579975539)), class = "data.frame",
>>> row.names = c(NA, -10L))
>>> 
>>> fitchange <- lm(post-pre~pre,data=mydata2)
>>> zz <- summary(fitchange)["coefficients"]
>>> class(zz)
>>> print(zz)
>>> zz[[2,1]]
>>> zz[2,1]
>>> zz["pre","Estimate"]
>>> 
>>> 
>>> Any help you can offer would be appreciated.
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> John David Sorkin M.D., Ph.D.
>>> Professor of Medicine
>>> Chief, Biostatistics and Informatics
>>> University of Maryland School of Medicine Division of Gerontology and
>>> Geriatric Medicine
>>> Baltimore VA Medical Center
>>> 10 North Greene Street
>>> GRECC (BT/18/GR)
>>> Baltimore, MD 21201-1524
>>> (Phone) 410-605-7119
>>> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
>>> 
>>> 
>>> [[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>>> 
>> 
>> [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>        [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list