Difference between revisions of "Input.interp"

From Testiwiki
Jump to: navigation, search
(Answer)
(Answer)
Line 33: Line 33:
 
|----
 
|----
 
| 2;4;7 || || Each entry (2, 4, and 7 in this case) are equally likely to occur. Entries can also be text.||
 
| 2;4;7 || || Each entry (2, 4, and 7 in this case) are equally likely to occur. Entries can also be text.||
 +
|----
 +
| (0,1,0) || (#,#,#) ||Triangular distribution||
 
|----
 
|----
 
| * (in index, or explanatory, columns) || || The result applies to all locations of this index.|| With merge() function, this column is not used as a criterion when these rows are merged.
 
| * (in index, or explanatory, columns) || || The result applies to all locations of this index.|| With merge() function, this column is not used as a criterion when these rows are merged.

Revision as of 10:48, 13 June 2012


input.interp is an R function that interprets model inputs from a user-friendly format into explicit and exact mathematical format. The purpose is to make it easy for a user to give input without a need to worry about technical modelling details.

Question

What should be a list of important user input formats, and how should they be interpreted?

Answer

The basic feature is that if a text string can be converted to a meaningful numeric object, it will be. This function can be used when data is downloaded from Opasnet Base: if Result.Text contains this kind of numeric information, it is converted to numbers and fused with Result.

n is the number of iterations in the model. # is any numeric character in the text string.

Example Regular expression Interpretation Output in R
12 000 # # 12000. Text is interpreted as number if space removal makes it a number. as.numeric(gsub(" ", "", Result.text))
-14,23 -#  -14.23. Minus in the beginning of entry is interpreted as minus, not a sign for a range. # : Not needed. See above. --Jouni 15:30, 16 April 2012 (EEST)
50 - 125 # - # Uniform distribution between 50 and 125 data.frame(iter=1:n, result=runif(n,50,125))
-12 345 - -23.56 -# - -# Uniform distribution between -12345 and -23.56.
1 - 50 # - # Loguniform distribution between 1 and 50 (Lognormality 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 data.frame(iter=1:n, result=rnorm(n,3.1,1.2))
2.4 (1.8 - 3.0) # (# - #) Normal distribution with mean 2.4 and 95 % confidence interval from 1.8 to 3.0 data.frame(iter=1:n, result=rnorm(n,2.4,(3.0-1.8)/2/1.96))
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.
2;4;7 Each entry (2, 4, and 7 in this case) are equally likely to occur. Entries can also be text.
(0,1,0) (#,#,#) Triangular distribution
* (in index, or explanatory, columns) The result applies to all locations of this index. With merge() function, this column is not used as a criterion when these rows are merged.

How to actually make this happen in R?

  1. Make a temporary result temp by removing all spaces from Result.Text. Columns: Indices,Result.Result.Text,temp (Indices contains all explanatory columns.)
  2. Replace all "," with "."
  3. Check if there are parentheses "()". If yes, assume that they contain 95 % CI.
  4. Check if there are ranges "#-#".
  5. Divide the rows of the data.frame into two new data.frames with the same list of columns (Indices,Result).
    • If temp is a syntactically correct distribution, take the row to data.frame A and replace Result with temp.
    • Otherwise, take the row to data.frame B and replace Result with Result.Text if that is not NA.
  6. Create a new data.frame with index Iter = 1:n.
  7. Make a random sample from each probability distribution in data.frame A using Iter.
  8. Merge the data.frame B with Iter.
  9. Join data.frames A and B with rbind(). Columns: Iter,Index,Result.

Rcode

+ Show code

--# : Koodi on vielä vaiheessa, ottaa character vectorin alkiot ja antaa tulkinnat listana. Virhetoleranssi hyvin huono. --Teemu R 03:09, 24 January 2012 (EET) --# : Data.framelle oma wrapperi. Testaamaton, ottaa ja antaa data.framen. --Teemu R 14:15, 24 January 2012 (EET)

# Koodi näyttää rakenteellisesti monimutkaiselta. Olisiko helpompaa yrittää tällaista lähestymistä: 1) regexpressioneilla selvitetään, mitä tyyppiä sisältö on (normaalijakauma, tasajakauma, luokittelumuuttuja, ...). 2) Erotetaan stringistä lukuarvot vektoriksi. 3) Vektorin lukuja käytetään parametreina jakaumafunktioissa. 4) Sämplätyt arvot pistetään tulosvektorin perään pötköksi. 5) Tulosvektori liitetään data.framen Result-kentäksi. Katso esimerkki Object-oriented programming in Opasnet, jossa on käytetty tätä taktiikkaa mutta on sallittu vain yksi syötetyyppi. --Jouni 20:35, 5 April 2012 (EEST)
# : Juuri tällä periaatteella koodi toimii, mutta koska erilaisia konfiguraatioita on useita (normaalijakaumaa kuvailevassa stringissä voi olla 1-3 "-" merkkiä), niin lopputulos on monimutkainen. Muistaakseni tämä funktio toimi ihan hyvin ellei laita parametreja esim. väärään järjestykseen (isompi raja ensin, molemmat rajat yli keskiarvon jne.) tai jollain muulla tapaa pilaa noilla regexpeillä irti saatavia tiedonpalasia. --Teemu R 19:57, 26 April 2012 (EEST)

Independent or not?

Specific character string can be converted to distributions. There are two ways to do this, with parameter independent = TRUE in the first case, and FALSE in the second one:

  1. The character strings are interpreted one row at a time, and each row is made an independent distribution.
  2. The character strings are treated as a factor. The levels of the factor are converted to distributions. Therefore, all rows that have the same character string will have the an identical (not independent) distribution.