
Computes a vector of relative weights specific to a species in an entire data frame.
Source:R/wrAdd.R
wrAdd.Rd
Returns a vector that contains the relative weight specific to each species for all individuals in an entire data frame.
Arguments
- wt
A numeric vector that contains weight measurements or a formula of the form
wt~len+spec
where “wt” generically represents the weight variable, “len” generically represents the length variable, and “spec” generically represents the species variable. Note that this formula can only contain three variables and they must be in the order of weight first, length second, species third.- ...
Not used.
- len
A numeric vector that contains length measurements. Not used if
wt
is a formula.- spec
A character or factor vector that contains the species names. Not used if
wt
is a formula.- units
A string that indicates whether the weight and length data in
formula
are in"metric"
(DEFAULT; mm and g) or"English"
(in and lbs) units.- WsOpts
A named list that provides specific choices for
group
,ref
, ormethod
for species for which more than one standard weight equation exists inWSlit
.- data
A data.frame that minimally contains variables of the the observed lengths, observed weights, and the species names given in the
formula=
.
Details
This computes a vector that contains the relative weight specific to each species for all individuals in an entire data frame. The vector can be appended to an existing data.frame to create a variable that contains the relative weights for each individual. The relative weight value will be NA
for each individual for which a standard weight equation does not exist in WSlit
, a standard weight equation for the units given in units=
does not exist in WSlit
, or if the individual is shorter or longer than the lengths for which the standard weight equation should be applied. Either the linear or quadratic equation has been listed as preferred for each species, so only that equation will be used.
The species names in species
must match the spelling and capitalization of species
in WSlit
. Use wsVal()
to see a list of all species for which standard weight equations exist in WSlit
and, more importantly, how the species names are spelled and capitalized.
Some (few) species have more than one equation listed in WSlit
(for the specified units). In these instances the user must select one of the equations to use with WsOpts
. WsOpts
is a list of lists where the inside list contains one or more of group
, ref
, or method
(see WSlit
) required to specify a single equation for a particular species, which is the name of the inner list. See the examples for an illustration of how to use WsOpts
.
See examples and this article for a demonstration.
References
Ogle, D.H. 2016. Introductory Fisheries Analyses with R. Chapman & Hall/CRC, Boca Raton, FL.
Author
Derek H. Ogle, DerekOgle51@gmail.com
Examples
#===== Create random data for three species
#----- just to control the randomization
set.seed(345234534)
dbt <- data.frame(species=factor(rep(c("Bluefin Tuna"),30)),
tl=round(rnorm(30,1900,300),0))
dbt$wt <- round(4.5e-05*dbt$tl^2.8+rnorm(30,0,6000),1)
dbg <- data.frame(species=factor(rep(c("Bluegill"),30)),
tl=round(rnorm(30,130,50),0))
dbg$wt <- round(4.23e-06*dbg$tl^3.316+rnorm(30,0,10),1)
dlb <- data.frame(species=factor(rep(c("Largemouth Bass"),30)),
tl=round(rnorm(30,350,60),0))
dlb$wt <- round(2.96e-06*dlb$tl^3.273+rnorm(30,0,60),1)
df <- rbind(dbt,dbg,dlb)
str(df)
#> 'data.frame': 90 obs. of 3 variables:
#> $ species: Factor w/ 3 levels "Bluefin Tuna",..: 1 1 1 1 1 1 1 1 1 1 ...
#> $ tl : num 1371 1558 2031 2226 2124 ...
#> $ wt : num 19231 38147 90530 104718 88761 ...
#===== Add Wr variable
#----- using formula interface
df$Wr1 <- wrAdd(wt~tl+species,data=df)
#----- same but with non-formula interface
df$Wr2 <- wrAdd(df$wt,df$tl,df$species)
#----- same but using dplyr
if (require(dplyr)) {
df <- df %>%
mutate(Wr3=wrAdd(wt,tl,species))
}
#----- examine results
peek(df,n=10)
#> species tl wt Wr1 Wr2 Wr3
#> 1 Bluefin Tuna 1371 19231.0 NA NA NA
#> 10 Bluefin Tuna 2060 84458.4 NA NA NA
#> 20 Bluefin Tuna 2545 142705.1 NA NA NA
#> 30 Bluefin Tuna 1889 59611.6 NA NA NA
#> 40 Bluegill 52 -5.8 NA NA NA
#> 50 Bluegill 122 36.7 104.78446 104.78446 104.78446
#> 60 Bluegill 72 19.0 NA NA NA
#> 70 Largemouth Bass 396 900.0 95.49622 95.49622 95.49622
#> 80 Largemouth Bass 328 553.9 108.88706 108.88706 108.88706
#> 90 Largemouth Bass 371 612.6 80.46670 80.46670 80.46670
#===== Example with only one species in the data.frame
bg <- droplevels(subset(df,species=="Bluegill"))
bg$Wr4 <- wrAdd(wt~tl+species,data=bg)
bg
#> species tl wt Wr1 Wr2 Wr3 Wr4
#> 31 Bluegill 158 75.3 91.21118 91.21118 91.21118 91.21118
#> 32 Bluegill 157 80.3 99.33729 99.33729 99.33729 99.33729
#> 33 Bluegill 126 26.0 66.70289 66.70289 66.70289 66.70289
#> 34 Bluegill 203 196.2 103.52426 103.52426 103.52426 103.52426
#> 35 Bluegill 136 59.9 119.29213 119.29213 119.29213 119.29213
#> 36 Bluegill 192 159.4 101.17226 101.17226 101.17226 101.17226
#> 37 Bluegill 132 54.9 120.71165 120.71165 120.71165 120.71165
#> 38 Bluegill 121 30.7 90.07870 90.07870 90.07870 90.07870
#> 39 Bluegill 136 42.4 84.44051 84.44051 84.44051 84.44051
#> 40 Bluegill 52 -5.8 NA NA NA NA
#> 41 Bluegill 111 17.5 68.35134 68.35134 68.35134 68.35134
#> 42 Bluegill 226 267.8 98.98901 98.98901 98.98901 98.98901
#> 43 Bluegill 217 238.2 100.74912 100.74912 100.74912 100.74912
#> 44 Bluegill 163 86.9 94.93048 94.93048 94.93048 94.93048
#> 45 Bluegill 119 18.3 56.74628 56.74628 56.74628 56.74628
#> 46 Bluegill 79 17.9 NA NA NA NA
#> 47 Bluegill 78 3.5 NA NA NA NA
#> 48 Bluegill 172 113.9 104.11454 104.11454 104.11454 104.11454
#> 49 Bluegill 106 28.0 127.42181 127.42181 127.42181 127.42181
#> 50 Bluegill 122 36.7 104.78446 104.78446 104.78446 104.78446
#> 51 Bluegill 95 14.1 92.27576 92.27576 92.27576 92.27576
#> 52 Bluegill 47 -3.3 NA NA NA NA
#> 53 Bluegill 155 72.8 93.97048 93.97048 93.97048 93.97048
#> 54 Bluegill 167 101.5 102.31474 102.31474 102.31474 102.31474
#> 55 Bluegill 146 65.1 102.46768 102.46768 102.46768 102.46768
#> 56 Bluegill 125 54.5 143.56312 143.56312 143.56312 143.56312
#> 57 Bluegill 66 -7.1 NA NA NA NA
#> 58 Bluegill 160 92.1 107.00345 107.00345 107.00345 107.00345
#> 59 Bluegill 85 1.7 16.08777 16.08777 16.08777 16.08777
#> 60 Bluegill 72 19.0 NA NA NA NA
#===== Example with a species that has Ws eqns for multiple groups and a
# group needs to be specified with WsOpts
wae <- data.frame(species=factor(rep(c("Walleye"),30)),
tl=round(rnorm(30,500,200),0))
wae$wt <- round(3.33e-06*wae$tl^3.16+rnorm(30,0,50),1)
# wae$Wr <- wrAdd(wt~tl+species,data=wae) # will err b/c multiple groups
wae$Wr <- wrAdd(wt~tl+species,data=wae,
WsOpts=list(Walleye=list(group="overall")))
peek(wae,n=10)
#> species tl wt Wr
#> 1 Walleye 791 4768.4 82.25673
#> 3 Walleye 300 271.9 102.36786
#> 7 Walleye 659 2719.5 83.83656
#> 10 Walleye 493 1066.4 82.73074
#> 13 Walleye 505 1151.5 82.75559
#> 17 Walleye 676 2794.8 79.45482
#> 20 Walleye 754 4131.0 82.98781
#> 23 Walleye 500 1067.4 79.17765
#> 27 Walleye 544 1431.0 81.17740
#> 30 Walleye 490 1117.6 88.40216