Learn R Programming

collapse (version 1.7.6)

psmat: Matrix / Array from Panel Series

Description

psmat efficiently expands a panel-vector or plm::pseries into a matrix. If a data frame or plm::pdata.frame is passed, psmat returns (default) a 3D array or a list of matrices.

Usage

psmat(x, …)

# S3 method for default psmat(x, g, t = NULL, transpose = FALSE, …)

# S3 method for pseries psmat(x, transpose = FALSE, …)

# S3 method for data.frame psmat(x, by, t = NULL, cols = NULL, transpose = FALSE, array = TRUE, …)

# S3 method for pdata.frame psmat(x, cols = NULL, transpose = FALSE, array = TRUE, …)

# S3 method for psmat plot(x, legend = FALSE, colours = legend, labs = NULL, grid = FALSE, …)

Arguments

x

a vector, panel series (plm::pseries), data frame or panel data frame (plm::pdata.frame).

g

a factor, GRP object, atomic vector (internally converted to factor) or a list of vectors / factors (internally converted to a GRP object) used to group x. If the panel is balanced an integer indicating the number of groups can also be supplied. See Examples.

by

data.frame method: Same input as g, but also allows one- or two-sided formulas using the variables in x, i.e. ~ idvar or var1 + var2 ~ idvar1 + idvar2.

t

same inputs as g, to indicate the time-variable(s) or second identifier(s). g and t together should fully identify the panel. If t = NULL, the data is assumed sorted and seq_col is used to generate rownames for the output matrix.

cols

data.frame method: Select columns using a function, column names, indices or a logical vector. Note: cols is ignored if a two-sided formula is passed to by.

transpose

logical. TRUE generates the matrix such that g/by -> columns, t -> rows. Default is g/by -> rows, t -> columns.

array

data.frame / pdata.frame methods: logical. TRUE returns a 3D array (if just one column is selected a matrix is returned). FALSE returns a list of matrices.

arguments to be passed to or from other methods, or for the plot method additional arguments passed to ts.plot.

legend

logical. Automatically create a legend of panel-groups.

colours

either TRUE to automatically colour by panel-groups using rainbow or a character vector of colours matching the number of panel-groups (series).

labs

character. Provide a character-vector of variable labels / series titles when plotting an array.

grid

logical. Calls grid to draw gridlines on the plot.

Value

A matrix or 3D array containing the data in x, where by default the rows constitute the groups-ids (g/by) and the columns the time variable or individual ids (t). 3D arrays contain the variables in the 3rd dimension. The objects have a class 'psmat', and also a 'transpose' attribute indicating whether transpose = TRUE.

Details

For plm::pseries, the first index variable is taken to be the group-id and the second the time variable. If more than 2 index variables are attached to plm::pseries, the last one is taken as the time variable and the others are taken as group-id's and interacted.

See Also

Time Series and Panel Series, Collapse Overview

Examples

Run this code
# NOT RUN {
## World Development Panel Data
head(wlddev)                                                    # View data
qsu(wlddev, pid = ~ iso3c, cols = 9:12, vlabels = TRUE)         # Sumarizing data
str(psmat(wlddev$PCGDP, wlddev$iso3c, wlddev$year))             # Generating matrix of GDP
r <- psmat(wlddev, PCGDP ~ iso3c, ~ year)                       # Same thing using data.frame method
plot(r, main = vlabels(wlddev)[9], xlab = "Year")               # Plot the matrix
str(r)                                                          # See srructure
str(psmat(wlddev$PCGDP, wlddev$iso3c))                          # The Data is sorted, could omit t
str(psmat(wlddev$PCGDP, 216))                                   # This panel is also balanced, so
# ..indicating the number of groups would be sufficient to obtain a matrix

ar <- psmat(wlddev, ~ iso3c, ~ year, 9:12)                      # Get array of transposed matrices
str(ar)
plot(ar)
plot(ar, legend = TRUE)
plot(psmat(collap(wlddev, ~region+year, cols = 9:12),           # More legible and fancy plot
           ~region, ~year), legend = TRUE,
     labs = vlabels(wlddev)[9:12])

psml <- psmat(wlddev, ~ iso3c, ~ year, 9:12, array = FALSE)     # This gives list of ps-matrices
head(unlist2d(psml, "Variable", "Country", id.factor = TRUE),2) # Using unlist2d, can generate DF
# }
# NOT RUN {
 <!-- % No code relying on suggested package -->
## Using plm simplifies things
pwlddev <- plm::pdata.frame(wlddev, index = c("iso3c","year"))  # Creating a Panel Data Frame
PCGDP <- pwlddev$PCGDP                                          # A panel-Series of GDP per Capita
head(psmat(PCGDP), 2)                                           # Same as above, more parsimonious
plot(psmat(PCGDP))
plot(psmat(pwlddev[9:12]))
plot(psmat(G(pwlddev[9:12])))                                   # Here plotting panel- growth rates
# }

Run the code above in your browser using DataLab