Announcement

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

  • Year is not regularly spaced/must specify timevar

    Dear Statalist,

    I have some problems when using command xtgls to solve heteroskedasticity and autocorrelation. When I type the command, the respond I get is "Year is not regularly spaced or does not have intervals of delta -- use the force option to treat the intervals as though they were regular". Even when I xtset my data again and again, the response I get when using xtgls always "Year is not regularly spaced or does not have intervals of delta -- use the force option to treat the intervals as though they were regular" or "must specify timevar; use xtset" (r459). I don't know how to solve it even I had xtset do many times.

    More information about my data: I use 8 countries with 13 products code from year 2010-2021


    Please help me and thanks a lot
    Click image for larger version

Name:	Screenshot 2023-10-05 102251.png
Views:	1
Size:	23.0 KB
ID:	1729164

    Click image for larger version

Name:	Screenshot 2023-10-05 102338.png
Views:	1
Size:	6.5 KB
ID:	1729165


    in advance!

  • #2
    Well, your full data set must contain 8 countries * 13 product * 12 years = 1, 248 observations. The brief excerpt that you show in your post suggests that it is all nice and regular. But Stata is telling you that the rest of it isn't so nice and regular.

    It is telling you that in two ways. First, the message (note: 12 observations dropped because only 1 obs in group) tells you that there are 12 c_pcode's for which only one year's worth of data is available. (N.B. If any observation has a missing value for any variable mentioned in the -xtgls- command, it is omitted from the analysis. So it may be that you actually have 1,248 observations with 12 full years of data for all 13 products and 8 countries--but if some of those have missing values on some variables used in -xtgls-, they are not usable, and there are at least 12 cases where you have only a single year's data left.)

    The second way things are not so nice is the message that Year is not regularly spaced. Again, this probably results from some observations being dropped from the analysis due to missing values on some variable, leaving gaps in the spacing of the years.

    Now, the only part of your -xtgls- command that needs regularly spaced data is the -corr(ar1)- option. So try running the command without that. Then, immediately thereafter, run:
    Code:
    keep if e(sample) // RETAIN ONLY THE OBSERVATIONS USED BY -xtgls-
    by c_pcode (year), sort: gen byte problem = _N != (2021-2010+1)
    browse if problem
    This will show you all of the c_pcode's for which you lack regularly spaced data. You have three possible ways to go from here:

    1. If it is possible to fill in the missing values for the gap observations so you have a complete set of values for all variables for alll countries, products, and years, do that and then re-do the model with the -corr(ar1)- option. This will leave you with the analysis you originally wanted.
    2. If the missing values cannot be filled in, then you can drop the c_pcode groups that have problem == 1 and run your analysis again with -corr(ar1)-. This will give you an analysis that models the autocorrelation, but it will be on a subset of your data, possibly a biased subset.
    Or,
    3. Settle for the analysis without -corr(ar1)-.

    Comment


    • #3
      Thanks a lot for your response @Clyde Schechter!

      After I use the code you gave, the data editor appeared with variable names and blank spaces as in the image. Is that mean all of my c_pcode's lack regularly spaced data?


      Attached Files

      Comment


      • #4
        No, on the contrary, it means that it didn't find any observations with gaps. I'm perplexed, because if there are no observations with gaps, I don't know how to account for that error message. Let's try another approach. Start with your original data set and run:
        Code:
        xtset c_pcode Year
        display `r(gaps)'
        
        egen mcount = rowmiss(log_ex log_gdpvn log_gdppn log_AHStarr log_MFNtarr log_exrate dist)
        count if mcount
        
        keep if mcount > 0
        xtset, clear
        xtset c_pcode Year
        display `r(gaps)'
        What does this give you? Show all output including any error messages.

        Note: In that -egen- command, I have listed all of the variables that appear in your -xtgls- command. If I have misspelled or omitted any of them, please correct the -egen- command accordingly.

        Comment


        • #5
          Dear @Clyde Schechter,

          Thank you again and here is the output after applying all the above code:

          Click image for larger version

Name:	Screenshot 2023-10-06 095500.png
Views:	1
Size:	21.5 KB
ID:	1729306
          Click image for larger version

Name:	Screenshot 2023-10-06 095631.png
Views:	1
Size:	32.7 KB
ID:	1729307

          Comment


          • #6
            Sorry, I made a mistake in #4. What that has shown you is the observations that have missing values on regression variables.

            But this still doesn't help you find the gaps so you can decide how to handle them.

            Please re-do with the following code:
            Code:
            egen mcount = rowmiss(log_ex log_gdpvn log_gdppn log_AHStarr log_MFNtarr log_exrate dist)
            
            count if mcount
            
            keep if mcount == 0 // THIS IS THE REGRESSION ESTIMATION SAMPLE
            xtset, clear
            xtset c_pcode Year
            display `r(gaps)'

            Comment


            • #7
              Dear @Clyde Schechter,

              Here is the output of the updated code and I wonder if this output reveals the gaps. Thank you a lot.

              Click image for larger version

Name:	Screenshot 2023-10-06 161752.png
Views:	1
Size:	13.8 KB
ID:	1729347
              Click image for larger version

Name:	Screenshot 2023-10-06 161927.png
Views:	1
Size:	46.5 KB
ID:	1729348

              Comment


              • #8
                OK. So this confirms that there are time gaps in your data once the observations containing missing values are removed. The next task is to find them. Starting from the data as it is at the end of the code run for #7:
                Code:
                by c_pcode (Year), sort: gen gap = L.Year != Year-1 & _n > 1
                by c_pcode gap (Year), sort: replace gap = gap[_N]
                sort c_pcode Year
                browse if gap
                This will show you the observations that contain the gaps. You will then have to figure out whether it is possible to find the information necessary to fill the gaps. If not, then you must either abandon the -corr(ar1)- part of the model, or restrict the model to those c_pcode groups that have no gap.

                Comment


                • #9
                  Oh I see. Then, I will apply one of three possible ways that you suggested for next step. Thanks a lot for all your precious responses

                  Comment

                  Working...
                  X