if(interactive()) {
# loess fit
p <- l_plot(iris, color = iris$Species)
l1 <- l_layer_smooth(p, interval = "confidence")
l_layer_hide(l1)
# the fits are grouped by points color
l2 <- l_layer_smooth(p, group = "color",
method = "lm")
# so far, all intervals are hidden
ls <- l_layer_getChildren(l2)
intervals <- l_layer_getChildren(l_create_handle(c(p,ls[3])))
ci <- l_create_handle(c(p,intervals[3]))
l_layer_show(ci)
# show prediction interval
pi <- l_create_handle(c(p,intervals[2]))
l_layer_show(pi)
# hide all
l_layer_hide(l2)
# Draw a fitted line based on a new data set
shortSepalLength <- (iris$Sepal.Length < 5)
l3 <- l_layer_smooth(p,
x = iris$Sepal.Length[shortSepalLength],
y = iris$Sepal.Width[shortSepalLength],
method = "lm",
linecolor = "firebrick",
interval = "prediction")
l_layer_hide(l3)
if(require(mgcv)) {
# a full tensor product smooth
## linecolor is the same with the points color
l4 <- l_layer_smooth(p,
method = "gam",
formula = y~te(x))
l_layer_hide(l4)
}
# facets
fp <- l_facet(p, by = iris$Species, inheritLayers = FALSE)
l5 <- l_layer_smooth(fp, method = "lm")
# generalized linear model
if(require("loon.data")) {
data("SAheart")
# logit regression
chd <- as.numeric(SAheart$chd) - 1
age <- SAheart$age
p1 <- l_plot(age, chd,
title = "logit regression")
gl1 <- l_layer_smooth(p1,
method = "glm",
methodArgs = list(family = binomial()),
interval = "conf")
# log linear regression
counts <- c(18,17,15,20,10,20,25,13,12)
age <- c(40,35,53,46,20,33,48,25,23)
p2 <- l_plot(age, counts,
title = "log-linear regression")
gl2 <- l_layer_smooth(p2,
method = "glm",
methodArgs = list(family = poisson()),
interval = "conf")
}
}
Run the code above in your browser using DataLab