0
真的很感激我的整顿问题R.45度翻转
我已经写在R A KDE功能,我使用MASS :: kde2d作为我的参考任何帮助,绘制和示范犯罪数据。我所生产的密度值似乎在正确的范围内,以及密度的整体分布,但它似乎通过地块原点翻转了45度。
请注意下图中的形状几乎相同,但翻转。
任何帮助将不胜感激!如果任何人都可以发现错误,我在下面附加了我的2D功能。谢谢!
该函数的输入是一个空间点阵,坐标位于网格单元的中心,犯罪点的纬度和经度顺序,带宽h和开关参数。我正在考虑包括其他内核因此包含。
Kernel2D <- function(lattice, crime.locations, h, kernel){
if(is.data.frame(spatial.lattice)==FALSE) {lattice <- data.frame(lattice)}
d <- fields::rdist(lattice, crime.locations)
switch(kernel,
'normal'={
k=1
cat('k = 1, then rescaled so sum of densities = N. ')
cat("Normal - Function extends to boundary in all directions, applied to every location in the region regardless of h")
## This is the density calculation which iterates through the rows of d. I.e. for
## each cell in the lattice, it does a calculation on that row where the row contains the
## distances from that cell to each crime location. (Not trying to patronize anyone here
## with having dumb explanations! Trying to make it simple so I may understand it myself
## when explaining it to others haha) .
## I have assumed that this is correct as it it producing the same density shape as MASS::kde2d.
## There is an error when associating the density values to the right grid. Hmm. Ill have more of a think now.
density <- apply(d, 1, function(x) sum((1/(2*pi*h**2))*exp(-(x**2)/(2*h**2))))
k = nrow(crime.locations)/sum(density)
density <- k*density},
stop('Kernel Undefined. Available: triangular, uniform, negexp, normal, quartic'))
return(density)}