View source: R/net_matrix_ipf.R
| net_matrix_ipf | R Documentation |
The net_matrix_ipf function finds the maximum likelihood estimates for a flow matrix under the multiplicative log-linear model:
\log y_{ij} = \log \alpha_i + \log \alpha_j^{-1} + \log m_{ij}
where y_{ij} is the estimated migration flow from origin i to destination j, and m_{ij} is the prior flow.
The function iteratively adjusts origin and destination scaling factors (\alpha) to match directional net migration totals.
net_matrix_ipf(
net_tot,
m,
zero_mask = NULL,
maxit = 500,
tol = 1e-06,
verbose = FALSE
)
net_tot |
A numeric vector of net migration totals for each region. Must sum to zero. |
m |
A square numeric matrix providing prior flow estimates. Must have dimensions |
zero_mask |
A logical matrix of the same dimensions as |
maxit |
Maximum number of iterations to perform. Default is |
tol |
Convergence tolerance based on maximum change in |
verbose |
Logical flag to print progress and |
The function avoids matrix inversion by updating \alpha using a closed-form solution to a quadratic equation at each step.
Only directional net flows (column sums minus row sums) are matched, not marginal totals. Flows are constrained to be non-negative.
If multiple positive roots are available when solving the quadratic, the smaller root is selected for improved stability.
A named list with components:
nEstimated matrix of flows satisfying the net constraints.
itNumber of iterations used.
tolConvergence tolerance used.
valueSum of squared residuals between actual and target net flows.
convergenceLogical indicator of convergence within tolerance.
messageText description of convergence result.
Guy J. Abel, Peter W. F. Smith
m <- matrix(c(0, 100, 30, 70,
50, 0, 45, 5,
60, 35, 0, 40,
20, 25, 20, 0),
nrow = 4, byrow = TRUE,
dimnames = list(orig = LETTERS[1:4], dest = LETTERS[1:4]))
addmargins(m)
sum_region(m)
net <- c(30, 40, -15, -55)
result <- net_matrix_ipf(net_tot = net, m = m)
result$n |>
addmargins() |>
round(2)
sum_region(result$n)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.