首先,我使用traceback 和browser 函数。下面的脚本生成:
Error in na.omit.ts(data[, 1:3]) : time series contains internal NAs
脚本:
library(CausalImpact)
set.seed(123)
x1 <- 100 + arima.sim(model = list(ar = 0.999), n = 52)
y <- 1.2 * x1 + rnorm(52)
y[41:52] <- y[41:52] + 10
z <- 2 * y + rnorm(52)
z[22] <- NA
data <- cbind(y, x1, z)
str(data)
impact <- CausalImpact(na.omit(data[, 1:3]), c(1, 40), c(41, 52))
如果你输入:
traceback()
出现如下调用栈:
14: stop("time series contains internal NAs")
13: na.omit.ts(data[, 1:3])
12: na.omit(data[, 1:3])
11: xor(!is.null(data) && !is.null(pre.period) && !is.null(post.period) &&
is.null(bsts.model) && is.null(post.period.response), is.null(data) &&
is.null(pre.period) && is.null(post.period) && !is.null(bsts.model) &&
!is.null(post.period.response))
10: eval(assertion, env)
9: eval(assertion, env)
8: doTryCatch(return(expr), name, parentenv, handler)
7: tryCatchOne(expr, names, parentenv, handlers[[1L]])
6: tryCatchList(expr, classes, parentenv, handlers)
5: tryCatch({
eval(assertion, env)
}, assertError = function(e) {
structure(FALSE, msg = e$message)
})
4: see_if(..., env = env, msg = msg)
3: assert_that(xor(!is.null(data) && !is.null(pre.period) && !is.null(post.period) &&
is.null(bsts.model) && is.null(post.period.response), is.null(data) &&
is.null(pre.period) && is.null(post.period) && !is.null(bsts.model) &&
!is.null(post.period.response)), msg = paste0("must either provide data, pre.period, post.period, ",
"model.args; or bsts.model and post.period.response"))
2: FormatInputForCausalImpact(data, pre.period, post.period, model.args,
bsts.model, post.period.response, alpha)
1: CausalImpact(na.omit(data[, 1:3]), c(1, 40), c(41, 52))
您发现na.omit.ts 存在一些问题(将NA 引入时间序列不是一个好主意)。
然后我创建虚拟函数并将browser() 添加到它的主体中:
test <- function() {
browser()
CausalImpact(na.omit(data[, 1:3]), c(1, 40), c(41, 52))
}
当我运行 test() 时,我将进入调试模式。并且可以进行我想要的所有调试(检查变量、更改一些代码、输入函数、跳过行等)。您可以在调试模式下键入help 以获得更多选项。