|
|
(19 intermediate revisions by 3 users not shown) |
Line 1: |
Line 1: |
| [[Category:Costs and valuations]] | | [[Category:Costs and valuations]] |
| [[Category:Opasnet training]] | | [[Category:Opasnet training]] |
| + | [[Category:Contains R code]] |
| {{variable|moderator=Jouni|stub=Yes}} | | {{variable|moderator=Jouni|stub=Yes}} |
| | | |
Line 9: |
Line 10: |
| == Answer == | | == Answer == |
| | | |
− | There is no data; the answer is based on modelling only.
| + | <rcode graphics=1> |
| | | |
− | <rcode
| + | library(OpasnetUtils) |
− | name="answer"
| + | library(ggplot2) |
− | label="Run code"
| |
− | include="
| |
− | page:Object-oriented_programming_in_Opasnet|name:answer|
| |
− | page:OpasnetBaseUtils|name:generic|
| |
− | page:Training_health_impact|name:formula
| |
− | "
| |
− | >
| |
| | | |
− | cat("Initiation successful. Now starting the model.\n")
| + | objects.latest("Op_en5675", code_name = "initiate") # [[Training health impact]] |
| | | |
− | library(xtable)
| + | health_impact <- EvalOutput(health_impact) |
− | out <- make.ovariable(
| |
− | data = 0,
| |
− | formula = formula.Op_en5675,
| |
− | dependencies = dependencies.Op_en5675)
| |
− | cat("Computing training exposures.\n")
| |
− | print(out)
| |
| | | |
− | </rcode>
| + | oprint(summary(health_impact)) |
| | | |
| + | ggplot(health_impact@output, aes(x = health_impactResult, colour = Year)) + |
| + | geom_density() + |
| + | facet_grid(Trait ~ Year) + |
| + | theme_grey(base_size = 24) |
| | | |
− | == Rationale ==
| + | </rcode> |
| | | |
− | === Formula === | + | ==Rationale== |
| | | |
− | <rcode
| + | ===Data=== |
− | name="formula"
| |
− | label="Initiate functions"
| |
− | include="
| |
− | page:Object-oriented_programming_in_Opasnet|name:answer|
| |
− | page:OpasnetBaseUtils|name:generic|
| |
− | page:Training_exposure|name:formula
| |
− | "
| |
− | variables="name:population|description:What is the size of the population|default:100000"
| |
− | >
| |
| | | |
− | ########### update updates the sample of an ovariable based on data and function.
| + | <t2b index="Trait" obs="Result" desc="Description" unit="cases /(µg/m3) /a"> |
− | setMethod(
| + | Respiratory disease|20 - 30| |
− | f = "update",
| + | </t2b> |
− | signature = "ovariable",
| |
− | definition = function(object) {
| |
− | dat <- data.frame(Source = "Data", interpret(object@data))
| |
− | dep <- object@dependencies
| |
− | for(i in 1:length(dep)) {
| |
− | if(class(dep[[i]]) == "ovariable") {
| |
− | dep[[i]] <- dep[[i]]@sample
| |
− | } else {
| |
− | if(length(grep("Op_(en|fi)", dep[[i]])) > 0) {
| |
− | dep[[i]] <- op_baseGetData("opasnet_base", dep[[i]])}
| |
− | else {
| |
− | if(class(dep[[i]]) != "data.frame" & !is.numeric(dep[[i]])) {
| |
− | dep[[i]] <- get(dep[[i]])
| |
− | }
| |
− | }
| |
− | }
| |
− | }
| |
− | form <- data.frame(Source = "Formula", make.ovariable(object@formula(dep))@sample)
| |
− | object@sample <- orbind(dat, form)@sample
| |
− | return(object)
| |
− | }
| |
− | )
| |
| | | |
− | ########### make.ovariable takes a vector or data.frame and makes an ovariable out of it.
| + | ===Dependencies=== |
− | make.ovariable <- function(
| |
− | data,
| |
− | formula = function(dependencies){return(dependencies)},
| |
− | dependencies = list(x = 0)
| |
− | ) {
| |
− | if(class(data) == "ovariable") {
| |
− | out <- data}
| |
− | else {
| |
− | if(is.vector(data)) {data <- data.frame(Result = data)}
| |
− | sample <- interpret(data)
| |
− | out <- new("ovariable",
| |
− | sample = sample,
| |
− | data = data,
| |
− | marginal = ifelse(colnames(sample) %in% c("Result", "Unit"), FALSE, TRUE),
| |
− | formula = formula,
| |
− | dependencies = dependencies)
| |
− | # out <- update(out)
| |
− | }
| |
− | return(out)
| |
− | }
| |
| | | |
− | setGeneric("make.ovariable") # Makes make.ovariable a generic S4 function.
| + | * [[Training exposure]] |
| | | |
− | setMethod(
| + | ===Calculations=== |
− | f = "make.ovariable",
| |
− | signature = signature(data = "data.frame"),
| |
− | definition = function(
| |
− | data,
| |
− | formula = function(dependencies){return(dependencies)},
| |
− | dependencies = list(x = 0)
| |
− | ) {
| |
− | data <- movariable(data)
| |
− | return(data)
| |
− | }
| |
− | )
| |
| | | |
− | ########### movariable takes a vector or data.frame and makes an ovariable out of it. It is a
| + | <rcode label="Initiate ovariable" name="initiate"> |
− | #####copy of make.ovariable that prevents infinite recursion of S4 methods.
| |
− | movariable <- function(
| |
− | data,
| |
− | formula = function(dependencies){return(dependencies)},
| |
− | dependencies = list(x = 0)
| |
− | ) {
| |
− | if(class(data) == "ovariable") {
| |
− | out <- data}
| |
− | else {
| |
− | if(is.vector(data)) {data <- data.frame(Result = data)}
| |
− | sample <- interpret(data)
| |
− | out <- new("ovariable",
| |
− | sample = sample,
| |
− | data = data,
| |
− | marginal = ifelse(colnames(sample) %in% c("Result", "Unit"), FALSE, TRUE),
| |
− | formula = formula,
| |
− | dependencies = dependencies)
| |
− | # out <- update(out)
| |
− | }
| |
− | return(out)
| |
− | }
| |
− | | |
− | setMethod(
| |
− | f = "make.ovariable",
| |
− | signature = signature(data = "list"),
| |
− | definition = function(
| |
− | data,
| |
− | formula = function(dependencies){return(dependencies)},
| |
− | dependencies = list(x = 0)
| |
− | ) {
| |
− | for(i in 1:length(data)) {
| |
− | cat("Data[[i]] ", i, "\n")
| |
− | print(class(data[[i]]))
| |
− | data[[i]] <- make.ovariable(data[[i]])
| |
− | }
| |
− | return(data)
| |
− | }
| |
− | )
| |
| | | |
| + | library(OpasnetUtils) |
| | | |
− | #################### Math defines basic mathematical operations (log, exp, abs, ...) for ovariables
| + | health_impact <- Ovariable("health_impact", |
− | setMethod(
| + | dependencies = data.frame(Name = "exposure", Ident = "Op_en5674/initiate"), # [[Training exposure]] exposure |
− | f = "Math",
| + | formula = function(...) { |
− | signature = signature(x = "ovariable"),
| + | ERF <- Ovariable("ERF", ddata = "Op_en5675") # [[Training health impact]] |
− | definition = function(x) { | + | colnames(ERF@data) <- gsub("[ \\.]", "_", colnames(ERF@data)) |
− | x@sample$Result <- callGeneric(x@sample$Result) | |
− | return(x) | |
− | }
| |
− | ) | |
| | | |
− | ############ tapply of ovariables applies a function to each cell of a ragged array, that is to each (non-empty) group of
| + | out <- exposure * ERF |
− | ############ values given by a unique combination of the levels of certain factors.
| |
− | ### parameters (other parameters are as in generic tapply):
| |
− | ### X an ovariable
| |
| | | |
− | setMethod(f = "tapply",
| + | return(out) |
− | signature = signature(X = "ovariable"),
| |
− | definition = function(X, INDEX, FUN = NULL, ..., simplify = TRUE) {
| |
− | out <- as.data.frame(as.table(tapply(X@sample$Result, INDEX, FUN, ..., simplify = TRUE))) | |
− | colnames(out)[colnames(out) == "Freq"] <- "Result"
| |
− | X@sample <- out
| |
− | return(X)
| |
| } | | } |
| ) | | ) |
| | | |
− | dependencies.Op_en5675 <- list(
| + | objects.store(health_impact) |
− | exposure = "1-2", #"Op_en5674", # formula.Op_en5674(dependencies.Op_en5674), # Training exposure
| |
− | erf = data.frame(Unit = "RR per ug/m3", Result = 1.5),
| |
− | population = population,
| |
− | background = 100 / 100000 # cases per 100000 person-years
| |
− | )
| |
− | | |
− | formula.Op_en5675 <- function(x) {
| |
− | population <- make.ovariable(x$population)
| |
− | background <- make.ovariable(x$background)
| |
− | exposure <- make.ovariable(x$exposure)
| |
− | erf <- make.ovariable(x$erf)
| |
− | cases <- population * background * exp(exposure * log(erf))
| |
− | return(cases)
| |
− | }
| |
| | | |
− | | + | cat("Ovariable health_impact saved.\n") |
− | ###########################################################################################
| |
− | | |
− | cat("Initiation successful. Now starting the model.\n") | |
− | | |
− | library(xtable)
| |
− | out <- make.ovariable(
| |
− | data = "0 - 100000",
| |
− | formula = formula.Op_en5675,
| |
− | dependencies = dependencies.Op_en5675)
| |
− | cat("Computing training health impact.\n")
| |
− | | |
− | make.ovariable(dependencies.Op_en5675)
| |
− | | |
− | out <- update(out)
| |
− | print(out)
| |
| | | |
| </rcode> | | </rcode> |
− |
| |
− | {{attack|# |There are logical errors in the way that data and formula of a variable are used in an assessment. Think this through!|--[[User:Jouni|Jouni]] 06:48, 15 May 2012 (EEST)}}
| |
| | | |
| ==See also== | | ==See also== |
Line 226: |
Line 75: |
| | | |
| ==Related files== | | ==Related files== |
− |
| |
− | {{mfiles}}
| |