clear
set more off

* Select dependent variable
local depvar = "nbre_hospit_corona_cum"

* Set maximum number of iterations 
local max_iter = 10000

* Set prediction depth
local depth = 14

foreach end_day in 26 25 27{ // Loop over limits of calibration period

use "$temp/departement_covid_19", clear

egen id =  group(departement_code)

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

local end_time = mdy(03,`end_day',2020)

quietly {

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

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 == `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 == `i', se(se)
		replace prediction_`end_day'_log3 = temp if id == `i' & date <= `end_time' + `depth'
		replace prediction_`end_day'_log3_se = se if id == `i' & date <= `end_time' + `depth'
	drop temp* se*
	}
	}
	
} // End of forvalues i = `min_dep'(1)`max_dep'{ 

} // End of quietly

* Save output
compress
save "$temp/predictions_`end_day'", replace

} // End of foreach end_day in ...{
