clear
set more off

use "$temp/temp_final", clear

summ id_dep
local min_dep = r(min)
local max_dep = r(max)

* Select dependent variable
local depvar = "nbre_hospit_corona_cum"

* Set maximum number of iterations 
local max_iter = 10000

* Set earliest calibration period end
local early = mdy(03,25,2020)

* Set latest callibration period end
local late = mdy(03,26,2020)

quietly {

forvalues end_time = `early'(1)`late'{ // Loop over limits of calibration period

local end_day = day(`end_time')

foreach model in log3 {
	gen prediction_`end_day'_`model' = .
}

forvalues i = `min_dep'(1)`max_dep'{ // Loop over departements

* Display cosmetic counter
if  mod(`i',int(0.1*`max_dep')) == 0{
noisily : di "`i' / `max_dep'"
}

* Estimation

	capture{
	 nl log3 : `depvar' date if date <= `end_time' & id_dep == `i', iterate(`max_iter')
	}
	if !_rc & e(converged) == 1 { // Estimation success check #1
	if ~inlist(_se[/b1],.,0) & ~inlist(_se[/b2],.,0) & ~inlist(_se[/b3],.,0){ // Estimation success check #2
	predictnl temp = predict() if id_dep == `i'
		replace prediction_`end_day'_log3 = temp if id_dep == `i'
	drop temp*
	}
	}
	
	
} // End of forvalues i = `min_dep'(1)`max_dep'{ 


} // End of forvalues end_time = `early'(1)`late'{

} // End of quietly


* Save output
compress
save "$temp/predictions_final", replace
