时空中的随机点

时间:2018-08-28 07:29:55

标签: r random coordinates

我在道路上因数年的死亡而有一套约会堆肥。我的数据是坐标x&y。我需要创建数据的随机分布作为控件,以便能够将我的数据与随机控件进行比较,并查看是否存在死亡热点。我知道可以用R创建一个随机的点集,但是我不知道如何在时间上使两者具有可比性。我如何才能在几年内创造这些分数?

再次感谢您

极光

1 个答案:

答案 0 :(得分:0)

要识别热点,最好使用2D kernel density estimation。请参见以下示例,了解data portal for the State of Washington造成的道路交通事故死亡率。为了识别热点,使用了contour图和kde2d函数。请参见下面的代码:

library(lubridate)
mort <- read.csv("https://data.wa.gov/api/views/mcp7-tcwf/rows.csv?accessType=DOWNLOAD", stringsAsFactors = FALSE)

ll <- t(sapply(mort$Location, function(x) na.omit(as.numeric(unlist(strsplit(x, "\\(|\\,| |\\)"))))))
rownames(ll) <- NULL
colnames(ll) <- c("lat", "lon")
mort2 <- cbind(mort, ll)
mort2$Salvage.Date.Time2 <- mdy_hms(mort2$Salvage.Date.Time)
mort2$month <- month(mort2$Salvage.Date.Time2)
mort2$year <- year(mort2$Salvage.Date.Time2)
mort2 <- mort2[mort2$year> 2016, ]

mort3 <- mort2[with(mort2, lat > 45.5 & lat < 49.& lon > -125 & lon < -116), ]
f1 <- with(mort3, kde2d(lat, lon, n = 100))

plot(mort3$lat, mort3$lon, pch = 18, col = "lightblue")
contour(f1, levels  =  c(0.01, 0.05, 0.1, 0.2), add = TRUE, labcex = 1)

输出: plots