|
|
(2 intermediate revisions by the same user not shown) |
Line 23: |
Line 23: |
| ggplot(health_impact@output, aes(x = health_impactResult, colour = Year)) + | | ggplot(health_impact@output, aes(x = health_impactResult, colour = Year)) + |
| geom_density() + | | geom_density() + |
| facet_grid(Cleaning_policy ~ Health_promotion) + | | facet_grid(Trait ~ Year) + |
| theme_grey(base_size = 24) | | theme_grey(base_size = 24) |
|
| |
|
Line 32: |
Line 32: |
| ===Data=== | | ===Data=== |
|
| |
|
| <t2b index="Cleaning policy,Health promotion,Trait" obs="Result" desc="Description" unit="cases /a"> | | <t2b index="Trait" obs="Result" desc="Description" unit="cases /(µg/m3) /a"> |
| BAU|BAU|Total mortality|20 - 30|
| | Respiratory disease|20 - 30| |
| Reduce emissions|BAU|Total mortality|10 - 15|
| |
| BAU|Increase health education|Total mortality|18 - 28|
| |
| Reduce emissions|Increase health education|Total mortality|8 - 13|
| |
| </t2b> | | </t2b> |
|
| |
|
Line 66: |
Line 63: |
|
| |
|
| </rcode> | | </rcode> |
|
| |
| ===Explanations===
| |
|
| |
| First, you start the rcode by using the rcode tag. You may change the text that shows on the run button by using the parameter ''label''. You may also define user input form by using the parameter ''variable'' (not used here).
| |
|
| |
| <pre>
| |
| <rcode
| |
| label="Initiate functions"
| |
| >
| |
| </pre>
| |
|
| |
| Then, you define the libraries (or packages) that you want to use. You almost always need the library OpasnetUtils, and if you want to show decent tables in the model output, you also need xtable. If you want to draw graphs, you probably need ggplot2.
| |
|
| |
| <pre>
| |
| library(OpasnetUtils)
| |
| library(xtable)
| |
| </pre>
| |
|
| |
| With few exceptions, each Opasnet page used in a model has an R code that creates an information object, called ovariable. The object answers to the question presented on that page. In effect, it is equal to the answer of the page, but described in a way that it can be understood by a computer running models. An ovariable is properly defined only if it is possible to calculate the answer for the question. You can calculate the answer by using either measurement '''data''' about the issue or '''dependencies''' and '''formula'''. Dependencies are other objects that you can use to calculate (rather than directly measure) the answer. How to use dependencies to calculate the answer, that is described in the formula.
| |
|
| |
| Data, dependencies, and formula are parts of an ovariable. They are called slots. On this page, we define an object called ''health.impact''. It contains these three slots and a few others. In an R code, you can refer to a slot by adding the name of the slot after the name of the ovariable, separated by @: health.impact@data. If you want to refer to a column within a table in a slot, you say e.g. health.impact@dependencies$Name.
| |
|
| |
| The slot dependencies is a [[data.frame]] (a kind of a table) with Name and Key as the two columns, while formula is a [[function]]. Data is also a data.frame but it is more flexible about the names and amount of columns in it. Typical columns are Unit and Result.
| |
|
| |
| Dependencies is defined by using the function data.frame, to which the names of columns and then the content of the columns are given. c() is a function that creates a [[vector]] (a kind of list of numbers or text strings in a specified order). In this case, the vectors (or columns) Name and Key have only one row.
| |
|
| |
| <pre>
| |
| dependencies.input <- data.frame(
| |
| Name = c("exposure"),
| |
| Key = c("muRtYjhkphoNPQJI")
| |
| )
| |
| </pre>
| |
|
| |
| Functions are defined in a way that you first give the name of the new function, then you list its parameters. In this case, the only parameter defined is ''dependencies'', but the three dots ... indicate that other parameters can be given by the user.) After this is a description about what the function actually does. In practice, all formulas have the same first row that forces a formula to compute its dependencies first. So, remember to copy-paste the first row to your code, whatever your formula does. In this example, the formula defines three constants and then takes ''exposure'' from the dependencies; in other words, the exposure comes from another ovariable defined elsewhere. The number of cases of a health impact are calculated using there constants and the exposure. Finally the calculated number of cases is returned from the function.
| |
|
| |
| <pre>
| |
| formula.input <- function(dependencies, ...) {
| |
| ComputeDependencies(dependencies, ...)
| |
| erf <- 1.5 # RR per ug / m3
| |
|
| |
| background <- 100 / 100000 # cases per 100000 person-years
| |
|
| |
| population <- 50000
| |
|
| |
| cases <- exposure * population * background * (erf - 1)
| |
| return(cases)
| |
| }
| |
| </pre>
| |
|
| |
| In this case, data is defined by downloading a ready-made table from the [[Opasnet Base]]. Tidy and op_baseGetData are functions that actually do the downloading. For now, it is enough to say that you can just copy the code to another place and replace "Op_en5675" with the identifier of your page and "health.impact" with the name you want to give to your ovariable. In most cases it works even if you don't know all the details.
| |
|
| |
| <pre>
| |
| data.input <- tidy(op_baseGetData("opasnet_base", "Op_en5675"), "health.impact")
| |
| </pre>
| |
|
| |
| Now we have come to the place we we have done the background work and we actually create our ovariable called health.impact. Again, you can copy-paste this code to other places and just replace the parts you need. An ovariable works properly only if you have defined either data or dependencies together with formula. You can define all of them if they are available.
| |
|
| |
| <pre>
| |
| health.impact <- new("ovariable",
| |
| name = "health.impact",
| |
| formula = formula.input,
| |
| dependencies = dependencies.input,
| |
| data = data.input
| |
| )
| |
| </pre>
| |
|
| |
| The final part of the code is the actual computing. EvalOutput is a function that calculates the output of an ovariable, based on data or dependencies, whichever is available. If the dependencies themselves depend on other objects, they are fetched and evaluated as well, and if they have dependencies, they are also fetched and so on, until everything that is needed is fetched. The parameter ''N = 10'' tells the number of [[Monte Carlo]] iterations that are used. First, you should start with rather low numbers until you see that the model has not grown too large.
| |
|
| |
| You probably want to print or plot model outputs. in this example, the output table is printed on screen. You can search for ''ggplot'' within Opasnet to find out pages where different kinds of plots have been used. Often, you can just copy-paste the code with ggplot2 function to your own code, change the names of the objects you use, and you get fancy plots in no time!
| |
|
| |
| Finally, the ovariable you have created is saved onto the Opasnet server by using the function ''objects.put''. You can call that particular object in the particular run by copying the key from the url to a dependencies table of some other ovariable. In this example, the upstream dependency object ''exposure'' was created in a model run with the key muRtYjhkphoNPQJI. If you want to go back to that run, you can use the key in this way:
| |
|
| |
| http://en.opasnet.org/en-opwiki/index.php/Special:R-tools?id=muRtYjhkphoNPQJI
| |
|
| |
| <pre>
| |
| out <- EvalOutput(health.impact, N = 10)
| |
|
| |
| print(xtable(out@output), type = "html")
| |
|
| |
| objects.put(health.impact)
| |
| </rcode>
| |
|
| |
| </pre>
| |
|
| |
| Remember to close your rcode by using the end tag <nowiki></rcode></nowiki>.
| |
|
| |
|
| ==See also== | | ==See also== |