OpasnetUtils/Drafts: Difference between revisions
Jump to navigation
Jump to search
(→Calculations: semi-working showind saved) |
|||
| (12 intermediate revisions by the same user not shown) | |||
| Line 23: | Line 23: | ||
=== Calculations === | === Calculations === | ||
'''Functions for ovariable diagnostics | ====Expand_index==== | ||
<rcode name="expand_index" label="Initiate expand_index" embed=1> | |||
# This is code Op_en6007/expand_index in page [[OpasnetUtils/Drafts]] | |||
library(OpasnetUtils) | |||
#' Copies results from one location to other locations while keeping everything else constant | |||
#' @param ova ovariable to be used | |||
#' @param locations list of relevant indices. Each item is a list that has a name of an existing locations, and the content is a vector of new location names. | |||
#' @return the input ovariable with new rows containing the new locations | |||
expand_index <- function(ova, locations) { | |||
if("ovariable" %in% class(ova)) out <- ova@output else out <- ova | |||
for(i in names(locations)) { | |||
for(j in names(locations[[i]])) { | |||
addpiece <- out[out[[i]]==j,] | |||
for(k in locations[[i]][[j]]) { | |||
addpiece[[i]] <- k | |||
out <- rbind(out,addpiece) | |||
} | |||
} | |||
} | |||
if("ovariable" %in% class(ova)) ova@output <- out else ova <- out | |||
return(ova) | |||
} | |||
objects.store(expand_index) | |||
cat("Function expand_index stored.\n") | |||
</rcode> | |||
====Functions for ovariable diagnostics==== | |||
showind has problems with get() but this version of code was acceptable [http://en.opasnet.org/en-opwiki/index.php?title=Special:RTools&id=QcfKMkCd2ewUtZqP]. | showind has problems with get() but this version of code was acceptable [http://en.opasnet.org/en-opwiki/index.php?title=Special:RTools&id=QcfKMkCd2ewUtZqP]. | ||
| Line 39: | Line 71: | ||
deptable <- data.frame() | deptable <- data.frame() | ||
for(i in ls(name = name)) { | for(i in ls(name = name)) { | ||
d = list(get(i))[[1]] | |||
if(class( | if(class(d) == "ovariable") { | ||
depind <- list() | depind <- list() | ||
if(nrow( | if(nrow(d@dependencies)>0) { | ||
dep <- paste( | dep <- paste(d@dependencies$Name, collapse = ", ") | ||
for(k in | for(k in d@dependencies$Name){ | ||
if(!exists(k)) cat(k, "does not exist.\n") else { | if(!exists(k)) cat(k, "does not exist.\n") else { | ||
if(class(get(k)) != "ovariable") cat(k, "is not an ovariable.\n") else { | if(class(get(k)) != "ovariable") cat(k, "is not an ovariable.\n") else { | ||
ko <- list(get(k)@output)[[1]] | |||
if("Iter" %in% colnames(ko)) ko$Iter <- as.factor(max(as.numeric(as.character(ko$Iter)))) | |||
cols <- colnames(ko) | |||
if("Iter" %in% colnames( | |||
cols <- colnames( | |||
if(!sources) cols <- cols[!grepl("Source$", cols)] | if(!sources) cols <- cols[!grepl("Source$", cols)] | ||
if(!prevresults) cols <- cols[!grepl("Result$", cols)] | if(!prevresults) cols <- cols[!grepl("Result$", cols)] | ||
for(l in cols) { | for(l in cols) { | ||
if(l %in% names(depind)) { | if(l %in% names(depind)) { | ||
depind[[l]] <- union(depind[[l]], unique( | depind[[l]] <- union(depind[[l]], unique(ko[[l]])) | ||
} else { | } else { | ||
newind <- list(unique( | newind <- list(unique(ko[[l]])) | ||
names(newind) <- l | names(newind) <- l | ||
depind <- c(depind, newind) | depind <- c(depind, newind) | ||
| Line 69: | Line 99: | ||
dep <- "No dependencies" | dep <- "No dependencies" | ||
} | } | ||
curcols <- colnames( | curcols <- colnames(d@output) | ||
if(!sources) curcols <- curcols[!grepl("Source$", curcols)] | if(!sources) curcols <- curcols[!grepl("Source$", curcols)] | ||
if(!prevresults) curcols <- curcols[!grepl("Result$", curcols)] | if(!prevresults) curcols <- curcols[!grepl("Result$", curcols)] | ||
droploc <- character() | droploc <- character() | ||
for(m in curcols) { | for(m in curcols) { | ||
if(!is.numeric( | if(!is.numeric(d@output[[m]])) { | ||
drops <- setdiff(depind[[m]], unique( | drops <- setdiff(depind[[m]], unique(d@output[[m]])) | ||
if(length(drops>0)) { | if(length(drops>0)) { | ||
droploc <- paste( | droploc <- paste( | ||
| Line 196: | Line 226: | ||
</rcode> | </rcode> | ||
====Functions for Webropol data==== | |||
<rcode name="webropol" embed=1> | <rcode name="webropol" embed=1> | ||
| Line 268: | Line 298: | ||
</rcode> | </rcode> | ||
'''Miscellaneous functions | ==== HNH2035 functions ==== | ||
These are functions that were needed and developed for the [[:op_fi:Hiilineutraali Helsinki 2035]] work. | |||
<rcode name="hnh2035" label="Initiate functions for HNH2035" embed=1> | |||
#This is code Op_en6007/hnh2035 on page [OpasnetUtils/Drafts]] | |||
library(OpasnetUtils) | |||
# pushIndicatorGraph was moved to package https://github.com/jtuomist/CNH-energy | |||
# Colors from the Helsinki theme | |||
colhki <- rep(c("#0072c6","#00d7a7","#c2a251","#9fc9eb","#ffc61e","#009246"),5) | |||
#' Remove overlap with happened and scenarios, and then combine lines | |||
#' @param ova ovariable where lines are combined | |||
#' @param measured name for the scenario that contain actual data about measured things | |||
combineLines <- function(ova, measured="toteutunut") { | |||
tst <- unique(ova[ova$Scenario==measured,]$Year) # Years with data | |||
out <- ova[ova$Scenario==measured | !ova$Year %in% tst,] # Remove scenarios if data | |||
tmp <- out[out$Scenario==measured & out$Year == max(tst),colnames(out@output)!="Scenario"] | |||
tmp <- tmp * Ovariable( | |||
output=data.frame( | |||
Scenario=setdiff(unique(out$Scenario),measured), | |||
Result=1 | |||
), | |||
marginal=c(TRUE,FALSE) | |||
) | |||
out <- combine(tmp,out, name=ova@name) | |||
return(out) | |||
} | |||
objects.store(combineLines, colhki) | |||
cat("Function combineLines and colour vector colhki stored.\n") | |||
</rcode> | |||
====Miscellaneous functions==== | |||
<rcode name="miscellaneous" embed=1> | <rcode name="miscellaneous" embed=1> | ||
#This is code Op_en6007/ | #This is code Op_en6007/miscellaneous on page [OpasnetUtils/Drafts]] | ||
library(OpasnetUtils) | library(OpasnetUtils) | ||
| Line 755: | Line 821: | ||
</rcode> | </rcode> | ||
====Functions for GIS data==== | |||
<rcode name="gis" embed=1> | <rcode name="gis" embed=1> | ||
| Line 1,039: | Line 1,105: | ||
objects.store(MyPointKML, ova2spat, MyRmap, MyPlotKML) | objects.store(MyPointKML, ova2spat, MyRmap, MyPlotKML) | ||
cat("Functions MyPointKML, ova2spat, MyRmap, MyPlotKML stored.\n") | cat("Functions MyPointKML, ova2spat, MyRmap, MyPlotKML stored.\n") | ||
</rcode> | |||
==== Timelineplot ==== | |||
<rcode name="timelineplot" label="Initiate function timelineplot" embed=1> | |||
# This is code Op_en6007/timelineplot on page [[OpasnetUtils/Drafts]] | |||
library(OpasnetUtils) | |||
#' @description plots timeline for an ovariable or data frame. | |||
#' @param ova an ovariable | |||
#' @param noshow character vector of Work items that are not shown on graph. | |||
timelineplot <- function(ova, noshow=c("Ulos","Out")) { | |||
require(ggplot2) | |||
resn <- paste0(ova@name,"Result") | |||
if(class(ova)=="ovariable") out <- ova@output else out <- ova | |||
colnames(out)[colnames(out)==resn] <- "Time" | |||
# Make sure that data is ordered correcly. | |||
out <- out[order(out$Person, out$Time) , ] | |||
out$Row <- 1:nrow(out) | |||
# Make endpoint for each task. | |||
b <- cbind( | |||
out[!colnames(out) %in% c("Work","Row")], | |||
Work=c(NA,as.character(out$Work)[1:(nrow(out)-1)]), | |||
Row =c(NA,out$Row[1:(nrow(out)-1)]) | |||
) | |||
b$Work[match(unique(b$Person),b$Person)] <- NA | |||
out <- rbind(out,b) | |||
out <- na.omit(out[!out$Work %in% noshow , ]) | |||
if(is.numeric(out$Time)) out$Time <- as.POSIXct(out$Time, origin="1970-01-01 00:00:00") | |||
pl <- ggplot(out, aes(x=Time,y=Person,colour=Work,group=Row))+geom_line(size=5) | |||
return(pl) | |||
} | |||
objects.store(timelineplot) | |||
cat("Function timelineplot stored.\n") | |||
</rcode> | </rcode> | ||
Latest revision as of 10:37, 6 March 2021
| Moderator:Jouni (see all) |
|
|
| Upload data
|
Question
Which functions are so useful that they should be taken into OpasnetUtils package? This page contains draft function which will be included when they are good enough and found important.
Answer
Call the objects stored by this code from another rode with this command:
objects.latest("Op_en6007", code_name = "answer") # Old version that fetches all objects, depreciated and not updated.
objects.latest("Op_en6007", code_name = "diagnostics") # Functions for ovariable and model diagnostics: ovashapetest, showLoctable, binoptest
objects.latest("Op_en6007", code_name = "webropol") # Functions for operating with Webropol data
objects.latest("Op_en6007", code_name = "miscellaneous") # Functions for various tasks
objects.latest("Op_en6007", code_name = "gis") # Functions for ovariable, KML and Googl maps interactions
Rationale
Calculations
Expand_index
Functions for ovariable diagnostics
showind has problems with get() but this version of code was acceptable [1].
Functions for Webropol data
HNH2035 functions
These are functions that were needed and developed for the op_fi:Hiilineutraali Helsinki 2035 work.
Miscellaneous functions
Functions for GIS data
Timelineplot
See also
- OpasnetUtils/Ograph, a previous code, now depreciated.
- en:Matrix multiplication in Wikipedia, Matmult in R
References
Related files
<mfanonymousfilelist></mfanonymousfilelist>