Announcement

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

  • I cannot pet the unit root tests in Word file

    Hi everyone,
    I am trying to transfer the unit root tests in Time-Series (ADF, PP, ADF-GS) to Word file. I hope some one can help me!
    Here the code:

    Code:
    clear all
    use https://www.stata-press.com/data/r18/wpi1.dta, clear
    tsset t
    
    // Define temporary names and files
    tempname anees_uroot
    tempfile unitroots
    
    // Create a postfile to store results
    postfile `anees_uroot' str12 name float(dfuller_statistic dfuller_pvalue dfuller_lags) ///
                         float(pperron_statistic pperron_rho pperron_pvalue pperron_lags) ///
                         float(dfgls_statistic dfgls_pvalue dfgls_lags) ///
             using `unitroots'
    
    // Run tests and store results
    foreach var in ln_wpi {
        // Run dfuller test and capture results
        dfuller `var'
        local dfuller_statistic = r(Zt)
        local dfuller_pvalue = r(p)
        local dfuller_lags = r(lags)
    
        // Run pperron test and capture results
        pperron `var'
        local pperron_statistic = r(Zt)
        local pperron_rho = r(Zrho)
        local pperron_pvalue = r(p)
        local pperron_lags = r(lags)
    
        // Run dfgls test and capture results
        dfgls `var'
        local dfgls_statistic = r(Zt)
        local dfgls_pvalue = r(p)
        local dfgls_lags = r(lags)
    
        // Post results to the temporary file, handling missing values
        post `anees_uroot' ("`var'") ///
                           (cond(missing(`dfuller_statistic'), ., `dfuller_statistic')) ///
                           (cond(missing(`dfuller_pvalue'), ., `dfuller_pvalue')) ///
                           (cond(missing(`dfuller_lags'), ., `dfuller_lags')) ///
                           (cond(missing(`pperron_statistic'), ., `pperron_statistic')) ///
                           (cond(missing(`pperron_rho'), ., `pperron_rho')) ///
                           (cond(missing(`pperron_pvalue'), ., `pperron_pvalue')) ///
                           (cond(missing(`pperron_lags'), ., `pperron_lags')) ///
                           (cond(missing(`dfgls_statistic'), ., `dfgls_statistic')) ///
                           (cond(missing(`dfgls_pvalue'), ., `dfgls_pvalue')) ///
                           (cond(missing(`dfgls_lags'), ., `dfgls_lags'))
    }
    
    // Close the postfile
    postclose `anees_uroot'
    
    // Load the results from the temporary file
    use `unitroots', clear
    
    // Get the number of rows (number of observations) in the dataset
    local rows = _N + 1  // +1 for the header row
    
    // Export results to a Word document using putdocx
    putdocx begin
    putdocx paragraph, style(Heading1)
    putdocx text ("Unit Root Test Results")
    putdocx paragraph
    
    // Initialize a table with the calculated number of rows and 11 columns
    putdocx table results = (`rows', 11)
    
    // Add column headers to the first row
    putdocx table results(1,1) = ("Variable")
    putdocx table results(1,2) = ("Dickey-Fuller Statistic")
    putdocx table results(1,3) = ("DF P-value")
    putdocx table results(1,4) = ("DF Lags")
    putdocx table results(1,5) = ("Phillips-Perron Statistic")
    putdocx table results(1,6) = ("PP Rho")
    putdocx table results(1,7) = ("PP P-value")
    putdocx table results(1,8) = ("PP Lags")
    putdocx table results(1,9) = ("DF-GLS Statistic")
    putdocx table results(1,10) = ("DF-GLS P-value")
    putdocx table results(1,11) = ("DF-GLS Lags")
    
    // Loop through the rows and add data manually to each column
    forval i = 1/`=_N' {
        local row = `i' + 1
        putdocx table results(`row', 1) = name[`i']
        putdocx table results(`row', 2) = dfuller_statistic[`i']
        putdocx table results(`row', 3) = dfuller_pvalue[`i']
        putdocx table results(`row', 4) = dfuller_lags[`i']
        putdocx table results(`row', 5) = pperron_statistic[`i']
        putdocx table results(`row', 6) = pperron_rho[`i']
        putdocx table results(`row', 7) = pperron_pvalue[`i']
        putdocx table results(`row', 8) = pperron_lags[`i']
        putdocx table results(`row', 9) = dfgls_statistic[`i']
        putdocx table results(`row', 10) = dfgls_pvalue[`i']
        putdocx table results(`row', 11) = dfgls_lags[`i']
    }
    // Save the Word document
    putdocx save "unitroots_results.docx", replace
    I got:
    Code:
    // Loop through the rows and add data manually to each column
     forval i = 1/`=_N' {
         local row = `i' + 1
           putdocx table results(`row', 1) = name[`i']
         putdocx table results(`row', 2) = dfuller_statistic[`i']
        putdocx table results(`row', 3) = dfuller_pvalue[`i']
         putdocx table results(`row', 4) = dfuller_lags[`i']
        putdocx table results(`row', 5) = pperron_statistic[`i']
         putdocx table results(`row', 6) = pperron_rho[`i']
         putdocx table results(`row', 7) = pperron_pvalue[`i']
         putdocx table results(`row', 8) = pperron_lags[`i']
         putdocx table results(`row', 9) = dfgls_statistic[`i']
         putdocx table results(`row', 10) = dfgls_pvalue[`i']
         putdocx table results(`row', 11) = dfgls_lags[`i']
      }
    syntax error
    r(198);
    
    end of do-file

  • #2
    You are missing parentheses when doing the assignment, hence the r(198) syntax error. Look at your other putdocx table commands.

    Code:
    // Loop through the rows and add data manually to each column
    forval i = 1/`=_N' {
        local row = `i' + 1
        putdocx table results(`row', 1) = (name[`i'])
        putdocx table results(`row', 2) = (dfuller_statistic[`i'])
        putdocx table results(`row', 3) = (dfuller_pvalue[`i'])
        putdocx table results(`row', 4) = (dfuller_lags[`i'])
        putdocx table results(`row', 5) = (pperron_statistic[`i'])
        putdocx table results(`row', 6) = (pperron_rho[`i'])
        putdocx table results(`row', 7) = (pperron_pvalue[`i'])
        putdocx table results(`row', 8) = (pperron_lags[`i'])
        putdocx table results(`row', 9) = (dfgls_statistic[`i'])
        putdocx table results(`row', 10) = (dfgls_pvalue[`i'])
        putdocx table results(`row', 11) = (dfgls_lags[`i'])
    }

    Comment


    • #3
      Dear Andrew Musau,
      Thank you very much for your help. It is working now.

      Comment

      Working...
      X