[R] Obtaining values of estimates from a regression; How do I get values from a list?
Marc Schwartz
m@rc_@chw@rtz @end|ng |rom me@com
Fri Feb 22 15:21:25 CET 2019
Hi,
I was just about to reply with coef() when I saw John's reply come through.
Note that this is covered in An Introduction to R:
https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Generic-functions-for-extracting-model-information
If your model object is 'MOD', note that you can use:
coef(MOD)
and
coef(summary(MOD))
depending upon the specific information that you need.
John provides the output of coef(fitchange) below. To contrast that with:
> coef(summary(fitchange))
Estimate Std. Error t value Pr(>|t|)
(Intercept) -54.1010158 32.7217093 -1.653368 0.13685562
pre 0.6557661 0.3332963 1.967517 0.08466821
These extractor functions are also covered in some of the "See Also" sections of the help pages for the modeling functions such as ?lm.
A key advantage of using the extractor functions, is that they are independent of the underlying object structure. So, while it may be unlikely that the structure of these objects would change in the future, that is not guaranteed. Thus, using the extractor functions is more "future-proof" than directly accessing object components directly.
Regards,
Marc Schwartz
> On Feb 22, 2019, at 8:56 AM, Fox, John <jfox using mcmaster.ca> wrote:
>
> 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)
>>>>
More information about the R-help
mailing list