Calculation


CALC allows to build calculation steps in three ways:

  • Writing a script in R
  • Using external equations previously added in the Settings section
  • Introducing a new Categorical variable 

To create a new Calculation step click on the “plus” icon

Then select the type of calculation you wish to create by clicking one of the three options:

 

 

 

Calculation writing a script in R

 

The following is an example of how to create a calculation step using R script to assign a height value to each tree record according to a linear fit model.

Note: useful sources for learning about R: Quick-RCodeschool - Try R

 

 

  • Caption: indicates the name that the user chooses to give to the calculation [in the test example this calculation step is called tree_height]
  • Entity: allows to select the entity on which the calculation will be created. [In the test example the entity selected is tree]
  • Variable: indicates the name of the new variable that will be created through the calculation step. [In the test example the variable will be called est_height]
  • Script: the actual R script with instruction on how to perform the calculation. In the test example the R script is as follows.

 

sample_trees <- tree[ !is.na( tree$total_height ) , ]

sample_trees <- sample_trees[ sample_trees$total_height > 0 , ];

height_model <- with( sample_trees, lm(total_height ~ dbh + I(dbh ^2) + I(dbh ^3)) );
tree$est_height <- predict( height_model, newdata = tree[ , c('dbh','total_height')] );
tree$est_height <- ifelse( is.na(tree$total_height), tree$est_height, tree$total_height) ;

 

Below (Click on Examples of R scripts) find some examples of R scripts used to perform Calculations

The following are some examples of R scripts used to perform Calculations

-----------------------------------------------------------------------------------------------

Calculation name: Tree - Basal area

Calculation type: R script

Script:

tree$basal_area <- with(tree, pi * (0.01*dbh/2)^2)

-----------------------------------------------------------------------------------------------

Calculation name: Tree - Volume

Calculation type: R script

Script:

# Basic form factor volume model

ff <- 0.515;

tree$volume <- with( tree, (0.1291+1.5984 * ff) * pi * (0.01 * dbh / 2)^2 * est_height^0.764 );

-----------------------------------------------------------------------------------------------

Calculation name: Stand - IPCC class

Calculation type: Category

Script:

Stand - IPCC class

# '1' Forest land, '2' Grass land, '3' Cropland, '4' Settlements, '5' Wetland, '-1' NA

stand$ipcc_class = with ( stand, 

ifelse(forest_status ==160 | forest_status == 630, 5,

ifelse(forest_status < 440, 1,

ifelse(forest_status == 440, 2,

ifelse(forest_status < 600, 3, 4)

)));

-----------------------------------------------------------------------------------------------

Calculation name: Tree - AG Biomass

Calculation type: R script

Script:

Tree - AG Biomass

BEF_pinus <- 1.3;

tree$genus_code <- substr( tree$species_code, 1, 3 ); # compute AGB in kg

tree$aboveground_biomass <- with ( tree,

ifelse( genus_code=='PIN', BEF_pinus * volume * 500, # Pinus, wood density = 500 kg/m3

269.63396 * (((dbh/100)^2*est_height)^0.95193) # Evergreen forest

))

# convert kg -> tons

tree$aboveground_biomass <- tree$aboveground_biomass / 1000

-----------------------------------------------------------------------------------------------

Calculation name: Tree - BG Biomass

Calculation type: R script

Script:

# conversion factor source:

tree$belowground_biomass <- tree$aboveground_biomass * 0.265;

-----------------------------------------------------------------------------------------------

Calculation name: Tree - Total biomass

Calculation type: R script

Script:

tree$total_biomass <- tree$aboveground_biomass + tree$belowground_biomass ;

-----------------------------------------------------------------------------------------------

Calculation name: Stump - Count

Calculation type: R script

Script:

stump$quantity[is.na(stump$quantity)] <- 1

stump$quantity[stump$quantity =='']   <- 1

stump$count_stump <- stump$quantity

-----------------------------------------------------------------------------------------------

Calculation using external equations

 

The following is an example of calculating tree volume using a list of volume models that was previously uploaded into Calc during the Settings phase

  

The fields Caption, Entity and Variable have the same function as in the previous example. 

  • Equation list : this field is used to select the equation list that should be used to perform the calculation. [In the test example the only equation list that was uploaded is the one called volume].

When an equation list is selected, Calc recognizes which are the variables involved in the calculation, by analyzing the variables required in the equations (you may want to have another look at the equations listed in calc-volume-models.csv). Then new fields appear and the appropriate variable should be selected. In the test example:

  • Code variable = species_code ;
  • Variable ‘vegetation_type’ = vegetation_type ;
  • Variable ‘h’= est_height (the variable created in the example using R script) ;
  • Variable ‘dbh’ = dbh

 

 

 

Calculation introducing a new Categorical variable

 

The following is an example of how to create a new Categorical variable to define dbh classes and assign each tree record to a specific class according to its dbh.

 

 

The fields Caption and Entity have the same function as in the previous examples.

In our test example we assign Dbh class as the caption of the new category and then then select tree as the entity of our interest.

Define the name of the new Variable 

 

 

Click Save

The new Category can be added and defined by clicking on Plus sign [+] on the right.

A new mask will open requesting to indicate a Caption for the new category as well as a Code and a Caption for each class.

 

[In the test example we wish to create four dbh classes with codes ranging from 1 to 4 and dbh classes of <10; 10-20; 20-30; 30+].

 

 

Next step involves writing the R script for the Calculation step

Note

  • Press Ctrl+Space to activate auto-complete
  • Press F11 toggle full screen

 

 

In the text example the R-script will be entered as follows:

 

 

R script:

 

# '-1' NA, '1' <10, '2' <20, '3' <30, '4' <40, '5' >=40 tree$dbh_class <- ifelse( tree$dbh < 10 , 1 , 

ifelse( tree$dbh < 20 , 2 ,

ifelse( tree$dbh < 30 , 3 ,

ifelse( tree$dbh < 40 , 4, 5 )

)

)

);

Testing / Executing

When a calculation step is created in one of the three modalities, the user can Save it, Edit it and Test/Execute it.

The Edit function simply allows to modify the parameters set in the calculation step. The Test function allows to run the calculation on a “controlled” data set, with parameters defined by the user, in order to check the logical validity of the calculation. The Execute function runs the calculation on the actual data.

 

Test

The following example shows how to test the Calculation previously created which intended to assign each tree record to a dbh class. 

Select a minimum and a maximum value for the variable to be tested (in this case dbh) and then set the desired increment: interval between one record and the next. In the example below Calc will test the calculation step on dbh values from 0 to 100 with an increment of 10.

Click Test 

Calc will switch to Data view to display the results as shown below. The results indicated that the dbh test values have been assigned to a dbh class correctly. 

 

Execute

When a calculation step is Executed, Calc runs it on the actual data that was imported from Collect.

A Running window will open and the results will be displayed in Data view.

 

The calculation step has been performed on all records and the results can be visualized and downloaded in csv format.

 

 

 

 

Running Calculation Chain

 

Once the calculations are saved they will appear in the main panel as shown below. Calculation steps can be deleted by drag and drop in the trash icon on the right side.

Each calculation step can be activated or deactivated.

Click on the small icon within each calculation step to Activate/Deactivate.

Note: Pay attention to the dependencies among calculation steps. If a calculation step is needed by another calculation step it needs to be activated otherwise it will interrupt the calculation chain and result in Runtime error. 

The total number of active calculation steps is visible next to the "Play" button.

To start the Calculation Chain press the icon Play  

A Running Window will show progress and in case of mistakes they be shown in the Log box.

 

 

Click Close

Results are ready to be displayed in Saiku, a web-based open source software for data visualization and data querying. 

Access Saiku interface by clicking on the Saiku icon   

 

 

 

 

 

Click here to return to Tutorials Main Menu