Andrewjohnclose
2008-Aug-08 14:40 UTC
[R] Polygon issue in lattice & Bivariate density estimation
Dear all, I am trying to set a specific bandwidth for a bivariate kernel density estimation and then plot it in lattice: managed all that except that the plot appears to have an issue regards the setting of the polygon and as a result I end up with horizontal lines disecting my plotting region. If I lower the bandwidth excessively then the issue resolves itself, but this defeats the object as I wish and need h=0.8481. Any suggestions would be much appreciated. Thank you Here is the code I have used: library(lattice) library(KernSmooth) ##### CAPEL2=read.csv("CAPEL2.csv",header=T,row.names=1) ##### panel.density2d <- function(x, y, nlevels = 10, bandwidth, ...) { k2d <- bkde2D(na.omit(cbind(x, y)), bandwidth = bandwidth) cl <- contourLines(x = k2d$x1, y = k2d$x2, z = k2d$fhat, nlevels = nlevels) lapply(cl, panel.polygon, ...) } ##### xyplot(PCO2~PCO1,data=CAPEL2,panel = panel.density2d,bandwidth=0.8481) http://www.nabble.com/file/p18893107/CAPEL2.csv CAPEL2.csv -- View this message in context: http://www.nabble.com/Polygon-issue-in-lattice---Bivariate-density-estimation-tp18893107p18893107.html Sent from the R help mailing list archive at Nabble.com.
Deepayan Sarkar
2008-Aug-08 17:54 UTC
[R] Polygon issue in lattice & Bivariate density estimation
On Fri, Aug 8, 2008 at 7:40 AM, Andrewjohnclose <a.j.close at ncl.ac.uk> wrote:> > Dear all, > > I am trying to set a specific bandwidth for a bivariate kernel density > estimation and then plot it in lattice: managed all that except that the > plot appears to have an issue regards the setting of the polygon and as a > result I end up with horizontal lines disecting my plotting region. If I > lower the bandwidth excessively then the issue resolves itself, but this > defeats the object as I wish and need h=0.8481. > > Any suggestions would be much appreciated. Thank you > > Here is the code I have used: > library(lattice) > library(KernSmooth) > ##### > CAPEL2=read.csv("CAPEL2.csv",header=T,row.names=1) > ##### > panel.density2d <- > function(x, y, nlevels = 10, bandwidth, ...) > { > k2d <- bkde2D(na.omit(cbind(x, y)), bandwidth = bandwidth) > cl <- > contourLines(x = k2d$x1, y = k2d$x2, z = k2d$fhat, > nlevels = nlevels) > lapply(cl, panel.polygon, ...) > }Without looking at this in detail, I would suggest using lapply(cl, panel.lines, ...) instead. Polygons are drawn as closed, whereas contourLines does not necessarily give you closed contours. -Deepayan> ##### > xyplot(PCO2~PCO1,data=CAPEL2,panel = panel.density2d,bandwidth=0.8481) > http://www.nabble.com/file/p18893107/CAPEL2.csv CAPEL2.csv > -- > View this message in context: http://www.nabble.com/Polygon-issue-in-lattice---Bivariate-density-estimation-tp18893107p18893107.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >