Predicted values of response based on plm models.

# S3 method for class 'plm'
predict(
  object,
  newdata = NULL,
  na.fill = !inherits(newdata, "pdata.frame"),
  ...
)

Arguments

object

An object of class "plm",

newdata

An optional pdata.frame in which to look for variables to be used for prediction. If NULL, the fitted values are returned. For fixed effects models, supplying a pdata.frame is recommended.

na.fill

A logical, only relevant if object is a pdata.frame, indicating whether for any supplied out-of-sample indexes (individual, time, combination of both), the missing fixed effect estimate is filled with the weighted mean of the model's present fixed effect estimates or not.

...

further arguments.

Value

A numeric (or a pseries if newdata is a pdata.frame) carrying the predicted values with length equal to the number of rows as the data supplied in newdata and with names the row names of newdata or, if newdata = NULL, the original model's fitted values given in object.

Details

predictcalculates predicted values by evaluating the regression function of a plm model for newdata or, if newdata = NULL, it returns the fitted values the plm model.

The fixed effects (within) model is somewhat special in prediction as it has fixed effects estimated per individual, time period (one-way) or both (two-ways model) which should to be respected when predicting values relating to these fixed effects in the model: To do so, it is recommended to supply a pdata.frame (and not a plain data.frame) in newdata as it describes the relationship between the data supplied to the individual. and/or time periods. In case the newdata´'s pdata.frame has out-of-sample data (data contains individuals and/or time periods not contained in the original model), it is not clear how values are to be predicted and the result will contain NA values for these out-of-sample data. Argument na.fill can be set to TRUE to apply the original model's weighted mean of fixed effects for the out-of-sample data to derive a prediction.

If a plain data.frame is given in newdata for a fixed effects model, the weighted mean is used for all fixed effects as newdata for prediction as a plain data.frame cannot describe any relation to individuals/time periods (na.fill is automatically set to TRUE and the function warns).

See also Examples.

Examples

library(plm)
data("Grunfeld", package = "plm")

# fit a fixed effect model
fit.fe <- plm(inv ~ value + capital, data = Grunfeld, model = "within")

# generate 55 new observations of three firms used for prediction:
#  * firm 1 with years 1935:1964 (has out-of-sample years 1955:1964), 
#  * firm 2 with years 1935:1949 (all in sample),
#  * firm 11 with years 1935:1944 (firm 11 is out-of-sample)
set.seed(42L)

new.value2   <- runif(55, min = min(Grunfeld$value),   max = max(Grunfeld$value))
new.capital2 <- runif(55, min = min(Grunfeld$capital), max = max(Grunfeld$capital))

newdata <- data.frame(firm = c(rep(1, 30), rep(2, 15), rep(11, 10)),
                      year = c(1935:(1935+29), 1935:(1935+14), 1935:(1935+9)),
                      value = new.value2, capital = new.capital2)
# make pdata.frame
newdata.p <- pdata.frame(newdata, index = c("firm", "year"))

## predict from fixed effect model with new data as pdata.frame
predict(fit.fe, newdata = newdata.p)
#>      1-1935      1-1936      1-1937      1-1938      1-1939      1-1940 
#> 1076.004030 1041.817156  249.382145  682.016667  728.325204  756.038053 
#>      1-1941      1-1942      1-1943      1-1944      1-1945      1-1946 
#> 1116.131850  552.178048  774.642348 1002.800934  378.801842  613.239385 
#>      1-1947      1-1948      1-1949      1-1950      1-1951      1-1952 
#> 1144.296647  588.634653  417.142349  606.127848  699.421823  165.672694 
#>      1-1953      1-1954      1-1955      1-1956      1-1957      1-1958 
#>  590.614615  454.138644 1048.352154   36.248584  868.853809  935.961571 
#>      1-1959      1-1960      1-1961      1-1962      1-1963      1-1964 
#>   -6.427882  687.845139  311.026985  800.870214  686.238426 1040.993885 
#>      2-1935      2-1936      2-1937      2-1938      2-1939      2-1940 
#>  999.771634  822.116973  434.931319  634.203638  321.858983 1136.294201 
#>      2-1941      2-1942      2-1943      2-1944      2-1945      2-1946 
#>  113.713368  393.885367 1369.753558 1163.892195  873.580755  635.133459 
#>      2-1947      2-1948      2-1949     11-1935     11-1936     11-1937 
#>  489.462939 1284.875386  829.810404          NA          NA          NA 
#>     11-1938     11-1939     11-1940     11-1941     11-1942     11-1943 
#>          NA          NA          NA          NA          NA          NA 
#>     11-1944 
#>          NA 

## set na.fill = TRUE to have the weighted mean used to for fixed effects -> no NA values
predict(fit.fe, newdata = newdata.p, na.fill = TRUE)
#>      1-1935      1-1936      1-1937      1-1938      1-1939      1-1940 
#> 1076.004030 1041.817156  249.382145  682.016667  728.325204  756.038053 
#>      1-1941      1-1942      1-1943      1-1944      1-1945      1-1946 
#> 1116.131850  552.178048  774.642348 1002.800934  378.801842  613.239385 
#>      1-1947      1-1948      1-1949      1-1950      1-1951      1-1952 
#> 1144.296647  588.634653  417.142349  606.127848  699.421823  165.672694 
#>      1-1953      1-1954      1-1955      1-1956      1-1957      1-1958 
#>  590.614615  454.138644 1048.352154   36.248584  868.853809  935.961571 
#>      1-1959      1-1960      1-1961      1-1962      1-1963      1-1964 
#>   -6.427882  687.845139  311.026985  800.870214  686.238426 1040.993885 
#>      2-1935      2-1936      2-1937      2-1938      2-1939      2-1940 
#>  999.771634  822.116973  434.931319  634.203638  321.858983 1136.294201 
#>      2-1941      2-1942      2-1943      2-1944      2-1945      2-1946 
#>  113.713368  393.885367 1369.753558 1163.892195  873.580755  635.133459 
#>      2-1947      2-1948      2-1949     11-1935     11-1936     11-1937 
#>  489.462939 1284.875386  829.810404 1032.116109  702.279274  533.146400 
#>     11-1938     11-1939     11-1940     11-1941     11-1942     11-1943 
#>  877.484982 1019.650117  839.202948  694.563415  725.233519  851.950695 
#>     11-1944 
#>  -24.012863 

## predict with plain data.frame from fixed effect model: uses mean fixed effects 
## for prediction and, thus, yields different result with a warning
predict(fit.fe, newdata = newdata)
#> Warning: Data supplied in argument 'newdata' is not a pdata.frame; weighted mean of fixed effects as in original model used for prediction, see ?predict.plm.
#>           1           2           3           4           5           6 
#> 1087.556808 1053.369934  260.934923  693.569445  739.877982  767.590831 
#>           7           8           9          10          11          12 
#> 1127.684628  563.730826  786.195126 1014.353712  390.354620  624.792163 
#>          13          14          15          16          17          18 
#> 1155.849425  600.187431  428.695127  617.680626  710.974601  177.225472 
#>          19          20          21          22          23          24 
#>  602.167393  465.691422 1059.904932   47.801362  880.406587  947.514349 
#>          25          26          27          28          29          30 
#>    5.124896  699.397917  322.579764  812.422992  697.791204 1052.546663 
#>          31          32          33          34          35          36 
#>  839.121881  661.467220  274.281566  473.553885  161.209230  975.644448 
#>          37          38          39          40          41          42 
#>  -46.936385  233.235614 1209.103805 1003.242442  712.931002  474.483706 
#>          43          44          45          46          47          48 
#>  328.813186 1124.225633  669.160651 1032.116109  702.279274  533.146400 
#>          49          50          51          52          53          54 
#>  877.484982 1019.650117  839.202948  694.563415  725.233519  851.950695 
#>          55 
#>  -24.012863