Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting an upper bound of 0 on a 3d loess smoothing with negative values in R

I have a bit of a bizarre question, but hoping someone can help me. I am attempting to create a surface plot of the bottom of a lake and then add some points showing plant frequency for a visual of where aquatic plants are occurring throughout the lake.

Right now I am working on creating the surface plot in both scatterplot3d and wireframe using the scatterplot3d and lattice packages, respectively, in R. In order to achieve the type of plot I am interested in I have converted the depths to negative values (imagine the lake's water surface as 0 on the z-axis), then created a loess model of depth by latitude and longitude coordinates. However, one problem that I'm having is that the loess model predicts positive depths (which is, of course, impossible in a lake; one can only go down into the water column from a depth of 0).

Example

x <- seq(1,100,1)
y <- seq(1,100,1)
depth <- rbeta(100, 1, 50)*100
depth <- -depth

dep.lo <- loess(depth~x*y, degree=2, span=.25) # this shows a big warning, but it works
coord.fit <- expand.grid(x=x, y=y)
coord.fit$depth <- as.numeric(predict(dep.lo, newdata=coord.fit))
range(coord.fit$depth)
  # -14.041011   6.986745

As you can see, my depth goes from -14 to almost 7. Is there a way to set an upper bound on a loess model so that my model doesn't achieve these sorts of positive values?

Thanks for any help,
Paul

like image 331
logicForPresident Avatar asked Jan 16 '14 15:01

logicForPresident


1 Answers

If you want to use a loess model, you can use a transformation to ensure your variable remains negative. You were getting the warnings because all your points were over a line, so changing a bit the data:

set.seed(123)
n = 100
x <- c(0, runif(n, min=1, max=100), 100)
y <- c(0, runif(n, min=1, max=100), 100)
depth <- rbeta(n+2, 1, 50)*100
depth <- -depth
range(depth)

[1] -13.27248715  -0.01520178

using your original example, you would get:

dep.lo <- loess(depth~x*y, degree=2, span=.25) 
coord.fit <- expand.grid(x=seq(1,100,1), y=seq(1,100,1))
coord.fit$depth <- as.numeric(predict(dep.lo, newdata=coord.fit))
range(coord.fit$depth)

[1] -7.498542  2.397855

The transformation can be log(-depth) for example:

tiny = 1e-3
nlogdepth = log(-depth + tiny) # adding 'tiny' to ensure depth is not 0
dep.lo <- loess(nlogdepth~x*y, degree=2, span=.25)
coord.fit <- expand.grid(x=x, y=y)
coord.fit$depth <- -exp(as.numeric(predict(dep.lo, newdata=coord.fit))) + tiny
range(coord.fit$depth)

[1] -16.9366043  -0.1091614
like image 80
Ricardo Oliveros-Ramos Avatar answered Sep 30 '22 06:09

Ricardo Oliveros-Ramos