| Title: | Reverse the Order of Values of Time Series and Multidimensional Panels |
|---|---|
| Description: | Reverses the order of values of selected variables in time series and multidimensional panels, either by replacing the original values or by creating new variables with prefixed names. Optionally evaluates user-supplied pre-estimation, estimation, and post-estimation functions using the transformed data. |
| Authors: | Ilya Bolotov [aut, cre] (ORCID: <https://orcid.org/0000-0003-1148-7144>) |
| Maintainer: | Ilya Bolotov <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-07-22 12:27:08 UTC |
| Source: | https://github.com/cran/rpanelrev |
Reverses the values of selected variables along the last dimension of a data frame. Reversal is performed separately for every combination of the preceding dimensions, while all dimension variables remain unchanged.
panelrev( data, vars, dimensions, replace = FALSE, prefix = "rev_", preestimation = NULL, estimate = NULL, formula = NULL, postestimation = NULL, ... )panelrev( data, vars, dimensions, replace = FALSE, prefix = "rev_", preestimation = NULL, estimate = NULL, formula = NULL, postestimation = NULL, ... )
data |
data frame Input data containing the dimension variables and the variables to be reversed. |
vars |
character vector Names of the variables whose values are to be reversed. |
dimensions |
character vector Names of the variables that uniquely identify observations. The last variable specifies the dimension along which values are reversed, while all preceding variables define independent groups. |
replace |
logical scalar, default = |
prefix |
character scalar, default = |
preestimation |
function or |
estimate |
function or |
formula |
formula or |
postestimation |
function or |
... |
Optional.
Additional arguments passed to |
The reversed values can either replace the original variables or be stored in new variables constructed by adding a prefix to the original variable names. Optional pre-estimation, estimation, and post-estimation functions can be evaluated after the reversal.
The final variable in dimensions determines the ordering along which
the selected variables are reversed. All preceding dimension variables
define independent groups.
For example, with
dimensions = c("country", "sector", "year")
reversal is performed along year, separately for each
country-sector combination.
The dimension variables themselves are not modified, and the original row
order of data is preserved.
Missing values in variables listed in vars are treated as ordinary
values and therefore participate in the reversal. Missing values in
dimension variables are not permitted.
Each combination of the dimension variables must uniquely identify one observation.
The order of operations is:
Reverse the selected variables.
Apply preestimation to the transformed data.
Evaluate estimate.
Apply postestimation to the transformed data and fitted model.
The preestimation function must return a data frame. The
postestimation function must return the complete final result as a
named list containing data and model. This allows predictions,
residuals, standard errors, and other post-estimation quantities to be added
to the transformed data.
When replace = TRUE, preestimation, estimate, and
postestimation use the overwritten variables. When
replace = FALSE, they should refer to the prefixed variable names.
A named list containing at least the following components:
dataThe transformed data frame after reversal and, when supplied,
application of preestimation and postestimation.
modelThe object returned by estimate, or the model component returned
by postestimation. If no estimation function is supplied, this
component is NULL.
## Example: reversal and linear estimation data <- data.frame( country = rep(c("A", "B"), each = 3L), year = rep(2020:2022, 2L), y = c(10, 20, 30, 40, 50, 60), x = c(1, 2, 3, 4, 5, 6) ) # create prefixed reversed variables result <- panelrev( data = data, vars = c("y", "x"), dimensions = c("country", "year"), prefix = "rev_" ) print(result$data) print(result$model) # NULL # overwrite variables and estimate a linear model result <- panelrev( data = data, vars = c("y", "x"), dimensions = c("country", "year"), replace = TRUE, estimate = stats::lm, formula = y ~ x ) print(result$data) print(result$model) print(summary(result$model)) # pre-estimation, estimation, and prediction result <- panelrev( data = data, vars = c("y", "x"), dimensions = c("country", "year"), replace = TRUE, preestimation = function(data) { data$x_squared <- data$x^2 data }, estimate = stats::lm, formula = y ~ x + x_squared, na.action = stats::na.exclude, postestimation = function(data, model) { prediction <- stats::predict( model, newdata = data, se.fit = TRUE ) data$prediction <- as.numeric(prediction$fit) data$stdp <- as.numeric(prediction$se.fit) data$residual <- as.numeric(stats::residuals(model)) list( data = data, model = model ) } ) print(result$data) print(result$model)## Example: reversal and linear estimation data <- data.frame( country = rep(c("A", "B"), each = 3L), year = rep(2020:2022, 2L), y = c(10, 20, 30, 40, 50, 60), x = c(1, 2, 3, 4, 5, 6) ) # create prefixed reversed variables result <- panelrev( data = data, vars = c("y", "x"), dimensions = c("country", "year"), prefix = "rev_" ) print(result$data) print(result$model) # NULL # overwrite variables and estimate a linear model result <- panelrev( data = data, vars = c("y", "x"), dimensions = c("country", "year"), replace = TRUE, estimate = stats::lm, formula = y ~ x ) print(result$data) print(result$model) print(summary(result$model)) # pre-estimation, estimation, and prediction result <- panelrev( data = data, vars = c("y", "x"), dimensions = c("country", "year"), replace = TRUE, preestimation = function(data) { data$x_squared <- data$x^2 data }, estimate = stats::lm, formula = y ~ x + x_squared, na.action = stats::na.exclude, postestimation = function(data, model) { prediction <- stats::predict( model, newdata = data, se.fit = TRUE ) data$prediction <- as.numeric(prediction$fit) data$stdp <- as.numeric(prediction$se.fit) data$residual <- as.numeric(stats::residuals(model)) list( data = data, model = model ) } ) print(result$data) print(result$model)