在R中将负值设置为3d黄土平滑时,将上限设置为0 [英] 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.
现在,我正在分别使用R中的scatterplot3d和grid包在scatterplot3d和线框中创建表面图.为了实现我感兴趣的图的类型,我已将深度转换为负值(将湖泊的水面在z轴上想象为0),然后通过纬度和经度坐标创建深度的黄土模型.但是,我遇到的一个问题是黄土模型预测的深度为正(当然,在湖中这是不可能的;只能从0的深度向下进入水柱).
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).
示例
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
如您所见,我的深度从-14到几乎7.是否有办法为黄土模型设定上限,以使我的模型无法达到这些正值?
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
推荐答案
如果要使用黄土模型,可以使用转换来确保变量保持负值.之所以收到警告,是因为您的所有要点都在一条线上,所以稍微更改一下数据即可:
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
例如,转换可以为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
这篇关于在R中将负值设置为3d黄土平滑时,将上限设置为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!