conroylau / lpinfer

lpinfer: An R Package for Inference in Linear Programs
GNU General Public License v3.0
3 stars 5 forks source link

Strange bug in invertci #85

Closed a-torgovitsky closed 4 years ago

a-torgovitsky commented 4 years ago
devtools::load_all()
source("../lpinfer/example/dgp_mixedlogit.R")

dgp <- mixedlogit_dgp()
set.seed(1)
data <- mixedlogit_draw(dgp, n = 2000)

lpm <- lpmodel(A.obs = mixedlogit_Aobs(dgp),
               beta.obs = function(d) mixedlogit_betaobs(d, dgp),
               A.shp = rep(1, nrow(dgp$vdist)),
               beta.shp = 1,
               A.tgt = mixedlogit_Atgt_dfelast(dgp, w2eval = 1, eeval = -1))

r <- invertci(f = fsst, farg = list(lpm = lpm, data = data), lb0 = 0, ub0 = 1)

gives

< Constructing confidence interval for alpha = 0.05 >

 === Computing upper bound of confidence interval ===
 Iteration   Lower bound     Upper bound     Test point      p-value     Reject?                                   
 Left end pt.    0.00000     NA      0.00000     0.87017     FALSE  
 Right end pt.   NA      1.00000     1.00000     0.85935     FALSE                                              
Error in ci.bisection(f, farg, alpha[i], ub1, ub0, tol, max.iter, df_ci,  : 
  Please choose another interval.
In addition: Warning messages:
1: In `[<-.data.frame`(`*tmp*`, df_n + 1, 2, value = list(lambda = 0.870165051448408,  :
  provided 2 variables to replace 1 variables
2: In if (returnlist$pval < alphahalf) { :
  the condition has length > 1 and only the first element will be used
3: In `[<-.data.frame`(`*tmp*`, df_bis_row + 1, 5, value = list(lambda = 0.870165051448408,  :
  provided 2 variables to replace 1 variables
4: In `[<-.data.frame`(`*tmp*`, df_n + 1, 2, value = list(lambda = 0.85935000936219,  :
  provided 2 variables to replace 1 variables
5: In if (returnlist$pval < alphahalf) { :
  the condition has length > 1 and only the first element will be used
6: In `[<-.data.frame`(`*tmp*`, df_bis_row + 1, 5, value = list(lambda = 0.85935000936219,  :
  provided 2 variables to replace 1 variables
7: In if ((fb1 - alpha_2sided) * (fb0 - alpha_2sided) > 0) { :

 Error in ci.bisection(f, farg, alpha[i], ub1, ub0, tol, max.iter, df_ci,  : 
  Please choose another interval. 
conroylau commented 4 years ago

Done! The strange bug is because invertci extracted the wrong component in the table of p-values. It has now been fixed and the following output is now returned:

 === Computing upper bound of confidence interval ===
 Iteration   Lower bound     Upper bound     Test point      p-value     Reject?               
 Left end pt.    0.00000     NA      0.00000     0.00000     TRUE   
 Right end pt.   NA      1.00000     1.00000     0.00000     TRUE   
 1       0.00000     1.00000     0.50000     0.95000     FALSE         
 2       0.50000     1.00000     0.75000     0.45000     FALSE    
 3       0.75000     1.00000     0.87500     0.00000     TRUE     
 4       0.75000     0.87500     0.81250     0.08000     FALSE         
 5       0.81250     0.87500     0.84375     0.01000     TRUE 
 6       0.81250     0.84375     0.82812     0.04000     FALSE 
 7       0.82812     0.84375     0.83594     0.04000     FALSE 
 8       0.83594     0.84375     0.83984     0.00000     TRUE   
 9       0.83594     0.83984     0.83789     0.01000     TRUE   
 10          0.83594     0.83789     0.83691     0.03000     FALSE     
 11          0.83691     0.83789     0.83740     0.00000     TRUE   
 12          0.83691     0.83740     0.83716     0.01000     TRUE 
 13          0.83691     0.83716     0.83704     0.03000     FALSE   
 14          0.83704     0.83716     0.83710     0.00000     TRUE      
 >>> Length of interval is below tolerance level. Bisection method is completed.    

 === Computing lower bound of confidence interval ===
 Iteration   Lower bound     Upper bound     Test point      p-value     Reject?
 Left end pt.    0.00000     NA      0.00000     0.00000     TRUE   
 Right end pt.   NA      1.00000     1.00000     0.00000     TRUE   
 1       0.00000     1.00000     0.50000     0.95000     FALSE  
 2       0.00000     0.50000     0.25000     0.55000     FALSE 
 3       0.00000     0.25000     0.12500     0.05000     FALSE                   
 4       0.00000     0.12500     0.06250     0.00000     TRUE   
 5       0.06250     0.12500     0.09375     0.00000     TRUE                 
 6       0.09375     0.12500     0.10938     0.05000     FALSE  
 7       0.09375     0.10938     0.10156     0.01000     TRUE                
 8       0.10156     0.10938     0.10547     0.03000     FALSE        
 9       0.10156     0.10547     0.10352     0.03000     FALSE                
 10          0.10156     0.10352     0.10254     0.04000     FALSE           
 11          0.10156     0.10254     0.10205     0.09000     FALSE             
 12          0.10156     0.10205     0.10181     0.01000     TRUE                       
 13          0.10181     0.10205     0.10193     0.03000     FALSE                 
 14          0.10181     0.10193     0.10187     0.06000     FALSE            
 >>> Length of interval is below tolerance level. Bisection method is completed.   

Thanks!