This tutorial explains how to load GFPM data into R. And how to prepare the data for further analysis. GFPM simulation results are stored in plain text format in ".DAT" files under the C:\PELPS\pelps folder. After each simulation we copy these files for further analysis with the R statistical programming software.

library(plyr)
library(knitr)
# Set same path for knitr evaluation as for interactive use
opts_knit$set(root.dir = '../..')

Load the package

library(GFPMoutput)

3 ways to load GFPM data into R

There are 3 ways to load GFPM data into R. If GFPM is running on a virtual machine or on a different computer than R, then you should go with the first method, copying the PELPS folder "by hand" in a zip archive. If GFPM is running on the same machine, 2 alternatives are possible below, but if you don't know what to choose from, you should be fine with the zip archive method.

Copy GFPM data from c:\PELPS\pelps by means of a zip archive

Make sure you rename the "PELPS" folder into your scenario name before you compress it to zip.

  1. Rename the folder "C:\PELPS\PELPS" to a name of your choice. For example "your_scenario_name".
  2. Compress this PELPS folder as a zip file: "your_scenario_name.zip"
  3. Copy the zip archive to a \rawdata folder accessible by R.

Now you can convert the content of this zip archive to an RDATA object:

savePELPSToRdata("your_scenario_name", "zip")

Then prepare the data for further analysis with

your_scenario_name = clean(fileName = "your_scenario_name.RDATA",
                           scenario_name = "your_scenario_name")

If you have 2 scenarios you can clean them and combine them in a list object with

scenario1 <- clean("scenario1.RDATA", "scenario1")
scenario2 <- clean("scenario2.RDATA", "scenario2")
all_your_scenarios <- bindScenarios(scenario1, scenario2)

Then you can explore the dataset following examples available in the explore document.

Import GFPM data directly after a simulation

This command loads GFPM data in the form of a list of data frames. It saves the list in R data format under the folder ./enddata. Give a scenario name of your choice.

load_and_clean_gfpm_data(scenario_name = "your_scenario_name", 
                         pelps_folder = "C:/PELPS/pelps/") 

# The cleaned data can be loaded with 
load("enddata/your_scenario_name.RDATA")
# It has the form of a large list containing several data frames
# You can see the begining of each data frame with the command
lapply(scenario,head)

Now you can skip the rest of this document and jump to tutorial/explore.

Copy GFPM data from c:\PELPS\pelps with a function

If the GFPM simulation and R analysis are on the same computer, This function can be used to copy the PELPS folder and load the data into R. After each scenario run, copy this folder into another folder or archive. Copying the PELPS folder can also be done by hand if R is not installed on that machine (see above).

In this example, we give the name "your_scenario_name" to the scenario

copy_pelps_folder(scenario_name = "your_scenario_name")

To save space, data can be compressed

copy_pelps_folder(scenario_name = "your_scenario_name", compression="bzip2")

Then save the base scenario from a bzip2 archive to a RDATA file

savePELPSToRdata("your_scenario_name", compressed="bzip2")

Then prepare GFPM scenario data with clean()

your_scenario_name = clean(fileName = "your_scenario_name.RDATA",
                           scenario_name = "your_scenario_name")

The your_scenario_name object is a list of dataframe. The command str(your_scenario_name) gives details about the structure and content of this data object.

More details on the clean() function

This section provides more details about the various steps performed by the clean() function. PELPS data is transformed by adding column titles, and translating product and country codes into product and country names. available for analysis with R.

Load raw PELPS tables from a .RDATA file

print(load("rawdata/PELPS 105Base.RDATA"))
llply(PELPS,head)

Example using the function splittrade

PELPS2 = splittrade(PELPS)
lapply(PELPS[c("trade")],nrow) #number of rows in PELPS
lapply(PELPS2[c("trade", "import", "export")],nrow) #number of rows in PELPS2

Example using the function reshapeLong

head(PELPS$demand)
demand = reshapeLong(PELPS$demand, "Demand")
head(demand)

Example using the function add product and country

demand = addProductAndCountry(demand)
head(demand)

Example using reshapeLong and addProductAndCountry on World price

wp = reshapeLong(PELPS$worldPrice, "World_Price")
head(wp)
wp = addProductAndCountry(wp)
wp$World_Price = wp$Volume
wp = wp[,c("Product_Code", "Product", "Period", "World_Price")]
head(wp)

List available files

List Zip archives available

list.files("rawdata", ".zip", full.names = TRUE) 

List Raw RDATA files available

list.files("rawdata", ".RDATA", full.names = TRUE) 


paul4forest/GFPMoutput documentation built on May 24, 2019, 8:25 p.m.