This function makes the data balanced, i.e., each individual has the same time periods, by filling in or dropping observations

make.pbalanced(
  x,
  balance.type = c("fill", "shared.times", "shared.individuals"),
  ...
)

# S3 method for class 'pdata.frame'
make.pbalanced(
  x,
  balance.type = c("fill", "shared.times", "shared.individuals"),
  ...
)

# S3 method for class 'pseries'
make.pbalanced(
  x,
  balance.type = c("fill", "shared.times", "shared.individuals"),
  ...
)

# S3 method for class 'data.frame'
make.pbalanced(
  x,
  balance.type = c("fill", "shared.times", "shared.individuals"),
  index = NULL,
  ...
)

Arguments

x

an object of class pdata.frame, data.frame, or pseries;

balance.type

character, one of "fill", "shared.times", or "shared.individuals", see Details,

...

further arguments.

index

only relevant for data.frame interface; if NULL, the first two columns of the data.frame are assumed to be the index variables; if not NULL, both dimensions ('individual', 'time') need to be specified by index as character of length 2 for data frames, for further details see pdata.frame(),

Value

An object of the same class as the input x, i.e., a pdata.frame, data.frame or a pseries which is made balanced based on the index variables. The returned data are sorted as a stacked time series.

Details

(p)data.frame and pseries objects are made balanced, meaning each individual has the same time periods. Depending on the value of balance.type, the balancing is done in different ways:

  • balance.type = "fill" (default): The union of available time periods over all individuals is taken (w/o NA values). Missing time periods for an individual are identified and corresponding rows (elements for pseries) are inserted and filled with NA for the non–index variables (elements for a pseries). This means, only time periods present for at least one individual are inserted, if missing.

  • balance.type = "shared.times": The intersect of available time periods over all individuals is taken (w/o NA values). Thus, time periods not available for all individuals are discarded, i. e., only time periods shared by all individuals are left in the result).

  • balance.type = "shared.individuals": All available time periods are kept and those individuals are dropped for which not all time periods are available, i. e., only individuals shared by all time periods are left in the result (symmetric to "shared.times").

The data are not necessarily made consecutive (regular time series with distance 1), because balancedness does not imply consecutiveness. For making the data consecutive, use make.pconsecutive() (and, optionally, set argument balanced = TRUE to make consecutive and balanced, see also Examples for a comparison of the two functions.

Note: Rows of (p)data.frames (elements for pseries) with NA values in individual or time index are not examined but silently dropped before the data are made balanced. In this case, it cannot be inferred which individual or time period is meant by the missing value(s) (see also Examples). Especially, this means: NA values in the first/last position of the original time periods for an individual are dropped, which are usually meant to depict the beginning and ending of the time series for that individual. Thus, one might want to check if there are any NA values in the index variables before applying make.pbalanced, and especially check for NA values in the first and last position for each individual in original data and, if so, maybe set those to some meaningful begin/end value for the time series.

See also

is.pbalanced() to check if data are balanced; is.pconsecutive() to check if data are consecutive; make.pconsecutive() to make data consecutive (and, optionally, also balanced).
punbalancedness() for two measures of unbalancedness, pdim() to check the dimensions of a 'pdata.frame' (and other objects), pvar() to check for individual and time variation of a 'pdata.frame' (and other objects), lag() for lagging (and leading) values of a 'pseries' object.
pseries(), data.frame(), pdata.frame().

Author

Kevin Tappe

Examples


# take data and make it unbalanced
# by deletion of 2nd row (2nd time period for first individual)
data("Grunfeld", package = "plm")
nrow(Grunfeld)                            # 200 rows
#> [1] 200
Grunfeld_missing_period <- Grunfeld[-2, ]
pdim(Grunfeld_missing_period)$balanced    # check if balanced: FALSE
#> [1] FALSE
make.pbalanced(Grunfeld_missing_period)   # make it balanced (by filling)
#>     firm year     inv   value capital
#> 1      1 1935  317.60 3078.50    2.80
#> 2      1 1936      NA      NA      NA
#> 3      1 1937  410.60 5387.10  156.90
#> 4      1 1938  257.70 2792.20  209.20
#> 5      1 1939  330.80 4313.20  203.40
#> 6      1 1940  461.20 4643.90  207.20
#> 7      1 1941  512.00 4551.20  255.20
#> 8      1 1942  448.00 3244.10  303.70
#> 9      1 1943  499.60 4053.70  264.10
#> 10     1 1944  547.50 4379.30  201.60
#> 11     1 1945  561.20 4840.90  265.00
#> 12     1 1946  688.10 4900.90  402.20
#> 13     1 1947  568.90 3526.50  761.50
#> 14     1 1948  529.20 3254.70  922.40
#> 15     1 1949  555.10 3700.20 1020.10
#> 16     1 1950  642.90 3755.60 1099.00
#> 17     1 1951  755.90 4833.00 1207.70
#> 18     1 1952  891.20 4924.90 1430.50
#> 19     1 1953 1304.40 6241.70 1777.30
#> 20     1 1954 1486.70 5593.60 2226.30
#> 21     2 1935  209.90 1362.40   53.80
#> 22     2 1936  355.30 1807.10   50.50
#> 23     2 1937  469.90 2676.30  118.10
#> 24     2 1938  262.30 1801.90  260.20
#> 25     2 1939  230.40 1957.30  312.70
#> 26     2 1940  361.60 2202.90  254.20
#> 27     2 1941  472.80 2380.50  261.40
#> 28     2 1942  445.60 2168.60  298.70
#> 29     2 1943  361.60 1985.10  301.80
#> 30     2 1944  288.20 1813.90  279.10
#> 31     2 1945  258.70 1850.20  213.80
#> 32     2 1946  420.30 2067.70  132.60
#> 33     2 1947  420.50 1796.70  264.80
#> 34     2 1948  494.50 1625.80  306.90
#> 35     2 1949  405.10 1667.00  351.10
#> 36     2 1950  418.80 1677.40  357.80
#> 37     2 1951  588.20 2289.50  342.10
#> 38     2 1952  645.50 2159.40  444.20
#> 39     2 1953  641.00 2031.30  623.60
#> 40     2 1954  459.30 2115.50  669.70
#> 41     3 1935   33.10 1170.60   97.80
#> 42     3 1936   45.00 2015.80  104.40
#> 43     3 1937   77.20 2803.30  118.00
#> 44     3 1938   44.60 2039.70  156.20
#> 45     3 1939   48.10 2256.20  172.60
#> 46     3 1940   74.40 2132.20  186.60
#> 47     3 1941  113.00 1834.10  220.90
#> 48     3 1942   91.90 1588.00  287.80
#> 49     3 1943   61.30 1749.40  319.90
#> 50     3 1944   56.80 1687.20  321.30
#> 51     3 1945   93.60 2007.70  319.60
#> 52     3 1946  159.90 2208.30  346.00
#> 53     3 1947  147.20 1656.70  456.40
#> 54     3 1948  146.30 1604.40  543.40
#> 55     3 1949   98.30 1431.80  618.30
#> 56     3 1950   93.50 1610.50  647.40
#> 57     3 1951  135.20 1819.40  671.30
#> 58     3 1952  157.30 2079.70  726.10
#> 59     3 1953  179.50 2371.60  800.30
#> 60     3 1954  189.60 2759.90  888.90
#> 61     4 1935   40.29  417.50   10.50
#> 62     4 1936   72.76  837.80   10.20
#> 63     4 1937   66.26  883.90   34.70
#> 64     4 1938   51.60  437.90   51.80
#> 65     4 1939   52.41  679.70   64.30
#> 66     4 1940   69.41  727.80   67.10
#> 67     4 1941   68.35  643.60   75.20
#> 68     4 1942   46.80  410.90   71.40
#> 69     4 1943   47.40  588.40   67.10
#> 70     4 1944   59.57  698.40   60.50
#> 71     4 1945   88.78  846.40   54.60
#> 72     4 1946   74.12  893.80   84.80
#> 73     4 1947   62.68  579.00   96.80
#> 74     4 1948   89.36  694.60  110.20
#> 75     4 1949   78.98  590.30  147.40
#> 76     4 1950  100.66  693.50  163.20
#> 77     4 1951  160.62  809.00  203.50
#> 78     4 1952  145.00  727.00  290.60
#> 79     4 1953  174.93 1001.50  346.10
#> 80     4 1954  172.49  703.20  414.90
#> 81     5 1935   39.68  157.70  183.20
#> 82     5 1936   50.73  167.90  204.00
#> 83     5 1937   74.24  192.90  236.00
#> 84     5 1938   53.51  156.70  291.70
#> 85     5 1939   42.65  191.40  323.10
#> 86     5 1940   46.48  185.50  344.00
#> 87     5 1941   61.40  199.60  367.70
#> 88     5 1942   39.67  189.50  407.20
#> 89     5 1943   62.24  151.20  426.60
#> 90     5 1944   52.32  187.70  470.00
#> 91     5 1945   63.21  214.70  499.20
#> 92     5 1946   59.37  232.90  534.60
#> 93     5 1947   58.02  249.00  566.60
#> 94     5 1948   70.34  224.50  595.30
#> 95     5 1949   67.42  237.30  631.40
#> 96     5 1950   55.74  240.10  662.30
#> 97     5 1951   80.30  327.30  683.90
#> 98     5 1952   85.40  359.40  729.30
#> 99     5 1953   91.90  398.40  774.30
#> 100    5 1954   81.43  365.70  804.90
#> 101    6 1935   20.36  197.00    6.50
#> 102    6 1936   25.98  210.30   15.80
#> 103    6 1937   25.94  223.10   27.70
#> 104    6 1938   27.53  216.70   39.20
#> 105    6 1939   24.60  286.40   48.60
#> 106    6 1940   28.54  298.00   52.50
#> 107    6 1941   43.41  276.90   61.50
#> 108    6 1942   42.81  272.60   80.50
#> 109    6 1943   27.84  287.40   94.40
#> 110    6 1944   32.60  330.30   92.60
#> 111    6 1945   39.03  324.40   92.30
#> 112    6 1946   50.17  401.90   94.20
#> 113    6 1947   51.85  407.40  111.40
#> 114    6 1948   64.03  409.20  127.40
#> 115    6 1949   68.16  482.20  149.30
#> 116    6 1950   77.34  673.80  164.40
#> 117    6 1951   95.30  676.90  177.20
#> 118    6 1952   99.49  702.00  200.00
#> 119    6 1953  127.52  793.50  211.50
#> 120    6 1954  135.72  927.30  238.70
#> 121    7 1935   24.43  138.00  100.20
#> 122    7 1936   23.21  200.10  125.00
#> 123    7 1937   32.78  210.10  142.40
#> 124    7 1938   32.54  161.20  165.10
#> 125    7 1939   26.65  161.70  194.80
#> 126    7 1940   33.71  145.10  222.90
#> 127    7 1941   43.50  110.60  252.10
#> 128    7 1942   34.46   98.10  276.30
#> 129    7 1943   44.28  108.80  300.30
#> 130    7 1944   70.80  118.20  318.20
#> 131    7 1945   44.12  126.50  336.20
#> 132    7 1946   48.98  156.70  351.20
#> 133    7 1947   48.51  119.40  373.60
#> 134    7 1948   50.00  129.10  389.40
#> 135    7 1949   50.59  134.80  406.70
#> 136    7 1950   42.53  140.80  429.50
#> 137    7 1951   64.77  179.00  450.60
#> 138    7 1952   72.68  178.10  466.90
#> 139    7 1953   73.86  186.80  486.20
#> 140    7 1954   89.51  192.70  511.30
#> 141    8 1935   12.93  191.50    1.80
#> 142    8 1936   25.90  516.00    0.80
#> 143    8 1937   35.05  729.00    7.40
#> 144    8 1938   22.89  560.40   18.10
#> 145    8 1939   18.84  519.90   23.50
#> 146    8 1940   28.57  628.50   26.50
#> 147    8 1941   48.51  537.10   36.20
#> 148    8 1942   43.34  561.20   60.80
#> 149    8 1943   37.02  617.20   84.40
#> 150    8 1944   37.81  626.70   91.20
#> 151    8 1945   39.27  737.20   92.40
#> 152    8 1946   53.46  760.50   86.00
#> 153    8 1947   55.56  581.40  111.10
#> 154    8 1948   49.56  662.30  130.60
#> 155    8 1949   32.04  583.80  141.80
#> 156    8 1950   32.24  635.20  136.70
#> 157    8 1951   54.38  723.80  129.70
#> 158    8 1952   71.78  864.10  145.50
#> 159    8 1953   90.08 1193.50  174.80
#> 160    8 1954   68.60 1188.90  213.50
#> 161    9 1935   26.63  290.60  162.00
#> 162    9 1936   23.39  291.10  174.00
#> 163    9 1937   30.65  335.00  183.00
#> 164    9 1938   20.89  246.00  198.00
#> 165    9 1939   28.78  356.20  208.00
#> 166    9 1940   26.93  289.80  223.00
#> 167    9 1941   32.08  268.20  234.00
#> 168    9 1942   32.21  213.30  248.00
#> 169    9 1943   35.69  348.20  274.00
#> 170    9 1944   62.47  374.20  282.00
#> 171    9 1945   52.32  387.20  316.00
#> 172    9 1946   56.95  347.40  302.00
#> 173    9 1947   54.32  291.90  333.00
#> 174    9 1948   40.53  297.20  359.00
#> 175    9 1949   32.54  276.90  370.00
#> 176    9 1950   43.48  274.60  376.00
#> 177    9 1951   56.49  339.90  391.00
#> 178    9 1952   65.98  474.80  414.00
#> 179    9 1953   66.11  496.00  443.00
#> 180    9 1954   49.34  474.50  468.00
#> 181   10 1935    2.54   70.91    4.50
#> 182   10 1936    2.00   87.94    4.71
#> 183   10 1937    2.19   82.20    4.57
#> 184   10 1938    1.99   58.72    4.56
#> 185   10 1939    2.03   80.54    4.38
#> 186   10 1940    1.81   86.47    4.21
#> 187   10 1941    2.14   77.68    4.12
#> 188   10 1942    1.86   62.16    3.83
#> 189   10 1943    0.93   62.24    3.58
#> 190   10 1944    1.18   61.82    3.41
#> 191   10 1945    1.36   65.85    3.31
#> 192   10 1946    2.24   69.54    3.23
#> 193   10 1947    3.81   64.97    3.90
#> 194   10 1948    5.66   68.00    5.38
#> 195   10 1949    4.21   71.24    7.39
#> 196   10 1950    3.42   69.05    8.74
#> 197   10 1951    4.67   83.04    9.07
#> 198   10 1952    6.00   74.42    9.93
#> 199   10 1953    6.53   63.51   11.68
#> 200   10 1954    5.12   58.12   14.33
make.pbalanced(Grunfeld_missing_period, balance.type = "shared.times") # (shared periods)
#>     firm year     inv   value capital
#> 1      1 1935  317.60 3078.50    2.80
#> 3      1 1937  410.60 5387.10  156.90
#> 4      1 1938  257.70 2792.20  209.20
#> 5      1 1939  330.80 4313.20  203.40
#> 6      1 1940  461.20 4643.90  207.20
#> 7      1 1941  512.00 4551.20  255.20
#> 8      1 1942  448.00 3244.10  303.70
#> 9      1 1943  499.60 4053.70  264.10
#> 10     1 1944  547.50 4379.30  201.60
#> 11     1 1945  561.20 4840.90  265.00
#> 12     1 1946  688.10 4900.90  402.20
#> 13     1 1947  568.90 3526.50  761.50
#> 14     1 1948  529.20 3254.70  922.40
#> 15     1 1949  555.10 3700.20 1020.10
#> 16     1 1950  642.90 3755.60 1099.00
#> 17     1 1951  755.90 4833.00 1207.70
#> 18     1 1952  891.20 4924.90 1430.50
#> 19     1 1953 1304.40 6241.70 1777.30
#> 20     1 1954 1486.70 5593.60 2226.30
#> 21     2 1935  209.90 1362.40   53.80
#> 23     2 1937  469.90 2676.30  118.10
#> 24     2 1938  262.30 1801.90  260.20
#> 25     2 1939  230.40 1957.30  312.70
#> 26     2 1940  361.60 2202.90  254.20
#> 27     2 1941  472.80 2380.50  261.40
#> 28     2 1942  445.60 2168.60  298.70
#> 29     2 1943  361.60 1985.10  301.80
#> 30     2 1944  288.20 1813.90  279.10
#> 31     2 1945  258.70 1850.20  213.80
#> 32     2 1946  420.30 2067.70  132.60
#> 33     2 1947  420.50 1796.70  264.80
#> 34     2 1948  494.50 1625.80  306.90
#> 35     2 1949  405.10 1667.00  351.10
#> 36     2 1950  418.80 1677.40  357.80
#> 37     2 1951  588.20 2289.50  342.10
#> 38     2 1952  645.50 2159.40  444.20
#> 39     2 1953  641.00 2031.30  623.60
#> 40     2 1954  459.30 2115.50  669.70
#> 41     3 1935   33.10 1170.60   97.80
#> 43     3 1937   77.20 2803.30  118.00
#> 44     3 1938   44.60 2039.70  156.20
#> 45     3 1939   48.10 2256.20  172.60
#> 46     3 1940   74.40 2132.20  186.60
#> 47     3 1941  113.00 1834.10  220.90
#> 48     3 1942   91.90 1588.00  287.80
#> 49     3 1943   61.30 1749.40  319.90
#> 50     3 1944   56.80 1687.20  321.30
#> 51     3 1945   93.60 2007.70  319.60
#> 52     3 1946  159.90 2208.30  346.00
#> 53     3 1947  147.20 1656.70  456.40
#> 54     3 1948  146.30 1604.40  543.40
#> 55     3 1949   98.30 1431.80  618.30
#> 56     3 1950   93.50 1610.50  647.40
#> 57     3 1951  135.20 1819.40  671.30
#> 58     3 1952  157.30 2079.70  726.10
#> 59     3 1953  179.50 2371.60  800.30
#> 60     3 1954  189.60 2759.90  888.90
#> 61     4 1935   40.29  417.50   10.50
#> 63     4 1937   66.26  883.90   34.70
#> 64     4 1938   51.60  437.90   51.80
#> 65     4 1939   52.41  679.70   64.30
#> 66     4 1940   69.41  727.80   67.10
#> 67     4 1941   68.35  643.60   75.20
#> 68     4 1942   46.80  410.90   71.40
#> 69     4 1943   47.40  588.40   67.10
#> 70     4 1944   59.57  698.40   60.50
#> 71     4 1945   88.78  846.40   54.60
#> 72     4 1946   74.12  893.80   84.80
#> 73     4 1947   62.68  579.00   96.80
#> 74     4 1948   89.36  694.60  110.20
#> 75     4 1949   78.98  590.30  147.40
#> 76     4 1950  100.66  693.50  163.20
#> 77     4 1951  160.62  809.00  203.50
#> 78     4 1952  145.00  727.00  290.60
#> 79     4 1953  174.93 1001.50  346.10
#> 80     4 1954  172.49  703.20  414.90
#> 81     5 1935   39.68  157.70  183.20
#> 83     5 1937   74.24  192.90  236.00
#> 84     5 1938   53.51  156.70  291.70
#> 85     5 1939   42.65  191.40  323.10
#> 86     5 1940   46.48  185.50  344.00
#> 87     5 1941   61.40  199.60  367.70
#> 88     5 1942   39.67  189.50  407.20
#> 89     5 1943   62.24  151.20  426.60
#> 90     5 1944   52.32  187.70  470.00
#> 91     5 1945   63.21  214.70  499.20
#> 92     5 1946   59.37  232.90  534.60
#> 93     5 1947   58.02  249.00  566.60
#> 94     5 1948   70.34  224.50  595.30
#> 95     5 1949   67.42  237.30  631.40
#> 96     5 1950   55.74  240.10  662.30
#> 97     5 1951   80.30  327.30  683.90
#> 98     5 1952   85.40  359.40  729.30
#> 99     5 1953   91.90  398.40  774.30
#> 100    5 1954   81.43  365.70  804.90
#> 101    6 1935   20.36  197.00    6.50
#> 103    6 1937   25.94  223.10   27.70
#> 104    6 1938   27.53  216.70   39.20
#> 105    6 1939   24.60  286.40   48.60
#> 106    6 1940   28.54  298.00   52.50
#> 107    6 1941   43.41  276.90   61.50
#> 108    6 1942   42.81  272.60   80.50
#> 109    6 1943   27.84  287.40   94.40
#> 110    6 1944   32.60  330.30   92.60
#> 111    6 1945   39.03  324.40   92.30
#> 112    6 1946   50.17  401.90   94.20
#> 113    6 1947   51.85  407.40  111.40
#> 114    6 1948   64.03  409.20  127.40
#> 115    6 1949   68.16  482.20  149.30
#> 116    6 1950   77.34  673.80  164.40
#> 117    6 1951   95.30  676.90  177.20
#> 118    6 1952   99.49  702.00  200.00
#> 119    6 1953  127.52  793.50  211.50
#> 120    6 1954  135.72  927.30  238.70
#> 121    7 1935   24.43  138.00  100.20
#> 123    7 1937   32.78  210.10  142.40
#> 124    7 1938   32.54  161.20  165.10
#> 125    7 1939   26.65  161.70  194.80
#> 126    7 1940   33.71  145.10  222.90
#> 127    7 1941   43.50  110.60  252.10
#> 128    7 1942   34.46   98.10  276.30
#> 129    7 1943   44.28  108.80  300.30
#> 130    7 1944   70.80  118.20  318.20
#> 131    7 1945   44.12  126.50  336.20
#> 132    7 1946   48.98  156.70  351.20
#> 133    7 1947   48.51  119.40  373.60
#> 134    7 1948   50.00  129.10  389.40
#> 135    7 1949   50.59  134.80  406.70
#> 136    7 1950   42.53  140.80  429.50
#> 137    7 1951   64.77  179.00  450.60
#> 138    7 1952   72.68  178.10  466.90
#> 139    7 1953   73.86  186.80  486.20
#> 140    7 1954   89.51  192.70  511.30
#> 141    8 1935   12.93  191.50    1.80
#> 143    8 1937   35.05  729.00    7.40
#> 144    8 1938   22.89  560.40   18.10
#> 145    8 1939   18.84  519.90   23.50
#> 146    8 1940   28.57  628.50   26.50
#> 147    8 1941   48.51  537.10   36.20
#> 148    8 1942   43.34  561.20   60.80
#> 149    8 1943   37.02  617.20   84.40
#> 150    8 1944   37.81  626.70   91.20
#> 151    8 1945   39.27  737.20   92.40
#> 152    8 1946   53.46  760.50   86.00
#> 153    8 1947   55.56  581.40  111.10
#> 154    8 1948   49.56  662.30  130.60
#> 155    8 1949   32.04  583.80  141.80
#> 156    8 1950   32.24  635.20  136.70
#> 157    8 1951   54.38  723.80  129.70
#> 158    8 1952   71.78  864.10  145.50
#> 159    8 1953   90.08 1193.50  174.80
#> 160    8 1954   68.60 1188.90  213.50
#> 161    9 1935   26.63  290.60  162.00
#> 163    9 1937   30.65  335.00  183.00
#> 164    9 1938   20.89  246.00  198.00
#> 165    9 1939   28.78  356.20  208.00
#> 166    9 1940   26.93  289.80  223.00
#> 167    9 1941   32.08  268.20  234.00
#> 168    9 1942   32.21  213.30  248.00
#> 169    9 1943   35.69  348.20  274.00
#> 170    9 1944   62.47  374.20  282.00
#> 171    9 1945   52.32  387.20  316.00
#> 172    9 1946   56.95  347.40  302.00
#> 173    9 1947   54.32  291.90  333.00
#> 174    9 1948   40.53  297.20  359.00
#> 175    9 1949   32.54  276.90  370.00
#> 176    9 1950   43.48  274.60  376.00
#> 177    9 1951   56.49  339.90  391.00
#> 178    9 1952   65.98  474.80  414.00
#> 179    9 1953   66.11  496.00  443.00
#> 180    9 1954   49.34  474.50  468.00
#> 181   10 1935    2.54   70.91    4.50
#> 183   10 1937    2.19   82.20    4.57
#> 184   10 1938    1.99   58.72    4.56
#> 185   10 1939    2.03   80.54    4.38
#> 186   10 1940    1.81   86.47    4.21
#> 187   10 1941    2.14   77.68    4.12
#> 188   10 1942    1.86   62.16    3.83
#> 189   10 1943    0.93   62.24    3.58
#> 190   10 1944    1.18   61.82    3.41
#> 191   10 1945    1.36   65.85    3.31
#> 192   10 1946    2.24   69.54    3.23
#> 193   10 1947    3.81   64.97    3.90
#> 194   10 1948    5.66   68.00    5.38
#> 195   10 1949    4.21   71.24    7.39
#> 196   10 1950    3.42   69.05    8.74
#> 197   10 1951    4.67   83.04    9.07
#> 198   10 1952    6.00   74.42    9.93
#> 199   10 1953    6.53   63.51   11.68
#> 200   10 1954    5.12   58.12   14.33
nrow(make.pbalanced(Grunfeld_missing_period))
#> [1] 200
nrow(make.pbalanced(Grunfeld_missing_period, balance.type = "shared.times"))
#> [1] 190

# more complex data:
# First, make data unbalanced (and non-consecutive) 
# by deletion of 2nd time period (year 1936) for all individuals
# and more time periods for first individual only
Grunfeld_unbalanced <- Grunfeld[Grunfeld$year != 1936, ]
Grunfeld_unbalanced <- Grunfeld_unbalanced[-c(1,4), ]
pdim(Grunfeld_unbalanced)$balanced        # FALSE
#> [1] FALSE
all(is.pconsecutive(Grunfeld_unbalanced)) # FALSE
#> [1] FALSE

g_bal <- make.pbalanced(Grunfeld_unbalanced)
pdim(g_bal)$balanced        # TRUE
#> [1] TRUE
unique(g_bal$year)          # all years but 1936
#>  [1] 1935 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
#> [16] 1951 1952 1953 1954
nrow(g_bal)                 # 190 rows
#> [1] 190
head(g_bal)                 # 1st individual: years 1935, 1939 are NA
#>   firm year   inv  value capital
#> 1    1 1935    NA     NA      NA
#> 3    1 1937 410.6 5387.1   156.9
#> 4    1 1938 257.7 2792.2   209.2
#> 5    1 1939    NA     NA      NA
#> 6    1 1940 461.2 4643.9   207.2
#> 7    1 1941 512.0 4551.2   255.2

# NA in 1st, 3rd time period (years 1935, 1937) for first individual
Grunfeld_NA <- Grunfeld
Grunfeld_NA[c(1, 3), "year"] <- NA
g_bal_NA <- make.pbalanced(Grunfeld_NA)
head(g_bal_NA)        # years 1935, 1937: NA for non-index vars
#>   firm year   inv  value capital
#> 1    1 1935    NA     NA      NA
#> 2    1 1936 391.8 4661.7    52.6
#> 3    1 1937    NA     NA      NA
#> 4    1 1938 257.7 2792.2   209.2
#> 5    1 1939 330.8 4313.2   203.4
#> 6    1 1940 461.2 4643.9   207.2
nrow(g_bal_NA)        # 200
#> [1] 200

# pdata.frame interface
pGrunfeld_missing_period <- pdata.frame(Grunfeld_missing_period)
make.pbalanced(Grunfeld_missing_period)
#>     firm year     inv   value capital
#> 1      1 1935  317.60 3078.50    2.80
#> 2      1 1936      NA      NA      NA
#> 3      1 1937  410.60 5387.10  156.90
#> 4      1 1938  257.70 2792.20  209.20
#> 5      1 1939  330.80 4313.20  203.40
#> 6      1 1940  461.20 4643.90  207.20
#> 7      1 1941  512.00 4551.20  255.20
#> 8      1 1942  448.00 3244.10  303.70
#> 9      1 1943  499.60 4053.70  264.10
#> 10     1 1944  547.50 4379.30  201.60
#> 11     1 1945  561.20 4840.90  265.00
#> 12     1 1946  688.10 4900.90  402.20
#> 13     1 1947  568.90 3526.50  761.50
#> 14     1 1948  529.20 3254.70  922.40
#> 15     1 1949  555.10 3700.20 1020.10
#> 16     1 1950  642.90 3755.60 1099.00
#> 17     1 1951  755.90 4833.00 1207.70
#> 18     1 1952  891.20 4924.90 1430.50
#> 19     1 1953 1304.40 6241.70 1777.30
#> 20     1 1954 1486.70 5593.60 2226.30
#> 21     2 1935  209.90 1362.40   53.80
#> 22     2 1936  355.30 1807.10   50.50
#> 23     2 1937  469.90 2676.30  118.10
#> 24     2 1938  262.30 1801.90  260.20
#> 25     2 1939  230.40 1957.30  312.70
#> 26     2 1940  361.60 2202.90  254.20
#> 27     2 1941  472.80 2380.50  261.40
#> 28     2 1942  445.60 2168.60  298.70
#> 29     2 1943  361.60 1985.10  301.80
#> 30     2 1944  288.20 1813.90  279.10
#> 31     2 1945  258.70 1850.20  213.80
#> 32     2 1946  420.30 2067.70  132.60
#> 33     2 1947  420.50 1796.70  264.80
#> 34     2 1948  494.50 1625.80  306.90
#> 35     2 1949  405.10 1667.00  351.10
#> 36     2 1950  418.80 1677.40  357.80
#> 37     2 1951  588.20 2289.50  342.10
#> 38     2 1952  645.50 2159.40  444.20
#> 39     2 1953  641.00 2031.30  623.60
#> 40     2 1954  459.30 2115.50  669.70
#> 41     3 1935   33.10 1170.60   97.80
#> 42     3 1936   45.00 2015.80  104.40
#> 43     3 1937   77.20 2803.30  118.00
#> 44     3 1938   44.60 2039.70  156.20
#> 45     3 1939   48.10 2256.20  172.60
#> 46     3 1940   74.40 2132.20  186.60
#> 47     3 1941  113.00 1834.10  220.90
#> 48     3 1942   91.90 1588.00  287.80
#> 49     3 1943   61.30 1749.40  319.90
#> 50     3 1944   56.80 1687.20  321.30
#> 51     3 1945   93.60 2007.70  319.60
#> 52     3 1946  159.90 2208.30  346.00
#> 53     3 1947  147.20 1656.70  456.40
#> 54     3 1948  146.30 1604.40  543.40
#> 55     3 1949   98.30 1431.80  618.30
#> 56     3 1950   93.50 1610.50  647.40
#> 57     3 1951  135.20 1819.40  671.30
#> 58     3 1952  157.30 2079.70  726.10
#> 59     3 1953  179.50 2371.60  800.30
#> 60     3 1954  189.60 2759.90  888.90
#> 61     4 1935   40.29  417.50   10.50
#> 62     4 1936   72.76  837.80   10.20
#> 63     4 1937   66.26  883.90   34.70
#> 64     4 1938   51.60  437.90   51.80
#> 65     4 1939   52.41  679.70   64.30
#> 66     4 1940   69.41  727.80   67.10
#> 67     4 1941   68.35  643.60   75.20
#> 68     4 1942   46.80  410.90   71.40
#> 69     4 1943   47.40  588.40   67.10
#> 70     4 1944   59.57  698.40   60.50
#> 71     4 1945   88.78  846.40   54.60
#> 72     4 1946   74.12  893.80   84.80
#> 73     4 1947   62.68  579.00   96.80
#> 74     4 1948   89.36  694.60  110.20
#> 75     4 1949   78.98  590.30  147.40
#> 76     4 1950  100.66  693.50  163.20
#> 77     4 1951  160.62  809.00  203.50
#> 78     4 1952  145.00  727.00  290.60
#> 79     4 1953  174.93 1001.50  346.10
#> 80     4 1954  172.49  703.20  414.90
#> 81     5 1935   39.68  157.70  183.20
#> 82     5 1936   50.73  167.90  204.00
#> 83     5 1937   74.24  192.90  236.00
#> 84     5 1938   53.51  156.70  291.70
#> 85     5 1939   42.65  191.40  323.10
#> 86     5 1940   46.48  185.50  344.00
#> 87     5 1941   61.40  199.60  367.70
#> 88     5 1942   39.67  189.50  407.20
#> 89     5 1943   62.24  151.20  426.60
#> 90     5 1944   52.32  187.70  470.00
#> 91     5 1945   63.21  214.70  499.20
#> 92     5 1946   59.37  232.90  534.60
#> 93     5 1947   58.02  249.00  566.60
#> 94     5 1948   70.34  224.50  595.30
#> 95     5 1949   67.42  237.30  631.40
#> 96     5 1950   55.74  240.10  662.30
#> 97     5 1951   80.30  327.30  683.90
#> 98     5 1952   85.40  359.40  729.30
#> 99     5 1953   91.90  398.40  774.30
#> 100    5 1954   81.43  365.70  804.90
#> 101    6 1935   20.36  197.00    6.50
#> 102    6 1936   25.98  210.30   15.80
#> 103    6 1937   25.94  223.10   27.70
#> 104    6 1938   27.53  216.70   39.20
#> 105    6 1939   24.60  286.40   48.60
#> 106    6 1940   28.54  298.00   52.50
#> 107    6 1941   43.41  276.90   61.50
#> 108    6 1942   42.81  272.60   80.50
#> 109    6 1943   27.84  287.40   94.40
#> 110    6 1944   32.60  330.30   92.60
#> 111    6 1945   39.03  324.40   92.30
#> 112    6 1946   50.17  401.90   94.20
#> 113    6 1947   51.85  407.40  111.40
#> 114    6 1948   64.03  409.20  127.40
#> 115    6 1949   68.16  482.20  149.30
#> 116    6 1950   77.34  673.80  164.40
#> 117    6 1951   95.30  676.90  177.20
#> 118    6 1952   99.49  702.00  200.00
#> 119    6 1953  127.52  793.50  211.50
#> 120    6 1954  135.72  927.30  238.70
#> 121    7 1935   24.43  138.00  100.20
#> 122    7 1936   23.21  200.10  125.00
#> 123    7 1937   32.78  210.10  142.40
#> 124    7 1938   32.54  161.20  165.10
#> 125    7 1939   26.65  161.70  194.80
#> 126    7 1940   33.71  145.10  222.90
#> 127    7 1941   43.50  110.60  252.10
#> 128    7 1942   34.46   98.10  276.30
#> 129    7 1943   44.28  108.80  300.30
#> 130    7 1944   70.80  118.20  318.20
#> 131    7 1945   44.12  126.50  336.20
#> 132    7 1946   48.98  156.70  351.20
#> 133    7 1947   48.51  119.40  373.60
#> 134    7 1948   50.00  129.10  389.40
#> 135    7 1949   50.59  134.80  406.70
#> 136    7 1950   42.53  140.80  429.50
#> 137    7 1951   64.77  179.00  450.60
#> 138    7 1952   72.68  178.10  466.90
#> 139    7 1953   73.86  186.80  486.20
#> 140    7 1954   89.51  192.70  511.30
#> 141    8 1935   12.93  191.50    1.80
#> 142    8 1936   25.90  516.00    0.80
#> 143    8 1937   35.05  729.00    7.40
#> 144    8 1938   22.89  560.40   18.10
#> 145    8 1939   18.84  519.90   23.50
#> 146    8 1940   28.57  628.50   26.50
#> 147    8 1941   48.51  537.10   36.20
#> 148    8 1942   43.34  561.20   60.80
#> 149    8 1943   37.02  617.20   84.40
#> 150    8 1944   37.81  626.70   91.20
#> 151    8 1945   39.27  737.20   92.40
#> 152    8 1946   53.46  760.50   86.00
#> 153    8 1947   55.56  581.40  111.10
#> 154    8 1948   49.56  662.30  130.60
#> 155    8 1949   32.04  583.80  141.80
#> 156    8 1950   32.24  635.20  136.70
#> 157    8 1951   54.38  723.80  129.70
#> 158    8 1952   71.78  864.10  145.50
#> 159    8 1953   90.08 1193.50  174.80
#> 160    8 1954   68.60 1188.90  213.50
#> 161    9 1935   26.63  290.60  162.00
#> 162    9 1936   23.39  291.10  174.00
#> 163    9 1937   30.65  335.00  183.00
#> 164    9 1938   20.89  246.00  198.00
#> 165    9 1939   28.78  356.20  208.00
#> 166    9 1940   26.93  289.80  223.00
#> 167    9 1941   32.08  268.20  234.00
#> 168    9 1942   32.21  213.30  248.00
#> 169    9 1943   35.69  348.20  274.00
#> 170    9 1944   62.47  374.20  282.00
#> 171    9 1945   52.32  387.20  316.00
#> 172    9 1946   56.95  347.40  302.00
#> 173    9 1947   54.32  291.90  333.00
#> 174    9 1948   40.53  297.20  359.00
#> 175    9 1949   32.54  276.90  370.00
#> 176    9 1950   43.48  274.60  376.00
#> 177    9 1951   56.49  339.90  391.00
#> 178    9 1952   65.98  474.80  414.00
#> 179    9 1953   66.11  496.00  443.00
#> 180    9 1954   49.34  474.50  468.00
#> 181   10 1935    2.54   70.91    4.50
#> 182   10 1936    2.00   87.94    4.71
#> 183   10 1937    2.19   82.20    4.57
#> 184   10 1938    1.99   58.72    4.56
#> 185   10 1939    2.03   80.54    4.38
#> 186   10 1940    1.81   86.47    4.21
#> 187   10 1941    2.14   77.68    4.12
#> 188   10 1942    1.86   62.16    3.83
#> 189   10 1943    0.93   62.24    3.58
#> 190   10 1944    1.18   61.82    3.41
#> 191   10 1945    1.36   65.85    3.31
#> 192   10 1946    2.24   69.54    3.23
#> 193   10 1947    3.81   64.97    3.90
#> 194   10 1948    5.66   68.00    5.38
#> 195   10 1949    4.21   71.24    7.39
#> 196   10 1950    3.42   69.05    8.74
#> 197   10 1951    4.67   83.04    9.07
#> 198   10 1952    6.00   74.42    9.93
#> 199   10 1953    6.53   63.51   11.68
#> 200   10 1954    5.12   58.12   14.33

# pseries interface
make.pbalanced(pGrunfeld_missing_period$inv)
#>  1-1935  1-1936  1-1937  1-1938  1-1939  1-1940  1-1941  1-1942  1-1943  1-1944 
#>  317.60      NA  410.60  257.70  330.80  461.20  512.00  448.00  499.60  547.50 
#>  1-1945  1-1946  1-1947  1-1948  1-1949  1-1950  1-1951  1-1952  1-1953  1-1954 
#>  561.20  688.10  568.90  529.20  555.10  642.90  755.90  891.20 1304.40 1486.70 
#>  2-1935  2-1936  2-1937  2-1938  2-1939  2-1940  2-1941  2-1942  2-1943  2-1944 
#>  209.90  355.30  469.90  262.30  230.40  361.60  472.80  445.60  361.60  288.20 
#>  2-1945  2-1946  2-1947  2-1948  2-1949  2-1950  2-1951  2-1952  2-1953  2-1954 
#>  258.70  420.30  420.50  494.50  405.10  418.80  588.20  645.50  641.00  459.30 
#>  3-1935  3-1936  3-1937  3-1938  3-1939  3-1940  3-1941  3-1942  3-1943  3-1944 
#>   33.10   45.00   77.20   44.60   48.10   74.40  113.00   91.90   61.30   56.80 
#>  3-1945  3-1946  3-1947  3-1948  3-1949  3-1950  3-1951  3-1952  3-1953  3-1954 
#>   93.60  159.90  147.20  146.30   98.30   93.50  135.20  157.30  179.50  189.60 
#>  4-1935  4-1936  4-1937  4-1938  4-1939  4-1940  4-1941  4-1942  4-1943  4-1944 
#>   40.29   72.76   66.26   51.60   52.41   69.41   68.35   46.80   47.40   59.57 
#>  4-1945  4-1946  4-1947  4-1948  4-1949  4-1950  4-1951  4-1952  4-1953  4-1954 
#>   88.78   74.12   62.68   89.36   78.98  100.66  160.62  145.00  174.93  172.49 
#>  5-1935  5-1936  5-1937  5-1938  5-1939  5-1940  5-1941  5-1942  5-1943  5-1944 
#>   39.68   50.73   74.24   53.51   42.65   46.48   61.40   39.67   62.24   52.32 
#>  5-1945  5-1946  5-1947  5-1948  5-1949  5-1950  5-1951  5-1952  5-1953  5-1954 
#>   63.21   59.37   58.02   70.34   67.42   55.74   80.30   85.40   91.90   81.43 
#>  6-1935  6-1936  6-1937  6-1938  6-1939  6-1940  6-1941  6-1942  6-1943  6-1944 
#>   20.36   25.98   25.94   27.53   24.60   28.54   43.41   42.81   27.84   32.60 
#>  6-1945  6-1946  6-1947  6-1948  6-1949  6-1950  6-1951  6-1952  6-1953  6-1954 
#>   39.03   50.17   51.85   64.03   68.16   77.34   95.30   99.49  127.52  135.72 
#>  7-1935  7-1936  7-1937  7-1938  7-1939  7-1940  7-1941  7-1942  7-1943  7-1944 
#>   24.43   23.21   32.78   32.54   26.65   33.71   43.50   34.46   44.28   70.80 
#>  7-1945  7-1946  7-1947  7-1948  7-1949  7-1950  7-1951  7-1952  7-1953  7-1954 
#>   44.12   48.98   48.51   50.00   50.59   42.53   64.77   72.68   73.86   89.51 
#>  8-1935  8-1936  8-1937  8-1938  8-1939  8-1940  8-1941  8-1942  8-1943  8-1944 
#>   12.93   25.90   35.05   22.89   18.84   28.57   48.51   43.34   37.02   37.81 
#>  8-1945  8-1946  8-1947  8-1948  8-1949  8-1950  8-1951  8-1952  8-1953  8-1954 
#>   39.27   53.46   55.56   49.56   32.04   32.24   54.38   71.78   90.08   68.60 
#>  9-1935  9-1936  9-1937  9-1938  9-1939  9-1940  9-1941  9-1942  9-1943  9-1944 
#>   26.63   23.39   30.65   20.89   28.78   26.93   32.08   32.21   35.69   62.47 
#>  9-1945  9-1946  9-1947  9-1948  9-1949  9-1950  9-1951  9-1952  9-1953  9-1954 
#>   52.32   56.95   54.32   40.53   32.54   43.48   56.49   65.98   66.11   49.34 
#> 10-1935 10-1936 10-1937 10-1938 10-1939 10-1940 10-1941 10-1942 10-1943 10-1944 
#>    2.54    2.00    2.19    1.99    2.03    1.81    2.14    1.86    0.93    1.18 
#> 10-1945 10-1946 10-1947 10-1948 10-1949 10-1950 10-1951 10-1952 10-1953 10-1954 
#>    1.36    2.24    3.81    5.66    4.21    3.42    4.67    6.00    6.53    5.12 

# comparison to make.pconsecutive
g_consec <- make.pconsecutive(Grunfeld_unbalanced)
all(is.pconsecutive(g_consec)) # TRUE
#> [1] TRUE
pdim(g_consec)$balanced        # FALSE
#> [1] FALSE
head(g_consec, 22)             # 1st individual:   no years 1935/6; 1939 is NA; 
#>    firm year    inv  value capital
#> 1     1 1937  410.6 5387.1   156.9
#> 2     1 1938  257.7 2792.2   209.2
#> 3     1 1939     NA     NA      NA
#> 4     1 1940  461.2 4643.9   207.2
#> 5     1 1941  512.0 4551.2   255.2
#> 6     1 1942  448.0 3244.1   303.7
#> 7     1 1943  499.6 4053.7   264.1
#> 8     1 1944  547.5 4379.3   201.6
#> 9     1 1945  561.2 4840.9   265.0
#> 10    1 1946  688.1 4900.9   402.2
#> 11    1 1947  568.9 3526.5   761.5
#> 12    1 1948  529.2 3254.7   922.4
#> 13    1 1949  555.1 3700.2  1020.1
#> 14    1 1950  642.9 3755.6  1099.0
#> 15    1 1951  755.9 4833.0  1207.7
#> 16    1 1952  891.2 4924.9  1430.5
#> 17    1 1953 1304.4 6241.7  1777.3
#> 18    1 1954 1486.7 5593.6  2226.3
#> 19    2 1935  209.9 1362.4    53.8
#> 20    2 1936     NA     NA      NA
#> 21    2 1937  469.9 2676.3   118.1
#> 22    2 1938  262.3 1801.9   260.2
                               # other indviduals: years 1935-1954, 1936 is NA
nrow(g_consec)                 # 198 rows
#> [1] 198

g_consec_bal <- make.pconsecutive(Grunfeld_unbalanced, balanced = TRUE)
all(is.pconsecutive(g_consec_bal)) # TRUE
#> [1] TRUE
pdim(g_consec_bal)$balanced        # TRUE
#> [1] TRUE
head(g_consec_bal)                 # year 1936 is NA for all individuals
#>   firm year   inv  value capital
#> 1    1 1935    NA     NA      NA
#> 2    1 1936    NA     NA      NA
#> 3    1 1937 410.6 5387.1   156.9
#> 4    1 1938 257.7 2792.2   209.2
#> 5    1 1939    NA     NA      NA
#> 6    1 1940 461.2 4643.9   207.2
nrow(g_consec_bal)                 # 200 rows
#> [1] 200

head(g_bal)                        # no year 1936 at all
#>   firm year   inv  value capital
#> 1    1 1935    NA     NA      NA
#> 3    1 1937 410.6 5387.1   156.9
#> 4    1 1938 257.7 2792.2   209.2
#> 5    1 1939    NA     NA      NA
#> 6    1 1940 461.2 4643.9   207.2
#> 7    1 1941 512.0 4551.2   255.2
nrow(g_bal)                        # 190 rows
#> [1] 190