r2d2/0000755000176200001440000000000014705622632011032 5ustar liggesusersr2d2/MD50000644000176200001440000000115414705622632011343 0ustar liggesusers834cf8c4617845a93803843dd495cf70 *DESCRIPTION 07a68f40f5947698c4975b897ec4725d *NAMESPACE 6bf76a9dc66001ef1f0a54649a41d4f1 *NEWS.md 56caac2c82f335fb09254dd92cef24ef *R/conf2d.R 4ca84e820699e8fed347a99d47212d49 *R/freq2d.R b52aabe9a2757fc86742763db9dafc9b *build/partial.rdb efe834e6614b999459f5dadc887f2c56 *data/Ushape.rda 88cbfc2116b8c3ad25350829e1cb7ecd *data/saithe.rda 87d865b3b578ce618c48d4bd690ed4cb *man/Ushape.Rd b04e5894ee78a98e8a7cdfb5ad53403c *man/conf2d.Rd b695210d6c04c5745c33c42d8566493b *man/freq2d.Rd 4f3115c7c2410ccea426ca14785138c0 *man/r2d2-package.Rd 1467a53b84328e2ee2eeb75a0493b2f9 *man/saithe.Rd r2d2/R/0000755000176200001440000000000014705604420011226 5ustar liggesusersr2d2/R/freq2d.R0000644000176200001440000000500214705604420012531 0ustar liggesusersfreq2d <- function(x, ...) { UseMethod("freq2d") } freq2d.formula <- function(formula, data, subset, ...) { m <- match.call(expand.dots=FALSE) if(is.matrix(eval(m$data,parent.frame()))) m$data <- as.data.frame(data) m$... <- NULL m[[1L]] <- as.name("model.frame") mf <- eval(m, parent.frame()) freq2d.default(mf[2:1], ...) } freq2d.default <- function(x, y, n=20, pad=0, layout=1, print=TRUE, dnn=NULL, ...) { method <- match.arg(as.character(layout), c("1","2","3")) dnn <- if(!is.null(dnn)) rep(dnn,length.out=2) else NULL xname <- dnn[1] yname <- dnn[2] ## 1 Extract data if(is.matrix(x)) x <- as.data.frame(x) if(is.list(x)) # data.frame or list { xname <- if(is.null(xname)) names(x)[1] else xname yname <- if(is.null(yname)) names(x)[2] else yname y <- x[[2]] x <- x[[1]] } ## 2 Create grid n <- rep(n, length.out=2) xmid <- pretty(x, n=n[1]) xstep <- diff(xmid)[1] xgrid <- c(xmid-0.5*xstep, max(xmid)+0.5*xstep) ymid <- pretty(y, n=n[2]) ystep <- diff(ymid)[1] ygrid <- c(ymid-0.5*ystep, max(ymid)+0.5*ystep) ## 3 Map data on grid xfac <- cut(x, xgrid, include.lowest=TRUE, labels=format(xmid)) if(is.null(xname)) xname <- deparse(substitute(x)) yfac <- cut(y, ygrid, include.lowest=TRUE, labels=format(ymid)) if(is.null(yname)) yname <- deparse(substitute(y)) z <- table(xfac, yfac, dnn=c(xname,yname)) ## 4 Remove existing edges with only zeros z <- z[cumsum(rowSums(z))>0, cumsum(colSums(z))>0] z <- z[rev(cumsum(rev(rowSums(z))))>0, rev(cumsum(rev(colSums(z))))>0] ## 5 Add edges with only zeros for(i in seq_len(pad)) { tmp <- cbind(0, rbind(0, z, 0), 0) rownames(tmp)[c(1,nrow(tmp))] <- as.numeric(rownames(z)[c(1,nrow(z))]) + c(-xstep,xstep) colnames(tmp)[c(1,ncol(tmp))] <- as.numeric(colnames(z)[c(1,ncol(z))]) + c(-xstep,xstep) names(dimnames(tmp)) <- names(dimnames(z)) z <- tmp } ## 5 Prepare output xnum <- as.numeric(rownames(z)) ynum <- as.numeric(colnames(z)) if(layout == 1) { output <- t(z)[ncol(z):1,] if(print) { print.table(output, zero.print=".") invisible(output) } else { output } } else if(layout == 2) { output <- list(x=xnum, y=ynum, z=z) output } else # layout 3 { output <- data.frame(x=rep(xnum,length(ynum)), y=rep(ynum,each=length(xnum)), z=c(z)) names(output) <- make.names(c(xname,yname,"Freq"), unique=TRUE) output } } r2d2/R/conf2d.R0000644000176200001440000000624214705604420012530 0ustar liggesusersconf2d <- function(x, ...) { UseMethod("conf2d") } conf2d.formula <- function(formula, data, subset, ...) { m <- match.call(expand.dots=FALSE) if(is.matrix(eval(m$data,parent.frame()))) m$data <- as.data.frame(data) m$... <- NULL m[[1L]] <- as.name("model.frame") mf <- eval(m, parent.frame()) conf2d.default(mf[2:1], ...) } conf2d.default <- function(x, y, level=0.95, n=200, method="wand", shape=1, smooth=50, plot=TRUE, add=FALSE, xlab=NULL, ylab=NULL, col.points="gray", col="black", lwd=2, ...) { method <- match.arg(tolower(method), c("wand","mass")) ## 1 Extract data if(is.matrix(x)) x <- as.data.frame(x) if(is.list(x)) # data.frame or list { xlab <- if(is.null(xlab)) names(x)[1] else xlab ylab <- if(is.null(ylab)) names(x)[2] else ylab y <- x[[2]] x <- x[[1]] } ## 2 Plot xy scatter if(plot && !add) { if(is.null(xlab)) xlab <- deparse(substitute(x)) if(is.null(ylab)) ylab <- deparse(substitute(y)) plot(x, y, xlab=xlab, ylab=ylab, lwd=1, col=col.points, ...) } ## 3 Compute surface matrix smooth <- rep(smooth, length.out=2) if(method == "wand") { bandwidth <- c(dpik(x), dpik(y)) surf <- bkde2D(cbind(x,y), bandwidth=1.3*bandwidth*shape, gridsize=c(smooth,smooth)) } else # mass { bandwidth <- c(bandwidth.nrd(x), bandwidth.nrd(y)) lx <- range(x) + c(-0.05,0.05)*diff(range(x)) ly <- range(y) + c(-0.05,0.05)*diff(range(y)) surf <- kde2d(x, y, h=bandwidth*shape, n=5*smooth/shape, lims=c(lx,ly)) } ## 4 Find best region output <- conf2d_int(x, y, surf, level, n) if(abs(level-output$prop) > 0.01) warning("appropriate region not found (desired level=", level, ", but actual prop=", round(output$prop,3), ")") ## 5 Overlay polygon if(plot || add) { polygon(output, lwd=lwd, border=col, col=NA, ...) invisible(output) } else { output } } conf2d_int <- function(x, y, surf, level, n) { ## 1 Slice surface into candidate regions (as contour lines) cl <- contourLines(surf[[1]], surf[[2]], surf[[3]], nlevels=n) ## 2 Find best region by counting the points inside pts <- SpatialPoints(cbind(x,y)) spols <- list() # spatial polygons representing candidate regions pin <- numeric(length(cl)) # number of points inside each region for(i in seq_len(length(cl))) { spol <- tryCatch(Polygon(cbind(cl[[i]]$x,cl[[i]]$y)), error=function(...) NA) spol <- tryCatch(Polygons(list(spol),ID=" "), error=function(...) NA) spol <- tryCatch(SpatialPolygons(list(spol)), error=function(...) NA) pin[i] <- tryCatch(sum(!is.na(over(pts,spol))), error=function(...) 0) spols[[i]] <- spol } pin <- pin / length(x) best <- which.min(abs(pin-level)) ## 3 Extract statistics best.spol <- spols[[best]]@polygons[[1]]@Polygons[[1]] xcoords <- best.spol@coords[,1] ycoords <- best.spol@coords[,2] inside <- !is.na(over(pts,spols[[best]])) area <- best.spol@area prop <- pin[best] output <- list(x=xcoords, y=ycoords, inside=inside, area=area, prop=prop) output } r2d2/data/0000755000176200001440000000000014705613744011747 5ustar liggesusersr2d2/data/saithe.rda0000644000176200001440000001635114705613744013722 0ustar liggesusers]\Uօd ,At2x-@ap0 3tWs;wo&kN5U}=nj[qs 6{haCf3WsSO< ==C陿/ٰI'OWJ=VjKS,qk(Vz•~a\1dM_nb.uͤ5'Jpx8գϟ8s%4RFP/ pio)[W_qTF4_zKfߐEHߝ"NX{Gn?o们s9; Ub԰P3J|wsoq}|bR/|w( I۳_;YfHĈvxBuK2d_>hCI~{"[.c|ɪWzds,+Jtz׼;lVuI4q̑,}T{q *vʤWE:~qh2O+=~glGwu_/G_[HQjvD9D~i{nnocH"vY$L?דq?=wJ$ +sCοWboIXxR/Vv)?L'Xo`' ~ #fɯ;䧖}WJS]gIɟ !Ϭ5$7ѹ雒5")t'%%vQm:C:R)8ϙCwD$="~*_$э^P){ͽ?TW։ )NaM >FӮ^N%I[, j~q/1]j).gKI{?/tҖ%g&NJ2 +gJp).5S3 u\~ZVl<@r3Pcԯ%6en;%x-U:İf_]w~jTuֱ»z5I68WPK :ұvĆ츧Tg $jsu*k߾yqhIA_xMɏޔ^R>m!)85\.1~J Rؿ^)j=XT5ZjۺޙQ)l} x5U;o&h)WWu- >Û2t@IcyKʋKJ!R'KLLj=$'$VY`eԧQ3#ig O1kVH^P\NTġKjn몗$0H!yLbx{] ڞL/2dn[ZKka7P\>f onN0۴ݺGm[ Ut,t>M\{dC(HZgevRP_mKoVܔ :-+%<Q~['$b!klHz_q$#? <^DUjґb.Ke:>geq҆;GVGn| [X?G&*9pt=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C]--=?j[;(K:[ػ}<9)/Vꂇ=I-mPo]j 6ؿcCGۀvsuP_6ȟ :7O5c:Mߣ y%6BM>߲s/Dx.Mi>OKs6igS϶ǚw|Ny#ssvsη MbŃ7ϯ[ giu^zu;߷<7>[Miy7Tyo&.}^_w1kҿ2nNo\ǵ_ٯ+=xayCcSm3wdA~;>\]R} ҷݧgvJrx]zUUc+Gܩ1׶ug9Kpþ5Oӷ$ykoU .T)?+!'2~U{p ] ~<ӿ)3Ro7V/3Vy.e)Op2qhe_SPV~9>To>̜b9k,(S''ُ 7-+d*)X5b{svl<ŏ>]/.1'bzLG}|8ؽS} jJD>skxn~'9*vO. sXFŵ}!u~!1(^{eGv8m~lL>B>!n<%/GءG޶3| 请e[t.#ul?p y:!OH} {1; ߻<¿r@\- W:?Mqٳ =C+>{s>n礃l. }=n=|O?;鳯oB됽}?{Z}ү9>ڏOק>ÓBZ@ӯ7{:}wkmљg,0~;㟗볾[BϼgC T?r2d2/NAMESPACE0000644000176200001440000000065214705604420012247 0ustar liggesusersimportFrom(graphics, polygon) importFrom(grDevices, contourLines) importFrom(KernSmooth, dpik, bkde2D) importFrom(MASS, bandwidth.nrd, kde2d) importFrom(sp, over, Polygon, Polygons, SpatialPoints, SpatialPolygons) export(conf2d, conf2d.default, conf2d.formula, conf2d_int, freq2d, freq2d.default, freq2d.formula) S3method(conf2d, default) S3method(conf2d, formula) S3method(freq2d, default) S3method(freq2d, formula) r2d2/NEWS.md0000644000176200001440000000026714705604512012132 0ustar liggesusers# r2d2 1.0.2 (2024-10-22) * Maintenance release, no user-visible changes. # r2d2 1.0.1 (2022-05-11) * Added doi to references. # r2d2 1.0.0 (2014-03-31) * Initial release. r2d2/build/0000755000176200001440000000000014705613743012134 5ustar liggesusersr2d2/build/partial.rdb0000644000176200001440000000007414705613743014262 0ustar liggesusersb```b`a 00 FN ͚Z d@$/7r2d2/man/0000755000176200001440000000000014705604420011600 5ustar liggesusersr2d2/man/r2d2-package.Rd0000644000176200001440000000250414705604420014232 0ustar liggesusers\name{r2d2-package} \alias{r2d2-package} \alias{r2d2} \docType{package} \title{ Bivariate (Two-Dimensional) Confidence Region and Frequency Distribution } \description{ This package provides generic functions to analyze the distribution of two continuous variables. } \details{ \emph{Bivariate calculations:} \tabular{ll}{ \code{\link{conf2d}} \tab empirical confidence region, a smooth polygon\cr \code{\link{freq2d}} \tab frequency distribution, a table } \emph{Examples:} \tabular{ll}{ \code{\link{saithe}} \tab MCMC results in two columns\cr \code{\link{Ushape}} \tab U-shaped cloud in two columns } } \author{ Arni Magnusson and Julian Burgos, based on earlier functions by Gregory R. Warnes. } \references{ Bivand, R.S., Pebesma, E., and Gomez-Rubio, V. (2013). \emph{Applied Spatial Data Analysis with R}. Second edition. New York: Springer. Venables, W.N. and Ripley, B.D. (2002). \emph{Modern Applied Statistics with S}. Fourth edition. New York: Springer. Wand, M.P. and Jones, M.C. (1995). \emph{Kernel Smoothing}. London: Chapman and Hall. } \seealso{ Combines existing tools from the \pkg{KernSmooth}, \pkg{MASS}, and \pkg{sp} packages. } % Graphics \keyword{dplot} % Basics \keyword{manip} % Statistics \keyword{distribution} \keyword{multivariate} \keyword{smooth} r2d2/man/conf2d.Rd0000644000176200001440000001117114705604420013243 0ustar liggesusers\name{conf2d} \alias{conf2d} \alias{conf2d.default} \alias{conf2d.formula} \alias{conf2d_int} \title{Bivariate (Two-Dimensional) Confidence Region} \description{ Calculate an empirical confidence region for two variables, and optionally overlay the smooth polygon on a scatterplot. } \usage{ conf2d(x, \dots) \method{conf2d}{formula}(formula, data, subset, \dots) \method{conf2d}{default}(x, y, level=0.95, n=200, method="wand", shape=1, smooth=50, plot=TRUE, add=FALSE, xlab=NULL, ylab=NULL, col.points="gray", col="black", lwd=2, \dots) conf2d_int(x, y, surf, level, n) # internal function } \arguments{ \item{x}{a vector of x values, or a data frame whose first two columns contain the x and y values.} \item{y}{a vector of y values.} \item{formula}{a \code{\link{formula}}, such as \code{y~x}.} \item{data}{a \code{data.frame}, \code{matrix}, or \code{list} from which the variables in \code{formula} should be taken.} \item{subset}{an optional vector specifying a subset of observations to be used.} \item{level}{the proportion of points that should be inside the region.} \item{n}{the number of regions to evaluate, before choosing the region that matches \code{level} best.} \item{method}{kernel smoothing function to use: \code{"wand"} or \code{"mass"}.} \item{shape}{a bandwidth scaling factor, affecting the polygon shape.} \item{smooth}{the number of bins (scalar or vector of length 2), affecting the polygon smoothness.} \item{plot}{whether to plot a scatterplot and overlay the region as a polygon.} \item{add}{whether to add a polygon to an existing plot.} \item{xlab}{a label for the x axis.} \item{ylab}{a label for the y axis.} \item{col.points}{color of points.} \item{col}{color of polygon.} \item{lwd}{line width of polygon.} \item{\dots}{further arguments passed to \code{plot} and \code{polygon}.} \item{surf}{a list whose first three elements are x coordinates, y coordinates, and a surface matrix.} } \details{ This function constructs a large number (\code{n}) of smooth polygons, and then chooses the polygon that comes closest to containing a given proportion (\code{level}) of the total points. The default \code{method="wand"} calls the \code{\link[KernSmooth]{bkde2D}} kernel smoother from the \pkg{KernSmooth} package, while \code{method="mass"} calls \code{\link[MASS]{kde2d}} from the \pkg{MASS} package. The \code{conf2d} function calls \code{bkde2D} or \code{kde2d} to compute a smooth surface from \code{x} and \code{y}. If users already have a smoothed surface to work from, the internal \code{conf2d_int} can be used directly to find the empirical confidence region that matches \code{level} best. } \value{ List containing five elements: \item{x}{x coordinates defining the region.} \item{y}{y coordinates defining the region.} \item{inside}{logical vector indicating which of the original data coordinates are inside the region.} \item{area}{area inside the region.} \item{prop}{actual proportion of points inside the region.} } \note{ The \code{area} of a bivariate region is analogous to the range of a univariate interval. This allows a quantitative comparison of different confidence regions. Ellipses are a more restrictive approach to calculate an empirical bivariate confidence region. Smooth polygons make fewer assumptions about how x and y covary. The \code{conf2d} and \code{freq2d} functions are closely related. The advantage of \code{conf2d} is that it returns a region as a smooth polygon. The advantage of \code{freq2d} is that it returns a set that is guaranteed to contain the correct proportion of points, even for spatially complex datasets. } \author{ Arni Magnusson and Julian Burgos, based on an earlier function by Gregory R. Warnes. } \seealso{ \code{\link{quantile}} is the corresponding univariate equivalent. The \pkg{distfree.cr} package uses a different smoothing algorithm to calculate bivariate empirical confidence regions. \code{ci2d} in the \pkg{gplots} package is a predecessor of \code{conf2d}. \code{\link{freq2d}} calculates a discrete frequency distribution for two continuous variables. \code{\link{r2d2-package}} gives an overview of the package. } \examples{ conf2d(Ushape)$prop conf2d(saithe, pch=16, cex=1.2, col.points=rgb(0,0,0,0.1), lwd=3) # First surface, then region plot(saithe, col="gray") surf <- MASS::kde2d(saithe$Bio, saithe$HR, h=0.25, n=100) region <- conf2d_int(saithe$Bio, saithe$HR, surf, level=0.95, n=200) polygon(region, lwd=2) } % Graphics \keyword{dplot} % Statistics \keyword{distribution} \keyword{multivariate} \keyword{smooth} r2d2/man/saithe.Rd0000644000176200001440000000201514705604420013342 0ustar liggesusers\name{saithe} \alias{saithe} \docType{data} \title{MCMC Results from Saithe Assessment} \description{ Markov chain Monte Carlo results from the analysis of the saithe (\emph{Pollachius virens}) fishery in Icelandic waters. } \usage{saithe} \format{ Data frame containing 1000 rows and 2 columns: \tabular{ll}{ \code{Bio}\tab population biomass in 2013, relative to the expected long-term biomass under optimal harvest rate.\cr \code{HR}\tab harvest rate in 2013, relative to the optimal harvest rate. } } \references{ Magnusson, A. (2013). Icelandic saithe. In: \emph{Report of the North Western Working Group (NWWG)}. ICES CM 2013/ACOM:07, pp. 231--252. \doi{10.17895/ices.pub.5284}. Magnusson, A., Punt, A.E., and Hilborn, R. (2013). Measuring uncertainty in fisheries stock assessment: the delta method, bootstrap, and MCMC. \emph{Fish and Fisheries} \bold{14}, 325--342. \doi{10.1111/j.1467-2979.2012.00473.x}. } \examples{ conf2d(saithe, level=0.9) freq2d(saithe) } \keyword{datasets} r2d2/man/Ushape.Rd0000644000176200001440000000060114705604420013311 0ustar liggesusers\name{Ushape} \alias{Ushape} \docType{data} \title{U-Shaped Cloud} \description{ Bivariate scatter shaped like an open circle, for testing spatial algorithms. } \usage{Ushape} \format{ Matrix containing 1000 rows and 2 columns: \tabular{ll}{ \code{x}\tab x coordinates.\cr \code{y}\tab y coordinates. } } \examples{ freq2d(Ushape) conf2d(Ushape) } \keyword{datasets} r2d2/man/freq2d.Rd0000644000176200001440000000552714705604420013263 0ustar liggesusers\name{freq2d} \alias{freq2d} \alias{freq2d.default} \alias{freq2d.formula} \title{Bivariate (Two-Dimensional) Frequency Distribution} \description{ Calculate a frequency distribution for two continuous variables. } \usage{ freq2d(x, \dots) \method{freq2d}{formula}(formula, data, subset, \dots) \method{freq2d}{default}(x, y, n=20, pad=0, layout=1, print=TRUE, dnn=NULL, \dots) } \arguments{ \item{x}{a vector of x values, or a data frame whose first two columns contain the x and y values.} \item{y}{a vector of y values.} \item{formula}{a \code{\link{formula}}, such as \code{y~x}.} \item{data}{a \code{data.frame}, \code{matrix}, or \code{list} from which the variables in \code{formula} should be taken.} \item{subset}{an optional vector specifying a subset of observations to be used.} \item{n}{the desired number of bins for the output, a scalar or a vector of length 2.} \item{pad}{number of rows and columns to add to each margin, containing only zeros.} \item{layout}{one of three layouts for the output: \code{1}, \code{2}, or \code{3}.} \item{print}{whether to display the resulting table on the screen using dots for zeros.} \item{dnn}{the names to be given to the dimensions in the result.} \item{\dots}{named arguments to be passed to the default method.} } \details{ The exact number of bins is determined by the \code{\link{pretty}} function, based on the value of \code{n}. Padding the margins with zeros can be helpful for subsequent analysis, such as smoothing. The \code{print} logical flag only has an effect when \code{layout=1}. } \value{ The \code{layout} argument specifies one of the following formats for the binned frequency output: \enumerate{ \item{\code{table} that is easy to read, aligned like a scatterplot.} \item{\code{list} with three elements (x, y, table) that can be passed to various plotting functions.} \item{\code{data.frame} with three columns (x, y, frequency) that can be analyzed further.} } } \author{Arni Magnusson.} \seealso{ \code{\link{cut}}, \code{\link{table}}, and \code{\link{print.table}} are the basic underlying functions. \code{hist2d} in the \pkg{gplots} package is a related function with graphical capabilities. \code{\link{conf2d}} calculates a bivariate empirical confidence region, a smooth polygon. \code{\link{r2d2-package}} gives an overview of the package. } \examples{ freq2d(Ushape) freq2d(quakes$long, quakes$lat, dnn="") freq2d(lat~long, quakes, n=c(10,20), pad=1) # Supress display freq2d(saithe) range(freq2d(saithe, print=FALSE)) # Layout, plot freq2d(saithe, layout=2) freq2d(saithe, layout=3) contour(freq2d(saithe, layout=2)) lattice::contourplot(Freq~Bio+HR, freq2d(saithe,layout=3)) } % Graphics \keyword{dplot} % Basics \keyword{manip} % Statistics \keyword{distribution} \keyword{multivariate} r2d2/DESCRIPTION0000644000176200001440000000163114705622632012541 0ustar liggesusersPackage: r2d2 Version: 1.0.2 Date: 2024-10-22 Title: Bivariate (Two-Dimensional) Confidence Region and Frequency Distribution Authors@R: c(person("Arni", "Magnusson", role=c("aut","cre"), email="thisisarni@gmail.com"), person("Julian", "Burgos", role="aut"), person(c("Gregory","R."), "Warnes", role="ctb")) Imports: graphics, grDevices, KernSmooth, MASS, sp Suggests: lattice LazyData: yes Description: Generic functions to analyze the distribution of two continuous variables: 'conf2d' to calculate a smooth empirical confidence region, and 'freq2d' to calculate a frequency distribution. License: GPL-3 URL: https://github.com/arni-magnusson/r2d2 NeedsCompilation: no Packaged: 2024-10-22 03:11:32 UTC; arnim Author: Arni Magnusson [aut, cre], Julian Burgos [aut], Gregory R. Warnes [ctb] Maintainer: Arni Magnusson Repository: CRAN Date/Publication: 2024-10-22 04:10:02 UTC