Announcement

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

  • tsappend

    Can we do tsappend with some formula. Suppose we have yearly time series up to 2019. I extend 4 observations. So 4 missing values generated. Now I want to fill it considering moving average of last 3 years. So if the variable is growth, then 2020 growth will be average growth of 2017, 2018, 2019./ 2021 growth will be average growth of 2018, 2019 and 2020. How to write the command without typing in data editor of STATA.

  • #2
    You didn't provide example data to work with, but I'll illustrate the approach with a toy data set.

    Code:
    clear*
    
    //  CREATE DEMONSTRATION DATASET
    set obs 19
    gen year = 2000 + _n
    set seed 1234
    gen growth = rgamma(1, 1)
    tsset year
    tsappend, add(4)
    
    local obs2020 = _N - 3
    
    forvalues i = `obs2020'/`=_N' {
        replace growth = (L1.growth + L2.growth + L3.growth)/3 in `i'
    }
    In the future, when asking for help with code, always show an example of your data set, and one that illustrates the problem you want solved. To do that, use the -dataex- command. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    I do have a question about the usefulness of doing this. After all, starting with year 2020, your variable "growth" is increasingly based on previously imputed, or perhaps I might say, imagined values. The value for year 2023, in fact, has no "real" data contribution at all.

    Finally, I will just note that I continue to be gobsmacked by seeing people on this Forum viewing typing hand-calculated results into the Editor as an acceptable fallback position. Unless you are just playing around with the data for fun, you shouldn't even consider that as a possibility. If others will be seeing your analysis you have an obligation to them to assure that all of your calculations from the starting dataset through to the final analysis have a complete audit trail, so that others can assure themselves that everything was done correctly. Anything done "off line" is unprofessional data management.

    Added: As an aside, this solution involves a loop over (a small subset of) observations. In recent years, a number of community-contributed commands have been written to bypass and improve upon such processes when they are based on calculations involving a range of observations defined by intervals on some variable. I'm thinking particularly of -rangestat- and -rangerun-. But this problem, as far as I can tell, cannot be solved with those because of the need to "live update" the growth variable before calculating the result for the next observation. If somebody else has a solution that uses one of these commands, I'd be eager to see it.
    Last edited by Clyde Schechter; 18 Nov 2019, 21:46.

    Comment


    • #3
      Many thanks for the reply. My intention is to do some scenario analysis. I shall assume future values of exogenous variables in a simultaneous equation model.

      Comment


      • #4
        Hi Clyde Schechter, if I want to do the similar exercise with two or more variables rather than one, then how can I write the commands. Thanks.

        Comment


        • #5
          I'm not sure I understand what the "similar exercise" you want to do with two or more variables is, but if you just mean that you want to do what is shown in #2 to some variables in addition to growth, then just create a foreach loop:

          Code:
          forvalues i = `obs2020'/`=_N' {
              foreach v of varlist growth var2 variable3 etc {
                  replace `v' = (L1.`v' + L2.`v' + L3.`v')/3 in `i'
              }
          }
          Do read -help foreach-. It is one of the most useful commands in Stata.

          Comment


          • #6
            I need help.
            I estimated VAR_DCC MGARCH for returns on the Brent Future prices, Gold Spot prices, S&P500 index and 10-years Treasury Bonds
            with a daily data, everything is ok, however, I couldn't forecast out-of-sample, so I can forecast the hedge ratio and the optimal weights for those. can anyone help me
            My stata code:
            mgarch dcc (gold oil SP bond=L1.gold L1.oil L1.SP L1.bond), arch(1) garch(1) iterate(1000)
            predict H*, variance
            twoway (tsline H*)
            predict rho*, correlation
            twoway (tsline rho*)
            test lambda1 = lambda2
            corr H*
            so I can have nice graphs

            Comment


            • #7
              #6 is completely off topic for this thread. Please repost as a New Topic with an appropriate title so that others who are looking for advice on your question (or a similar question) will be able to find it in the future. Thank you.

              Comment


              • #8
                Hi Clyde, I used following commands. I have two variables, names are growth and gdp. However, after executing the command, it says: variable var not found. Any suggestion. If I write v instead of var, same error occurs. variable v not found.
                clear all
                use "C:\Users\HP\Documents\Forecasting with STATA\gdp.dta", clear
                tsset year
                tsappend, add(4)

                local obs2020 = _N - 3

                forvalues i = `obs2020'/`=_N' {
                foreach var of varlist growth gdp {
                replace `var' = (L1.var + L2.var + L3.var)/3 in `i'
                }
                }

                Comment


                • #9
                  You did not adapt the code correctly. The line in the center of the loop has to read:
                  Code:
                  replace `var' = (L1.`var'+ L2.`var' + L3.`var')/3 in `i'

                  The macro quotes, shown above in red, are not optional.

                  Comment


                  • #10
                    Many thanks Clyde. Now it works.

                    Comment

                    Working...
                    X