Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the output of the summary() command if it is called within a loop?

Tags:

r

Suppose Z is a vector of feature names.

How can I get the summary command in the following Rscript to actually print?

for (var in Z)                
{                             
#cat(i)                       
form = paste('crim ~', var)   
lm.fit=lm(form, data=Boston)  
summary(lm.fit)               
}                             

If I type summary(lm.fit) at the R prompt, it works, but when I source the Rscript which contains this loop, I get no output. I have already tried the solution How can I run an 'R' script without suppressing output? but it does not cause summary to print.

like image 512
merlin2011 Avatar asked Oct 18 '25 13:10

merlin2011


1 Answers

summary() is supposed to return a object of class "summary.foo" assuming that the summary.foo() method was called. Then a print() method for that class, print.summary.foo() is supposed to print the object returned by summary.foo().

Automatic printing is turned off in for () loops (and some other circumstances. You need an explicit call to print(). When you call summary(bar) at the prompt, you are effectively doing print(summary(bar)). It is that addition of the print() call that is suppressed in for () loops.

Hence, write

print(summary(lm.fit))

in your loop and you'll see the output.

like image 144
Gavin Simpson Avatar answered Oct 21 '25 03:10

Gavin Simpson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!