Skip to content

Commit

Permalink
Created additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Nov 22, 2013
1 parent 7b78f2d commit fb3b49a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
34 changes: 18 additions & 16 deletions inst/tests/test-rxF1score.R
@@ -1,38 +1,40 @@
# Test F1 score
### Test F1 score
rxOptions(reportProgress=0)

context("F1 score")


# ------------------------------------------------------------------------

test_that("rxF1score works with data frame",{
dat <- data.frame(iris[, -5], Virg = iris$Species == "virginica")
fit <- suppressWarnings(
rxLogit(Virg ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
dat)
)
prd <- rxPredict(fit, dat)$Virg_Pred
cmb <- data.frame(Virg=dat$Virg, Pred=prd)
tst <- rxF1score("Virg", "Pred", cmb)
dat <- data.frame(iris[, -5], Vers = iris$Species == "versicolor")
fit <- rxLogit(Vers ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, dat)
prd <- rxPredict(fit, dat)$Vers_Pred
cmb <- data.frame(Vers=dat$Vers, Pred=prd)
tst <- rxF1score("Vers", "Pred", cmb)

expect_is(tst, "list")
expect_equal(names(tst), c("precision", "recall", "trueNegRate", "accuracy", "F1"))
expect_equivalent(unname(unlist(tst)), c(0.98, 0.98, 0.99, 0.98666667, 0.98))
expect_equal(unname(unlist(tst)), c(0.641025641025641, 0.5, 0.86, 0.74, 0.561797752808989))
})


# ------------------------------------------------------------------------

test_that("rxF1score works with XDF",{
dataFile <- tempfile(pattern = ".data", fileext = ".xdf")
dat <- data.frame(iris[, -5], Virg = iris$Species == "virginica")
dat <- data.frame(iris[, -5], Vers = iris$Species == "versicolor")
rxDataStep(dat, outFile=dataFile, rowsPerRead=50)
fit <- suppressWarnings(
rxLogit(Virg ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
rxLogit(Vers ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
dat)
)
prd <- rxPredict(fit, dat)$Virg_Pred
cmb <- data.frame(Virg=dat$Virg, Pred=prd)
tst <- rxF1score("Virg", "Pred", cmb)
prd <- rxPredict(fit, dat)$Vers_Pred
cmb <- data.frame(Vers=dat$Vers, Pred=prd)
tst <- rxF1score("Vers", "Pred", cmb)

expect_is(tst, "list")
expect_equal(names(tst), c("precision", "recall", "trueNegRate", "accuracy", "F1"))
expect_equivalent(unname(unlist(tst)), c(0.98, 0.98, 0.99, 0.98666667, 0.98))
expect_equal(unname(unlist(tst)), c(0.641025641025641, 0.5, 0.86, 0.74, 0.561797752808989))
})

12 changes: 7 additions & 5 deletions inst/tests/test-rxLinPredError.R
@@ -1,26 +1,28 @@
# Test For the rxLinPredError() function
rxOptions(reportProgress=0)

context("MSE, MAPE, MPE, MSWD")
context("rxLinPredError")

test_that("rxLinPredError works with data frame",{
dat <- data.frame(actual = c(1.264896, 1.964210, 2.671872, 3.838703, 5.252300),
pred = 1:5, weights = rep(1, 5))
tst <- rxLinPredError("actual", "pred", dat, "weights")
tst <- rxLinPredError("actual", "pred", dat, "weights", reportProgress=0)
expect_is(tst, "list")
expect_equal(names(tst), c("MAPE", "MPE", "MSE", "MSWD"))
expect_equivalent(unname(unlist(tst)), c(0.08810105, -0.01488186, 0.05375816, 1.07516325))
})

test_that("rxLinPredError works with XDF",{
dataFile <- tempfile(pattern = ".data", fileext = ".xdf")
on.exit({
file.remove(dataFile)
})
dat <- data.frame(actual = c(1.264896, 1.964210, 2.671872, 3.838703, 5.252300),
pred = 1:5, weights = rep(1, 5))
rxDataStep(dat, outFile=dataFile, rowsPerRead=50)
tst <- rxLinPredError("actual", "pred", dataFile, "weights")
rxDataStep(dat, outFile=dataFile, rowsPerRead=50, reportProgress=0)
tst <- rxLinPredError("actual", "pred", dataFile, "weights", reportProgress=0)
expect_is(tst, "list")
expect_equal(names(tst), c("MAPE", "MPE", "MSE", "MSWD"))
expect_equivalent(unname(unlist(tst)), c(0.08810105, -0.01488186, 0.05375816, 1.07516325))
file.remove(dataFile)
})

30 changes: 30 additions & 0 deletions inst/tests/test-rxMoments.R
@@ -0,0 +1,30 @@
### Test rxMoments
rxOptions(reportProgress=0)

context("rxMoments")


# ------------------------------------------------------------------------

test_that("rxMoments works with data frame",{
stopifnot(require("e1071"))
tst <- rxMoments(~Sepal.Length, iris)

g1 <- skewness(iris$Sepal.Length, type=1)
G1 <- skewness(iris$Sepal.Length, type=2)
b1 <- skewness(iris$Sepal.Length, type=3)

expect_equal(g1, tst[["skewness"]][["g1"]])
expect_equal(G1, tst[["skewness"]][["G1"]])
expect_equal(b1, tst[["skewness"]][["b1"]])

g2 <- kurtosis(iris$Sepal.Length, type=1)
G2 <- kurtosis(iris$Sepal.Length, type=2)
b2 <- kurtosis(iris$Sepal.Length, type=3)
expect_equal(g2, tst[["kurtosis"]][["g2"]])
expect_equal(G2, tst[["kurtosis"]][["G2"]])
expect_equal(b2, tst[["kurtosis"]][["b2"]])

})


0 comments on commit fb3b49a

Please sign in to comment.