在 R 中并使用 Lattice,我试图按年份提取直方图和密度图数据以供其他程序使用。到目前为止,我有直方图部分工作:
library(lattice)
x <- round(runif(1000, 1999.5, 2012.5))
y <- rlnorm(1000, meanlog = log(40000), sdlog = log(40000) - log(33000))
data <- data.frame(cbind(x,y))
data$y <- data$y + 2000*(data$x-2000)
GenerateBinComputations <- function(x, breaks, equal.widths=TRUE, type="percent", nint,...) { hist(x, breaks = breaks, plot = FALSE) }
a <- histogram(~data$y | data$x, type="percent", nint=101, endpoints=c(-500, 100500), equal.widths = TRUE)
results <- data.frame(seq(0, 100000, 1000))
colnames(results)[1] <- "Midpoint"
for (index in 1:13) {
b <- trellis.panelArgs(a, index)
b$breaks <- seq(-500, 100500, by=1000)
c <- do.call(GenerateBinComputations, b)
results <- cbind(results, c$density)
colnames(results)[index+1] <- index + 1999
}
print(results)
现在,问题是如何用密度图做到这一点。我认为它会是类似的东西,但是我遇到了中断问题(它们不包含在密度图格子对象中)以及在密度图格子对象中找到正确的对象以用于生成箱的密度/百分位数。这是我到目前为止所拥有的:
d <- densityplot(~data$y | data$x, type="percent", nint=101, endpoints=c(-500, 100500))
我应该参考的密度图格子中是否有不同的位置?还是我必须直接使用密度函数,比如这样的?
density.estimate <- density(data$y, n = 512 * 8)
density.interpolation <- approxfun(x = density.estimate$x, y = density.estimate$y)
density.results <- within(data, Density <- density.interpolation(y))
提前致谢!