Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes and optimizes the Covy() covariance estimator by reordering its arguments, reducing repeated computations, and improving how pairwise terms are accumulated, with corresponding doc/test updates.
Changes:
- Reordered
Covy()parameters toN, w1, g1, w2, g2, W, G, Y, p, Ne, Ne_listand updated documentation accordingly. - Refactored
Covy()internals to pre-allocatevariaband cachepij()results to avoid duplicate calls. - Updated the
Covy()unit test to match the new argument order.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
R/Covy.R |
Reorders signature and refactors pairwise computation to avoid repeated calls and vector growth. |
man/Covy.Rd |
Updates usage/argument order to match the new Covy() signature. |
tests/testthat/test-Covy.R |
Updates the Covy() invocation to align with the reordered parameters. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| Ne_list <- list(2,1,4,3,6,5) | ||
|
|
||
| c0010 <- Covy(0,0,1,0, N, Y, W, G, p, Ne, Ne_list) | ||
| c0010 <- Covy(N = N, 0,0,1,0, W, G, Y, p, Ne, Ne_list) |
There was a problem hiding this comment.
The test call mixes a named argument (N = N) with positional arguments. This makes the test brittle to future signature changes (especially since this PR is about reordering args) and harder to read. Prefer passing all arguments by name (or pass N positionally as the first argument).
| c0010 <- Covy(N = N, 0,0,1,0, W, G, Y, p, Ne, Ne_list) | |
| c0010 <- Covy(N, 0,0,1,0, W, G, Y, p, Ne, Ne_list) |
| pairs <- expand.grid.unique(which(W == w1 & G == g1), | ||
| which(W == w2 & G == g2), | ||
| include.equals = FALSE) | ||
|
|
||
| # Initialize | ||
| variab = numeric(nrow(pairs)) | ||
|
|
There was a problem hiding this comment.
The pairs creation and variab initialization are inside the if (...) { block but are not indented like the rest of the block. This makes it easy to misread the control flow and inconsistent with the surrounding style in this repo (e.g., other functions indent block bodies). Please indent these lines to clearly show they are guarded by the sample-size check.
| pairs <- expand.grid.unique(which(W == w1 & G == g1), | |
| which(W == w2 & G == g2), | |
| include.equals = FALSE) | |
| # Initialize | |
| variab = numeric(nrow(pairs)) | |
| pairs <- expand.grid.unique(which(W == w1 & G == g1), | |
| which(W == w2 & G == g2), | |
| include.equals = FALSE) | |
| # Initialize | |
| variab = numeric(nrow(pairs)) | |
Reordered parameters: N, Y, W, G → N, W, G, Y
Removed varzero = NULL initialization
Moved pairs computation inside the sample size check and pre-allocated variab = numeric(nrow(pairs)) instead of growing with c()
Cached pij() result in pij_val to avoid calling it twice per iteration
Removed unnecessary sum() wrapping a scalar expression
Updated man/Covy.Rd
Updated tests to fix the parameter passing in the Covy() call
Added N back to the signature and the docs