knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(dplyr) library(IEATools) library(tidyr)
After loading IEA data and ensuring that the data are balanced
(see the vignette about loading data) and
specifying many of the flows
(see the vignette about specifying flows),
the analyst can prepare for physical supply-use table (PSUT) analysis
of an energy conversion chain.
This vignette shows how to use the functions in the IEATools
package
for that purpose.
add_psut_matnames()
)The first step to converting the IEA extended energy balance data into PSUT matrices
is identifying the matrix in which each datum belongs.
The add_psut_matnames()
function performs this task.
It adds a column named "matnames" (by default but changeable by the user)
that contains one of R
, U
, V
, or Y
to identify the
Resources, Use, Make, or Final demand matrix.
load_tidy_iea_df() %>% add_psut_matnames() %>% glimpse()
Note that when a matnames
column already exists in .tidy_iea_df
,
add_psut_matnames()
returns the input data frame without modification.
This behavior allows other matrix naming conventions.
add_row_col_meta()
)The next step is to add information to identify the rows and columns
for each datum.
The add_row_col_meta()
function performs this task
by adding rowname
, colname
, rowtype
, and coltype
columns which identify the names and types of rows and columns in the PSUT matrices.
add_row_col_meta()
should be called after
calling add_psut_matnames()
,
because it relies on the the matnames
column to
determine names and types.
load_tidy_iea_df() %>% add_psut_matnames() %>% add_row_col_meta() %>% glimpse()
Note that if all of rowname
, colname
, rowtype
, or coltype
columns
are already present in .tidy_iea_data
,
add_row_col_meta()
returns the input data frame without modification.
This behavior allows other matrix row, column,
rowtype, and coltype naming conventions.
If some but not all of rowname
, colname
, rowtype
, and coltype
columns
are present in in .tidy_iea_data
,
add_row_col_meta()
returns an error.
collapse_to_tidy_psut()
)After identifying row and column names and types,
the next step is to collapse the data into PSUT matrices.
The collapse_to_tidy_psut()
function performs this task
using the matsindf::collapse_to_matrices()
function internally.
The matvals
column is added to the data frame and
contain matrix names for each row of the data frame.
collapse_to_tidy_psut()
ensures that all
entries in each matrix are non-negative.
Tidy_PSUT <- load_tidy_iea_df() %>% specify_all() %>% add_psut_matnames() %>% add_row_col_meta() %>% collapse_to_tidy_psut() Tidy_PSUT %>% glimpse() class(Tidy_PSUT$matvals[[1]]) Tidy_PSUT$matvals[[1]]
S_units
matrices (extract_S_units_from_tidy()
)A matrix of Products and Units can be obtained from a tidy IEA data frame
with the extract_S_units_from_tidy()
function.
S_units_df <- load_tidy_iea_df() %>% specify_all() %>% extract_S_units_from_tidy() S_units_df %>% glimpse() S_units_df$S_units[[1]]
prep_psut()
)prep_psut()
is a convenience function.
It converts a tidy IEA data frame
that has gone through specify_all()
into a PSUT data frame
by collapsing the IEA data into PSUT matrices (R, U, V, and Y).
This function bundles several others:
1. add_psut_matnames()
2. add_row_col_meta()
3. collapse_to_tidy_psut()
Furthermore, it extracts S_units
matrices using extract_S_units_from_tidy()
and adds those matrices to the data frame.
Simple <- load_tidy_iea_df() %>% specify_all() %>% prep_psut() %>% pivot_longer(cols = c(R, U_EIOU, U_feed, V, Y, S_units), names_to = "matnames", values_to = "matvals") S_units <- load_tidy_iea_df() %>% specify_all() %>% extract_S_units_from_tidy() Complicated <- load_tidy_iea_df() %>% specify_all() %>% add_psut_matnames() %>% add_row_col_meta() %>% collapse_to_tidy_psut() %>% spread(key = matnames, value = matvals) %>% full_join(S_units, by = c("Method", "Energy.type", "Last.stage", "Country", "Year")) %>% gather(key = matnames, value = matvals, R, U_EIOU, U_feed, V, Y, S_units) # Simple and Complicated are same. full_join(Simple %>% rename(matval_simple = matvals), Complicated %>% rename(matval_complicated = matvals), by = c("Method", "Energy.type", "Last.stage", "Country", "Year", "matnames")) %>% dplyr::mutate( same = matsbyname::equal_byname(matval_simple, matval_complicated) ) %>% magrittr::extract2("same") %>% as.logical() %>% all() Simple %>% glimpse() # U_EIOU for Ghana in 1971 Simple$matvals[[5]]
Taken together, the functions in this package make it possible to prepare IEA extended energy balance data for use with PSUT analysis techniques. A sequence of three function calls is all that is required.
wide_PSUT_mats <- load_tidy_iea_df() %>% specify_all() %>% prep_psut() wide_PSUT_mats %>% glimpse()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.