James-Thorson-NOAA / VAST

Spatio-temporal analysis of univariate or multivariate data, e.g., standardizing data for multiple species or stages
http://www.FishStats.org
GNU General Public License v3.0
124 stars 53 forks source link

Default for c_iz argument in make_data() needs to be a matrix #255

Open John-R-Wallace-NOAA opened 4 years ago

John-R-Wallace-NOAA commented 4 years ago

Hi Jim,

Vlada and I were working on doing VAST comps this Spring and your talk got me back to trying a 2 sex model on Tantalus. There was a large delay as Marcus in IT and I got R-MKL 4.X working with support packages for VAST ( https://github.com/John-R-Wallace-NOAA/R_4.X_MRO_Windows_and_R_MKL_Linux ).

The default for the 'c_iz' argument in make_data() needs to be a matrix since the default for the next argument, 'e_i', wants a matrix for which it takes the first column:

VAST::make_data 

 function (b_i, a_i, t_i, c_iz = rep(0, length(b_i)), e_i = c_iz[,  1], v_i = rep(0, length(b_i)),   ...

To find this, I followed back this error:

### Making data object
Error in matrix(ObsModel_ez, ncol = 2, nrow = n_e, byrow = TRUE) : 
 invalid 'nrow' value (too large or NA)

Using:

c_iz = matrix(rep(0, length(b_i)), ncol = 1)

explicitly in the call to fit_model() is now working for me.

James-Thorson-NOAA commented 4 years ago

I'm not following, do you have a simple example of this problem?

There are many instances on the wiki of using c_i is a vector, so presumably your problem is more context dependent than you're stating...?

John-R-Wallace-NOAA commented 4 years ago

I meant to add package versions:

Package: VAST Version: 3.6.0 Date: 2020-09-22

Package: FishStatsUtils Version: 2.8.0 Date: 2020-09-22

Perhaps you never left both c_iz and e_i at their defaults (This is c_iz, you mentioned c_i.)

I have shared the file Image.RData here:

 https://drive.google.com/drive/folders/1BtLB6Qy2lm86f7m77WJUOQccDCo3pRbY

Load that into R and run:

fit <- FishStatsUtils::fit_model( 
     settings = settings, 
        Lat_i = LengthCompWithZero$Lat, 
        Lon_i = LengthCompWithZero$Lon,
          t_i = LengthCompWithZero$Year, 
          c_i = LengthCompWithZero$Length_bin_num,
          b_i = LengthCompWithZero$First_stage_expanded_numbers,
  #       c_iz = matrix(rep(0, length(b_i)), ncol = 1),
          a_i = LengthCompWithZero$AreaSwept_km2, 
   model_args = list(Npool = 200), newtonsteps = 1, test_fit = c(TRUE, FALSE)[2] )

### Writing output from `fit_model` in directory: ...

### Making extrapolation-grid
Using strata c("Coastwide", "CA", "OR", "WA")c(49, 42, 46, 49)c(32, 32, 42, 46)c(55, 55, 55, 55)c(549, 549, 549, 549)
For the UTM conversion, automatically detected zone 10.
# Reducing extrapolation-grid from 45628 to 3000 cells for Region(s): California_current
Num=1 Current_Best=Inf New=296049.4
Num=2 Current_Best=296049.4 New=296252.9
...
Num=99 Current_Best=291365.9 New=296729.5
Num=100 Current_Best=291365.9 New=295467.5

### Making spatial information
Num=1 Current_Best=Inf New=83698.2
Num=2 Current_Best=83698.2 New=88892.1
...
Num=99 Current_Best=79974 New=80655.7
Num=100 Current_Best=79974 New=92221.6
Calculated and saved to ...

### Making data object
Error in matrix(ObsModel_ez, ncol = 2, nrow = n_e, byrow = TRUE) : 
  invalid 'nrow' value (too large or NA)
John-R-Wallace-NOAA commented 4 years ago

I figured out that the error:

 ### Making data object
 Error in matrix(ObsModel_ez, ncol = 2, nrow = n_e, byrow = TRUE) : 
  invalid 'nrow' value (too large or NA)

happens in VAST::make_data() when there are NA's in c_iz and e_i is at its default of:

 163 e_i = c_iz[,1]

At line 261 (see below) the NA's are removed, but on line 262 the NA's are not removed, which makes n_e = NA and leads to the error on line 270.

 256  # Coerce c_iz to be a matrix
 257  if( !is.matrix(c_iz) ) c_iz = matrix(c_iz,ncol=1)

 259  # Determine dimensions
 260 n_t = max(tprime_i,na.rm=TRUE) + 1  
 261 n_c = max(c_iz,na.rm=TRUE) + 1 # NA's removed 
 262 n_e = max(e_i) + 1             # NA's not removed => n_e is NA
 263 n_v = length(unique(v_i))   # If n_v=1, then turn off overdispersion later
 264 n_i = length(b_i)
 265 n_x = nrow(a_gl)
 266 n_l = ncol(a_gl)
 267 n_g = ifelse( is.null(spatial_list), 1, spatial_list$n_g )

 269  # Coerce ObsModel_ez to be a matrix
 270  if( !is.matrix(ObsModel_ez) ) ObsModel_ez = matrix( ObsModel_ez, ncol=2, nrow=n_e, byrow=TRUE )  # Error since n_e = NA

After keeping NA's out of c_iz, VAST ver 3.6 has worked well for a long running length comp model.