flr / flcore Goto Github PK
View Code? Open in Web Editor NEWCore package of FLR, fisheries modelling in R
Core package of FLR, fisheries modelling in R
Bug in:
Lines 183 to 184 in 12ef46c
The following generates an error in the lattice panels:
library(FLCore)
# Loading required package: lattice
# Loading required package: iterators
# FLCore (Version 2.6.12, packaged: 2019-01-22 16:23:11 UTC)
data(ple4)
plot(ple4)
plot(propagate(ple4, 10))
It is fixed if replaced by:
do.call(xyplot, c(list(x=data ~ year|panel, data=obj, panel=pfun,
groups=expression(qname), options = options), options))
Those four elements in @range are not different from the object dimensions, and could be computed on the fly from the object, saving lots of updating in methods that change the dimensions of an FLComp object, say '['
The range() method would return them in the same format, but certain attributes, life fmin, fmax and plusgroup would stay there.
Other packages accessing those numbers directly e.g. object@range['min'], would have to change to
range(object)['min']
or better still
range(object, 'min')
A simple
/pkg/R $ grep '@range' *.R
would show the places.
qmax simply passes the @.Data arrays to pmax, but maybe it could deal better with cases where one dim is different between FLQuants.
eom
When transforming a FLBiol object, with more than one iteration, into a FLStock object, the object obtained has different number of iterations within slots, as the number of iterations is set to one for the slots which are not inherited from the original object (i.e. all slots except: stock.n, stock.wt, m, mat, m.swpn and harvest.spwn).
Here it is an example and information on my R session:
library(FLBEIA)
data(oneIt)
a <- as(oneItBio[[1]],'FLStock')
dim(a@stock)
[1] 1 36 1 1 1 1
dim([email protected])
[1] 12 36 1 1 1 3
dim(a@catch)
[1] 1 36 1 1 1 1
dim([email protected])
[1] 12 36 1 1 1 1sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252 LC_MONETARY=Spanish_Spain.1252
[4] LC_NUMERIC=C LC_TIME=Spanish_Spain.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] FLAssess_2.5.20150717 FLash_2.5.7 FLBEIA_1.15 ggplot2_2.2.1 FLFleet_2.5.20150311
[6] FLCore_2.6.4.9000 lattice_0.20-33 MASS_7.3-45 devtools_1.12.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.11 magrittr_1.5 munsell_0.4.3 colorspace_1.2-6 R6_2.1.2
[6] stringr_1.0.0 httr_1.2.1 plyr_1.8.4 tools_3.3.1 grid_3.3.1
[11] gtable_0.2.0 latticeExtra_0.6-28 git2r_0.15.0 withr_1.0.2 lazyeval_0.2.0
[16] digest_0.6.9 assertthat_0.1 tibble_1.1 Matrix_1.2-6 reshape2_1.4.2
[21] RColorBrewer_1.1-2 curl_0.9.7 memoise_1.0.0 labeling_0.3 stringi_1.1.1
[26] scales_0.4.1 stats4_3.3.1
The following example fails with R 3.2.3, but not R 3.1.0 - No name is passed to the newly created level in an FLStocks object.
library(FLCore)
data(ple4)
# Ex. adding levels to an empty FLStocks obj by name fails to record name in R 3.2.3, but not in R 3.1.0
tmp <- FLStocks()
tmp[["stk1"]] <- ple4
summary(tmp)
names(tmp)
length(tmp)
tmp[["stk2"]] <- ple4
summary(tmp)
names(tmp)
length(tmp)
Note: method with signature 'FLQuant#ANY#ANY' chosen for function 'ifelse',
target signature 'FLQuant#numeric#FLQuant'.
"ANY#ANY#FLQuant" would also be valid
Propagate behaviour for FLQuants is that, if is the object has n
iters and you ask for n
iters you get the object back, which is quite nice :)
flq <- propagate(FLQuant(1), 2)
identical(flq, propagate(flq, 2))
## [1] TRUE
But for FLPar object we get an error
flp <- propagate(FLPar(1), 2)
propagate(flp, 2)
## Error in do.call("[<-", c(list(x = [email protected]), dx, list(value = value))) :
## number of items to replace is not a multiple of replacement length
Would it be possible to have the same behaviour for FLPar objects?
Both methods are somehow different and they appear to give different results. They should be checked, and maybe make one method call the other, if possible.
dims only returns the max iters from the FLQuant slots in an FLComp. Should it not return the max iters in FLPar and FLQuant slots
Should work as as.FLQuant(data.frame) does, talking as input the output of as.data.frame(FLStock). Need to think about units: add them as attribute, or if not there being given as list in argument.
The function dimnames allows to change the names of the dimensions for all the slots from an FLStock all at once.
However, when I try to do this for the first dimensión I get an error. I am working with an stock in biomass and therefore the age dimension should be "all".
Reproducing the error:
data(ple4)
dat <- ple4[1,]
dimnames(dat) <- list(age='all')
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘range<-’ for signature ‘"FLStock", "character", "character"’
It can be avoidid with this trick:
dimnames(dat) <- list(quant='all')
dimnames(dat) <- list(age='all')
But, is there any other way to do it? I guess this comes from the fact that if I change the names in the age dimension, it also tries to make changes in range slot, but it is not possible as "all" is not numeric.
I'm having difficulties trying to replace some NAs in the @mat slot of an FLBiol object (also applies to @fec slot). The error reads "Error in names(lst) <- names(x) : 'names' attribute [1] must be the same length as the vector [0]". As far I can tell, the names of FLQuant objects (original and replacement) are the same, so I don't see where the problem lies.
Here is an example:
library(FLCore)
data(ple4.biol)
# replacement fails
tmp <- replace(ple4.biol@mat$mat, ple4.biol@mat$mat==0, 0.1)
ple4.biol@mat$mat <- tmp
# names are identical
names(ple4.biol@mat$mat)
names(tmp)
# same operation in another slot works
tmp <- replace(ple4.biol@spwn, ple4.biol@spwn==0, 0.1)
ple4.biol@spwn <- tmp
Some colleagues and I are having some difficulty setting up parallelization using FLR. Specifically, we are trying to apply a genetic algorithm (GA::ga), which I believe uses the parallel
package. As long as we leave parallel=FALSE in that function, it runs without errors. With parallelization, we receive the following error
Error in { :
task 1 failed - "error in evaluating the argument 'j' in selecting a method for function '[<-': Error in Summary.factor(1L, na.rm = FALSE) :
'min' not meaningful for factors
Calls: ac -> Summary.factor
"
This code is not part of our "fitness" function, nor do I see it within ga
, so my guess is that it may be somewhere in FLCore. I had a previous problem with parallelization using the parallel
package, which I believe dealt with conflicting iter()
functions. I solved this by using snow
for parallelization. I'm an not sure if this is a related issue, but was hoping someone might have some ideas.
Cheers
qapply(FLBiol)
should apply the function to any predictModel slot, as they are considered extensions of FLQuant
The function fails when the slots have different number of iterations
a<-setPlusGroup(stk, 20)
[1] TRUE
Error in do.call("[<-", c(list(x = y), iper, list(value = aperm(unname(value), :
number of items to replace is not a multiple of replacement length In addition: Warning messages:
1: In FLCore:::uom(op, units(e1), units(e2)) :
incompatible units of measurements in FLQuant objects: NA + f
2: In FLCore:::uom(op, units(e1), units(e2)) :
incompatible units of measurements in FLQuant objects: f - NA
3: In FLCore:::uom(op, units(e1), units(e2)) :
incompatible units of measurements in FLQuant objects: f + NA
8: do.call("[<-", c(list(x = y), iper, list(value = aperm(unname(value),
dper))))
7: .local(x, i, j, ..., value = value)
6:[<-
(*tmp*
, ac((oldMaxage):maxage), value = c(6681.42197841516,
5048.06055397189, 3813.99579893194, 2881.61439403199, 2177.16587894976,
...
5:[<-
(*tmp*
, ac((oldMaxage):maxage), value = c(6681.42197841516,
5048.06055397189, 3813.99579893194, 2881.61439403199, 2177.16587894976,
...
4: expandAgeFLStock(x, plusgroup, keepPlusGroup = keepPlusGroup)
3: .local(x, plusgroup, ...)
2: setPlusGroup(stk, 20)
1: setPlusGroup(stk, 20)
a<-setPlusGroup(stk, 20)
[1] TRUE
Error in do.call("[<-", c(list(x = y), iper, list(value = aperm(unname(value), :
number of items to replace is not a multiple of replacement length In addition: Warning messages:
1: In FLCore:::uom(op, units(e1), units(e2)) :
incompatible units of measurements in FLQuant objects: NA + f
2: In FLCore:::uom(op, units(e1), units(e2)) :
incompatible units of measurements in FLQuant objects: f - NA
3: In FLCore:::uom(op, units(e1), units(e2)) :
incompatible units of measurements in FLQuant objects: f + NA
8: do.call("[<-", c(list(x = y), iper, list(value = aperm(unname(value),
dper))))
7: .local(x, i, j, ..., value = value)
6:[<-
(*tmp*
, ac((oldMaxage):maxage), value = c(6681.42197841516,
5048.06055397189, 3813.99579893194, 2881.61439403199, 2177.16587894976,
...
5:[<-
(*tmp*
, ac((oldMaxage):maxage), value = c(6681.42197841516,
5048.06055397189, 3813.99579893194, 2881.61439403199, 2177.16587894976,
...
4: expandAgeFLStock(x, plusgroup, keepPlusGroup = keepPlusGroup)
3: .local(x, plusgroup, ...)
2: setPlusGroup(stk, 20)
1: setPlusGroup(stk, 20)
Equivalent to
ple4 <- qapply(ple4, function(x) {
dimnames(x)[['year']] <- as.numeric(dimnames(x)[['year']]) + 10
return(x)
})
but fixing range too. Should work on common dimnames across all FLQs (e.g. 'year')
The helpfile should include an example for each implemented SR model. There are 15 models/submodels and an example for only BH. In particular, we should include a geomean example since that is not covered in the tutorial either.
I am trying to replace some specific elements in an FLQuant, as identified by a 1d vector of indices.
The only way that I have found to do this is by some trickery of converting the FLQuant to an array during the replacement operation. I'm a bit confused as to why one needs to do this, given the FLQuant is already a 6d array. My fix seems to work, but I thought I would notify you of this behaviour.
Here's an example:
# load data
library(FLCore)
data("ple4.biol")
data("ple4")
ple4 <- window(ple4, start=2006, end=2008)
ple4.biol <- window(ple4.biol, start=2006, end=2008)
# identify indices where discards.wt equals zero
idx <- which([email protected] == 0)
idx # 9 10 19 20 29 30
# viewing requires 'drop=T'
[email protected][idx] # this doesn't work
[email protected][idx, drop=TRUE] # this does
# How to replace specific indicies? Direct replacement doesn't work
[email protected][idx] <- ple4.biol@wt[idx]
# Error in [email protected][i, j, k, l, m, n, drop = FALSE] :
# subscript out of bounds
# Is this an acceptable solution, or should class FLQuant allow a more direct replacement?
as([email protected], 'array')[idx] <- ple4.biol@wt[idx, drop=TRUE]
[email protected]
FLCore::vb methods calculate the vulnerable biomass according to either the global selectivity, as contained in harvest, or a new FLQuant, for a given fleet.
A new name is needed for the method, referring to exploitable biomass, and whether other calculations are needed.
I have just updated both FLCore and FLBEIA to current versions on GitHub, and am now getting an error that I didn't have before.
I have traced the error to MANAGEMENT PROCEDURE / OBSERVATION MODEL step in FLBEIA; specifically, in the functionFLBEIA:::observation.mp
, FLBEIA:::perfectObs
is called in my example. Therein is a step where a given FLBiol object is converted to FLStock as such:
res <- as(biol, "FLStock")
I get the error:
Error in if (!is.na(dms$min) & ran["minfbar"] < dms$min) return("minfbar is lower than first age") : missing value where TRUE/FALSE needed
.
I have tried to create a minimal example outside of FLBEIA. I get an error, but it is slightly different:
library(FLCore)
data("ple4.biol")
tmp <- as(ple4.biol, "FLStock")
# Error in validObject(res) : invalid class “FLStock” object:
# All elements must share quant names: Error in FLStock harvest.spwn
Maybe the FLBEIA folks (@dorleta, @ssanchezAZTI) are aware of this issue?
Thanks for your help.
Hi There,
I just noticed that window(biol, start=, end=)
does not work completely on the new FLBiol class. The elements mat
, fec
and rec
are not affected. Perhaps this also hols true for trim()
.
Hi,
I get the error:
"Error in names(object@mat) <- name :
'names' attribute [200] must be the same length as the vector [1]"
when using window() on an FLBiol to expand the year range. This has only occurred since the change to FLCore_2.6.5.9018, so something to do with the new predictModel for fec and mat. Note though, it did not occur in the previous version I was using (.9010, I think) which already had the predictModel class.
A minimum example is:
biol <- FLBiol()
window(biol, 1, 200)
Any ideas?
Thanks,
Paul
It gives the warning
In [<-.factor
(*tmp*
, ri, value = c(1, 1, 1, 1, 1, 1, 1, 1, 1, :
invalid factor level, NA generated
which should be removed.
Just a small clarification needed for the FLStock class - The help file states that the harvest slot should contain "Harvest rate or fishing mortality. The units of the FLQuant should be set to 'harvest' or 'f' accordingly (FLQuant)". From what I can tell, "hr" should be defined as the unit in the case of harvest rate. Or are both "hr" and "harvest" accepted?
Can FLStockLen use breaks that are unequal? If so, what is halfwidth meant to return?
stkl <- FLStockLen(m=FLQuant(0.2, dimnames=list(len=c(5,10,20,25,50), year=2015:2020)))
halfwidth(stkl)
[1] 2.5 5.0 2.5 12.5
breaks(stkl)
[1] 5 10 20 25 50 55 60 55 75
Should this be valid?
Should this be allowed?
Behaviour is weird. Tried to repeat example with ple4 but couldn't
old max age, and new max age (plus group) overwritten with NA
hi you lot FL-lot
please modernize you package code. use Depends rather than Imports in DESCRIPTION.
and change your codes within functions accordingly (ie. to things like MASS::whatever)
say in part because your usage of Imports loads all functions from a referred package. like all functions from MASS. including MASS::select. which you may not actually be using at all.
but is in conflict with dplyr::select
einar
ps! is anything like a vague vignette being cooked up by you lot? say this because there has been absolutely no user manual available since you closed your wiki-page some years ago.
Change in behaviour occurs in commit: 6704455
Starting with a simple 2D quant:
library(FLCore)
## FLCore (Version 2.6.3.9005, packaged: Sat Sep 30 02:06:33 2017)
(x <- FLQuant(as.numeric(paste0(rep(1:2, 9),
rep(1:9, each = 2))), dim = c(2, 9)))
## An object of class "FLQuant"
## , , unit = unique, season = all, area = unique
##
## year
## quant 1 2 3 4 5 6 7 8 9
## 1 11 12 13 14 15 16 17 18 19
## 2 21 22 23 24 25 26 27 28 29
##
## units: NA
commit: 26fc70c
install_github("flr/flcore@26fc70ceceb1608a44440c8f4688e822fe8e731d")
## Reloading installed FLCore
## FLCore (Version 2.6.3.9005, packaged: Sat Sep 30 02:07:00 2017)
# up till now the following are and have been equivalent
x@.Data[1:2,,,,,, drop=TRUE]
## year
## quant 1 2 3 4 5 6 7 8 9
## 1 11 12 13 14 15 16 17 18 19
## 2 21 22 23 24 25 26 27 28 29
x[1:2,,,,,, drop=TRUE]
## year
## quant 1 2 3 4 5 6 7 8 9
## 1 11 12 13 14 15 16 17 18 19
## 2 21 22 23 24 25 26 27 28 29
x[1:2, drop=TRUE]
## year
## quant 1 2 3 4 5 6 7 8 9
## 1 11 12 13 14 15 16 17 18 19
## 2 21 22 23 24 25 26 27 28 29
following commit: 6704455
install_github("flr/flcore@6704455aa29390c834310572c165025341be3231")
## Reloading installed FLCore
## FLCore (Version 2.6.3.9005, packaged: Sat Sep 30 02:07:25 2017)
# after this commit the following are no longer equivalent
x@.Data[1:2,,,,,, drop=TRUE]
## year
## quant 1 2 3 4 5 6 7 8 9
## 1 11 12 13 14 15 16 17 18 19
## 2 21 22 23 24 25 26 27 28 29
x[1:2,,,,,, drop=TRUE]
## [1] 11 21
x[1:2, drop=TRUE]
## [1] 11 21
units(FLIndex(effort=FLQuant(1:10, units='kw')))
In the a4a submodels class we inherit from FLComp to get a name, desc and range slot. But there is no slot containing an array (i.e. it is not an FLArray decendent).
The FLComp methods
dims
and
summary
require the object to be coerceable to an FLArray with at least one slot, so both these methods fail for our submodel class.
Quick example:
setClass("comptest",
contains = "FLComp",
slots = c(data = "numeric")
)
x <- new("comptest", data = 1:5, name = "test", range = c(min = 1, max = 7, plusgroup = NA, minyear = 1990, maxyear = 2000))
x
## An object of class "comptest"
## Slot "data":
## [1] 1 2 3 4 5
##
## Slot "name":
## [1] "test"
##
## Slot "desc":
## character(0)
##
## Slot "range":
## min max plusgroup minyear maxyear
## 1 7 NA 1990 2000
##
range(x)
## min max plusgroup minyear maxyear
## 1 7 NA 1990 2000
summary(x)
## An object of class "comptest"
##
## Name: test
## Description:
## Error in slot(X, slots[1]) :
## no slot of name "NA" for this object of class "comptest"
dims(x)
## Error in slot(X, slots[1]) :
## no slot of name "NA" for this object of class "comptest"
I am trying to create an FLStocks object from various individual FLStock objects, and get the following message:
> stocks <- FLStocks(sapply(paste0(stk_names, "_stock"), FUN = get))
Error in if (!is.na(ran["plusgroup"]) & ran["plusgroup"] > dms$max) return("plusgroup is higher than last age") :
missing value where TRUE/FALSE needed
I have tracked this error to the following code, and the error is caused by several stock objects that contain a single age group:
> range(NEP9_stock)
min max plusgroup minyear maxyear minfbar maxfbar
1 1 1 2003 2015 1 1
> unlist(dims(NEP9_stock))
quant age min max year minyear maxyear plusgroup unit
"age" "1" NA NA "13" "2003" "2015" "1" "1"
season area iter
"1" "1" "1"
Somehow, the dims
function is returning NAs even though values are correctly contained in @range. This code worked before my latest FLCore update.
Here in one other example illustrating the issue with a reproducible example:
# make FLStock
flq <- FLStock(FLQuant(rnorm(90), dimnames=list(age=1:10, year=2000:2016)))
range(flq)
unlist(dims(flq))
# trim to one age
flq2 <- trim(flq, year=2000:2005, age=1)
flq2@range["plusgroup"] <- 1
range(flq2)
unlist(dims(flq2))
Thanks in advance for any advice on how I might proceed with creating my FLStocks object.
It would be simpler to use the same name for the 'qname' and 'slot' columns currently output by as.data.frame, for example
data(ple4)
names(as.data.frame(FLQuants(ssb=ssb(ple4), fbar=fbar(ple4))))
names(as.data.frame(ple4))
This isn't working;
b <- FLBiol()
b_ext <- window(b, 1, 20) ## Now working
b_prop <- propagate(b_prop, iter =2)
Gives
"Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘propagate’ for signature ‘"predictModel"’
Paul
The show
method for FLComp
-derived classes is simply carrying out a print of the whole object contents. This is not terribly useful, in my opinion. I suggest we define show(FLComp)
to call summary, which is already class-specific. The full output of an object can still be obtained using print()
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.