Performs the chi-squared goodness-of-fit test for the particular case of an uniform distribution in the unit interval (by grouping data into bins, y = floor(nclass*x) + 1, and calling chisq.test(y)). Used for testing random number generators.

freq.test(x, nclass = floor(length(x)/10))

Arguments

x

numeric vector containing the observed values (or the pseudorandom numbers).

nclass

number of bins (to partition the unit interval).

Value

A list with class "htest" returned by chisq.test().

Examples

set.rng(321, "lcg", a = 5, c = 1, m = 512)    # set.seed(321)
nsim <- 500
res <- freq.test(rng(nsim))
res # Pearson's Chi-squared test
#> 
#> 	Chi-squared test for given probabilities
#> 
#> data:  rng(nsim)
#> X-squared = 2.2, df = 49, p-value = 1
#> 
# Pearson's Chi-squared test table
sapply(res[c("observed", "expected", "residuals", "stdres")], as.vector)
#>       observed expected  residuals     stdres
#>  [1,]       11       10  0.3162278  0.3194383
#>  [2,]        9       10 -0.3162278 -0.3194383
#>  [3,]       10       10  0.0000000  0.0000000
#>  [4,]       10       10  0.0000000  0.0000000
#>  [5,]       11       10  0.3162278  0.3194383
#>  [6,]       10       10  0.0000000  0.0000000
#>  [7,]        9       10 -0.3162278 -0.3194383
#>  [8,]       10       10  0.0000000  0.0000000
#>  [9,]       10       10  0.0000000  0.0000000
#> [10,]       10       10  0.0000000  0.0000000
#> [11,]       10       10  0.0000000  0.0000000
#> [12,]        9       10 -0.3162278 -0.3194383
#> [13,]       11       10  0.3162278  0.3194383
#> [14,]        9       10 -0.3162278 -0.3194383
#> [15,]       10       10  0.0000000  0.0000000
#> [16,]       10       10  0.0000000  0.0000000
#> [17,]       11       10  0.3162278  0.3194383
#> [18,]       10       10  0.0000000  0.0000000
#> [19,]       10       10  0.0000000  0.0000000
#> [20,]        9       10 -0.3162278 -0.3194383
#> [21,]       11       10  0.3162278  0.3194383
#> [22,]       10       10  0.0000000  0.0000000
#> [23,]        9       10 -0.3162278 -0.3194383
#> [24,]       10       10  0.0000000  0.0000000
#> [25,]       10       10  0.0000000  0.0000000
#> [26,]       11       10  0.3162278  0.3194383
#> [27,]       10       10  0.0000000  0.0000000
#> [28,]       10       10  0.0000000  0.0000000
#> [29,]       10       10  0.0000000  0.0000000
#> [30,]       11       10  0.3162278  0.3194383
#> [31,]       10       10  0.0000000  0.0000000
#> [32,]        9       10 -0.3162278 -0.3194383
#> [33,]        9       10 -0.3162278 -0.3194383
#> [34,]       11       10  0.3162278  0.3194383
#> [35,]       10       10  0.0000000  0.0000000
#> [36,]       10       10  0.0000000  0.0000000
#> [37,]        9       10 -0.3162278 -0.3194383
#> [38,]       11       10  0.3162278  0.3194383
#> [39,]       10       10  0.0000000  0.0000000
#> [40,]       10       10  0.0000000  0.0000000
#> [41,]       10       10  0.0000000  0.0000000
#> [42,]       11       10  0.3162278  0.3194383
#> [43,]       10       10  0.0000000  0.0000000
#> [44,]       10       10  0.0000000  0.0000000
#> [45,]        9       10 -0.3162278 -0.3194383
#> [46,]       11       10  0.3162278  0.3194383
#> [47,]       10       10  0.0000000  0.0000000
#> [48,]        9       10 -0.3162278 -0.3194383
#> [49,]       10       10  0.0000000  0.0000000
#> [50,]       10       10  0.0000000  0.0000000
# Plot observed and expected counts.
plot(res$observed)
abline(h = res$expected[1], lty = 2)