psmat {collapse} | R Documentation |
Matrix / Array from Panel Series
Description
psmat
efficiently expands a panel-vector or 'indexed_series' ('pseries') into a matrix. If a data frame or 'indexed_frame' ('pdata.frame') is passed, psmat
returns a 3D array or a list of matrices.
Usage
psmat(x, ...)
## Default S3 method:
psmat(x, g, t = NULL, transpose = FALSE, ...)
## S3 method for class 'data.frame'
psmat(x, by, t = NULL, cols = NULL, transpose = FALSE, array = TRUE, ...)
# Methods for indexed data / compatibility with plm:
## S3 method for class 'pseries'
psmat(x, transpose = FALSE, drop.index.levels = "none", ...)
## S3 method for class 'pdata.frame'
psmat(x, cols = NULL, transpose = FALSE, array = TRUE, drop.index.levels = "none", ...)
## S3 method for class 'psmat'
plot(x, legend = FALSE, colours = legend, labs = NULL, grid = FALSE, ...)
Arguments
x |
a vector, indexed series 'indexed_series' ('pseries'), data frame or 'indexed_frame' ('pdata.frame'). |
g |
a factor, |
by |
data.frame method: Same input as |
t |
same inputs as |
cols |
data.frame method: Select columns using a function, column names, indices or a logical vector. Note: |
transpose |
logical. |
array |
data.frame / pdata.frame methods: logical. |
drop.index.levels |
character. Either |
... |
arguments to be passed to or from other methods, or for the plot method additional arguments passed to |
legend |
logical. Automatically create a legend of panel-groups. |
colours |
either |
labs |
character. Provide a character-vector of variable labels / series titles when plotting an array. |
grid |
logical. Calls |
Details
If n > 2 index variables are attached to an indexed series or frame, the first n-1 variables in the index are interacted.
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
.
Note
The pdata.frame
method only works for properly subsetted objects of class 'pdata.frame'. A list of 'pseries' won't work. There also exist simple aperm
and [
(subset) methods for 'psmat' objects. These differ from the default methods only by keeping the class and the 'transpose' attribute.
See Also
Time Series and Panel Series, Collapse Overview
Examples
## 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
## Indexing simplifies things
wldi <- findex_by(wlddev, iso3c, year) # Creating an indexed frame
PCGDP <- wldi$PCGDP # An indexed_series of GDP per Capita
head(psmat(PCGDP), 2) # Same as above, more parsimonious
plot(psmat(PCGDP))
plot(psmat(wldi[9:12]))
plot(psmat(G(wldi[9:12]))) # Here plotting panel-growth rates