Collection of (hopefully) useful functions for dealing with panel datasets.
pcum(const series y)
Compute cumulated value for each panel unit. This is an alternative to gretl's built-in cum()
function which also computes cumulated values for each cross-sectional unit but handles NAs differently (see below).
y
: series, target series
Series with cumulated sum for each cross-sectional unit. NAs are replaced by zero before cumulation for each unit.
Note: In contrast, gretl's built-in cum()
function stops computing cumulated values for a unit if the series has NAs between observations.
pmode(const series y)
Compute most frequent value for each cross-sectional unit.
y
: series, target series
Series with most frequent values for each cross-sectional unit. NA
values are ignored. If all values are NA
for a unit, a series consisting only NA
values is returned for that unit.
pfirst(const series y)
Compute first valid value for each panel unit.
y
: series, target series
Series with the first valid value of y
for each cross-sectional unit. If no valid values exists for a unit, NA
is returned.
plast(const series y)
Compute last valid value for each panel unit.
y
: series, target series
Series with the last valid value of y
for each cross-sectional unit. If no valid values exists for a unit, NA
is returned.
pquantile(const series y, const scalar value "p-th quantile")
Compute the p-th quantile for each unit.
y
: series, target seriesvalue
: scalar, p-th quantile; must be in the range 0 < value < 1
Series with the p-th quantile for each cross-sectional unit. NA
values are ignored. If all values are NA
for a unit, a series consisting only NA
values is returned for that unit.
pxquantile(const series y, const scalar value "p-th quantile")
Compute the p-th quantile for each time period across all units.
y
: series, target seriesvalue
: scalar, p-th quantile; must be in the range 0 < value < 1
Series with the p-th quantile for each time period across-sectional units. NA
values are ignored. If all values are NA
for a unit, a series consisting only NA
values is returned for that unit.
pxmean(const series y)
Compute mean value of y
across all units per time period. Missing values are ignored for computing the mean.
y
, series, target series
Series with the mean value value across all units per observation. NA
values are completely ignored and are not counted.
pxfraction(const series y, const scalar value "Value to search",
const bool fraction[FALSE] "Fraction instead of total number")
Compute the total number of observations of "value" in y
across all units per
time period. Missing values are completely ignored.
y
: series, target seriesvalue
: scalar, Value to search iny
fraction
: bool, ifTRUE
(=1) compute the fraction of observations taking the valuevalue
, ifFALSE
(=0, default) compute the number of observations instead.
Series with the respective value across all units per observation. NA
values are completely ignored and are not counted.
ppolyfit(const series y, const int order[1::])
Fits a polynomial trend of order order
to the input series y
using OLS for each cross-sectional unit separately. This function mimics gretl's built-in function polyfit()
which works only for time-series data correctly before version 2021c.
y
: series, target seriesorder
: integer, Order of the polynomial trend
Series of fitted values for each unit. In case y
has missing values for a specific unit, for this unit a time-series with NAs is returned.
panelinfo()
Compute basic information about the panel dimension.
Nothing.
mpmean(const matrix m, const int N)
Compute vector of mean values of m
for each cross-sectional unit. Matrix equivalent to gretl's built-in pmean()
function. Missing values are ignored.
m
: matrix, Column vector of dimension TxNN
: int, Number of cross-sectional units
Matrix of stacked time-series of means of m
for each unit. NA
values are completely ignored.
fe_estimator (const matrix y, const matrix X, const int N[1::])
Matrix-based equivalent of gretl's built-in fixed-effect panel estimator (see help panel
). Currently no inference is done.
y
: matrix, Column vector of stacked time-seriesX
: matrix, Matrix of regressors of stacked time-seriesN
: integer, Number of cross-sectional units
Bundle object comprising both point estimates of coefficients (key: coeff
) and estimated fixed-effects stored as stacked time-series (key: ahat
).
pols_estimator (const matrix y, const matrix X, const int N[1::])
Matrix-based equivalent of gretl's built-in pooled ols
command (see help panel
).
y
: matrix, Column vector of stacked time-seriesX
: matrix, Matrix of regressors of stacked time-seriesN
: integer, Number of cross-sectional units
Bundle object comprising point estimates of coefficients (key: coeff
).
pfcast (const matrix X, const bundle Model, const bool verbose[FALSE])
Compute static predictions for a given model. For the fixed-effects, the fixed-effects are added. This is the same treatment as gretl does for the panel fixed-effects model when applying the fcast
command.
X
: matrix, Matrix of regressors of stacked time-series for test set.Model
: bundle, Model object holding information on estimation resultsverbose
: bool, print details (optional, default =FALSE
)
Matrix of stacked forecasts. N stacked time-series of length horizon. The horizon is internally determined by the input dimension of X and the number of cross-sectional units N as specified in the Model
bundle.
mean_robust (const matrix X)
Works as gretl's built-in function meanc()
. Compute mean values for each column even in case of missing values.
X
: matrix, Matrix with data and possibly missing values
Row vector with mean values for each column.
sdc_robust (const matrix X)
Works as gretl's built-in function sdc()
. Compute standard deviation for each column even in case of missing values.
X
: matrix, Matrix with data and possibly missing values
Row vector with standard deviation values for each column.
panplot_quantile(const series y,
const bool do_pxquantile[FALSE],
bundle self[null])
Plot quantiles over time.
y
: series, target seriesdo_pxquantile
: bool, IfFALSE
(default) compute the quantiles across all observations. IfTRUE
compute the quantiles for each time period across all units.self
: bundle, Optionally pass parameters for tweaking the plot.
Supported parameters for self
are:
dateformat
: string (default "%Y-%m") Set date format of xtics. Requires that panel-time is set via e.g.setobs 4 "2010:1" --panel-time
fontsize
: scalar (default 10)fontsize_tics
: scalar (default 8)filename
: string (default "display", immediately show on screen)key_position
: string (default "top left")noenhanced
: string (default "noenhanced")offset_xtics_x
: scalar (default = 0) Offset xtics along the x-axisoffset_xtics_y
: scalar (default = 0) Offset xtics along the y-axisoptions_string
: string (default ""; gnuplot strings e.g.set logscale y 2
)rotate_xtics
: scalar (default = 0) Rotate xtics by some degreestitle
: string (default "")xlabel
: string (default "")ylabel
: string (default "")yrange_min
: scalar (defaultNA
(automatically set))yrange_max
: scalar (defaultNA
(automatically set))quantiles
: matrix (default {0.05, 0.5, 0.95})
FALSE if no error occurs, otherwise TRUE.
-
v0.6 (April 2023)
- Print dates on x-axis for the
panplot_quantile()
function when the accessor$obsdate
is available (panel time must be set) - Make use of Markdown format for help text
- Print dates on x-axis for the
-
v0.5 (March 2023)
panplot_quantile()
now returns an error value- Add new optional parameter
noenhanced
forpanplot_quantile()
for deciding whether to consider underscores in labels and titles (default) or not - Bugfix: Parameter
options_string
did not work before forpanplot_quantile()
-
v0.4 (October 2022)
- add new
pxquantile()
function - add new
panplot_quantile()
function
- add new
-
v0.3 (March 2022)
- Add new functions
mpmean()
,meanc_robust()
andsdc_robust()
- Add new function
fe_estimator()
for estimating a fixed-effects model using matrices as inputs (no inference supported, yet) - Add new function
pols_estimator()
for estimating a pooled-OLS model using matrices as inputs - Add new function
pfcast()
for computing predictions using some supported panel model using matrices.
- Add new functions
-
v0.2 (July 2021)
- Add new
ppolyfit()
function
- Add new
-
v0.1 (July 2021)
- Initial version