library(stream)
# create a data stream for the iris dataset
data <- iris[sample(nrow(iris)), ]
stream <- DSD_Memory(data)
stream
## Example 1: Use a function on the sliding window
summarizer <- function(data) summary(data)
s <- DST_SlidingWindow(summarizer,
window = 100, rebuild = 50)
s
# update window with 49 points. The function is not yet called
update(s, stream, 49)
get_model(s)
# updating with the 50th point will trigger a function call (see rebuild parameter)
# note that the window is only 1/2 full and we have 50 NAs
update(s, stream, 1)
get_model(s)
# 50 more points and the function will be recomputed
update(s, stream, 50)
get_model(s)
## Example 2: Use classifier on the sliding window
reset_stream(stream)
# rpart, like most models in R, already have a formula interface that uses a
# data parameter. We can use these types of models directly
library(rpart)
cl <- DST_SlidingWindow(
rpart, formula = Species ~ Petal.Length + Petal.Width,
window = 100, rebuild = 50)
cl
# update window with 50 points so the model is built
update(cl, stream, 50)
get_model(cl)
# update with 40 more points and force the function to be recomputed even though it would take
# 50 points to automatically rebuild.
update(cl, stream, 40, rebuild = TRUE)
get_model(cl)
# rpart supports predict, so we can use it directly with the DST_SlidingWindow
new_points <- get_points(stream, n = 5)
predict(cl, new_points, type = "class")
## Example 3: Regression using a sliding window
reset_stream(stream)
## lm can be directly used
reg <- DST_SlidingWindow(
lm, formula = Sepal.Length ~ Petal.Width + Petal.Length,
window = 100, rebuild = 50)
reg
update(reg, stream, 100)
get_model(reg)
# lm supports predict, so we can use it directly with the DST_SlidingWindow
new_points <- get_points(stream, n = 5)
predict(reg, new_points)
Run the code above in your browser using DataLab