在R和使用莱迪思,我试图按年提取直方图和密度图数据供其他程序使用。到目前为止,我有直方图部分工作:
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)
现在,问题是如何使用densityplot来做到这一点。我假设它会有类似的东西,但我遇到了断裂问题(它们不包括在densityplot格子对象中),并且在densityplot trellis对象中找到正确的对象,用于生成容器的密度/百分位数。以下是我到目前为止的情况:
d <- densityplot(~data$y | data$x, type="percent", nint=101, endpoints=c(-500, 100500))
我应该引用的densityplot格子中有不同的位置吗?或者我是否必须直接使用密度函数,例如类似的东西?
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))
提前致谢!