Dear R users, I have a function (below) which encompasses several tests. However, when I run it, only the output of the last test is displayed. How can I ensure that the function root(var) will run and display the output from all tests, and not just the last one? Thank you, b. root <- function(var) { #---Phillips-Perron PP.test(var, lshort = TRUE) PP.test(var, lshort = FALSE) #---Augmented Dickey-Fuller adf.test(var, alternative = "stationary", k trunc((length(var)-1)^(1/3))) #---KPSS kpss.test(var, null = "Level", lshort = TRUE) kpss.test(var, null = "Trend", lshort = FALSE) }
I would return the values from the various tests in a list. If you only want them to print and not for use in other parts of your program you could explicitly print each test using print(). root <- function(var) { #---Phillips-Perron test1 <- PP.test(var, lshort = TRUE) test2 <- PP.test(var, lshort = FALSE) #---Augmented Dickey-Fuller test3 <- adf.test(var, alternative = "stationary", k test4 <- trunc((length(var)-1)^(1/3))) #---KPSS test5 <- kpss.test(var, null = "Level", lshort = TRUE) test6 <- kpss.test(var, null = "Trend", lshort = FALSE) list(test1, test2, test3, test4, test5, test6) } --Matt -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of bogdan romocea Sent: Wednesday, October 13, 2004 10:20 AM To: r-help at stat.math.ethz.ch Subject: [R] incomplete function output Dear R users, I have a function (below) which encompasses several tests. However, when I run it, only the output of the last test is displayed. How can I ensure that the function root(var) will run and display the output from all tests, and not just the last one? Thank you, b. root <- function(var) { #---Phillips-Perron PP.test(var, lshort = TRUE) PP.test(var, lshort = FALSE) #---Augmented Dickey-Fuller adf.test(var, alternative = "stationary", k trunc((length(var)-1)^(1/3))) #---KPSS kpss.test(var, null = "Level", lshort = TRUE) kpss.test(var, null = "Trend", lshort = FALSE) } ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Only the last appears, because the function returns only the last, which then gets printed. The answer to your question depends on whether you want to see the results or store them. To see them, try the following: root.print <- function(var) { #---Phillips-Perron print(PP.test(var, lshort = TRUE) ) print(PP.test(var, lshort = FALSE) ) #---Augmented Dickey-Fuller print(adf.test(var, alternative = "stationary", k trunc((length(var)-1)^(1/3)))) #---KPSS print(kpss.test(var, null = "Level", lshort = TRUE)) print(kpss.test(var, null = "Trend", lshort = FALSE)) } To store the results, try: root.store <- function(var) { #---Phillips-Perron t1 <- PP.test(var, lshort = TRUE) t2 <- PP.test(var, lshort = FALSE) #---Augmented Dickey-Fuller t3 <- adf.test(var, alternative = "stationary", k trunc((length(var)-1)^(1/3))) #---KPSS t4 <- kpss.test(var, null = "Level", lshort = TRUE) t5 <- kpss.test(var, null = "Trend", lshort = FALSE) list(PP.testT=t1, PP.testF=t2, adf.test=t3, kpss.testT=t4, kpss.testF=t5) } hope this helps. spencer graves bogdan romocea wrote:>Dear R users, > >I have a function (below) which encompasses several tests. >However, when I run it, only the output of the last test is >displayed. How can I ensure that the function root(var) >will run and display the output from all tests, and not >just the last one? > >Thank you, >b. > >root <- function(var) >{ >#---Phillips-Perron >PP.test(var, lshort = TRUE) >PP.test(var, lshort = FALSE) > >#---Augmented Dickey-Fuller >adf.test(var, alternative = "stationary", k >trunc((length(var)-1)^(1/3))) > >#---KPSS >kpss.test(var, null = "Level", lshort = TRUE) >kpss.test(var, null = "Trend", lshort = FALSE) >} > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >-- Spencer Graves, PhD, Senior Development Engineer O: (408)938-4420; mobile: (408)655-4567
bogdan romocea wrote:> Dear R users, > > I have a function (below) which encompasses several tests. > However, when I run it, only the output of the last test is > displayed. How can I ensure that the function root(var) > will run and display the output from all tests, and not > just the last one? > > Thank you, > b. > > root <- function(var) > { > #---Phillips-Perron > PP.test(var, lshort = TRUE) > PP.test(var, lshort = FALSE) > > #---Augmented Dickey-Fuller > adf.test(var, alternative = "stationary", k > trunc((length(var)-1)^(1/3))) > > #---KPSS > kpss.test(var, null = "Level", lshort = TRUE) > kpss.test(var, null = "Trend", lshort = FALSE) > } >You should store all your results in a list and return the list: root <- function(var) { # create empty list ret <- list() #---Phillips-Perron ret[[1]] <- PP.test(var, lshort = TRUE) ret[[2]] <- PP.test(var, lshort = FALSE) #---Augmented Dickey-Fuller ret[[3]] <- adf.test(var, alternative = "stationary", k = trunc((length(var)-1)^(1/3))) #---KPSS ret[[4]] <- kpss.test(var, null = "Level", lshort = TRUE) ret[[5]] <- kpss.test(var, null = "Trend", lshort = FALSE) # give `ret' some meaningful names names(ret) <- c("PP1", "PP2", "ADF", "KPSS1", "KPSS2") # return list ret } results <- root(somevar) HTH, --sundar P.S. Also note my indenting which makes code more readable, especially if you expect other to try to read it.
You can return a list of them all root <- function(var) { return(list(test1 = PP.test(var, lshort = T), test2 = ...)) } output <- root(var) Then you can print them, save them, or whatever. Or you could just print them out in the function root <- function(var) { #---Phillips-Perron print(PP.test(var, lshort = TRUE)) print(PP.test(var, lshort = FALSE)) . . . } Randy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Randy Johnson [Contr.] Laboratory of Genomic Diversity |\ NCI Frederick ___lll__/| |\ () (301)846-1304 (_|||_)\| () ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of bogdan romocea Sent: Wednesday, October 13, 2004 1:20 PM To: r-help at stat.math.ethz.ch Subject: [R] incomplete function output Dear R users, I have a function (below) which encompasses several tests. However, when I run it, only the output of the last test is displayed. How can I ensure that the function root(var) will run and display the output from all tests, and not just the last one? Thank you, b. root <- function(var) { #---Phillips-Perron PP.test(var, lshort = TRUE) PP.test(var, lshort = FALSE) #---Augmented Dickey-Fuller adf.test(var, alternative = "stationary", k trunc((length(var)-1)^(1/3))) #---KPSS kpss.test(var, null = "Level", lshort = TRUE) kpss.test(var, null = "Trend", lshort = FALSE) } ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html