-
Notifications
You must be signed in to change notification settings - Fork 0
⚡ Bolt: Replace ifelse with vectorized subsetting in llcont.glm #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,12 +53,22 @@ llcont.glm <- function(x, ...){ | |
| if(is.matrix(y)) { | ||
| ## Bolt: replaced apply(..., 1, sum) with optimized rowSums() for performance | ||
| n <- rowSums(y) | ||
| y <- ifelse(n == 0, 0, y[, 1]/n) | ||
| ## Bolt: replaced ifelse with preallocation and vectorized subsetting for performance | ||
| y_res <- y[, 1] * 0 | ||
| cond_n <- n > 0 | ||
| cond_n[is.na(cond_n)] <- FALSE | ||
| if (any(cond_n)) y_res[cond_n] <- (y[, 1]/n)[cond_n] | ||
| y <- y_res | ||
| } else { | ||
| n <- rep.int(1, length(y)) | ||
| } | ||
| m <- if (any(n > 1)) n else wt | ||
| wt <- ifelse(m > 0, (wt/m), 0) | ||
| ## Bolt: replaced ifelse with preallocation and vectorized subsetting for performance | ||
| wt_res <- wt * 0 | ||
| cond_m <- m > 0 | ||
| cond_m[is.na(cond_m)] <- FALSE | ||
| if (any(cond_m)) wt_res[cond_m] <- (wt/m)[cond_m] | ||
| wt <- wt_res | ||
|
Comment on lines
+56
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: ifelse rewrite preserves binomial log-likelihood Both rewrites in the binomial branch are equivalent. Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| dbinom(round(m * y), round(m), mpreds, log = TRUE) * wt | ||
| }, | ||
| quasibinomial = { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: ContextualWisdomLab/nonnest2
Length of output: 211
🏁 Script executed:
Repository: ContextualWisdomLab/nonnest2
Length of output: 3918
🌐 Web query:
R base ifelse documentation evaluation yes no branches TRUE FALSE💡 Result:
In R, the base
ifelse(test, yes, no)function does not perform lazy evaluation in the way users might expect for conditional logic [1][2]. Instead, it evaluates theyesandnoarguments in their entirety before the conditional selection takes place, with specific exceptions [1][2]. According to the official R documentation, "yeswill be evaluated if and only if any element oftestis true, and analogously forno" [3][4]. This means: 1. If thetestvector contains at least oneTRUEvalue, theyesargument is evaluated [1][2]. 2. If thetestvector contains at least oneFALSEvalue, thenoargument is evaluated [1][2]. If yourtestvector contains bothTRUEandFALSEvalues, both theyesandnobranches will be fully computed [1][2]. This often leads to performance overhead or side effects (such as errors or warnings) if the expressions being evaluated are not intended to run for every element of the input [1][2]. For scenarios where you need true short-circuiting (lazy) behavior, it is recommended to use the standardif (test) yes else nocontrol flow structure, which only evaluates the branch corresponding to the result oftest[3][4]. This is much more efficient and appropriate whenevertestis a single logical value [3][4]. Top Results: [3] https://stat.ethz.ch/R-manual/R-devel/library/base/html/ifelse.html [1] https://stackoverflow.com/questions/16275149/does-ifelse-really-calculate-both-of-its-vectors-every-time-is-it-slow [2] https://stackoverflow.com/questions/61913639/strange-behavior-of-ifelse-in-r-when-are-the-values-evaluatedCitations:
중복 항목을 통합하고
ifelse()설명을 수정하세요..jules/bolt.md의 2024-05-15 항목을 하나로 통합하세요.ifelse()는test에TRUE가 하나라도 있을 때yes를,FALSE가 하나라도 있을 때no를 평가합니다. 따라서 두 분기가 모두 평가되는 경우는test에 두 값이 모두 있을 때입니다.🤖 Prompt for AI Agents