Often in ecology our response variables are strictly positive
(e.g. population density). As a result some standard default
proceedures won't work well. For example, here are some data,

``````set.seed(1)
x <- rnorm(10)
y <- exp(-x^2 - 2 + rnorm(10))
df <- data.frame(x, y)
``````

Here's a standard ggplot exploration,

``````library(ggplot2)
ggplot(df) + geom_point(aes(x, y)) + geom_smooth(aes(x, y), method = "loess")
`````` But wait, that's not right, the prediction interval dips below zero!

The best way to deal with this problem is to use models that account
for bounded data censored responses (so-called left-censored in this case). But if you just want to do some exploration and be quick about it, here's a
simple-minded solution that works well. Start by log-transforming (you might need log + 1) the response and fitting a loess model to the transformed data,

``````m <- loess(log(y) ~ x)
``````

Then use this hidden ggplot function for getting prediction

``````pdf <- ggplot2:::predictdf(m, seq(min(x), max(x), length = 100), TRUE, 0.95)
``````

Then back-transform,

``````pdf <- within(pdf, {
y <- exp(y)
ymin <- exp(ymin)
ymax <- exp(ymax)
})
``````

and re-plot,

``````ggplot(pdf) + geom_path(aes(x, y), colour = "blue") + geom_ribbon(aes(x, ymin = ymin,
ymax = ymax), alpha = 0.2) + geom_point(aes(x, y), data = df)
`````` That's better!

1. • 