Refine your model: Diet


In this tutorial we will start to refine the mizer model that we created in the previous tutorial. That model already has the broad features correct: In all the species we decided to include are coexisting in a steady state with the desired biomasses and growth rates. Mizer itself determined the size distribution of the species. We did not need to specify many parameters to achieve that. For most of the parameters that we did supply we said that it did not matter that we could only make educated guesses for their values or even just put NA, because we could refine the model later. We will start this refinement process in this tutorial and continue it in the next.

As always, we start by updating and loading packages.

try(unload("mizerExperimental"), silent = TRUE)
remotes::install_github("sizespectrum/mizerExperimental", quiet = TRUE)

We load the model we created in the previous tutorial.

cel_model <- readParams("cel_model.rds")

When you work through this material in worksheet 3 in your worksheet repository for this week you will be using your own model that you created in worksheet 2.

Resource abundance

One bit of information that we did not supply when we set up the model was the abundance of the resource. Let us take a look at the size-spectrum plot to see what value mizer chose:

plotlySpectra(cel_model, power = 2, total = TRUE)

We have put total = TRUE to include the total community spectrum in the plot in black. At the smallest sizes the community is comprised of the resource only, plotted in green, but then at larger sizes the fish contribute. Sheldon’s observation was that the community size spectrum would be approximately flat all the way from bacteria to whales. We notice that the above plot does not quite conform to that observation. Instead, the spectrum is quite a bit lower at small sizes, then rises at the sizes where the fish dominate. It then drops off again because we have not included anything larger than cod in our model. No whales here. To get a community spectrum more in line with Sheldon’s observation we should increase the resource abundance

There is another plot that shows us that our model currently has too little resource. We plot the feeding level:


Recall from the section on the feeding level last week that the feeding level is the ratio between the maximum intake rate and the actual intake rate, so can never exceed 1. The closer it is to 1 the more satiated the fish is and the less of the encountered prey it will consume. The reason the feeding levels in the above plot is higher at larger sizes than at smaller sizes is that at larger sizes the fish start feeding on other fish while at smaller sizes they have to rely on the resource, and the resource is not as abundant as it should be.

We will now want to increase the abundance of resource, both to get the community abundance more in line with Sheldon’s observation and to give the fish a more constant feeding level throughout their life. We will first start doing this the tedious way in code and then introduce the tuneParams() shiny gadget to do it with point and click.


We don’t know by exactly what factor we need to scale up the resource. Let’s try increasing it by a factor of 2:

cel_model <- scaleDownBackground(cel_model, factor = 1/2)

That the function scales down rather than up, so that we need to set the scaling factor to 1/2 rather than 2, is a historical accident. Let’s look at the spectrum plot now:

plotlySpectra(cel_model, power = 2, total = TRUE)

The resource has increased by a factor of 2, even if this is not very noticeable on this logarithmic y axis. But we are now no longer in steady state. As always after we have made a modification, we need to run the dynamics to get back to steady state. But before we do that, we also want to match the growth rates again because they will of course have increased by increasing the resource abundance. So we do

cel_model <- cel_model |> matchGrowth() |> steady()

This has now messed up the biomasses in the model:

So we also do

cel_model <- cel_model |> matchGrowth() |> steady()

This is what the feeding levels look like now:


A little bit better but clearly not enough. So we need to do it again. But you will already have gotten the sense that this is going to be tedious: making the change, running to steady state, plotting the result, trying again ….

Shiny gadget

We will now introduce a shiny gadget (that is a technical term) that greatly facilitates this iterative tuning of the model. The gadget allows quick experimentation with changes to model parameters. It provides sliders for adjusting model parameters and tabs with various plots to immediately see the result of the changes. You can choose which parameter sliders and which plot tabs to include.

We start the gadget by calling the tuneParams() function.

cel_model <- tuneParams(cel_model)

This will open the gadget in your web browser with our current model cel_model loaded. The following video shows what we do on that web page. After making the changes we want to make, we click the “Return” button in the gadget and the tuneParams() function returns the model in that updated state. The above code then assigns that updated model to the variable cel_model.

Next we will use the gadget to refine our model using diet data.

Diet data

In preparation

We’ll skip this for now and just save the model in its present state

saveParams(cel_model, "cel_model_diet.rds")