Closed sou412 closed 1 day ago
@kkawato Please test the proposed change with a simulated / real data, before merging. Please make sure there are no diffs in the output.
One thing I'm not quite following is the use of map
in here https://github.com/kkawato/rdlearn/blob/70e9ae114bcfd07cb28b30804dcf6c5da9a52d47/R/1-4-1extrapolation.R#L43-L44
If I understand correctly, this function is applied to a scalar value (i.e., x.train
is a single number when the function is called in safelearn()
.
Do you need to use map here? It seems to be throwing warning possibly due to this reason. Also, upper
and lower
object becomes a list, which add a bit of overhead when parsing the results in safelearn()
?
$lower[[1]]
Adding @carolyyzz as a reviewer too as I assume some parts of the code come from @carolyyzz as well.
@kkawato Please test the proposed change with a simulated / real data, before merging. Please make sure there are no diffs in the output.
It seems that the output is different... I am trying to detect the reason
It seems that the output is different... I am trying to detect the reason
Oh that's not good -- sorry for making more work for you.
Do you think you could add (a code to generate) a simulated data so that we can debug easily? I can look at the diff in this branch while you investigate the diff from the paper on the main branch.
I apologize for the delayed response. I replaced map
function with apply
in the extrapolation
function in 1-4-1extrapolation.R
. Also, I modified logic in 1-4-0safelearn.R
as follows and replicate the result:
data_all[eval_cond, paste0("d", d)] <-
apply(temp_df, 1, function(x) {
temp_result <- extrapolation(
x.train = x[1],
treat = d,
g = g,
g.pr = x[2] + (1 - d),
Lip_1 = Lip_1,
Lip_0 = Lip_0,
dif_1 = dif_1,
dif_0 = dif_0,
G = G,
C = C
)
sum(temp_result)
})
@kkawato Thanks for the update! It seems now this branch has a conflict. Should I just follow the main branch?
Reopen the branch (closed by accident).
Yes! thank you
@kkawato Sorry it's been a while -- I resolved the conflict. Please take a look if it's good to merge. Thank you!
Refactoring a complex
apply
logic in1-4-0safelearn.R
In the script, there's a very complex logic: https://github.com/kkawato/rdlearn/blob/70e9ae114bcfd07cb28b30804dcf6c5da9a52d47/R/1-4-0safelearn.R#L88-L105
It is not immediately clear what's being computed here. I proposed the following refactoring:
Idea behind the proposed change
apply()
is acting ontemp_df
, which is an by 2
data frame: https://github.com/kkawato/rdlearn/blob/70e9ae114bcfd07cb28b30804dcf6c5da9a52d47/R/1-4-0safelearn.R#L85x
passed to the (anonymous functionfunction(x) { ... }
) is a vector of two elements. This means thatg.temp
is same asx[2] + (1 - d)
.[2,]
part is parsing the second element from the output ofextrapolation()
function, which corresponds to the lower bound (lower
object): https://github.com/kkawato/rdlearn/blob/70e9ae114bcfd07cb28b30804dcf6c5da9a52d47/R/1-4-1extrapolation.R#L46Test
I tested the proposed change with the test code in
testthat
directory.Minor edits
style_file()
to1-4-0safelearn.R
script.if (x == TRUE)
withif(isTRUE(x))
print()
withcat()
. This has a benefit of clean(er) display of messages.The output from
cat()
will bewhile the use of
print()
introduces[1]
at the begining of each line: