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).
现在我正在使用分别在R中的scatterplot3d和lattice包在scatterplot3d和wireframe中创建曲面图。为了实现我感兴趣的绘图类型,我将深度转换为负值(想象一下湖的水面在z轴上为0),然后通过纬度和经度坐标创建一个深度的黄土模型。然而,我遇到的一个问题是黄土模型预测正深度(当然,在湖中是不可能的;人们只能从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?
正如你所看到的,我的深度从-14到接近7.有没有办法在黄土模型上设置上限,这样我的模型就不会达到这些正值?
Thanks for any help,
Paul
谢谢你的帮助,保罗
1 个解决方案
#1
7
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:
转换可以是log(-depth),例如:
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
#1
7
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:
转换可以是log(-depth),例如:
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