Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
(Answer: MyPlotKML moved here)
(Answer: ovaMatrixProduct added)
Line 375: Line 375:
 
}
 
}
  
objects.store(ograph, fillna, collapsemarg, MyPointKML, ova2spat, MyRmap, MyPlotKML)
+
###### ovaMatrixProduct is a matrix multiplication (%*%) for ovariables.
  
cat("The following objects are stored: ograph, fillna, collapsemarg, MyPointKML, ova2spat, MyRmap, MyPlotKML.\n")
+
ovaMatrixProduct <- function(
 +
e1, # The first ovariable in the multiplication.
 +
e2, # The second ovariable in the multiplication.
 +
matrind.a, # The index names used in the matrix (row, column) for the first ovariable. Character vector, length 2.
 +
matrind.b # The index names used in the matrix (row, column) for the second ovariable.
 +
) {
 +
#matrind.a <- c("A", "B")
 +
#matrind.b <- c("B", "C")
 +
 
 +
#Take note of names and marginals
 +
#Merge the two ovariables
 +
#Make a data frame with all non-matrix indices, unique rows.
 +
#Make a for loop and go through non-matrix index rows one by one.
 +
## Slice the merged ovariable and take the result columns and the matrix indices for each original ovariable.
 +
## Reshape the two new data.frames to wide format and convert to matrices.
 +
## Make the matrix product.
 +
## Convert back to data frame.
 +
## Append to previous result data.frame together with non-matrix indices.
 +
# Return a new ovariable.
 +
 
 +
matricise <- function( # Produce a matrix of the key data from a data.frame.
 +
x, # data.frame with the data for making a matrix.
 +
matrind, # Index names in x for the matrix.
 +
name # Name of the result column in x.
 +
) {
 +
out <- unique(x[c(matrind, name)])
 +
 
 +
out <- reshape(out, idvar = matrind[1], timevar = matrind[2], direction = "wide")
 +
colnames(out) <- gsub(paste(name, ".", sep = ""), "", colnames(out))
 +
rownames(out) <- out[[1]]
 +
out[[1]] <- NULL
 +
out <- as.matrix(out)
 +
return(out)
 +
}
 +
 
 +
temp <- merge(e1, e2) # Combine ovariables to make all indices match in the future calculations.
 +
 
 +
# Create a data.frame that contains unique rows consisting of other marginal indices than those needed in the matrices.
 +
 
 +
temp2 <- c(colnames(e1@output)[e1@marginal], colnames(e2@output)[e2@marginal])
 +
temp2 <- temp2[! temp2 %in% c(matrind.a, matrind.b)]
 +
nonmatrixind <- unique(temp@output[temp2])
 +
 
 +
# Create an output data.frame that collects a matrix product result for each rows of nonmatrixind.
 +
 
 +
out <- data.frame()
 +
 
 +
for(i in 1:nrow(nonmatrixind))
 +
{
 +
temp3 <- merge(temp@output, nonmatrixind[i, , drop = FALSE])
 +
 
 +
# Create matrices for ovariables a and b from data.frame temp3.
 +
 +
tempa <- matricise(temp3, matrind.a, comment(result(e1)))
 +
tempb <- matricise(temp3, matrind.b, comment(result(e2)))
 +
 
 +
matr <- tempa %*% tempb # The actual matrix product.
 +
 
 +
# Convert matrix back to data.frame and combine with other results.
 +
 +
matr <- as.data.frame(as.table(matr))
 +
colnames(matr) <- c(matrind.a[1], matrind.b[2], "Result")
 +
matr <- cbind(nonmatrixind[i, , drop = FALSE], matr)
 +
out <- rbind(out, matr)
 +
}
 +
 
 +
out <- new("ovariable", output = out)
 +
 +
out <- CheckMarginals(out, deps = list(e1, e2), verbose = FALSE)
 +
 
 +
return(out)
 +
}
 +
 
 +
objects.store(ograph, fillna, collapsemarg, MyPointKML, ova2spat, MyRmap, MyPlotKML, ovaMatrixProduct)
 +
 
 +
cat("The following objects are stored: ograph, fillna, collapsemarg, MyPointKML, ova2spat, MyRmap, MyPlotKML, ovaMatrixProduct.\n")
  
 
</rcode>
 
</rcode>

Revision as of 17:00, 27 December 2013



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

+ Show code

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>