Skip to content

spatial.Scheme.Conversion

Steve Hastings edited this page Oct 9, 2013 · 2 revisions

Converting between different spatial schemes.

Sometimes you may find that your data layers are partitioned into spatial regions differently depending on the specific data layer. For instance, some layers might have data for the continental US, Alaska, and Hawaii separately, while others might combine all the data into just the US. At some point in your analysis, these layers will need to be unified under a single "spatial scheme." This package provides a simplified, yet flexible way to do this via the spatial.Scheme.Conversion function. The method used to transform from one spatial scheme to another can be completely arbitrary, limited only by the capabilities of R. Full documentation of spatial.Scheme.Conversion can be found elsewhere; however, here I will walk through the process for transforming layers according to a simple weighted average. Lets jump right in.

Background

Let's say you have a number of layers describing data from Canada and the United States, but some of the layers give two data values for each country, one for the Pacific region and one for the Atlantic region, whereas other layers just give one data value for each country. We'll call these spatial schemes 'expanded' and 'contracted' respectively. You want to conduct your analysis with data in the contracted scheme. Additionally, you decided (through a long and tedious decision process) that for Canada the data from the Pacific region should be weighted twice as much as the data from the Atlantic region, but for the US the data from the Pacific region should be weighted only half as much as the data from the Atlantic region.

In other words:

Canada.Full = (0.67 * Canada.Pacific) + (0.33 * Canada.Atlantic)

and

US.Full = (0.33 * US.Pacific) + (0.67 * US.Atlantic)

Describe the Relationship

Describing this relationship between the expanded ('origin') scheme and the contracted ('destination') scheme is actually quite simple. You just make a CSV file where rows represent the region in the destination scheme and columns represent the region in the origin scheme, so each cell represents the weighting of the origin region in the destination region. For cells where the origin has no influence on the destination, you can either enter 0 in the cell, or leave it blank (or a combination of the two). Finally, you need to enter the origin/destination region names in the first row/column.

Your CSV should then look something like this:

| Canada.Atlantic | Canada.Pacific | US.Atlantic | US.Pacific :-- | :---: | :---: | :---: | :---: Canada.Full | 0.33 | 0.67 | | US.Full | 0 | 0 | 0.67 | 0.33

Get Your Data in R

Now that you have your relationship defined in a CSV file, we need to get it into R. You can do that like this:

myMatrix = as.matrix(
    read.csv('myMatrix.csv', header=T, row.names=1)
)

You'll see here that we imported the data as a matrix. This is important. The spatial.Scheme.Conversion function we will be using can do its work in different ways, and it automatically determines what to do based on the type of data it's passed. By passing it a matrix we are telling the function to calculate the transformation via a weighted average, and additionally to make sure that for each destination region the weights sum to 1.0 (if you don't want to use a weighted average, or you don't want your weights to have to sum to 1.0, read the full documentation to learn how to do use this function in other ways).

Now let's assume you already have your layer data in R. And furthermore you already have a data frame (myData.frame) with all your layer data stacked up. It might look something like this table:

expanded contracted category value
Canada.Atlantic NA yield 1000
Canada.Pacific NA yield 700
US.Atlantic NA yield 1400
US.Pacific NA yield 300
Canada.Atlantic NA pollution 3
Canada.Pacific NA pollution 2.6
US.Atlantic NA pollution 5.4
US.Pacific NA pollution 4.5
NA Canada.Full jobs 100000
NA US.Full jobs 300000

Uh-oh you say, our data frame has a mix of layers and layers in a mix of different schemes. Surely we're going to have to subset our data frame, right? Wrong. spatial.Scheme.Conversion is smart enough to separate out the layers, and figure out which ones to transform according to which rules.

Run the Analysis

Finally we get transform the data. But we need to make sure to pass arguments to spatial.Scheme.Conversion in the right form. First let's make a list.

m = list(
   'expanded' = myMatrix
)

Here our list has only one item, which means only one transformation will be done. If we wanted to do multiple transformations (from multiple origin schemes to our chosen destination scheme) we would do that by including multiple items in the list. The name of our list item ('expanded') corresponds to the origin scheme we want to transform from, and needs to be matched to a column name in myData.frame. The item itself is our matrix which we defined earlier. Now call our function.

spatial.Scheme.Conversion(
    myData.frame,
    'contracted',
    m
)

You'll see we passed three arguments: the data frame containing the layers we want to be transformed, the string 'contracted' representing the scheme we want to transform to, and the list which defines the transformations we want done and how to do them.

If everything went well, the function will return a data frame containing the transformed (and untransformed) data. In our case it should look something like this table:

contracted category value
Canada.Full yield 799
US.Full yield 1037
Canada.Full pollution 2.732
US.Full pollution 5.103
Canada.Full jobs 100000
US.Full jobs 300000

Concluding Remarks

Here we walked through the process to transform data from one spatial scheme to another according to a simple weighted average. While this is a good starting place, and probably a useful transformation in a variety of use cases, it certainly will not satisfy every use case. However, the ohicore package provides a simplified interface for performing just about any transformation a user can dream up. For more information on how to utilize this feature of ohicore, please read the full documentation for spatial.Scheme.Conversion.