How to take the 1st difference in Gauss?

Hello,
I am performing a panel data analysis with a dataset, I am performing unit root tests, but I need the 1st difference in a unit root test analysis.

How can I take the first difference and include it in the analysis?
Thank you very much.

2 Answers



0



GAUSS version 25 which is coming out this month, has a new built in function to compute differences of panel data.

Here is how you can do it now:

Let's say you have a dataframe that looks like this that is called df:

 Country     Year lgaspcar  lrpmg
 AUSTRIA     1960 4.173244 -0.3345476
 AUSTRIA     1961 4.100989 -0.3513276
 AUSTRIA     1962 4.073177 -0.3795177
 AUSTRIA     1963 4.059509 -0.4142514
 BELGIUM     1960 4.164016 -0.1657096
 BELGIUM     1961 4.124356 -0.171731
 BELGIUM     1962 4.075962 -0.2222914
 BELGIUM     1963 4.001266 -0.2504623

This code will create the first difference:

unique_ids = unique(df[.,id]);

// Find the first row of each id
id_idx = indnv(unique_ids, df[.,"Country"]) | (rows(df) + 1);

// Create a new dataframe with the same id's
// and dates, but missing values for the data
df_diff = df;
df_diff[.,3:4] = miss();

for i(1, rows(id_idx)-1, 1);
    // find the first and last rows of the i'th id
    start_ = id_idx[i];
    end_ = id_idx[i+1]-1;

    // Pull out the data for the i'th id
    diff_tmp = df[start_:end_,3:4];

    // Compute and assign the first difference
    df_diff[start_+1:end_,3:4] = trimr(diff_tmp, 1, 0)-trimr(diff_tmp, 0, 1);
endfor;

Which will equal:

 Country     Year   lgaspcar   lrpmg
 AUSTRIA     1960        .        .
 AUSTRIA     1961 -0.07225509 -0.01678
 AUSTRIA     1962 -0.02781255 -0.02819008
 AUSTRIA     1963 -0.01366743 -0.0347337
 BELGIUM     1960        .        .
 BELGIUM     1961 -0.03966033 -0.006021372
 BELGIUM     1962 -0.04839395 -0.05056039
 BELGIUM     1963 -0.07469562 -0.02817088

admin

47


0



GAUSS 25 is now available and includes pdDiff and pdLag for differencing and lagging panel data, respectively.

The use these functions, your panel data should be stored in a GAUSS dataframe and should:

  1. Contain at least one categorical variable.
  2. Contain at least one date variable.
  3. Be sorted by group and date. If your data is not sorted, the new pdSort can be used to sort your data.

GAUSS 25 Functions for Panel Data Transformations

Function Name Description Example
pdLag Computes lags of panel data. lag_pd = pdLag(df)
pdDiff Computes differences of panel data. delta_pd = pdDiff(df)

All the new panel data functions, including pdLag and pdDiff can intelligently detect your panel:

  • They will default to the first categorical or string variable in the dataframe to identify the panel groups.
  • They will default to the first date variable in the dataframe to identify the time dimension.

Alternatively, you can use optional arguments to specify the desired group variable and time variable.

Continuing with the example from the first response, using the df dataframe, GAUSS would use:

  • The Country variable to identify the panel groups.
  • The Year variable to identify the date variable (assuming Year is a date variable).

The first difference can now be found using a single line of code:

// Take first difference of data
// using the 'country' variable as the groupvar
// and using the 'year' variable as the date variable
delta_df = pdDiff(df);

If you're interested in trying these new functions out, contact us for a GAUSS 25 demo!


Eric

105

Your Answer

2 Answers

0

GAUSS version 25 which is coming out this month, has a new built in function to compute differences of panel data.

Here is how you can do it now:

Let's say you have a dataframe that looks like this that is called df:

 Country     Year lgaspcar  lrpmg
 AUSTRIA     1960 4.173244 -0.3345476
 AUSTRIA     1961 4.100989 -0.3513276
 AUSTRIA     1962 4.073177 -0.3795177
 AUSTRIA     1963 4.059509 -0.4142514
 BELGIUM     1960 4.164016 -0.1657096
 BELGIUM     1961 4.124356 -0.171731
 BELGIUM     1962 4.075962 -0.2222914
 BELGIUM     1963 4.001266 -0.2504623

This code will create the first difference:

unique_ids = unique(df[.,id]);

// Find the first row of each id
id_idx = indnv(unique_ids, df[.,"Country"]) | (rows(df) + 1);

// Create a new dataframe with the same id's
// and dates, but missing values for the data
df_diff = df;
df_diff[.,3:4] = miss();

for i(1, rows(id_idx)-1, 1);
    // find the first and last rows of the i'th id
    start_ = id_idx[i];
    end_ = id_idx[i+1]-1;

    // Pull out the data for the i'th id
    diff_tmp = df[start_:end_,3:4];

    // Compute and assign the first difference
    df_diff[start_+1:end_,3:4] = trimr(diff_tmp, 1, 0)-trimr(diff_tmp, 0, 1);
endfor;

Which will equal:

 Country     Year   lgaspcar   lrpmg
 AUSTRIA     1960        .        .
 AUSTRIA     1961 -0.07225509 -0.01678
 AUSTRIA     1962 -0.02781255 -0.02819008
 AUSTRIA     1963 -0.01366743 -0.0347337
 BELGIUM     1960        .        .
 BELGIUM     1961 -0.03966033 -0.006021372
 BELGIUM     1962 -0.04839395 -0.05056039
 BELGIUM     1963 -0.07469562 -0.02817088

0

GAUSS 25 is now available and includes pdDiff and pdLag for differencing and lagging panel data, respectively.

The use these functions, your panel data should be stored in a GAUSS dataframe and should:

  1. Contain at least one categorical variable.
  2. Contain at least one date variable.
  3. Be sorted by group and date. If your data is not sorted, the new pdSort can be used to sort your data.


GAUSS 25 Functions for Panel Data Transformations

Function Name Description Example
pdLag Computes lags of panel data. lag_pd = pdLag(df)
pdDiff Computes differences of panel data. delta_pd = pdDiff(df)

All the new panel data functions, including pdLag and pdDiff can intelligently detect your panel:

  • They will default to the first categorical or string variable in the dataframe to identify the panel groups.
  • They will default to the first date variable in the dataframe to identify the time dimension.

Alternatively, you can use optional arguments to specify the desired group variable and time variable.

Continuing with the example from the first response, using the df dataframe, GAUSS would use:

  • The Country variable to identify the panel groups.
  • The Year variable to identify the date variable (assuming Year is a date variable).

The first difference can now be found using a single line of code:

// Take first difference of data
// using the 'country' variable as the groupvar
// and using the 'year' variable as the date variable
delta_df = pdDiff(df);

If you're interested in trying these new functions out, contact us for a GAUSS 25 demo!


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.