patrick-eng / its.analysis

Copy of CRAN listed package for running time series analysis on small samples using the 'interruption treatment' approach
https://cran.r-project.org/web/packages/its.analysis/index.html
Other
6 stars 4 forks source link

Example fails if treatment is permanent #1

Closed lrose1 closed 5 years ago

lrose1 commented 5 years ago

The example does not work if the treatmeant does not "turn off". Reprex:

depv <- c(8.22, 8.19, 8.23, 8.28, 8.32, 8.39, 8.02,7.92, 7.62, 7.23, 7.1, 6.95, 7.36, 7.51, 7.78, 7.92)`
interruption <- c(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
cov1 <- c(3.1, 3.3, 5.1, 5.2, 5.4, 4.5, 4.7, 4.9, 5.3,5.6, 5.8, 4.8, 5.2, 4.5, 4.6, 5.1)
x <- as.data.frame(cbind(year, depv, interruption, cov1))
# First example model
itsa.model(data=x, time="year", depvar="depv", interrupt_var = "interruption",alpha=0.05, bootstrap=TRUE, Reps = 250)

Gives: Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels

patrick-eng commented 5 years ago

Hi there, thanks so much for getting in touch about this error!

I've tested this out for myself and cannot reproduce the error, which is strange! I've just updated all the code here with the latest files from my local system. Could you try running this again and see if the error still happens?

Note: there is a stray ` on the depv line in the code above which I am sure isn't causing the issue, but just FYI

lrose1 commented 5 years ago

The session info that gives the error is below. Digging it in to it a bit, I believe it is from the bootstrapped F-stats. I think occasionally the part sampled excludes all of the pre period. You might be able to reproduce by increasing the number of bootstraps.

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

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

other attached packages:
[1] its.analysis_1.4.0

loaded via a namespace (and not attached):
 [1] zip_2.0.2         Rcpp_1.0.1        urca_1.3-0        cellranger_1.1.0  pillar_1.3.1      compiler_3.5.1   
 [7] plyr_1.8.4        forcats_0.4.0     tseries_0.10-47   tools_3.5.1       xts_0.11-2        boot_1.3-20      
[13] nlme_3.1-137      tibble_2.1.1      gtable_0.3.0      lattice_0.20-35   pkgconfig_2.0.2   rlang_0.3.4      
[19] openxlsx_4.1.0.1  rstudioapi_0.10   curl_3.3          yaml_2.2.0        parallel_3.5.1    haven_2.1.0      
[25] rio_0.5.16        dplyr_0.8.0.1     hms_0.4.2         lmtest_0.9-36     grid_3.5.1        nnet_7.3-12      
[31] forecast_8.7      tidyselect_0.2.5  data.table_1.12.2 glue_1.3.1        R6_2.4.0          readxl_1.3.1     
[37] foreign_0.8-70    carData_3.0-2     car_3.0-3         ggplot2_3.1.1     purrr_0.3.2       TTR_0.23-4       
[43] magrittr_1.5      scales_1.0.0      abind_1.4-5       assertthat_0.2.1  quantmod_0.4-15   timeDate_3043.102
[49] colorspace_1.4-1  fracdiff_1.4-2    labeling_0.3      quadprog_1.5-7    lazyeval_0.2.2    munsell_0.5.0    
[55] crayon_1.3.4      zoo_1.8-5    
patrick-eng commented 5 years ago

Hi Liam,

Thanks again for looking into this, really appreciate it.

The theory is very plausible! I've tested this with n=5 for bootstraps and still don't get the same error, though. I wonder if this is a versioning issue, particularly in reference to the boot package (very important to find out, if so!) I'm running R version 3.6.0 (2019-04-26). Is it possible for you to update/try this again on an updated machine and see if you still get the error?

lrose1 commented 5 years ago

Looks like that was it, no error with R 3.6.1. Unfortunately I can't guess what exactly is different to cause the error.

patrick-eng commented 5 years ago

Thanks for this Liam, really important information for the next package update. I'll include the dependency requirement of >= 3.6.0 in the next release! Cheers again for getting in touch!