Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to create impulse response after local projection?

    I am trying to estimate fiscal multiplier with local projection with IV but I couldn't create graph and cumulative multiplier too. Please help.

    The code I used are:

    * Import Data
    import excel "C:\Users\absol\Downloads\data set (1).xlsx", sheet("Sheet4") firstrow
    tsset Date, yearly
    sort Date

    * Normalize variables by potential GDP
    gen milshock = military_spending / potential_gdp // Fix potential typo in original variable name
    gen y = real_gdp / potential_gdp
    gen g = gov_spending / potential_gdp

    * Generate cumulative sums for forecast horizons
    gen cumuly = y
    gen cumulg = g

    forvalues h = 1/20 {
    gen double f`h'cumuly = F`h'.y + L.cumuly if !missing(F`h'.y) & !missing(L.cumuly)
    gen double f`h'cumulg = F`h'.g + L.cumulg if !missing(F`h'.g) & !missing(L.cumulg)
    }

    * Fix missing values using linear interpolation
    foreach var in milshock y g {
    replace `var' = (L.`var' + F.`var') / 2 if missing(`var') & !missing(L.`var') & !missing(F.`var')
    }

    * Ensure no missing values for main variables
    drop if missing(milshock) | missing(g) | missing(y)

    * Instrument correlation check
    correlate milshock L1.milshock L2.milshock L3.milshock

    * Define instrument set
    local instr milshock L1.milshock L2.milshock L3.milshock

    * Initialize storage variables
    gen multiplier = .
    gen stderr = .
    gen kleibergen_papp = .

    * Run IV regressions for horizons 1-5
    forvalues h = 1/5 {
    capture noisily ivreg2 cumulg (F`h'.cumulg = `instr'), robust bw(auto) first
    if _rc == 0 {
    replace multiplier = _b[F`h'.cumulg] if _n == `h'
    replace stderr = _se[F`h'.cumulg] if _n == `h'
    scalar kp_stat = e(widstat)
    replace kleibergen_papp = kp_stat if _n == `h'
    }
    }

    * Prepare data for impulse response plot
    gen upper = multiplier + 1.96*stderr
    gen lower = multiplier - 1.96*stderr
    gen horizon = _n

    * Create impulse response graph
    twoway (connected multiplier horizon, lcolor(navy) mcolor(navy) msymbol(D)) ///
    (rcap upper lower horizon, lcolor(maroon) lwidth(0.8)), ///
    xtitle("Years After Shock", size(medsmall)) ///
    ytitle("Fiscal Multiplier", size(medsmall)) ///
    title("Impulse Response of Fiscal Multiplier", size(medlarge)) ///
    legend(order(1 "Point Estimate" 2 "95% CI") pos(6) rows(1)) ///
    xlabel(1(1)5, grid) ylabel(, grid) ///
    yline(0, lcolor(gs8) lpattern(dash)) ///
    plotregion(color(white)) graphregion(color(white)) ///
    note("Instrument: Military Spending Shocks", span)

    * Display numerical results
    list horizon multiplier stderr kleibergen_papp if _n <=5, sep(0)


    This is the error I am getting.

    . * Create impulse response graph

    .
    . twoway (connected multiplier horizon, lcolor(navy) mcolor(navy) msymbol(D)) ///
    / / / is not a twoway plot type
    r(198);

    .
    . (rcap upper lower horizon, lcolor(maroon) lwidth(0.8)), ///
    ( is not a valid command name
    r(199);

    .
    . xtitle("Years After Shock", size(medsmall)) ///
    command xtitle is unrecognized
    r(199);

    .
    . ytitle("Fiscal Multiplier", size(medsmall)) ///
    command ytitle is unrecognized
    r(199);

    .
    . title("Impulse Response of Fiscal Multiplier", size(medlarge)) ///
    command title is unrecognized
    r(199);

    .
    . legend(order(1 "Point Estimate" 2 "95% CI") pos(6) rows(1)) ///
    command legend is unrecognized
    r(199);

    .
    . xlabel(1(1)5, grid) ylabel(, grid) ///
    command xlabel is unrecognized
    r(199);

    .
    . yline(0, lcolor(gs8) lpattern(dash)) ///
    command yline is unrecognized
    r(199);

    .
    . plotregion(color(white)) graphregion(color(white)) ///
    command plotregion is unrecognized
    r(199);

    .
    . note("Instrument: Military Spending Shocks", span)
    type mismatch
    r(109);

    Thank you in advance

  • #2
    i am working on stata 17

    Comment


    • #3
      Did you run this code in Command window? or in Do-file Editor?
      /// lets you split long lines across multiple lines in the do-file, you may not use them interactively.
      Last edited by Chen Samulsion; 08 Mar 2025, 01:02.

      Comment

      Working...
      X