[R] Working with Numbers generated from Regression Output

David Winsemius dwinsemius at comcast.net
Fri Jul 27 18:48:34 CEST 2012


On Jul 27, 2012, at 3:10 AM, Krunal Nanavati wrote:

> Hi David,
>
> Thanks for the reply.
>
> With your first alternative, I am getting the beta values in different
> cells in the excel file.
>
> Is there a way to get all the information generated by the summary
> function in different cells in a excel file, through the write  
> function?

The summary function returns a complex list object. You can see that  
list structure with this code:

#Using the example in help(lm)
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2,10,20, labels=c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D90 <- lm(weight ~ group - 1) # omitting intercept

str(summary(lm.D90))

What you see at the console whaen you type summary(lm.D90) is somewhat  
different. It is the result of another function, print.summary(). It  
is not a visible function so when you type its name you get:

 > print.summary.lm
Error: object 'print.summary.lm' not found

But it has to be there and I took an educated guess that it was in the  
stats package and can see it by using the triple-colon infix function:

stats:::print.summary.lm

You could wrap 'as.character' around the summary object:

as.character( summary(lm.D90))

I'm guessing you do not really want all of that complexity and would  
probably be better served by learning the proper extractor functions  
for regression or summary.lm arguments linked from the help(lm) page  
in the "See Also" section;

?lm
?coef
?vcov
?fitted
?residuals
?terms

Notice that the extractor functions will return different objects from  
the model object than they do from the summary object:

 > coef( lm.D90)
groupCtl groupTrt
    5.032    4.661

 > coef( summary(lm.D90) )
          Estimate Std. Error  t value     Pr(>|t|)
groupCtl    5.032  0.2202177 22.85012 9.547128e-15
groupTrt    4.661  0.2202177 21.16542 3.615345e-14

>
> Also, can you please elaborate on your third option. I went into the  
> data
> tab in excel, and chose from text option, as I had pasted the console
> output of regression in a text file. But by doing this I am getting  
> all
> the output in a single cell in excel.

As an example, copy the output of the three lines following the  
console call to "coef( summary(lm.D90) )" into an Excel spreadsheet.  
If the copied three cells should remain highlighted. Now click the  
Data menu and pick the "Text to Columns... " item and accept the  
defaults in  the which should be "fixed" in the fist dialog,  and the  
correct locations for the cell splits in the next ones.  You should  
now get the results split into cells. Sometimes you need to adjust the  
guesses that the program makes for where you want splits to occur, but  
the little fixed-import dialog is fairly handy, so you should learn  
it. But ... further such questions are off-topic for this list, and  
you should be getting you instructions on an Excel forum.

Also read:

http://rwiki.sciviews.org/doku.php?id=tips:data-io:ms_windows

(Note: I'm not a regular user of R or Excel on Windows, but I believe  
the actions on the  Excel for Mac 2011 should very similar to the  
behavior I dimly remember in the Windows version. I'm also old enough  
to remember the several years when Excel only existed on the Mac, but  
in those years I was using GLIM.)

-- 
David

>
> Thanks for your time.
>
> Thanks & Regards,
>
> Krunal Nanavati
> 9769-919198
>
>
> -----Original Message-----
> From: David Winsemius [mailto:dwinsemius at comcast.net]
> Sent: 27 July 2012 14:41
> To: Krunal Nanavati
> Cc: Jeff Newmiller; Jean V Adams; r-help at r-project.org
> Subject: Re: [R] Working with Numbers generated from Regression Output
>
>
> On Jul 27, 2012, at 12:14 AM, Krunal Nanavati wrote:
>
>> Hi Jeff,
>>
>> Sorry for the previous email.
>>
>> I tried using write function, and used the following syntax
>>
>> write(result,file="C:\\Users\\Krunal\\Desktop\\Book1.csv")
>>
>> but it is giving the following error
>>
>> Error in cat(list(...), file, sep, fill, labels, append) :
>> argument 1 (type 'list') cannot be handled by 'cat'
>>
>> Can you tell me where I am going wrong
>
> (First off, we have no wat y=to know what result is. I'm guessing  
> its an
> lm-object.
>
> If that's correct, then you could try:
>
>   write.csv( coef(result), file="C:\\Users\\Krunal\\Desktop\
> \Book1.csv")
>
> It is designed to write dataframes, but a simple list or vector of
> coefficients sould get written (after coercion).
>
> I think you can also do this (in Windows)
>
>   write.csv( coef(result), file="clipboard")  # and then paste into  
> Excel
>
> Excel doesn't really have a corresponding data structure to a named
> vector, so you won't get the names if you go the second route.
>
> And finally, Excel has a /Data/Text to Columns facility that is  
> useful for
> turning console output into columnar data. Choose the fixed format  
> menu.
>
> -- 
> David.
>
>>
>>
>>
>>
>> Thanks & Regards,
>>
>> Krunal Nanavati
>> 9769-919198
>>
>> -----Original Message-----
>> From: Jeff Newmiller [mailto:jdnewmil at dcn.davis.CA.us]
>> Sent: 27 July 2012 12:11
>> To: Krunal Nanavati; Jean V Adams
>> Cc: r-help at r-project.org
>> Subject: Re: [R] Working with Numbers generated from Regression  
>> Output
>>
>> Stop posting HTML. What you see is NOT what we see.
>>
>> As regards to your problems... you need to learn how to get data
>> into and
>> out of R, so please read the R Input/Output document supplied with
>> R. The
>> most foolproof way is to write the data to a CSV file and read it
>> from there
>> into a spreadsheet. Depending on your operating system you may be
>> able to
>> write into a clipboard for more convenience.
>>
>> As to your goal of making predictions, with only a few more steps
>> you can
>> make those predictions using R. See the examples in the help for
>> predict (
>> type "?predict.lm" without the quotes).
>>
> --------------------------------------------------------------------------
> -
>> Jeff Newmiller                        The     .....       .....  Go
>> Live...
>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.
>> Live Go...
>>                                     Live:   OO#.. Dead: OO#..
>> Playing
>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>> /Software/Embedded Controllers)               .OO#.       .OO#.
>> rocks...1k
>>
> --------------------------------------------------------------------------
> -
>> Sent from my phone. Please excuse my brevity.
>>
>> Krunal Nanavati <krunal.nanavati at cogitaas.com> wrote:
>>
>>> Hi Jean,
>>>
>>>
>>>
>>> Thank you very much for getting back to me.
>>>
>>>
>>>
>>> I tried the solutions that you have provided.
>>>
>>>
>>>
>>> First I tried the  coef(result) statement .and I got the below  
>>> output
>>>
>>>
>>>
>>>> coef(result)
>>>
>>> (Intercept)            X       Volume
>>>
>>> -30.40275264   0.57786290   0.02594024
>>>
>>>
>>>
>>> Then, I simply selected this output from the R window, and pasted it
>>> into an Excel Sheet, and it go pasted in the below manner.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> (Intercept)            X       Volume
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> -30.40275264   0.57786290   0.02594024
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> So, here all the output is getting pasted in a single cell. What I  
>>> am
>>> looking for is something different.
>>>
>>>
>>>
>>> Here is what I am looking for
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> (Intercept)
>>>
>>> X
>>>
>>> Volume
>>>
>>>
>>>
>>>
>>>
>>> -30.40275264
>>>
>>> 0.57786292
>>>
>>> 0.02594024
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> So, once the beta values are placed in different cells, I can work  
>>> on
>>> those numbers individually to calculate elasticities.
>>>
>>>
>>>
>>> I tried with the other statements as well, but they are not
>>> addressing
>>> this issue.
>>>
>>>
>>>
>>> Can you please help me out with this. I really appreciate your time
>>> and
>>> effort.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> Thanks & Regards,
>>>
>>>
>>>
>>> Krunal Nanavati
>>>
>>> 9769-919198
>>>
>>>
>>>
>>> *From:* Jean V Adams [mailto:jvadams at usgs.gov]
>>> *Sent:* 26 July 2012 20:53
>>> *To:* Krunal Nanavati
>>> *Cc:* r-help at r-project.org
>>> *Subject:* Re: [R] Working with Numbers generated from Regression
>>> Output
>>>
>>>
>>>
>>> You can learn a lot from the help files.  Check out the help files
>>> for
>>> the
>>> lm() and summary.lm() functions
>>>
>>> ?lm
>>> ?summary.lm
>>>
>>> You can extract the beta values in a few different ways.
>>> These two will give you just the estimates in a vector:
>>>
>>> coef(result)
>>> result$coef
>>>
>>> These two will give you the estimates and more in a matrix:
>>>
>>> coef(summary(result))
>>> summary(result)$coef
>>>
>>> Jean
>>>
>>>
>>> Krunal Nanavati <krunal.nanavati at cogitaas.com> wrote on 07/26/2012
>>> 07:28:02
>>> AM:
>>>>
>>>> Hi,
>>>>
>>>> I have a query on regression output generated by R.
>>>>
>>>>> result=lm( Y~X , data=trail)
>>>>> summary(result)
>>>>
>>>> After running this 2 statements the following output is generated.
>>>>
>>>> Call:
>>>> lm(formula = Y ~ X, data = trail)
>>>>
>>>> Residuals:
>>>>   Min      1Q  Median      3Q     Max
>>>> -245.30  -90.77  -30.30   54.99  532.78
>>>>
>>>> Coefficients:
>>>>           Estimate Std. Error t value Pr(>|t|)
>>>> (Intercept) 245.2982    62.1307   3.948 0.000376 ***
>>>> X             0.5192     0.1752   2.963 0.005533 **
>>>> ---
>>>> Signif. codes:  0  ***  0.001  **  0.01  *  0.05  .  0.1     1
>>>>
>>>> Residual standard error: 169.1 on 34 degrees of freedom
>>>> Multiple R-squared: 0.2052,     Adjusted R-squared: 0.1818
>>>> F-statistic: 8.777 on 1 and 34 DF,  p-value: 0.005533
>>>>
>>>> From this output, I intend to use the beta values to calculate
>>>> elasticities. Is this possible directly in R?
>>>>
>>>> If not, then when I paste this output in Excel, It is pasted as an
>>> image,
>>>> and thus I cannot use the beta values for calculating any other
>>> metric.
>>>>
>>>> Can anyone please help me out!!!
>>>>
>>>> Thanks & Regards,
>>>>
>>>> Krunal Nanavati
>>>> 9769-919198
>>>
>>> 	[[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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.
>>
>> ______________________________________________
>> 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.
>
> David Winsemius, MD
> Alameda, CA, USA

David Winsemius, MD
Alameda, CA, USA



More information about the R-help mailing list