-
Notifications
You must be signed in to change notification settings - Fork 155
Description
I had some comparability problems between plyr and tidyverse (dplyr) in the following function that draws ellipses:
plyr implantation:
ell <- plyr::ddply(df.u, "groups", function(x) {
if (nrow(x) <= 2) {
return(NULL)
}
sigma <- var(cbind(x$xvar, x$yvar))
mu <- c(mean(x$xvar), mean(x$yvar))
ed <- sqrt(qchisq(ellipse.prob, df = 2))
data.frame(sweep(circle %*% chol(sigma) * ed, 2,
mu, FUN = "+"), groups = x$groups[1])
})
I have rewritten this function in dplyr It is a bit long, but it does the job and does not have comparability issues. The run time is practically the same.
ell <-
df.u %>%
group_by(groups) %>%
filter(n() > 2) %>%
summarize(
sigma = list(var(cbind(xvar, yvar))),
mu = list(c(mean(xvar), mean(yvar))),
ed = sqrt(qchisq(ellipse.prob, df = 2)),
circle_chol = list(circle %*% chol(sigma[[1]]) * ed),
ell = list(sweep(circle_chol[[1]], 2, mu[[1]], FUN = "+")),
xvar = map(ell, ~.x[,1]),
yvar = map(ell, ~.x[,2]),
.groups = "drop"
) %>%
select(xvar, yvar, groups) %>%
unnest(c(xvar, yvar))