Theory Building in Psychology (Summer School 2026)
Source:vignettes/articles/summerschool2026.Rmd
summerschool2026.RmdA Concrete Example: A Queue of People Waiting for Service
Inflows Increase a Stock
| Time | People Waiting in Queue | Arrivals |
|---|---|---|
| 0.00 | 0.00 | 1 |
| 0.01 | 0.01 | 1 |
| 0.02 | 0.02 | 1 |
| 0.03 | 0.03 | 1 |
| 0.04 | 0.04 | 1 |
| 0.05 | 0.05 | 1 |
Outflows Decrease a Stock
What processes cause the state to change?
| Time | People Waiting in Queue | Arrivals | Service |
|---|---|---|---|
| 0.00 | 0.000 | 1 | 0.5 |
| 0.01 | 0.005 | 1 | 0.5 |
| 0.02 | 0.010 | 1 | 0.5 |
| 0.03 | 0.015 | 1 | 0.5 |
| 0.04 | 0.020 | 1 | 0.5 |
| 0.05 | 0.025 | 1 | 0.5 |
What Happens When the Outflow Is Larger Than the Inflow?
| Time | People Waiting in Queue | Arrivals | Service |
|---|---|---|---|
| 0.00 | 0.00 | 1 | 2 |
| 0.01 | -0.01 | 1 | 2 |
| 0.02 | -0.02 | 1 | 2 |
| 0.03 | -0.03 | 1 | 2 |
| 0.04 | -0.04 | 1 | 2 |
| 0.05 | -0.05 | 1 | 2 |
Making the Outflow Stock-Dependent
This is a negative feedback loop: the more people, the greater the outflow, which reduces the amount of people in the queue.
A positive feedback loop amplifies, whereas a negative feedback loop dampens or balances.
Applied Examples of Stock-and-Flow Models
This framework can be applied to model a wide range of phenomena.
Package Setup
The workshop materials are available at https://kcevers.github.io/sdbuildR/articles/summerschool2026.html.
Install the package from GitHub:
Load the stock-and-flow model of people waiting in a queue from the package’s model library:
sfm <- stockflow("queue")sfm stands for
stock-and-flow
model.
Look at the model’s structure:
print(sfm)
#>
#> ── Stock-and-Flow Model ────────────────────────────────────────────────────────
#> 2 stocks • 3 flows • 1 constant • 1 auxiliary
#>
#> ── Stock-Flow Structure ──
#> queue: + arrivals - leave - service
#> served: + service
#>
#> ── Other Variables ──
#> Constants: `service_rate`
#> Auxiliaries: `satisfaction`
#>
#> ── Simulation Settings ──
#> Time: 0.0 to 10.0 hours (dt = 0.01) • euler • R
#> Simulation output: all variablesOr inspect it in data frame format:
as.data.frame(sfm, properties = "eqn")
#> type name eqn
#> 1 stock queue 0
#> 2 stock served 0
#> 3 flow arrivals 1
#> 4 flow leave 0.1 * queue^2
#> 5 flow service service_rate * queue
#> 6 constant service_rate 0.5
#> 7 aux satisfaction service / (service + leave)Plot the stock-and-flow diagram:
plot(sfm, show_constants = TRUE)Simulate the model and plot the resulting time series:
The pipe operator |> passes the result on its left
into the function on its right. For example, x |> f(y)
is interpreted as f(x, y).
The timeseries can also be inspected in data frame format:
sfm |> simulate() |> as.data.frame(direction = "wide") |> head()
#> time queue served service leave arrivals satisfaction
#> 1 0.00 0.00000000 0.0000000000 0.00000000 0.000000e+00 1 NaN
#> 2 0.01 0.01000000 0.0000000000 0.00500000 1.000000e-05 1 0.9980040
#> 3 0.02 0.01994990 0.0000500000 0.00997495 3.979985e-05 1 0.9960259
#> 4 0.03 0.02984975 0.0001497495 0.01492488 8.910077e-05 1 0.9940655
#> 5 0.04 0.03969961 0.0002989983 0.01984981 1.576059e-04 1 0.9921226
#> 6 0.05 0.04949954 0.0004974963 0.02474977 2.450204e-04 1 0.9901971A Simplified Model of Burnout
Draw the target phenomenon:
Building a Model from Scratch
Create an empty stock-and-flow model:
sfm <- stockflow()
print(sfm)
#>
#> ── Stock-and-Flow Model ────────────────────────────────────────────────────────
#> ℹ Empty model without any variables.
#>
#> ── Simulation Settings ──
#> Time: 0 to 100 seconds (dt = 0.01) • euler • R
#> Simulation output: stocks onlyChange name of the model:
sfm <- meta(sfm, name = "Burnout")Defining the Time Horizon and Time Unit
Change simulation settings:
sfm <- sim_settings(sfm,
# Run simulation for 6 months (~ 180 days)
stop = round(365/2), time_unit = "days",
# Return all variables in output (not just stocks)
only_stocks = FALSE)
print(sfm)
#>
#> ── Stock-and-Flow Model: Burnout ───────────────────────────────────────────────
#> ℹ Empty model without any variables.
#>
#> ── Simulation Settings ──
#> Time: 0.0 to 182.0 days (dt = 0.01) • euler • R
#> Simulation output: all variables