Skip to content

Info objective - #91

Open
kaibagley wants to merge 33 commits into
biometryhub:mainfrom
kaibagley:info-objective
Open

kaibagley wants to merge 33 commits into
biometryhub:mainfrom
kaibagley:info-objective

Conversation

@kaibagley

Copy link
Copy Markdown
Contributor

Fisher information based objective function, with A and D optimality criterion, and optimises spatially using a user-supplied covariance structure. It is calculated from treatment contrasts, adjusted for nuisance fixed effects like block and intercept.

$$ I = X_1^{\intercal} L X_1 $$

And L removes nuisance effects under a spatial correlation structure from user-supplied $n \times n$ covariance $\Sigma$ and nuisance effects $X_2$

$$ L = \Sigma^{-1} - \Sigma^{-1} X_2 (X_2^{\intercal} \Sigma^{-1} X_2)^{-1} X_2^{\intercal}\Sigma^{-1} $$

With A-optimality $\mathrm{tr}(I^-)$ or contrast-space D-optimality (pseudo determinant since I has rank $\nu - 1$) $-\log \left| I \right|$.

So basically the user specifies the correlation structure they want to use at analysis, such as $AR1 \times AR1$, and the design is optimised to be efficient under that spatial structure. So it should be directly targeting the Cramér-Rao lower bound rather than the adjacency proxy currently used in speed.

@wvjgsuhp

wvjgsuhp commented May 4, 2026

Copy link
Copy Markdown
Contributor

Thank you for additional objective function.
Please have a look at _pkgdown.yml and see if new functions could be populated where appropriate.
Also, I'd mark helper functions with #' @keywords internal instead of #' @export if they're not expected for users.

@kaibagley

kaibagley commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks, i updated the yaml so the new public functions are in the index. The low level helpers are already @noRd, so they shouldn't be exported, and I left some of the functions like compute_L_projection and others because I thought they might be useful for users who might want to inspect the information objective workflow. But I'm happy to hide them if you want!

Comment thread R/objectives.R Resolved
Comment thread R/objectives.R Resolved
Comment thread R/objectives.R Resolved
Comment thread R/objectives.R Resolved
Comment thread R/objectives.R Resolved
Comment thread R/objectives.R Resolved
Comment thread R/objectives.R Resolved
@rogerssam

Copy link
Copy Markdown
Contributor

Hi @kaibagley,
We've done quite a bit of work since you opened this PR. Can I help with updating the current state of the code in this PR and addressing some of the issues raised by Wasin so we can get it merged?

@rogerssam rogerssam mentioned this pull request Aug 9, 2026
@kaibagley

Copy link
Copy Markdown
Contributor Author

Hey, sorry for the delay! I'll get to work on addressing some of these issues now, but please feel free to make any changes you'd like if you need things to move a bit faster. I've been quite busy with PhD stuff recently, sorry!

# Conflicts:
#	DESCRIPTION
#	_pkgdown.yml
@kaibagley

Copy link
Copy Markdown
Contributor Author

Alright made a couple changes + merged. Please let me know what you think

@kaibagley
kaibagley requested a review from wvjgsuhp August 26, 2026 01:24

@wvjgsuhp wvjgsuhp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few things here with these variables for reproducibility:

require(speed)

df <- initialise_design_df(items = 6, nrows = 4, ncols = 6, block_nrows = 1, block_ncols = 6)
score <- objective_function_info
  1. The score from D criterion can be negative where speed's simulated annealing stops when the score <= 0.
    res <- speed(
      df,
      swap = "treatment",
      swap_within = "block",
      obj_function = score,
      criterion = "D",
      iterations = 5000,
      seed = 1
    )
    res$scores
  2. If Sigma provided, order matters. grid_index (see calculate_adjacency_score) might work here in case users are calling the function outside speed calls. The ideal is the scoring should work regardless of the order of dataframe order (again, reusing grid_index might help).
    sigma <- cor_ar1_ar1(4, 6, rho_row = 0.6, rho_col = 0.3)
    c(
      as_built = score(df, "treatment", c("row", "col"), Sigma = Sigma)$score,
      sorted = score(df_sorted, "treatment", c("row", "col"), Sigma = Sigma)$score
    )
  3. When there is <= 1 block, the error should be raised accordingly. Docstring should state that a blocked design is required.

@kaibagley

Copy link
Copy Markdown
Contributor Author

Again, sorry for the ultra late reply... Been very busy recently but I'm working on the branch now!!

@wvjgsuhp

Copy link
Copy Markdown
Contributor

That's alright. No rush here. (I was quite late to check)

@kaibagley

Copy link
Copy Markdown
Contributor Author

Just made a bunch of changes, let me know what you think!

@wvjgsuhp wvjgsuhp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor adjustments and we good.

An unnamed contrast_matrix can lead to unintended score calculation because it's matched to alphabetically sorted treatments (levels(factor(treatments)) -> T1, T10, T2, ...). See example below.

devtools::load_all()

df <- initialise_design_df(items = 10, nrows = 2, ncols = 10, block_nrows = 1, block_ncols = 10)
sigma <- cor_ar1_ar1(2, 10, rho_row = 0.6, rho_col = 0.3)
score <- objective_function_info

unnamed <- matrix(c(1, -1, rep(0, 8)), nrow = 1) # T1 vs T2
named <- unnamed
colnames(named) <- paste0("T", 1:10)

res_unnamed <- speed(
  df,
  swap = "treatment",
  swap_within = "block",
  obj_function = score,
  Sigma = sigma,
  contrast_matrix = unnamed,
  seed = 1
)
res_named <- speed(
  df,
  swap = "treatment",
  swap_within = "block",
  obj_function = score,
  Sigma = sigma,
  contrast_matrix = named,
  seed = 1
)

# T1 vs T2 variance of each optimised design
c(
  unnamed = score(res_unnamed$design_df, "treatment", c("row", "col"), Sigma = sigma, contrast_matrix = named)$score,
  named = score(res_named$design_df, "treatment", c("row", "col"), Sigma = sigma, contrast_matrix = named)$score
)

Comment thread R/speed.R Resolved
Comment thread tests/testthat/test-design_utils.R Resolved
Comment thread R/speed.R Resolved
@kaibagley

Copy link
Copy Markdown
Contributor Author

New changes. Let me know what you think!!

Comment thread R/speed.R Resolved
@kaibagley

Copy link
Copy Markdown
Contributor Author

Added a lower bound parameter, and tweaked strings and tests, and simplified stopping

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants