Re: [R] lattice densityplot with weights
Yes, the following should (mostly) work: lattice::densityplot(~ x, mydf, groups = name, weights = wt, auto.key = list(space = "inside"), grid = TRUE) I just realised that the 'subdensity' and 'warnWbw' arguments are not passed through by densityplot(), so there is no way to get rid of the corresponding warnings. But this should be OK otherwise. On Fri, 29 Sept 2023 at 10:35, Bert Gunter wrote: > > Unless I misunderstand... > > See ?panel.densityplot > > Lattice functions do their work through a host of panel functions, > typically passing their ... arguments to the panel functions. > panel.densityplot has a weights argument. 'weights' needs a bit more work because it has to do NSE like 'groups', but other than that, yes. The ?densityplot help page documents the 'weight' argument. Best, -Deepayan > Cheers, > Bert > > > > > On Fri, Sep 29, 2023 at 3:32 AM Naresh Gurbuxani < > naresh_gurbux...@hotmail.com> wrote: > > > > > density() function in R accepts weights as an input. Using this > > function, one can calculate density and plot it. Is it possible to > > combined these two operations in lattice densityplot()? > > > > mydf <- data.frame(name = "A", x = seq(-2.9, 2.9, by = 0.2), wt = > > diff(pnorm(seq(-3, 3, by = 0.2 > > mydf <- rbind(mydf, data.frame(name = "B", x = mydf$x + 0.5, wt = > > mydf$wt)) > > with(subset(mydf, name == "A"), density(x, weights = wt / sum(wt)) |> > > plot(xlim = c(-3, 3.5), xlab = "", main = "Density Plots")) > > with(subset(mydf, name == "B"), density(x, weights = wt / sum(wt)) |> > > lines(lty = 2, col = 2)) > > grid() > > legend("topright", legend = c("A", "B"), col = c(1, 2), lty = c(1, 2), > > bty = "n") > > > > # I want to do something like this: > > # densityplot(~ x, weights = wt, groups = name, data = mydf, type = c("l", > > "g")) > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > > > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Re: [R] lattice densityplot with weights
Unless I misunderstand... See ?panel.densityplot Lattice functions do their work through a host of panel functions, typically passing their ... arguments to the panel functions. panel.densityplot has a weights argument. Cheers, Bert On Fri, Sep 29, 2023 at 3:32 AM Naresh Gurbuxani < naresh_gurbux...@hotmail.com> wrote: > > density() function in R accepts weights as an input. Using this > function, one can calculate density and plot it. Is it possible to > combined these two operations in lattice densityplot()? > > mydf <- data.frame(name = "A", x = seq(-2.9, 2.9, by = 0.2), wt = > diff(pnorm(seq(-3, 3, by = 0.2 > mydf <- rbind(mydf, data.frame(name = "B", x = mydf$x + 0.5, wt = > mydf$wt)) > with(subset(mydf, name == "A"), density(x, weights = wt / sum(wt)) |> > plot(xlim = c(-3, 3.5), xlab = "", main = "Density Plots")) > with(subset(mydf, name == "B"), density(x, weights = wt / sum(wt)) |> > lines(lty = 2, col = 2)) > grid() > legend("topright", legend = c("A", "B"), col = c(1, 2), lty = c(1, 2), > bty = "n") > > # I want to do something like this: > # densityplot(~ x, weights = wt, groups = name, data = mydf, type = c("l", > "g")) > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[R] lattice densityplot with weights
density() function in R accepts weights as an input. Using this function, one can calculate density and plot it. Is it possible to combined these two operations in lattice densityplot()? mydf <- data.frame(name = "A", x = seq(-2.9, 2.9, by = 0.2), wt = diff(pnorm(seq(-3, 3, by = 0.2 mydf <- rbind(mydf, data.frame(name = "B", x = mydf$x + 0.5, wt = mydf$wt)) with(subset(mydf, name == "A"), density(x, weights = wt / sum(wt)) |> plot(xlim = c(-3, 3.5), xlab = "", main = "Density Plots")) with(subset(mydf, name == "B"), density(x, weights = wt / sum(wt)) |> lines(lty = 2, col = 2)) grid() legend("topright", legend = c("A", "B"), col = c(1, 2), lty = c(1, 2), bty = "n") # I want to do something like this: # densityplot(~ x, weights = wt, groups = name, data = mydf, type = c("l", "g")) __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.