Skip to content

Commit

Permalink
Function curve_area
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Alvarez committed Oct 27, 2016
1 parent c1ec8d3 commit 6fd049b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: Lexiguel
Version: 0.0.0.9008
Version: 0.0.0.9009
Date: 2016-10-27
Title: Miscellaneous Functions
Authors@R: person("Miguel", "Alvarez", email="[email protected]",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ importFrom("stats", "terms")

## Export regular function names
export(
curve_area,
error_bars,
gap_id,
gap_fill,
Expand Down
6 changes: 3 additions & 3 deletions R/sloss.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
################################################################################

# Internal function calculating area below the curve (trapezoids)
curve_area <- function(x, y) {
D1 <- c(0, diff(x))
D2 <- c(0, diff(y))
curve_area <- function(x, y, bottom=0) {
D1 <- c(bottom, diff(x))
D2 <- c(bottom, diff(y))
Area <- sum(D1*y + D1*D2/2)
return(Area)
}
Expand Down
47 changes: 47 additions & 0 deletions man/curve_area.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
\name{curve_area}
\alias{curve_area}

\title{Calculation of area below a curve.}
\description{
This function calculates the area below a curve by the trapezoidal rule.
}
\usage{
curve_area(x, y, bottom=0)
}
\arguments{
\item{x}{A numerical vector with the ordinate values.}
\item{y}{A numerical vector with the abscissa values.}
\item{bottom}{A numerical value indicating the bottom for area calculation.}
}
\details{
The argument \code{bottom} can be adjusted to the minimum observed values an
will be particularly useful when values of \code{y} are negative.

Curves going bellow the \code{bottom} value will get a negative value of area
and therefore subtracted when \code{y} cuts the bottom value.

This function was originally written as internal function for the calculations
done by \code{\link{sloss}}.
}
\value{
A numeric value.
}
\author{
Miguel Alvarez (\email{malvarez@uni-bonn.de}).
}
\examples{
## Load gaps from the Robinson Crusoe Island
library(Lexiguel)
data(rc_gaps)
data(rc_gaps.env)

## Calculation of curves
rc_curves <- sloss(rc_gaps, rc_gaps.env, area)

## Area calculated by function
rc_curves$Index

## Cross-check
with(rc_curves$SL, curve_area(area, species))/with(rc_curves$LS,
curve_area(area, species))
}
2 changes: 2 additions & 0 deletions man/gap_fill.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ is the vector \code{x} with NAs replaced by custom values.
Miguel Alvarez (\email{malvarez@uni-bonn.de}).
}
\examples{
library(Lexiguel)

## A vector with two gaps
A <- c(1:5, NA, NA, 7:10, NA, NA, NA, 16:20)
A
Expand Down

0 comments on commit 6fd049b

Please sign in to comment.