change-tutorial icon indicating copy to clipboard operation
change-tutorial copied to clipboard

Error when assigning values to 'TPR' column in data.table

Open nhintruong opened this issue 1 year ago • 3 comments

Hi @tdhock, I'm encountering an error while running the code in the figure-regression-interactive-some.R file. Specifically, the error occurs on the following line: https://github.com/tdhock/change-tutorial/blob/8df639ee7a6fa7fe8083255e4e29b7af4bb05e6e/figure-regression-interactive-some.R#L194

The error message I'm receiving is:

Error in `[.data.table`(auc, , `:=`(TPR, c(0.85, 0.9))) : 
  Supplied 2 items to be assigned to 1 items of column 'TPR'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.

I'm a bit confused about why this error is occurring. It seems that the code is trying to assign two values (0.85 and 0.9) to the 'TPR' column, but the error suggests there's only one item in the column to assign to. Could you please give me some guidance on what might be causing this error and how can I modify the code to correctly assign these values to the 'TPR' column?

Thank you very much for your time and guidace

nhintruong avatar Jul 24 '24 18:07 nhintruong

This is occurring because R has changed the way it handles recycling of values in vector assignment. This "error" is going to be happening a lot for most users because this has been a "feature" of R for a long time! It will take a while for all the package maintainers to find all the instances in their packages and correct them.

rkillick avatar Jul 24 '24 19:07 rkillick

This is occurring because R has changed the way it handles recycling of values in vector assignment. This "error" is going to be happening a lot for most users because this has been a "feature" of R for a long time! It will take a while for all the package maintainers to find all the instances in their packages and correct them.

Thank you for your explanation. I understand now that this is due to changes in R's handling of recycling in vector assignments. Given this information, I'm still a bit uncertain about how to resolve the error in my specific case. Could you please advise on the best way to modify the problematic line of code? Should I explicitly use rep() as the error message suggests?

For example,

auc[, TPR := rep(c(0.85, 0.9), length.out = nrow(auc))]

Or is there a more appropriate way to assign these values that aligns with current R behaviour?

Any specific guidance you can provide would be very helpful. Thank you again for your guidance.

nhintruong avatar Jul 25 '24 04:07 nhintruong

Yes, using rep() to get the desired length is the advised solution and that should fix the problem.

I'm going to leave this open for Toby to see and add any comments he has (and so we can update the file with the new practice).

rkillick avatar Jul 25 '24 08:07 rkillick

I re-run the code and I get the same error, it is reproducible using R-4.4.1 on windows. It is caused by a previous error, when model.name="learned" the code tries to run survreg (Gaussian AFT survival regression), which does not converge.

> reg.dt <- data.table(model.fun(model.train.dt), model.name, model.label)
Warning message:
In survreg.fit(X, Y, weights, offset, init = init, controlvals = control,  :
  Ran out of iterations and did not converge
> reg.line.list[[model.name]] <- reg.dt
> model.train.dt[, pred.log.lambda := reg.dt[, feature*slope+intercept] ]
> model.train.dt[, residual := {
+ targetIntervalResidual(
+ cbind(min.log.lambda, max.log.lambda), pred.log.lambda)
+ }]
Error in check_target_pred(target.mat, pred) : 
  pred must be a numeric vector or matrix with neither missing nor infinite entries

this code did work in the past at some point, so I suspect there is some new bug in survival that was introduced, and I can file an issue with the survival maintainer about that.

To work-around the issue and re-compute the data viz, we can use penaltyLearning instead of survival, as I have committed https://github.com/tdhock/change-tutorial/commit/acb686a1158273d29259ccc2d7cb5636da459c43 you should get a ggplot that looks like this image

tdhock avatar Jul 31 '24 20:07 tdhock

filed an issue, https://github.com/therneau/survival/issues/270

tdhock avatar Jul 31 '24 21:07 tdhock

this is fixed in https://github.com/tdhock/change-tutorial/commit/7699796266307388004bda9cea38771261425995 some code to reproduce the issue is still available https://github.com/tdhock/change-tutorial/blob/master/figure-regression-interactive-some-bug.R

tdhock avatar Aug 20 '24 20:08 tdhock