[R] data structure with coefficients, and call from lm()
P Ehlers
ehlers at math.ucalgary.ca
Mon Oct 1 09:22:55 CEST 2007
John,
Jim has shown how to accomplish what you want.
Here's a slight variation (for a single model):
y <- rnorm(20)
x <- runif(20)
z <- runif(20)
fm <- lm(y ~ x + z)
m <- cbind(NA, coef(summary(fm)))
colnames(m)[1] <- deparse(formula(fm))
print(m, na.print = "")
- Peter Ehlers
jim holtman wrote:
> This is close to what you want. I created the list by hand, but you
> can create it in your processing loop. Once you have the list
> created, you can create your own print routine.
>
>> x <- runif(100)
>> z <- runif(100)
>> y <- runif(100)
>>
>>
>> # I am doing this by hand, but you could easily automate it in a loop or function call
>> whatIwant <- list() # where results are stored
>>
>> lm.x <- lm(y~x)
>> whatIwant[[1]] <- list(summary(lm.x)$call, summary(lm.x)$coefficients)
>>
>> lm.z <- lm(y~z)
>> whatIwant[[2]] <- list(summary(lm.z)$call, summary(lm.z)$coefficients)
>>
>> lm.xz <- lm(y~x+z)
>> whatIwant[[3]] <- list(summary(lm.xz)$call, summary(lm.xz)$coefficients)
>>
>> # this is close
>> whatIwant
> [[1]]
> [[1]][[1]]
> lm(formula = y ~ x)
>
> [[1]][[2]]
> Estimate Std. Error t value Pr(>|t|)
> (Intercept) 0.46211645 0.05442237 8.4912958 2.244835e-13
> x 0.02387787 0.09709648 0.2459190 8.062592e-01
>
>
> [[2]]
> [[2]][[1]]
> lm(formula = y ~ z)
>
> [[2]][[2]]
> Estimate Std. Error t value Pr(>|t|)
> (Intercept) 0.49612568 0.05390759 9.2032627 6.488440e-15
> z -0.04600174 0.09298692 -0.4947119 6.219108e-01
>
>
> [[3]]
> [[3]][[1]]
> lm(formula = y ~ x + z)
>
> [[3]][[2]]
> Estimate Std. Error t value Pr(>|t|)
> (Intercept) 0.48617771 0.07502099 6.4805555 3.804443e-09
> x 0.01880206 0.09808697 0.1916877 8.483876e-01
> z -0.04400913 0.09402369 -0.4680643 6.407886e-01
>
>
>> #now create a function to print your list
>> printMyList <-
> + function(myList){
> + lapply(myList, function(.ele){
> + print(.ele[[1]])
> + print(.ele[[2]])
> + })
> + invisible(NULL)
> + }
>> printMyList(whatIwant)
> lm(formula = y ~ x)
> Estimate Std. Error t value Pr(>|t|)
> (Intercept) 0.46211645 0.05442237 8.4912958 2.244835e-13
> x 0.02387787 0.09709648 0.2459190 8.062592e-01
> lm(formula = y ~ z)
> Estimate Std. Error t value Pr(>|t|)
> (Intercept) 0.49612568 0.05390759 9.2032627 6.488440e-15
> z -0.04600174 0.09298692 -0.4947119 6.219108e-01
> lm(formula = y ~ x + z)
> Estimate Std. Error t value Pr(>|t|)
> (Intercept) 0.48617771 0.07502099 6.4805555 3.804443e-09
> x 0.01880206 0.09808697 0.1916877 8.483876e-01
> z -0.04400913 0.09402369 -0.4680643 6.407886e-01
>>
>
>
> On 9/30/07, John Sorkin <jsorkin at grecc.umaryland.edu> wrote:
>> Jim,
>> You are indeed trying to help, again my thanks.
>> What I want to do is make a single structure - a table is an apt description that will summarize all the regressions, something like:
>>
>> Estimate Std. Error t value Pr(>|t|)
>> (Intercept) lm(formula = y ~ x) 4.927791 2.62115494 1.880007 6.307727e-02
>> x 1.887634 0.04372724 43.168382 1.410167e-65
>> (Intercept) lm(formula = z ~ x) 6.927791 2.62115494 1.880007 6.307727e-02
>> x 1.887634 0.04372724 43.168382 1.410167e-65
>> (Intercept) lm(formula = z~x+z) 6.927791 2.62115494 1.880007 6.307727e-02
>> x 1.887634 0.04372724 43.168382 1.410167e-65
>> z 1.887634 0.04372724 43.168382 1.410167e-65
>>
>> If you use a non-proportional spaced font (e.g. Courier on a windows system)
>> the material above is a single "table" that contains all the data from my numerous regressions, including the call and the coefficients.(If I can get
>> this to work I will add the R squared values) n.b. In the example above I have
>> copied the estimates and changed the label of the lines just to demonstrate the
>> kind of output I desire. Of course when used on real data each line will have
>> different values.
>> Again thanks,
>> John
>>
>>
>> John Sorkin M.D., Ph.D.
>> Chief, Biostatistics and Informatics
>> University of Maryland School of Medicine Division of Gerontology
>> 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)
>>
>>>>> "jim holtman" <jholtman at gmail.com> 9/30/2007 11:24 PM >>>
>> The easiest thing is to same the result from 'lm' and then you can use
>> that to extract any of the information that you want. If you are
>> going to do thousands of regressions, then you can make a list of 'lm'
>> results, or if that is too memory intensive (depends on the size of
>> the regressions), you could same them to a file with 'save'. It all
>> depends on what you want to do with them. One of my favorite
>> questions is "tell me what you want to do, not how you want to do it".
>> What is the intended purpose?
>>
>> On 9/30/07, John Sorkin <jsorkin at grecc.umaryland.edu> wrote:
>>> Jim,
>>> Again thank you for your quick reply. Your suggestion does not give me exactly what I want:
>>>
>>>> whatIwant<-list(,summary(fitdelete)$call,summary(fitdelete)$coefficients)
>>>> whatIwant
>>> [[1]]
>>> lm(formula = y ~ x)
>>>
>>> [[2]]
>>> Estimate Std. Error t value Pr(>|t|)
>>> (Intercept) 4.927791 2.62115494 1.880007 6.307727e-02
>>> x 1.887634 0.04372724 43.168382 1.410167e-65
>>>
>>> What I want is something that looks more like a table:
>>>
>>> lm(formula = y ~ x) Estimate Std. Error t value Pr(>|t|)
>>> (Intercept) 4.927791 2.62115494 1.880007 6.307727e-02
>>> x 1.887634 0.04372724 43.168382 1.410167e-65
>>>
>>> Thanks,
>>> John
>>>
>>>
>>>
>>> John Sorkin M.D., Ph.D.
>>> Chief, Biostatistics and Informatics
>>> University of Maryland School of Medicine Division of Gerontology
>>> 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)
>>>
>>>>>> "jim holtman" <jholtman at gmail.com> 9/30/2007 11:00 PM >>>
>>> try using a 'list':
>>>
>>> whatIwant<-list(call=summary(myreg)$call, coef=summary(myreg)$coefficients)
>>>
>>> On 9/30/07, John Sorkin <jsorkin at grecc.umaryland.edu> wrote:
>>>> Widows XP
>>>> R 2.3.1
>>>>
>>>> I have been trying to make a data structure that will contain both the coefficients from a linear regression along with column and row titles AND the call, i.e.
>>>> myreg<-lm(y~x+y+z)
>>>> whatIwant<-cbind(c(summary(myreg)$call,"",""),summary(myreg)$coefficients)
>>>>
>>>> Neither the statement above, nor any one of twenty variations I have tried work. I would appreciate any advice.
>>>> Thanks,
>>>> John
>>>>
>>>>
>>>>
>>>>
>>>> John Sorkin M.D., Ph.D.
>>>> Chief, Biostatistics and Informatics
>>>> University of Maryland School of Medicine Division of Gerontology
>>>> 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)
>>>>
>>>> Confidentiality Statement:
>>>> This email message, including any attachments, is for the so...{{dropped}}
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org 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.
>>>>
>>>
>>> --
>>> Jim Holtman
>>> Cincinnati, OH
>>> +1 513 646 9390
>>>
>>> What is the problem you are trying to solve?
>>>
>>> Confidentiality Statement:
>>> This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
>>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem you are trying to solve?
>>
>> Confidentiality Statement:
>> This email message, including any attachments, is for the ...{{dropped}}
>
> ______________________________________________
> R-help at r-project.org 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.
>
>
--
Peter Ehlers
Director, StatCaR (Statistical Consulting and Research)
Department of Mathematics and Statistics
University of Calgary, 2500 University Dr. NW ph: 403-220-3936
Calgary, Alberta T2N 1N4, CANADA fax: 403-282-5150
email: ehlers at math.ucalgary.ca
More information about the R-help
mailing list