mcp
mcp copied to clipboard
Allow for NA values in the dependent variable throughout
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)
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)
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
- +-base::plot(f1)
- +-mcp:::plot.mcpfit(f1)
- | -mcp local_pp_eval("fitted")
- | +-... %>% dplyr::rename(!!yvar := !!type)
- | -mcp:::pp_eval(...)
- | -... %>% ...
- +-dplyr::rename(.,
:=
(!!yvar, !!type)) - +-dplyr::mutate(...)
- +-dplyr:::mutate.data.frame(...)
- | -dplyr:::mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
- | +-base::withCallingHandlers(...)
- | -mask$eval_all_mutate(quo)
- +-rlang::exec(...)
- +-mcp
<fn>
(...) - | -mcp:::assert_numeric(per)
- -base::.handleSimpleError(...)
- -dplyr h(simpleError(msg, call))
-
\-rlang::abort(...)
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)") )
Not able to reproduce with the example data. Here is a gist for the csv: https://gist.github.com/dwwolfson/3291e124879029b52fe1c7412b01b56a
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.
Sounds good, thanks for your help!
Reopening to remember to fix this bug in the next release.
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".