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.

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, ...)

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

logical. Automatically colour by panel-groups.

labs

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

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.

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

Examples

## World Development Panel Data head(wlddev) # View data
#> country iso3c date year decade region income OECD PCGDP #> 1 Afghanistan AFG 1961-01-01 1960 1960 South Asia Low income FALSE NA #> 2 Afghanistan AFG 1962-01-01 1961 1960 South Asia Low income FALSE NA #> 3 Afghanistan AFG 1963-01-01 1962 1960 South Asia Low income FALSE NA #> 4 Afghanistan AFG 1964-01-01 1963 1960 South Asia Low income FALSE NA #> 5 Afghanistan AFG 1965-01-01 1964 1960 South Asia Low income FALSE NA #> 6 Afghanistan AFG 1966-01-01 1965 1960 South Asia Low income FALSE NA #> LIFEEX GINI ODA #> 1 32.292 NA 114440000 #> 2 32.742 NA 233350000 #> 3 33.185 NA 114880000 #> 4 33.624 NA 236450000 #> 5 34.060 NA 302480000 #> 6 34.495 NA 370250000
qsu(wlddev, pid = ~ iso3c, cols = 9:12, vlabels = TRUE) # Sumarizing data
#> , , PCGDP: GDP per capita (constant 2010 US$) #> #> N/T Mean SD Min Max #> Overall 8995 11563.6529 18348.4052 131.6464 191586.64 #> Between 203 12488.8577 19628.3668 255.3999 141165.083 #> Within 44.3103 11563.6529 6334.9523 -30529.0928 75348.067 #> #> , , LIFEEX: Life expectancy at birth, total (years) #> #> N/T Mean SD Min Max #> Overall 11068 63.8411 11.4497 18.907 85.4171 #> Between 207 64.5285 10.0235 39.349 85.4171 #> Within 53.4686 63.8411 5.8292 33.4671 83.8595 #> #> , , GINI: GINI index (World Bank estimate) #> #> N/T Mean SD Min Max #> Overall 1356 39.3976 9.6764 16.2 65.8 #> Between 161 39.5799 8.3679 23.3667 61.7143 #> Within 8.4224 39.3976 3.0406 23.9576 54.7976 #> #> , , ODA: Net ODA received (constant 2015 US$) #> #> N/T Mean SD Min Max #> Overall 8336 428,746468 819,868971 -1.08038000e+09 2.45521800e+10 #> Between 178 418,026522 548,293709 423846.154 3.53258914e+09 #> Within 46.8315 428,746468 607,024040 -2.47969577e+09 2.35093916e+10 #>
str(psmat(wlddev$PCGDP, wlddev$iso3c, wlddev$year)) # Generating matrix of GDP
#> 'psmat' num [1:216, 1:59] NA NA NA NA NA ... #> - attr(*, "dimnames")=List of 2 #> ..$ : chr [1:216] "ABW" "AFG" "AGO" "ALB" ... #> ..$ : chr [1:59] "1960" "1961" "1962" "1963" ... #> - attr(*, "transpose")= logi FALSE
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
#> 'psmat' num [1:216, 1:59] NA NA NA NA NA ... #> - attr(*, "dimnames")=List of 2 #> ..$ : chr [1:216] "ABW" "AFG" "AGO" "ALB" ... #> ..$ : chr [1:59] "1960" "1961" "1962" "1963" ... #> - attr(*, "transpose")= logi FALSE
str(psmat(wlddev$PCGDP, wlddev$iso3c)) # The Data is sorted, could omit t
#> No timevar provided: Assuming Balanced Panel
#> 'psmat' num [1:216, 1:59] NA NA NA NA NA ... #> - attr(*, "dimnames")=List of 2 #> ..$ : chr [1:216] "ABW" "AFG" "AGO" "ALB" ... #> ..$ : chr [1:59] "1" "2" "3" "4" ... #> - attr(*, "transpose")= logi FALSE
str(psmat(wlddev$PCGDP, 216)) # This panel is also balanced, so
#> num [1:216, 1:59] NA NA 2466 NA NA ... #> - attr(*, "dimnames")=List of 2 #> ..$ : chr [1:216] "GRP.1" "GRP.2" "GRP.3" "GRP.4" ... #> ..$ : chr [1:59] "1" "2" "3" "4" ...
# ..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)
#> 'psmat' num [1:216, 1:59, 1:4] NA NA NA NA NA ... #> - attr(*, "dimnames")=List of 3 #> ..$ : chr [1:216] "ABW" "AFG" "AGO" "ALB" ... #> ..$ : chr [1:59] "1960" "1961" "1962" "1963" ... #> ..$ : chr [1:4] "PCGDP" "LIFEEX" "GINI" "ODA" #> - attr(*, "transpose")= logi FALSE
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
#> Variable Country 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 #> 1 PCGDP ABW NA NA NA NA NA NA NA NA NA NA NA NA #> 2 PCGDP AFG NA NA NA NA NA NA NA NA NA NA NA NA #> 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 #> 1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA #> 2 NA NA NA NA NA NA NA NA NA NA NA NA NA NA #> 1986 1987 1988 1989 1990 1991 1992 1993 #> 1 15669.62 18427.61 22134.02 24837.95 25357.79 26329.31 26401.97 26663.21 #> 2 NA NA NA NA NA NA NA NA #> 1994 1995 1996 1997 1998 1999 2000 2001 #> 1 27272.31 26705.18 26087.78 27190.5 27151.92 26954.4 28417.38 26966.05 #> 2 NA NA NA NA NA NA NA NA #> 2002 2003 2004 2005 2006 2007 2008 #> 1 25508.3025 25469.287 27005.5295 26979.8854 27046.7604 27428.1202 27367.2810 #> 2 339.6333 352.244 341.6125 365.5487 372.8967 412.9196 418.4788 #> 2009 2010 2011 2012 2013 2014 2015 #> 1 24464.1745 23512.603 24231.3389 23777.3161 24629.0800 24692.4972 24452.6066 #> 2 495.1089 550.515 536.0125 584.9074 597.5252 594.5741 585.7083 #> 2016 2017 2018 #> 1 24288.9871 24508.8091 NA #> 2 583.0551 583.8696 NA
## 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
#> 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 #> ABW NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA #> AFG NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA #> 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 #> ABW NA NA NA NA NA NA NA NA NA NA NA 15670 18428 22134 #> AFG NA NA NA NA NA NA NA NA NA NA NA NA NA NA #> 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 #> ABW 24838 25358 26329 26402 26663 27272 26705 26088 27191 27152 26954 28417 #> AFG NA NA NA NA NA NA NA NA NA NA NA NA #> 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 #> ABW 26966 25508 25469 27006 26980 27047 27428 27367 24464 23513 24231 23777 #> AFG NA 340 352 342 366 373 413 418 495 551 536 585 #> 2013 2014 2015 2016 2017 2018 #> ABW 24629 24692 24453 24289 24509 NA #> AFG 598 595 586 583 584 NA
plot(psmat(PCGDP))
plot(psmat(pwlddev[9:12]))
plot(psmat(G(pwlddev[9:12]))) # Here plotting panel- growth rates