## tidy 2-d deconvolved density estimate
library(ggplot2)
data(air, package="ks")
air <- na.omit(air[, c("time","co2","pm10")])
air <- dplyr::filter(air, time=="20:00")
air <- dplyr::select(air, co2, pm10)
## for details on computation of Sigma.air, see ?ks::kdcde
Sigma.air <- diag(c(6705.765, 957.664))
t1 <- tidy_kde(air)
t2 <- tidy_kdcde(air, Sigma=Sigma.air, reg=0.00021)
t3 <- dplyr::bind_rows(dplyr::mutate(t1, group=1L), dplyr::mutate(t2, group=2L))
t3$group <- factor(t3$group, label=c("Standard KDE","Deconvolved KDE"))
t3 <- as_tidy_ks(t3)
## deconvolved estimate is more clearly bimodal than standard KDE
gt <- ggplot(t3, aes(x=co2, y=pm10))
gt + geom_contour_filled_ks(colour=1) +
colorspace::scale_fill_discrete_sequential() + facet_wrap(~group)
Run the code above in your browser using DataLab