[R] confused about names()

John Aitchison jaitchis at lisp.com.au
Sun Feb 18 03:27:05 CET 2001


Hi all .. there is no doubt a simple answer to this, but it eludes me.

In the first session below ( with jarque.bera.test)  you will see that 
p.value prints with a name of X-squared .

This is easily fixed by changing the source to assign a 
more appropriate name - no name is assigned in the source listing
below (the original source code of jarque.bera.test() from tseries).. but 
what I don't understand is how this (the name of X-squared) has arisen.

In the second session (with Box.test from ts) , p.value prints without
a name as I would expect .. the code looks superficially similar to me, so
obviously I am missing something.

wait a minute  .. names seem to be inherited!  yes. so this is really
more likely a minor glitch in jarque.bera.test due to assigning
    names(STATISTIC) <- "X-squared"
too early, or omitting to assign a name to PVAL

I think I have it now.

thanks 

> 
> jbt<-jarque.bera.test(rnorm(1000))
> jbt$statistic
X-squared 
 1.575782 
> jbt$p.value
X-squared 
0.4548029 
> jbt

         Jarque Bera Test 

data:  rnorm(1000) 
X-squared = 1.5758, df = 2, p-value = 0.4548 

> jarque.bera.test
function (x) 
{
    if (NCOL(x) > 1) 
        stop("x is not a vector or univariate time series")
    if (any(is.na(x))) 
        stop("NAs in x")
    DNAME <- deparse(substitute(x))
    n <- length(x)
    m1 <- sum(x)/n
    m2 <- sum((x - m1)^2)/n
    m3 <- sum((x - m1)^3)/n
    m4 <- sum((x - m1)^4)/n
    b1 <- (m3/m2^(3/2))^2
    b2 <- (m4/m2^2)
    STATISTIC <- n * b1/6 + n * (b2 - 3)^2/24
    names(STATISTIC) <- "X-squared"
    PARAMETER <- 2
    names(PARAMETER) <- "df"
    PVAL <- 1 - pchisq(STATISTIC, df = 2)
    METHOD <- "Jarque Bera Test"
    structure(list(statistic = STATISTIC, parameter = PARAMETER, 
        p.value = PVAL, method = METHOD, data.name = DNAME), 
        class = "htest")
}



================ Box.test session ======================= 
> 
> jbox<-Box.test(rnorm(1000))
> jbox$statistic
X-squared 
 3.609782 
> jbox$p.value
[1] 0.05744064
> jbox

         Box-Pierce test 

data:  rnorm(1000) 
X-squared = 3.6098, df = 1, p-value = 0.05744 

> Box.test
function (x, lag = 1, type = c("Box-Pierce", "Ljung-Box")) 
{
    if (NCOL(x) > 1) 
        stop("x is not a vector or univariate time series")
    DNAME <- deparse(substitute(x))
    type <- match.arg(type)
    cor <- acf(x, lag.max = lag, plot = FALSE)
    n <- length(x)
    PARAMETER <- lag
    obs <- cor$acf[2:(lag + 1)]
    if (type == "Box-Pierce") {
        METHOD <- "Box-Pierce test"
        STATISTIC <- n * sum(obs^2)
        PVAL <- 1 - pchisq(STATISTIC, lag)
    }
    else {
        METHOD <- "Box-Ljung test"
        STATISTIC <- n * (n + 2) * sum(1/seq(n - 1, n - lag) * 
            obs^2)
        PVAL <- 1 - pchisq(STATISTIC, lag)
    }
    names(STATISTIC) <- "X-squared"
    names(PARAMETER) <- "df"
    structure(list(statistic = STATISTIC, parameter = PARAMETER, 
        p.value = PVAL, method = METHOD, data.name = DNAME), 
        class = "htest")
}
> 

================== version info

> R.Version()
$platform
[1] "i386-pc-mingw32"

$arch
[1] "x86"

$os
[1] "Win32"

$system
[1] "x86, Win32"

$status
[1] ""

$major
[1] "1"

$minor
[1] "2.1"

$year
[1] "2001"

$month
[1] "01"

$day
[1] "15"

$language
[1] "R"


John Aitchison
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list