Two weighted.mean methods are provided for objects of class FLQuants,
a list of FLQuant to be averaged element-wise. They differ in the
class of the weights argument, w, and, more importantly, in how each
one handles NA values in x. See NA handling below before choosing
between them.
Usage
# S4 method for class 'FLQuants,FLQuants'
weighted.mean(x, w)
# S4 method for class 'FLQuants,numeric'
weighted.mean(x, w, na.rm = TRUE, ...)Arguments
- x
An
FLQuantsobject containing the values to be averaged.- w
Weights to be used, either as an
FLQuantsobject of the same length and dimensions asx, or as anumericvector with one value per element ofx.- na.rm
Only used when
wisnumeric: shouldNAvalues be excluded element-wise, rather than propagated?logical, defaults toTRUE. Ignored, with no equivalent behaviour, whenwisFLQuants.- ...
Extra arguments, currently unused.
Details
w = "FLQuants": weights can vary across every dimension of x
(age, year, iter, ...), not just by element, since w is itself a list
of FLQuant matching x in length and dimensions. This is the method to
use when, for example, combining landings and discards weights-at-age
weighted by landings and discards numbers-at-age, where the weights
themselves change by age and year.
w = "numeric": a single scalar weight is given per element of x,
constant across all dimensions. This is the simpler case of combining a
small number of series (e.g. CPUE or survey indices) with fixed relative
importance.
NA handling — the key difference
For w = "FLQuants": NA values in x are zeroed out for the
numerator, and the corresponding weight is also zeroed via an internal
NA-flag FLQuants, so an NA element contributes neither to the
weighted sum nor to the sum of weights at that position. A small constant
(1e-36) is added to the summed weights to avoid division by zero where
every weight is zero. If the result is still NA at some position
(e.g. every element of x was NA there), it is replaced by the plain,
unweighted arithmetic mean of x, Reduce('+', x) / length(x) — note
this fallback is computed from the original x, so it can itself remain
NA if x is NA at every element for that position; this method has
no na.rm argument, the behaviour above is not toggleable.
For w = "numeric": NA handling is controlled by na.rm. With the
default na.rm = TRUE, NA values are excluded from both the weighted
sum and the sum of weights (wsum, tracked explicitly rather than via an
epsilon), so a missing index does not dilute the indices that do have
data; positions where every element of x is NA return NA directly,
with no arithmetic-mean fallback. With na.rm = FALSE, no special
handling is applied and any NA in x propagates to the result, as for
the default stats::weighted.mean.
Examples
data(ple4)
# w = "FLQuants": weights vary by age and year
x <- FLQuants(landings.wt(ple4), discards.wt(ple4))
w <- FLQuants(landings.n(ple4), discards.n(ple4))
weighted.mean(x, w)
#> An object of class "FLQuant"
#> , , unit = unique, season = all, area = unique
#>
#> year
#> age 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
#> 1 0.0440 0.0470 0.0510 0.0450 0.0440 0.0420 0.0480 0.0320 0.0380 0.0380
#> 2 0.1098 0.1055 0.1200 0.1150 0.1014 0.0993 0.1097 0.1260 0.0759 0.1043
#> 3 0.1943 0.1895 0.1915 0.2037 0.1801 0.1814 0.1748 0.2039 0.2144 0.1481
#> 4 0.2566 0.2563 0.2604 0.2871 0.3012 0.2731 0.3042 0.2710 0.3133 0.3148
#> 5 0.3491 0.3294 0.3453 0.3774 0.4024 0.3970 0.3919 0.3789 0.3814 0.4276
#> 6 0.4555 0.4523 0.4719 0.4799 0.5061 0.5380 0.5313 0.4847 0.4688 0.4829
#> 7 0.5330 0.5130 0.5920 0.6010 0.6040 0.5700 0.6240 0.6280 0.5390 0.5590
#> 8 0.5890 0.6150 0.6230 0.6830 0.6710 0.6920 0.6670 0.7000 0.6630 0.6240
#> 9 0.3960 0.6649 0.7499 0.7239 0.8119 0.7769 0.7150 0.7369 0.7259 0.6899
#> 10 0.9980 0.9920 1.0000 1.0940 1.0710 1.1270 1.0280 1.0050 0.8870 0.9330
#> year
#> age 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
#> 1 0.0360 0.0600 0.0520 0.0492 0.0571 0.0669 0.0453 0.0565 0.0688 0.0878
#> 2 0.1105 0.1163 0.1742 0.1311 0.1599 0.2072 0.2052 0.1208 0.1518 0.1809
#> 3 0.1896 0.2234 0.2716 0.2683 0.2766 0.2900 0.3340 0.3432 0.2050 0.2616
#> 4 0.2348 0.2745 0.2836 0.3522 0.3884 0.4074 0.4032 0.4037 0.3929 0.3473
#> 5 0.4220 0.3399 0.3560 0.3938 0.4443 0.4864 0.4782 0.4724 0.4925 0.5094
#> 6 0.5433 0.5161 0.4083 0.4409 0.5122 0.5398 0.5376 0.5523 0.5851 0.5916
#> 7 0.5970 0.5900 0.5730 0.4990 0.5420 0.6080 0.6050 0.6090 0.6360 0.6410
#> 8 0.6620 0.5960 0.6550 0.6720 0.6070 0.6460 0.6270 0.6929 0.7029 0.7049
#> 9 0.7379 0.6859 0.6580 0.7439 0.6989 0.6740 0.6769 0.7069 0.7829 0.7409
#> 10 0.9780 0.9110 0.8930 0.8920 0.8910 0.9390 0.8420 0.9260 1.0190 0.9800
#> year
#> age 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
#> 1 0.0713 0.0698 0.0668 0.0558 0.0484 0.0558 0.0517 0.0531 0.0541 0.0493
#> 2 0.2179 0.1903 0.1905 0.1970 0.1834 0.1520 0.1527 0.1499 0.1691 0.1414
#> 3 0.2450 0.3153 0.2954 0.3430 0.3268 0.3082 0.2748 0.2655 0.2680 0.2754
#> 4 0.3181 0.3643 0.3377 0.3994 0.4147 0.4206 0.3791 0.3210 0.3335 0.3105
#> 5 0.3960 0.4323 0.4265 0.4712 0.5017 0.5119 0.5070 0.4705 0.4439 0.4024
#> 6 0.5513 0.4864 0.4721 0.5424 0.5556 0.6059 0.6020 0.5883 0.5615 0.4718
#> 7 0.6469 0.6090 0.5500 0.5880 0.6040 0.6640 0.6770 0.6770 0.6670 0.6680
#> 8 0.7209 0.6869 0.6749 0.6620 0.6420 0.7119 0.7709 0.7259 0.7299 0.7499
#> 9 0.7149 0.7758 0.7959 0.7718 0.7249 0.7379 0.8149 0.8388 0.8069 0.8558
#> 10 0.9779 0.9499 0.9599 1.0129 1.0069 0.9839 0.9839 1.0359 1.0209 1.0139
#> year
#> age 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
#> 1 0.0430 0.0430 0.0471 0.0533 0.0563 0.0548 0.0626 0.0637 0.0711 0.0538
#> 2 0.1134 0.1018 0.1176 0.1297 0.1495 0.1454 0.1602 0.1791 0.1829 0.1402
#> 3 0.2186 0.1975 0.2151 0.2106 0.2109 0.2257 0.2500 0.2565 0.2826 0.2680
#> 4 0.3434 0.2759 0.2913 0.2905 0.2728 0.2752 0.3033 0.3314 0.3337 0.3363
#> 5 0.3754 0.4151 0.3643 0.3596 0.3494 0.3242 0.3397 0.3722 0.3729 0.4127
#> 6 0.4707 0.4767 0.5123 0.4398 0.4493 0.4103 0.4069 0.4161 0.4191 0.4636
#> 7 0.5740 0.5900 0.5910 0.5860 0.5260 0.5300 0.5120 0.4910 0.4740 0.4900
#> 8 0.7279 0.6799 0.6680 0.6899 0.6660 0.6070 0.6300 0.6100 0.5930 0.5530
#> 9 0.8348 0.8079 0.7848 0.7609 0.7429 0.7189 0.7199 0.7309 0.7339 0.7119
#> 10 0.9929 1.0169 0.9399 1.0099 0.9239 0.8909 0.8559 0.9059 0.9059 0.8579
#> year
#> age 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
#> 1 0.0451 0.0472 0.0538 0.0631 0.0901 0.0567 0.0657 0.0544 0.0674 0.0603
#> 2 0.1290 0.0939 0.1032 0.1232 0.1358 0.1308 0.1240 0.1249 0.1174 0.1394
#> 3 0.2204 0.2080 0.1986 0.2096 0.1966 0.2217 0.2264 0.2204 0.2129 0.2118
#> 4 0.3532 0.2987 0.2665 0.2742 0.2339 0.2851 0.2837 0.2969 0.2979 0.2933
#> 5 0.4084 0.4490 0.4133 0.3719 0.3032 0.3261 0.3357 0.3755 0.3516 0.3748
#> 6 0.4727 0.5440 0.4143 0.4523 0.4098 0.4264 0.3853 0.4212 0.3475 0.3834
#> 7 0.5410 0.6130 0.5379 0.5650 0.5765 0.4692 0.4191 0.5055 0.4532 0.4285
#> 8 0.5739 0.6729 0.6369 0.6004 0.7004 0.6436 0.6345 0.5598 0.5541 0.4569
#> 9 0.6159 0.6868 0.7478 0.7516 0.7954 0.7595 0.7624 0.7961 0.6169 0.5308
#> 10 0.9119 0.8989 0.8039 0.8878 0.7988 0.9037 0.8565 0.8712 0.9093 0.7477
#> year
#> age 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
#> 1 0.0588 0.0566 0.0612 0.0618 0.0476 0.0523 0.0509 0.0250 0.0260 0.0480
#> 2 0.1137 0.1235 0.1247 0.1317 0.1147 0.0962 0.0911 0.0934 0.0803 0.0838
#> 3 0.2230 0.2476 0.2321 0.2260 0.2119 0.1956 0.1786 0.1717 0.1618 0.1574
#> 4 0.3101 0.3246 0.3266 0.3113 0.2774 0.2944 0.2736 0.2533 0.2579 0.2430
#> 5 0.3512 0.3894 0.3991 0.3959 0.3695 0.3484 0.3447 0.3176 0.3256 0.2989
#> 6 0.3753 0.4368 0.4658 0.4424 0.4532 0.4254 0.4094 0.3961 0.3937 0.3525
#> 7 0.4907 0.3683 0.5184 0.4626 0.5954 0.5092 0.4902 0.4726 0.4613 0.4223
#> 8 0.3574 0.4687 0.4410 0.5734 0.4449 0.5572 0.5990 0.5416 0.4808 0.4652
#> 9 0.5865 0.6396 0.6674 0.6819 0.5558 0.5579 0.6069 0.6279 0.5820 0.5560
#> 10 0.6318 0.6377 0.7915 0.6487 0.8039 0.6798 0.6799 0.6499 0.5999 0.6839
#> year
#> age 2017
#> 1 0.0510
#> 2 0.0859
#> 3 0.1586
#> 4 0.2184
#> 5 0.3144
#> 6 0.3863
#> 7 0.4377
#> 8 0.5315
#> 9 0.6420
#> 10 0.7349
#>
#> units: kg
if (FALSE) { # \dontrun{
# w = "numeric": a single fixed weight per element of x
fqs <- FLQuants(a=catch(ple4), b=catch(ple4) * 1.1)
weighted.mean(fqs, w=c(1, 3))
} # }
