I have two questions about R: 1) Does anyone knows why I get the plots but not the summary results of the linear fitting when I run this code? When I run it outside the if condition it works fine. if (op==4) { t=1:length(time(SeaIce)) fitlinear=lm(SeaIce~t) windows() plot.ts(SeaIce) lines(ts(fitlinear$fitted.values,start=1979,frequency=12)) summary(fitlinear) fitlinear=ts(fitlinear$fitted.values,start=1979,frequency=12) c=cos(2*pi*t/12) s=sin(2*pi*t/12) fittotal=lm(SeaIce~t+c+s) summary(fittotal) windows() par(mfrow=c(2,1)) plot(t,SeaIce) lines(fittotal$fit, col=2) plot(t,fittotal$resid) windows() acf(fittotal$resid) } 2) Why does't this work? I get the message "I'here" ten times but the AIC results, none. for (i in 1:10) { f_arima = arima(SeaIce_STL_Remain,order=c(i,0,0)) AIC(f_arima) Message("I?here") } Thanks in advance, Luis Antunes -- View this message in context: http://www.nabble.com/if-condition-and-for-cycles-tf4882403.html#a13972845 Sent from the R help mailing list archive at Nabble.com.
Output from an R command is printed to the console ***only*** if the command is issued *directly* from the command line. Otherwise the output is invisible. This includes unassigned output from commands issued ***inside functions***. And, finally, functions include things like ``if'' and for loops. If you want such output to be visibly displayed you must wrap it in an explicit print statement. In short you can solve your problems by wrapping your summary commands inside print statements. E.g. instead of summary(fitlinear) write print(summary(fitlinear)) cheers, Rolf Turner On 28/11/2007, at 7:43 AM, ljantunes wrote:> > I have two questions about R: > > 1) Does anyone knows why I get the plots but not the summary > results of the > linear fitting when I run this code? When I run it outside the if > condition > it works fine. > > if (op==4) { > t=1:length(time(SeaIce)) > fitlinear=lm(SeaIce~t) > windows() > plot.ts(SeaIce) > lines(ts(fitlinear$fitted.values,start=1979,frequency=12)) > summary(fitlinear) > fitlinear=ts(fitlinear$fitted.values,start=1979,frequency=12) > c=cos(2*pi*t/12) > s=sin(2*pi*t/12) > fittotal=lm(SeaIce~t+c+s) > summary(fittotal) > windows() > par(mfrow=c(2,1)) > plot(t,SeaIce) > lines(fittotal$fit, col=2) > plot(t,fittotal$resid) > windows() > acf(fittotal$resid) > } > > 2) Why does't this work? I get the message "I'here" ten times but > the AIC > results, none. > > > for (i in 1:10) { > f_arima = arima(SeaIce_STL_Remain,order=c(i,0,0)) > AIC(f_arima) > Message("I?here")Surely this must be ``message("I'here")'' --- R is case sensitive!!! Attention to detail is vital. Loose lips sink ships! :-)> } > > Thanks in advance, > Luis Antunes###################################################################### Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ######################################################################
In both cases the problem is that you didn't print() the results. This is a similar issue to FAQ 7.16, which discusses the problem in more detail. -thomas On Tue, 27 Nov 2007, ljantunes wrote:>I have two questions about R: 1) Does anyone knows why I get the plots but not the summary results of the linear fitting when I run this code? When I run it outside the if condition it works fine. if (op==4) { t=1:length(time(SeaIce)) fitlinear=lm(SeaIce~t) windows() plot.ts(SeaIce) lines(ts(fitlinear$fitted.values,start=1979,frequency=12)) summary(fitlinear) fitlinear=ts(fitlinear$fitted.values,start=1979,frequency=12) c=cos(2*pi*t/12) s=sin(2*pi*t/12) fittotal=lm(SeaIce~t+c+s) summary(fittotal) windows() par(mfrow=c(2,1)) plot(t,SeaIce) lines(fittotal$fit, col=2) plot(t,fittotal$resid) windows() acf(fittotal$resid) } 2) Why does't this work? I get the message "I'here" ten times but the AIC results, none. for (i in 1:10) { f_arima = arima(SeaIce_STL_Remain,order=c(i,0,0)) AIC(f_arima) Message("I??here") } Thanks in advance, Luis Antunes -- View this message in context: http://www.nabble.com/if-condition-and-for-cycles-tf4882403.html#a13972845 Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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. Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
Thanks. It did solve the problem. Luis -- View this message in context: http://www.nabble.com/if-condition-and-for-cycles-tf4882403.html#a13982960 Sent from the R help mailing list archive at Nabble.com.
Compare the evaluation of for (i in 1:10) i to for (i in 1:10) print(i) See also http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-is-the-output-not-printed-when-I-source_0028_0029-a-file_003f Best regards Frede Aakmann T?gersen Scientist UNIVERSITY OF AARHUS Faculty of Agricultural Sciences Dept. of Genetics and Biotechnology Blichers All? 20, P.O. BOX 50 DK-8830 Tjele Phone: +45 8999 1900 Direct: +45 8999 1878 E-mail: FredeA.Togersen at agrsci.dk Web: http://www.agrsci.org This email may contain information that is confidential. Any use or publication of this email without written permission from Faculty of Agricultural Sciences is not allowed. If you are not the intended recipient, please notify Faculty of Agricultural Sciences immediately and delete this email.> -----Oprindelig meddelelse----- > Fra: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] P? vegne af ljantunes > Sendt: 27. november 2007 19:44 > Til: r-help at r-project.org > Emne: [R] if condition and for cycles > > > I have two questions about R: > > 1) Does anyone knows why I get the plots but not the summary > results of the linear fitting when I run this code? When I > run it outside the if condition it works fine. > > if (op==4) { > t=1:length(time(SeaIce)) > fitlinear=lm(SeaIce~t) > windows() > plot.ts(SeaIce) > lines(ts(fitlinear$fitted.values,start=1979,frequency=12)) > summary(fitlinear) > fitlinear=ts(fitlinear$fitted.values,start=1979,frequency=12) > c=cos(2*pi*t/12) > s=sin(2*pi*t/12) > fittotal=lm(SeaIce~t+c+s) > summary(fittotal) > windows() > par(mfrow=c(2,1)) > plot(t,SeaIce) > lines(fittotal$fit, col=2) > plot(t,fittotal$resid) > windows() > acf(fittotal$resid) > } > > 2) Why does't this work? I get the message "I'here" ten times > but the AIC results, none. > > > for (i in 1:10) { > f_arima = arima(SeaIce_STL_Remain,order=c(i,0,0)) > AIC(f_arima) > Message("I?here") > } > > Thanks in advance, > Luis Antunes > > -- > View this message in context: > http://www.nabble.com/if-condition-and-for-cycles-tf4882403.html#a13972845> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >