如何使用';ks';和';rgl';
我一直在使用“ks”软件包和“rgl”软件包来生成3D内核密度估计和3D绘图。这第一部分做得很好(下面是一个简单的例子)。我搞不清楚的是,是否有可能提取用于构建内核的给定xyz位置的内核值。换句话说,提取三维打印中点的值,类似于“光栅”包中用于二维曲面的提取命令。有没有人做过这样的事情,能为我指明正确的方向?多谢-DJ如何使用';ks';和';rgl';,r,3d,extract,rgl,kernel-density,R,3d,Extract,Rgl,Kernel Density,我一直在使用“ks”软件包和“rgl”软件包来生成3D内核密度估计和3D绘图。这第一部分做得很好(下面是一个简单的例子)。我搞不清楚的是,是否有可能提取用于构建内核的给定xyz位置的内核值。换句话说,提取三维打印中点的值,类似于“光栅”包中用于二维曲面的提取命令。有没有人做过这样的事情,能为我指明正确的方向?多谢-DJ library("rgl") library("ks") # call the plug-in bandwidth estimator H.pi <- Hpi(b,bin
library("rgl")
library("ks")
# call the plug-in bandwidth estimator
H.pi <- Hpi(b,binned=TRUE) ## b is a matrix of x,y,z points
# calculate the kernel densities
fhat2 <- kde(b, H=H.pi)
#plot the 50% and 95% kernels in gray and blue
plot(fhat2,cont=c(50,95),colors=c("gray","blue"),drawpoints=TRUE
,xlab="", ylab="", zlab="",size=2, ptcol="white", add=FALSE, box=TRUE, axes=TRUE)
#Structure of fhat2. Original df consists of ~6000 points.
List of 9
$ x : num [1:6173, 1:3] -497654 -497654 -497929 -498205 -498205 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:6173] "50" "57" "70" "73" ...
.. ..$ : chr [1:3] "x" "max_dep" "y"
$ eval.points:List of 3
..$ : num [1:51] -550880 -546806 -542733 -538659 -534586 ...
..$ : num [1:51] -7.9 -4.91 -1.93 1.06 4.05 ...
..$ : num [1:51] -376920 -374221 -371522 -368823 -366124 ...
$ estimate : num [1:51, 1:51, 1:51] 0 0 0 0 0 ...
$ H : num [1:3, 1:3] 3.93e+07 -2.97e+03 8.95e+06 -2.97e+03 2.63e+01 ...
$ gridtype : chr [1:3] "linear" "linear" "linear"
$ gridded : logi TRUE
$ binned : logi FALSE
$ names : chr [1:3] "x" "max_dep" "y"
$ w : num [1:6173] 1 1 1 1 1 1 1 1 1 1 ...
- attr(*, "class")= chr "kde"
库(“rgl”)
图书馆(“ks”)
#调用插件带宽估计器
H.pi试试这个
## from ?plot.kde
library(ks)
library(MASS)
data(iris)
## trivariate example
fhat <- kde(x=iris[,1:3])
## this indicates the orientation
image(fhat$eval.points[[1]], fhat$eval.points[[2]], apply(fhat$estimate, 1:2, sum))
points(fhat$x[,1:2])
library(raster)
## convert to RasterBrick from raw array
## with correct orientation relative to that of ?base::image
b <- brick(fhat$estimate[,ncol(fhat$estimate):1,],
xmn = min(fhat$eval.points[[1]]), xmx = max(fhat$eval.points[[1]]), ymn = min(fhat$eval.points[[2]]), ymx = max(fhat$eval.points[[2]]),
transpose = TRUE)
## check orientation
plot(calc(b, sum))
points(fhat$x[,1:2])
答案也可能在于评估分数。进一步研究,您可以在此处输入自己的点,因此您可以输入用于构建kde的点或一组全新的点。将fhat2转换为光栅,其中可能有一个“image()xyz”列表。什么是str(fhat2)?根据@mdsummer的要求添加了str(fhat2)。但是,转换为光栅或使用image()不会导致我丢失3D信息吗?在我的例子中,z是一个深度值,内核密度值是在Lat/Lon/depth空间中生成的。所以也许这更适合描述为4D?似乎image()会将这些信息展平,这不是我所需要的。谢谢@mdsumner。如果我理解正确的话,你要做的就是构建一系列垂直于z轴的光栅曲面——在本例中是petal.length。每个光栅表面由从数组fhat$estimate中获取的网格值组成(看起来kde构建了其中的51个)。然后,提取给定xy(此处为萼片长度和萼片宽度)坐标下每个光栅曲面的值,而不考虑z值。然后,在51个切片中,我得到了给定xy的内核密度值。听起来对吗?是的,你必须自己做sub-z,它不是内置的,但是如果你需要更多的帮助,它就足够容易了。
plot(b)
## note this is a matrix with nrows = nrow(fhat$x), ncols = nlayers(b)
extract(b, fhat$x[,1:2])