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)
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.
Make sure you rename the "PELPS" folder into your scenario name before you compress it to zip.
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.
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.
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.
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.
print(load("rawdata/PELPS 105Base.RDATA")) llply(PELPS,head)
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
head(PELPS$demand) demand = reshapeLong(PELPS$demand, "Demand") head(demand)
demand = addProductAndCountry(demand) head(demand)
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 Zip archives available
list.files("rawdata", ".zip", full.names = TRUE)
List Raw RDATA files available
list.files("rawdata", ".RDATA", full.names = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.