简体   繁体   English

在3d黄土平滑上设置0的上限,在R中使用负值

[英]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. 现在我正在使用分别在R中的scatterplot3d和网格包在scatterplot3d和wireframe中创建表面图。为了实现我感兴趣的图表类型,我已将深度转换为负值(想象一下湖的水面在z轴上为0),然后通过纬度和经度坐标创建一个深度的黄土模型。 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). 然而,我遇到的一个问题是黄土模型预测正深度(当然,在湖中是不可能的;人们只能从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 保罗

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM