predictor variables were used in the model-error in match.arg(y)

260 views
Skip to first unread message

tina....@googlemail.com

unread,
Feb 27, 2015, 1:20:29 PM2/27/15
to causal...@googlegroups.com
Hi, 

I was looking for the coefficient plot of variables being used in the below example code from the guidance website (see example below), but 
plot(impact$model$bsts.model, "coefficients")
returns an error: "Error in match.arg(y) : 'arg' must be NULL or a character vector"

The error occurs in the function match.arg. 
Could you explain the reason for this error? 
Thanks & Regards, 


library(CausalImpact)
set.seed(1)
x1 <- 100 + arima.sim(model = list(ar = 0.999), n = 100)
x2 <- 50 + arima.sim(model = list(ar = 0.5), n = 100)

y <- 1.2 * x1 + x2+rnorm(100)
y[71:100] <- y[71:100] + 10
data <- cbind(y, x1,x2)
pre.period <- c(1, 70)
post.period <- c(71, 100)
impact <- CausalImpact(data, pre.period, post.period)
plot(impact)

plot(impact$model$bsts.model, "coefficients")



Kay Brodersen

unread,
Feb 27, 2015, 1:56:52 PM2/27/15
to tina....@googlemail.com, causal...@googlegroups.com
Hi Tina,

Running your code should produce the plot below:

Inline images 1

I would try running update.packages() first. If the problem persists, you could run sessionInfo() and compare your output to mine below.

Kay


R version 3.1.1 (2014-07-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

other attached packages:
[1] CausalImpact_1.0.3  bsts_0.6.1         
[3] xts_0.9-7           zoo_1.7-11         
[5] BoomSpikeSlab_0.5.2 Boom_0.2           
[7] MASS_7.3-39        

loaded via a namespace (and not attached):
 [1] assertthat_0.1   colorspace_1.2-4 digest_0.6.8    
 [4] dplyr_0.4.1      ggplot2_1.0.0    grid_3.1.1      
 [7] gtable_0.1.2     labeling_0.3     lattice_0.20-30 
[10] munsell_0.4.2    parallel_3.1.1   plyr_1.8.1      
[13] proto_0.3-10     Rcpp_0.11.4      reshape2_1.4.1  
[16] scales_0.2.4     stringr_0.6.2    tools_3.1.1    


--
You received this message because you are subscribed to the Google Groups "CausalImpact" group.
To unsubscribe from this group and stop receiving emails from it, send an email to causalimpact...@googlegroups.com.
To post to this group, send email to causal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/causalimpact/9890c7d0-624c-437d-8da1-255a17dcf5f0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maartje

unread,
Mar 2, 2015, 5:08:48 AM3/2/15
to causal...@googlegroups.com, tina....@googlemail.com
Hi Kay,

I have the same problem Tina has. Tina, has your problem been solved by this solution? I tried running update.packages() first, but that dit not solve the problem. The SessionInfo() results are listed below.
As you can see, there are indeed some differences, but as far as I can see the packages loaded via a namespace are more advanced / in addition to your packages.

R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Dutch_Netherlands.1252  LC_CTYPE=Dutch_Netherlands.1252   
[3] LC_MONETARY=Dutch_Netherlands.1252 LC_NUMERIC=C                      
[5] LC_TIME=Dutch_Netherlands.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] CausalImpact_1.0.3  bsts_0.6.1          xts_0.9-7           zoo_1.7-11          BoomSpikeSlab_0.5.2
[6] Boom_0.2            MASS_7.3-39        

loaded via a namespace (and not attached):
 [1] assertthat_0.1   colorspace_1.2-4 DBI_0.3.1        digest_0.6.8     dplyr_0.4.1     
 [6] ggplot2_1.0.0    grid_3.1.2       gtable_0.1.2     labeling_0.3     lattice_0.20-30 
[11] lazyeval_0.1.10  magrittr_1.5     munsell_0.4.2    parallel_3.1.2   plyr_1.8.1      
[16] proto_0.3-10     Rcpp_0.11.4      reshape2_1.4.1   scales_0.2.4     stringr_0.6.2   
[21] tools_3.1.2     

Do you see what the problem is?

Best regards,
Maartje

Op vrijdag 27 februari 2015 19:20:29 UTC+1 schreef tina....@googlemail.com:

Kay Brodersen

unread,
Mar 2, 2015, 5:14:06 AM3/2/15
to Maartje, causal...@googlegroups.com, tina....@googlemail.com
Hi Maartje,

I'd suggest taking a look at the full stack trace or setting breakpoints in the code for plot.bsts() to locate the problem. For more information on this, see for example:


Kay


--
You received this message because you are subscribed to the Google Groups "CausalImpact" group.
To unsubscribe from this group and stop receiving emails from it, send an email to causalimpact...@googlegroups.com.
To post to this group, send email to causal...@googlegroups.com.

tina....@googlemail.com

unread,
Mar 2, 2015, 9:01:50 AM3/2/15
to causal...@googlegroups.com, maartje...@gmail.com, tina....@googlemail.com
Hi Kay,

This is a compatibility error occurring under R.3.1.2. 

Regards, 
Tina

Tina Wenzel

unread,
Mar 2, 2015, 9:01:50 AM3/2/15
to causal...@googlegroups.com
Hi, 

To give some more information, I debugged the functions PlotBstsCoefficients and plot.lm.spike to get to the error in match.arg(y), where the error occurs here:

function (x, y = c("coefficients", "size"), ...) 
{
    y <- match.arg(y)
    if (y == "coefficients") {
        return(PlotMarginalInclusionProbabilities(x$beta, ...))
    }
    else if (y == "size") {
        return(PlotModelSize(x$beta, ...))
    }
    else {
        stop("unknown option", y, " in plot.lm.spike")
    }
}

Tina
--
Tina Wenzel
23, Shaftesbury Rd

maartje...@gmail.com

unread,
Mar 6, 2015, 6:05:20 AM3/6/15
to causal...@googlegroups.com, tina....@googlemail.com
Hi Kay,

Do you know if the compatability problem under R.3.1.2. can be solved?

Best regards,

Maartje

Op maandag 2 maart 2015 15:01:50 UTC+1 schreef Tina Wenzel:

Kay Brodersen

unread,
Mar 13, 2015, 7:55:39 PM3/13/15
to Maartje Verhoek, causal...@googlegroups.com, Tina Wenzel
Hi Tina and Maartje,

This will be fixed with an update to bsts which Steve Scott will be releasing to CRAN shortly after R 3.2 has been released, which will be in the next week or two.

If you wish to fix this locally before the fix gets to CRAN: In the definition of PlotBstsCoefficients, the call to plot.lm.spike() is passed with positional arguments rather than named arguments.
The fix is to name the arguments, as in burn = burn, inclusion.threshold = inclusion.threshold, etc.

Kay


sno...@gmail.com

unread,
Mar 17, 2015, 10:01:29 PM3/17/15
to causal...@googlegroups.com, maartje...@gmail.com, tina....@googlemail.com
Hi Kay,

Thank you very much.

I had the same kind of problem.

Could you give more detailed fix please?



function (x, y = c("coefficients", "size"), ...) 
{
    y <- match.arg(y)
    if (y == "coefficients") {
        return(PlotMarginalInclusionProbabilities(x$beta, ...))
    }
    else if (y == "size") {
        return(PlotModelSize(x$beta, ...))
    }
    else {
        stop("unknown option", y, " in plot.lm.spike")
    }
}

So should I this part `PlotMarginalInclusionProbabilities(x$beta, ...)`


Thanks a lot.

Jon

tina....@googlemail.com

unread,
Mar 19, 2015, 11:43:00 PM3/19/15
to causal...@googlegroups.com, maartje...@gmail.com, tina....@googlemail.com
Hi Jon,
 
I just tried it and this is what works for me: 

debug(plot)
debug(UseMethod)
debug(PlotBstsCoefficients)
plot(impact$model$bsts.model, "coefficients")

Walk through the function in the debugging mode until 
class(tmp) <- "lm.spike"

and then use 
plot.lm.spike(tmp, burn=burn, inclusion.threshold=inclusion.threshold,number.of.variables = number.of.variables, ...)
instead of 
return(invisible(plot.lm.spike(tmp, burn, inclusion.threshold, 
    number.of.variables = number.of.variables, ...)))

Hope that helps. 
Tina

tina....@googlemail.com

unread,
Mar 19, 2015, 11:43:06 PM3/19/15
to causal...@googlegroups.com, maartje...@gmail.com, tina....@googlemail.com
Actually, you only need to debug(PlotBstsCoefficients), then replace as described. 

Am Mittwoch, 18. März 2015 02:01:29 UTC schrieb Jon Duan:
Reply all
Reply to author
Forward
0 new messages