Skip to content

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

4 messages · Sorkin, John, Eric Berger, Ivan Calandra

#
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)
#
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 at som.umaryland.edu>
wrote:

  
  
#
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 at gmail.com> 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)