mcp icon indicating copy to clipboard operation
mcp copied to clipboard

Allow for NA values in the dependent variable throughout

Open dwwolfson opened this issue 2 years ago • 8 comments

mcp Version 0.3.2 dplyr Version 1.0.8

When trying to plot a large fitted mcpfit object, I'm running into this error: Error in dplyr::mutate(): ! Problem while computing fitted = rlang::exec(...). Caused by error in if (any(x < lower) || any(x > upper)) ...: ! missing value where TRUE/FALSE needed`

I also get the same error if calling fitted(model)

dwwolfson avatar Mar 15 '22 18:03 dwwolfson

Thanks for reporting. Can you post code that reproduces this error? And can you post the stack trace? (use traceback() if it isn't returned automatically)

lindeloev avatar Mar 16 '22 09:03 lindeloev

The full traceback is pretty enormous. Here is the backtrace, hopefully that is helpful. I'll try to reproduce the error either by simulating data or posting data to a gist.


Backtrace: x

  1. +-base::plot(f1)
  2. +-mcp:::plot.mcpfit(f1)
  3. | -mcp local_pp_eval("fitted")
  4. | +-... %>% dplyr::rename(!!yvar := !!type)
  5. | -mcp:::pp_eval(...)
  6. | -... %>% ...
  7. +-dplyr::rename(., :=(!!yvar, !!type))
  8. +-dplyr::mutate(...)
  9. +-dplyr:::mutate.data.frame(...)
  10. | -dplyr:::mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
  11. | +-base::withCallingHandlers(...)
  12. | -mask$eval_all_mutate(quo)
  13. +-rlang::exec(...)
  14. +-mcp <fn>(...)
  15. | -mcp:::assert_numeric(per)
  16. -base::.handleSimpleError(...)
  17. -dplyr h(simpleError(msg, call))
  18. \-rlang::abort(...)
    

dwwolfson avatar Mar 16 '22 13:03 dwwolfson

Here is the dataframe: `> str(df) track_xyt [200 x 8] (S3: track_xyt/track_xy/tbl_df/tbl/data.frame) $ x_ : num [1:200] 23.7 26.7 13.3 28.5 96.8 ... $ y_ : num [1:200] -9.06 -11.33 2.95 26.63 121.78 ... $ t_ : Date[1:200], format: "2000-01-01" "2000-01-01" "2000-01-01" ... $ nsd : num [1:200] 0 14.1 254.4 1296.6 22454.4 ... $ speed : num [1:200] 0.001087 0.000437 0.002327 0.002607 0.002199 ... $ time : num [1:200] 0.18 0.22 0.74 0.88 1.4 1.41 1.48 2 2.15 2.16 ... $ per : num [1:200] NA -0.001071 0.000105 0.002324 0.002607 ... $ turning: num [1:200] NA 0.0001808 -0.0004243 -0.0001208 0.0000503 ...

  • attr(*, "crs_")= chr NA`

And here is the mcp code: m1<-list( per~1+ar(1), ~1+sigma(1)+ar(1), ~1+sigma(1)+ar(1), ~1+sigma(1)+ar(1), ~1+sigma(1)+ar(1) ) options(mc.cores=3) f1<-mcp(m1, data = df[,c("time", "per")], par_x = "time", adapt=20000, chains=4, cores=4, inits=list(cp_1=13, cp_2=19, cp_3=31, cp_4=47), prior=list(cp_1="dunif(5,15)", cp_2="dunif(15,25)", cp_3="dunif(25,35)", cp_4="dunif(45,55)") )

dwwolfson avatar Mar 16 '22 13:03 dwwolfson

Not able to reproduce with the example data. Here is a gist for the csv: https://gist.github.com/dwwolfson/3291e124879029b52fe1c7412b01b56a

dwwolfson avatar Mar 16 '22 14:03 dwwolfson

Thanks! This is definitely a bug. The post-processing functions should allow for NA values in the dependent variable and they don't. As a temporary workaround, use a dataset without NA in per. E.g., removing these rows or imputing values.

lindeloev avatar Mar 16 '22 14:03 lindeloev

Sounds good, thanks for your help!

dwwolfson avatar Mar 16 '22 14:03 dwwolfson

Reopening to remember to fix this bug in the next release.

lindeloev avatar Mar 16 '22 14:03 lindeloev

BTW, just looking at your data, perhaps remove the ar() terms in segment 2+. This will fit a single ar() parameter that apply to all segments. No ar() is implicitly ar(N, 0) where N is the order of the previous segment and "0" means "no change in intercept of the autocorrelation coefficient".

lindeloev avatar Mar 16 '22 15:03 lindeloev