Difference between revisions of "OpasnetUtils/Interpret"

From Testiwiki
Jump to: navigation, search
m
m
 
(5 intermediate revisions by 4 users not shown)
Line 5: Line 5:
  
 
==Description==
 
==Description==
interpret takes a vector and makes a data.frame out of it (to be used in e.g. make.ovariable).
 
  
It also changes abbreviations into probability samples.
+
Interpret takes a vector or data.frame as argument. And returns a data.frame with certain textual inputs interpreted as probability distributions.
 +
 
 +
{| {{prettytable}}
 +
!Example!!Regular expression!!Interpretation
 +
|----
 +
| 12 000 ||# # || 12000. Text is interpreted as number if space removal makes it a number.
 +
|----
 +
| -14,23 || -# || -14.23. Minus in the beginning of entry is interpreted as minus, not a sign for a range.
 +
|----
 +
| 50 - 125 ||# - # ||Uniform distribution between 50 and 125.
 +
|----
 +
| < 4 || < # || Uniform distribution between 0 and 4.
 +
|----
 +
| -12 345 - -23.56 || -# - -#|| Uniform distribution between -12345 and -23.56.
 +
|----
 +
| 1 - 150 ||# - # || Loguniform distribution between 1 and 150 (Loguniformity is assumed if the ratio of upper to lower is > 100)
 +
|----
 +
| 3.1 ± 1.2 or 3.1 +- 1.2||# ± # or # +- # ||Normal distribution with mean 3.1 and SD 1.2
 +
|----
 +
| 2.4 (1.8 - 3.0) || # (# - #) ||Normal distribution with mean 2.4 and 95 % confidence interval from 1.8 to 3.0
 +
|----
 +
| 2.4 (2.0 - 3.2) || # (# - #) ||Lognormal distribution with mean 2.4 and 95 % confidence interval from 2.0 to 3.0. Lognormality is assumed if the difference from mean to upper limit is => 50 % greater than from mean to lower limit.
 +
|----
 +
| 0:0.5:1 || #:#:# ||Triangular distribution. Inputs are always sorted so order of arguments doesn't matter.
 +
|}
 +
 
 +
{{comment|# |This would be nice as well: 2;4;7: Each entry (2, 4, and 7 in this case) are equally likely to occur. Entries can also be text.|--[[User:Jouni|Jouni]] 19:45, 24 January 2013 (EET)}}
  
 
==Code==
 
==Code==
<rcode
 
name="answer"
 
label="Initiate functions"
 
graphics="1"
 
>
 
# INTERPRET ################### interpret takes a vector and makes a data.frame out of it (to be used in e.g. make.ovariable).
 
### It also changes abbreviations into probability samples.
 
interpret <- function(data) {
 
sample <- NULL
 
if(is.vector(data)) {data <- data.frame(Result = data)}
 
if("Iter" %in% colnames(data)) {
 
out <- data}
 
else {
 
if(!"Result" %in% colnames(data)) {cat("There MUST be an observation column named 'Result'.\n")}
 
test <- !is.na(as.numeric(as.character(data$Result)))
 
 
for(i in 1:nrow(data)) {
 
if(test[i]) {
 
sample <- c(sample, rep(as.numeric(as.character(data[i, "Result"])), n))
 
} else {
 
samplingguide <- as.numeric(strsplit(gsub(" ", "", data[i, "Result"]), "-")[[1]])
 
if(is.na(samplingguide[1]) | is.na(samplingguide[2])) {
 
sample <- c(sample, rep(data[i, "Result"], n))
 
} else {
 
sample <- c(sample, runif(n, samplingguide[1], samplingguide[2]))
 
}
 
}
 
}
 
 
out <- as.data.frame(array(1:(n*nrow(data)*(ncol(data)+1)), dim = c(n*nrow(data), ncol(data) + 1)))
 
colnames(out) <- c("Iter", colnames(data))
 
 
for(i in colnames(data)) {
 
out[i] <- rep(data[, i], each = n)
 
}
 
out$Iter <- 1:n
 
out$Result <- sample
 
 
}
 
return(out)
 
}
 
  
</rcode>
+
https://www.opasnet.org/svn/opasnet_utils/trunk/R/Interpret.r
  
 
==See also==
 
==See also==
  
* [[OpasnetBaseUtils]]
+
* A previous version of this code was called [http://en.opasnet.org/en-opwiki/index.php?title=Input.interp&oldid=25631 Input.interp]
 
* [[Object-oriented programming in Opasnet]]
 
* [[Object-oriented programming in Opasnet]]
 
* [[Opasnet (R library)]]
 
* [[Opasnet (R library)]]

Latest revision as of 17:45, 24 January 2013



Description

Interpret takes a vector or data.frame as argument. And returns a data.frame with certain textual inputs interpreted as probability distributions.

Example Regular expression Interpretation
12 000 # # 12000. Text is interpreted as number if space removal makes it a number.
-14,23 -# -14.23. Minus in the beginning of entry is interpreted as minus, not a sign for a range.
50 - 125 # - # Uniform distribution between 50 and 125.
< 4 < # Uniform distribution between 0 and 4.
-12 345 - -23.56 -# - -# Uniform distribution between -12345 and -23.56.
1 - 150 # - # Loguniform distribution between 1 and 150 (Loguniformity is assumed if the ratio of upper to lower is > 100)
3.1 ± 1.2 or 3.1 +- 1.2 # ± # or # +- # Normal distribution with mean 3.1 and SD 1.2
2.4 (1.8 - 3.0) # (# - #) Normal distribution with mean 2.4 and 95 % confidence interval from 1.8 to 3.0
2.4 (2.0 - 3.2) # (# - #) Lognormal distribution with mean 2.4 and 95 % confidence interval from 2.0 to 3.0. Lognormality is assumed if the difference from mean to upper limit is => 50 % greater than from mean to lower limit.
0:0.5:1 #:#:# Triangular distribution. Inputs are always sorted so order of arguments doesn't matter.

--# : This would be nice as well: 2;4;7: Each entry (2, 4, and 7 in this case) are equally likely to occur. Entries can also be text. --Jouni 19:45, 24 January 2013 (EET)

Code

https://www.opasnet.org/svn/opasnet_utils/trunk/R/Interpret.r

See also