Object-oriented programming in Opasnet

From Opasnet
Jump to: navigation, search

Object-oriented programming is an approach where programs (or, in Opasnet, typically assessment models) have a modular structure in such a way that each part is considered as a separate object that has specific properties and interacts with other objects in standard ways.

Home work.2

1. What are S4 class objects

Learning to work with modelling objects in Opasnet

  • WATCH recorded shows online
  • Objective: To learn the structure and use of S4 class objects oassessment and ovariable.
    • How they are created in wiki?
    • How they are used in R?
  • Time: 16.5.2012 9.00 - 15.00
  • Place: Kielo?, THL, Kuopio
  • Language: In Finnish, unless foreigners show up.
  • Content (most material can be found from Object-oriented programming in Opasnet.
    • Basic ideas of open modelling
    • Structure of assessments and variables
    • S4 class objects
    • How S4 class objects are used to implement the structure and functionalities of assessments and variables
    • Structure of oassessment
    • Structure of ovariable
    • What do the most important functions do (interpret, make.ovariable, init.vars, ...)
    • Plans for summer work: which projects are used as playground first
    • Further training: How to organise?
  • My knowledge is our knowledge
  • EDS Airplane
  • Syvämiete
  • The Greek situation: what would it be if we had had an open assessment about it 10 years ago?
  • In the training, there are very advanced people and beginners. This is the purpose to force us focus on the big picture and the ideas, not so much to the details.
  • It's cool to make mistakes: you get guidance about the specific things you need it for; and other people learn about what the difficult things are. This is the strength of open assessment.

Overall picture:

How to perform an assessment

  1. Get the variable list. Make sure that upstream variables are first on the list.
  2. Go through each variable at a time.
    1. Based on data given, if any, bring in the data from Opasnet Base or make a data.frame out of the vector. Store the resulting data.frame as @data.
    2. Store the formula.objectname, if available, as @formula. This comes from including the formula named "formula" from the object page.
    3. Also dependencies are defined in the same formula. Store the dependencies.objectname, if.available, as @dependencies.
    4. Interpret the data and run the formulas with a systematic method such as hierarchical Bayes or other. So far, only this simplistic approach is available:
      1. Interpret data and make a data.frame with column Source and location Data.
      2. Run the formula and make a data.frame with column Source and location Formula.
      3. Combine the two data.frames with orbind and store the result as @sample.
    5. Apply the decision variable to the variable, if relevant.
    6. Go back to #2 until all variables have been calculated.
  3. Apply the stakeholder table to the assessment. Go through each decision.
    1. For each decision, integrate over all other indices to find the best decision option based on the outcome of interest for the stakeholder. Use probabilities described on page /Probabilities if available, otherwise assume that all locations are equally likely.
    2. Calculate total VOI for that decision.
    3. Go back to #4 until all stakeholders and decisions have been handled.


How should object-oriented programming be utilised in Opasnet in such a way that

  • it has seamless connections to R-tools,
  • it is easy to understand by non-expert users and contributors,
  • it uses the variable structure and other information structures (e.g. universal object) used in open assessment, and
  • it enables standards for typical processes in environmental health assessments (such as distribution modelling, life tables, decision optimising, etc.).


Structure of objects

Objects have two different implementations: wiki page in Opasnet, and S4 class object called ovariable (open assessment variable) in R-tools. The wiki page is the user-friendly interface for users, and ovariable is the versatile format for efficient, standardised modelling. The default direction for data is long (using the terminology in the merge function).

# Should we have attribute "target" that defines the target of the variable estimate. For example, "height" may estimate the whole variation of heights of individuals in a population, or it may estimate the mean height of the population. Somehow the population, the target that is the basic unit (individual in this case) and the statistical parameter should be explicitly described. Can this be done by using vector attributes that have a value for each index column in the sample? Is this an index-specific issue, or variable-specific? --Jouni 07:50, 9 April 2012 (EEST)

# : I don't understand what I am talking about. Maybe this should be removed. --Jouni 17:07, 16 October 2012 (EEST)
Attribute What it contains How implemented in the wiki How implemented in the R-tools as a S4 class object ovariable
These attributes are needed in R-tools.
name The name of the object. The name of the wiki page. The Name slot in ovariable; it must be the same as the name of the ovariable object.
data Observations, expert judgement, discussions, and other pieces of information. Subheading under Rationale Slot data = "data.frame". The data frame can contain Obs and Unit columns, at least one index column, and Result as the observation column. However, it must not contain Iter column.
output The output of the calculations. Not shown Slot output = "data.frame". The data frame may contain columns Iter, Obs, Unit, one column for each index, and NameResult (where Name is the name of the object).
marginal A Boolean vector with the size = number of indices = ncol(data) - 1. TRUE if an index is indexing a marginal distribution in sample, FALSE if joint distribution. The difference is that in a marginal distribution there are n iterations for each location of the index, while in joint distribution, there are altogether n iterations in such a way that the frequencies of locations match their probabilities. Not implemented in wiki. Slot marginal = "vector". Especially with indices with lots of locations, joint distribution needs much less memory.
dependencies A list of dependencies, i.e. objects that are causally upstream. List of links to other pages under subheading Dependencies. Slot dependencies = data.frame. Dependencies contains columns Name that contains object names and Key that contains keys for model runs.
formula A computer code or algorithm to derive the answer from rationale and objects listed in dependencies. The formula may assume a deterministic dependency (e.g. y <- k*x + b), a conditional probability structure (y ~ dnorm(x, sd)), or a rank correlation matrix. Subheading under Rationale, often using <rcode> tags. Slot formula = "function". Formula contains the R code that is needed for calculating the output.
These attributes are not (yet) implemented in R-tools. # : The text below is outdated. Don't believe all details. --Jouni 17:07, 16 October 2012 (EEST)
question A research question that defines the topic of the object First main heading Slot question = "character". Contains the question as text.
answer The current best answer to the question, shown as text, data table, or distribution. Second main heading; contains a single data table. NOTE! The data table is actually under ratonale/data but often it is the same as answer. The actual answer is precisely described by distribution and sample (see below). Only sub-attributes are implemented.
locations List of names for observation columns in the wide format (columns that are not indices), i.e. the same as measure.vars parameter in the function melt. The same as locations parameter in t2b tag. Slot locations = "vector". A vector with all observation column names. Can be integer (observation column position) or string (observation column name). By default, the column name for locations is "Parameter" and the column name for the actual observations is "Result". # : Do we actually need this in R, if always a variable is molten using melt before actual use? --Jouni 14:54, 4 April 2012 (EEST)
observation An identifier of an individual when the answer consists of a group of individuals. Obs column (usually implicit because the default is that each row is an observation) in the data table. Obs column in data.frames data and sample. Not explicitly needed as a slot in S4 object. --# : How do we operate, if there are different Obs in different variables and they are merged? a) Repeat shorter Obs's until they reach the length of the longest. b) Refuse to operate unless the user renames or removes all but one Obs column; however if Obs only has 1 observation, it can be temporarily removed during merge. --Jouni 14:54, 4 April 2012 (EEST) # : I prefer b). a) is too abstract so that the user is just confused. --Jouni 14:54, 4 April 2012 (EEST)
iteration An identifier of a probabilistic run or iteration. Sometimes it is also called a possible world or realisation. Iter column in the data table (data usually not shown probabilistically in wiki). Iter column in the data.frames sample (and rarely in data). Not explicitly needed as a slot in S4 object.
distribution A joint probability distribution (with indices as dimensions) describing the answer mathematically. Not shown Slot distribution = "distribution?". A distribution created with e.g. dnorm(0,1). --# : We don't know yet how to actually implement this and how the indices are included. --Jouni 14:54, 4 April 2012 (EEST)
rationale Any information that is needed to convince a critical reader that the answer is good. Third main heading Only sub-attributes are implemented.
unit The measurement unit(s) that are used in the answer to measure the topic. The format used is kg m^2 /s^2 where a space implies a multiplication. Subheading under Rationale with plain text. Also mentioned in the data table with parameter unit. If data table rows differ in units, there must be a Unit index. There is no separate slot for unit. The unit is merged with data and subsequently with sample. This must be done, because if rows are ordered, it is impossible to attach units to right rows based on separate information.
formula.prob A list of probabilities assigned to the competing algorithms in formula. The default is that each has an equal probability. A detail in <rcode> code. Slot formula.prob = "vector". Should have the same size as formula.


R code should be developed in such a way that there are object-specific implementations of critical functions. The user should see straightforward content, and all messy indexing etc should happen behind scenes.

These methods should be implemented for ovariable objects.

  • show, print: show the data slot.
  • plot: plot the sample, showing one (the first by default) marginal index with all locations and all other marginal indices with the first location only.
  • tidy: applies to data: remove id column; add Obs and Iter columns if they do not exist; Change the direction from wide to long.
  • createSample: create sample directly from data using interp.input.
  • GetSample, GetData: extract sample and data from the object, respectively.
  • Ops: applies to sample: merge two ovariables based on index columns, then perform the Ops operation to the result columns.
  • standardUnits: Based on units, transform the result column of data to SI units using Unit transformations table; then update unit.
  • demarginalize: turn one specified index from marginal to joint format. This function has parameter jointlimit: if the length(index_i) * n > jointlimit, then index_i is demarginalized. The default for jointlimit is 1000000. n is the length of Iter.

Oavariable functions available: make, update, plot, print, orbind, merge, callGeneric Ops

--# : Function merge only uses indices that are of type "marginal" and not indices that are of type "joint". For a non-ovariable data.frame, by default all indices are marginals. --Jouni 14:58, 30 April 2012 (EEST)


+ Show code

Example codes


Simple code

+ Show code

Making an ovariable

+ Show code

Making several differend ovariables.

What is the size of the population:

+ Show code

Important rules:

  1. Never use ".x" in the name of an ovariable.
  2. The observation column always must have name "Result".
  3. The iteration (aka. sample, run) column always must have name "Iter".
  4. In the data slot, there is always column "Obs". It defines the observations that are from the same individual. Of omitted, each row is assumed to be from different individual.
  5. Data slot must not have Iter column, but sample may or may not have. Typically it does have it.
  6. Variable n is reserved for number of iterations.
  7. Column name Source is reserved for the source of the result (either Data of Formula).
  8. After using make.ovariable(), use update() to update the sample of the ovariable based on both Data and Formula. This is not done automatically, because often there are problems to run the formula part of the code.

About oassessment (open assessments)

  • They are collections of information that are used on assessment level.
  • They contain the following parts:
    • Data frame about variables included. It has columns name (wiki page name), identifier (page identifier such as Op_fi2898), and alias (name that is short but descriptive in its context and that is used in rcode about the variable such as exposure, erf.radon etc.) Also, R object saved with object.put should be downloadable using this data. Should we also have hashtags available? How?
    • a list of ovariables (variables themselves, not just names)
    • If a variable is in the dependencies of a variable used in an assessment, the first variable is only included partly. In practice, if sample is available that is used, otherwise data. However, formula and dependencies are NOT used because they are outside the boundaries of the assessment. Otherwise the assessment may grow into an endless web.
  • There are several ways to treat dependencies.
    • Upstream functions: deterministic functions that take upstream variables as inputs and calculate outputs. This is implemented first.
    • JAGS or other hierarchical Bayesian approaches. What are the demands of this to oassessment? Should there be "model" or other slots for this?
    • BBN. What are the demands of this to oassessment? At least conditional probabilities.
  • We need a wrapper to op_baseGetData in such a way that the code may give a wiki page name, an identifier, or an alias, and the function always knows what data to download. This makes the rcode more user-friendly. But does it always need a oassessment, or does it get the identifier based on wiki page name from the wiki database directly? The latter would be more useful.

There should be a function that automatically drops dimensions of an ovariable. This is the priority list:

  • First drop dimensions that are explicitly mentioned in parameter "drop" (a vector of index names).
  • Drop dimensions that cause the size of the ovariable to grow larger than dropsize = 1000000.
  • If there is enough information available to perform a VOI analysis, drop all dimensions except those VOIsize = 3 indices that have the largest VOI. (But VOI is attached to ovariables, not indices; how does this work out? It works out in the way that P(B) is thought as a variable and its VOI can be estimated. The VOI of P(A) or P(A|B) is calculated by giving the original ovariable as parameter.)
  • Never drop indices that are mentioned in parameter keep (a vector of index names).
  • "Drop" function changes an index from marginal to joint. In practice, the size changes from size(index)*size(Iter)*size(other indices combined) to size(Iter)*size(other indices combined). The variable can also be thought as a conditional probability P(A|B) which is then changed to joint by sampling from index B using its probability: P(A|B)*P(B) = P(A).
  • If P(B) is omitted it is assumed that all instances of B are equally likely. Typically, P(B) is assessment-specific, and therefore there is a slot for all P(B)s in oassessment. This slot is a data.frame with columns Variable, Index, Location, P, Description.
  • However, even if an index is changed from marginal to joint, the column that contains information about the location of the index on each iteration is still kept.


See also

Materials and examples for training in Opasnet and open assessment
Help pages Wiki editingHow to edit wikipagesQuick reference for wiki editingDrawing graphsOpasnet policiesWatching pagesWriting formulaeWord to WikiWiki editing Advanced skills
Training assessment (examples of different objects) Training assessmentTraining exposureTraining health impactTraining costsClimate change policies and health in KuopioClimate change policies in Kuopio
Methods and concepts AssessmentVariableMethodQuestionAnswerRationaleAttributeDecisionResultObject-oriented programming in OpasnetUniversal objectStudyFormulaOpasnetBaseUtilsOpen assessmentPSSP
Terms with changed use ScopeDefinitionResultTool


Related files