# NOT RUN {
library(stochQN)
### Load Iris dataset
data("iris")
### Example with X + y interface
X <- as.matrix(iris[, c("Sepal.Length", "Sepal.Width",
"Petal.Length", "Petal.Width")])
y <- as.numeric(iris$Species == "setosa")
### Initialize model with default parameters
model <- stochastic.logistic.regression(dim = 4)
### Fit to 10 randomly-subsampled batches
batch_size <- as.integer(nrow(X) / 3)
for (i in 1:10) {
set.seed(i)
batch <- sample(nrow(X),
size = batch_size, replace=TRUE)
partial_fit_logistic(model, X, y)
}
### Check classification accuracy
cat(sprintf(
"Accuracy after 10 iterations: %.2f%%\n",
100 * mean(
predict(model, X, type = "class") == y)
))
### Example with formula interface
iris_df <- iris
levels(iris_df$Species) <- c("setosa", "other", "other")
### Initialize model with default parameters
model <- stochastic.logistic.regression(Species ~ .,
pos_class="setosa")
### Fit to 10 randomly-subsampled batches
batch_size <- as.integer(nrow(iris_df) / 3)
for (i in 1:10) {
set.seed(i)
batch <- sample(nrow(iris_df),
size=batch_size, replace=TRUE)
partial_fit_logistic(model, iris_df)
}
cat(sprintf(
"Accuracy after 10 iterations: %.2f%%\n",
100 * mean(
predict(
model, iris_df, type = "class") == iris_df$Species
)
))
# }
Run the code above in your browser using DataLab