Skip to content

Commit

Permalink
add additional checks for response for binary regression
Browse files Browse the repository at this point in the history
add banknote data
  • Loading branch information
merliseclyde committed May 3, 2023
1 parent 4555a90 commit 195a7c0
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 60 deletions.
3 changes: 2 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ run-checks.R
^CONTRIBUTING\.md$
^SECURITY\.md$
^CRAN-SUBMISSION$
bark-profiling.R
bark-profiling.R

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ src/*.dll

docs
inst/doc
tests/testthat/test_new_vs_old_code.R
15 changes: 14 additions & 1 deletion R/bark.r
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,19 @@ bark <- function(formula, data, subset, na.action = na.omit,
Terms <- attr(m, "terms")
attr(Terms, "intercept") <- 0
x.train <- model.matrix(Terms, m)
y.train <- model.extract(m, "response")
y.train <- model.extract(m, "response")
if (is.character(y.train)) {
stop("the response variable should be a double for regression problems
or a factor, integer or double for classification problems")
}
if (!is.double(y.train)) {
if (classification) {
y.train = as.double(y.train)
if (min(y.train) > 0) y.train = y.train - 1.0
}
else stop("response should be a double for regression problems")
}

attr(x.train, "na.action") <- attr(y.train, "na.action") <- attr(m, "na.action")

if (!is.logical(classification))
Expand Down Expand Up @@ -321,6 +333,7 @@ bark <- function(formula, data, subset, na.action = na.omit,
}
# burning the markov chain
fullXX <- NULL;
fullXX <- getfulldesign(x.train, x.train, theta)
for(i in 1:(keepevery*nburn)){
cur <- rjmcmcone(y.train, x.train, theta, fixed, tune, classification, type, fullXX);
theta <- cur$theta;
Expand Down
21 changes: 21 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#' @name banknotes
#' @title Swiss Bank Notes
#' @description This data set contains six measurements on 100 genuine and
#' 100 fradulent old Swiss banknotes
#' @docType data
#' @usage data(banknotes)
#' @format a dataframe with the following variables:
#' \describe{
#' \item{Status}{the status of the banknote: genuine or counterfeit}
#' \item{Length}{Length of bill (mm)}
#' \item{Left}{Width of left edge (mm)}
#' \item{Right}{Width of right edge (mm)}
#' \item{Bottom}{Bottom margin width (mm)}
#' \item{Top}{Top margin width (mm)}
#' \item{Diagonal}{Length of diagonal (mm)}
#' }
#' @keywords datasets
#' @source Flury, B. and Riedwyl, H. (1988). Multivariate Statistics: A
#' practical approach. London: Chapman & Hall, Tables 1.1 and 1.2, pp. 5-8.
#'
NULL
4 changes: 2 additions & 2 deletions R/depr_simCircle.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ NULL
## sim.Circle()
sim.Circle <- function(n, dim = 5) {
.Deprecated("sim_circle")
if (dim < 2) { # start nocov
if (dim < 2) { # nocov start
stop("number of variables must be >= 2.")
# end nocov
# nocov end
}
x <- matrix(runif(n * dim, min = -1, max = 1), nrow = n)
r2 <- x[, 1]^2 + x[, 2]^2
Expand Down
Binary file added data/banknotes.rda
Binary file not shown.
30 changes: 30 additions & 0 deletions man/banknotes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions tests/testthat/test-depr_bark.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test_that("old bark", {
# check main input argument types
# y is not a vector
expect_error(bark_mat( y.train=data.frame(traindata), x.train=traindata$x,
testdata= testdata$x,
x.test = testdata$x,
nburn=10, nkeep=100, keepevery=10,
classification = FALSE,
printevery=10^10))
Expand All @@ -27,7 +27,7 @@ expect_error(bark_mat(x.train=traindata, y.train = traindata$y,

# testdata is a dataframe
expect_error(bark_mat(x.train=traindata$x, y.train = traindata$y,
testdata=testdata,
x.test = testdata,
nburn=10, nkeep=100, keepevery=10,
classification = FALSE,
printevery=10^10))
Expand All @@ -50,7 +50,12 @@ expect_error(bark_mat(x.train=traindata, y.train = traindata$y,
nburn=10, nkeep=100, keepevery=10,
classification=5, type="e",
printevery=500))


expect_error(bark_mat(traindata$x, traindata$y, as.character(testdata$x),
nburn=10, nkeep=10, keepevery=10,
keeptrain=TRUE,
classification=FALSE, type="sd", printevery=10^10))


expect_no_error(bark_mat(traindata$x, traindata$y, testdata$x,
nburn=10, nkeep=10, keepevery=10,
Expand Down
53 changes: 0 additions & 53 deletions vignettes/bark.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -200,56 +200,3 @@ if (bart.available) {
```




## Ionosphere Example

```{r io-data}
set.seed(42)
data(ionosphere, package="fdm2id")
y.loc = ncol(ionosphere)
ionosphere[, y.loc] = 1L*(ionosphere[, y.loc] == "g")
train = sample(nrow(ionosphere), 200, rep=FALSE)
io.traindata = ionosphere[train,]
io.testdata = ionosphere[-train,]
```

### BARK

```{r io-bark}
if (io.available) {
set.seed(42)
io.bark <- bark(V35 ~ ., data= io.traindata,
testdata = io.testdata,
classification=TRUE,
selection = TRUE,
common_lambdas = FALSE,
nburn = 100,
nkeep = 2500,
keepevery = 100,
printevery = 10^10)
mean((io.bark$yhat.test.mean > 0) != io.testdata[, y.loc])
}
```


### BART
```{r bart-io}
if (bart.available & io.available) {
io.bart = pbart(x.train = as.matrix(io.traindata[, -y.loc]),
y.train = io.traindata[, y.loc]);
pred.bart = predict(io.bart, io.testdata[, -y.loc]);
mean((pred.bart$prob.test.mean > .5) != io.testdata[, y.loc])
}
```

### SVM
```{r svm-io}
if (svm.available & io.available) {
io.svm = svm(V35 ~ ., data=io.traindata, type="C")
pred.svm = predict(io.svm, io.testdata)
mean(pred.svm != io.testdata[, y.loc])
}
```

0 comments on commit 195a7c0

Please sign in to comment.