Skip to content

Instantly share code, notes, and snippets.

@fernandomayer
Created August 30, 2017 20:11
Show Gist options
  • Save fernandomayer/8443765e3e66413f5299d348f05dafd5 to your computer and use it in GitHub Desktop.
Save fernandomayer/8443765e3e66413f5299d348f05dafd5 to your computer and use it in GitHub Desktop.
## Segmented regression plots in lattice
## Example for view purposes only
head(iris)
library(xyplot)
xyplot(Sepal.Length ~ Sepal.Width, iris,
panel = function(x, y, ...){
panel.xyplot(x, y, ...)
le <- lm(Sepal.Length ~ Sepal.Width, iris,
subset = Sepal.Width <= 3)
llines(iris$Sepal.Width[iris$Sepal.Width <= 3],
predict(le))
ld <- lm(Sepal.Length ~ Sepal.Width, iris,
subset = Sepal.Width > 3)
llines(iris$Sepal.Width[iris$Sepal.Width > 3],
predict(ld), col = 2)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment