Description Fields Methods See Also Examples
This class stores a number of simulations each of which contains the same data as an IncidenceMatrix.
This is the full array. For extensibility, it cannot be written to directly and must be modified through methods.
A list of metadata associated with the cells of the data.
The names of columns in the data.
A list of metadata associated with the columns of the data.
The data associated with each dimension of the array.
The size of the array.
The names of dimensions of the data.
This is the matrix. For extensibility, it cannot be written to directly and must be modified through methods.
Any data not part of the main data structure.
The number of columns in the data.
The number of dimensions of the array.
The number of rows in the data
The number of simulations in self$simulaions
The names of rows in the data.
A list of metadata associated with the rows of the data.
An IncidenceMatrix sampled from the simulations.
The array of simulations. This is another name for 'arr'.
This method must be extended. This function adds empty columns to the right side of the data.
columns | - | The number of columns to add. |
mutate | - | Whether to change the original instance, or create a new one. If FALSE, the instance performing the method will be left unchanged, and a modified copy will be returned. If true, then the instance will modify itself and return nothing. |
If mutate=FALSE
, a clone of this object will run the method and be returned. Otherwise, there is no return.
This method must be extended. Add error of a particular type to the data.
type | - | What sort of error to add. |
This method must be extended. This function adds empty rows to the right side of the data.
rows | - | The number of rows to add. |
mutate | - | Whether to change the original instance, or create a new one. If FALSE, the instance performing the method will be left unchanged, and a modified copy will be returned. If true, then the instance will modify itself and return nothing. |
If mutate=FALSE
, a clone of this object will run the method and be returned. Otherwise, there is no return.
A function for debugging the methods of this class. It calls the browser command. In order for methods to opt into to debugging, they need to implement the following code at the beginning: if(<method_name> %in% private$.debug){browser()}. This method exists, because the debugger is not always intuitive when it comes to debugging R6 methods.
string | - | The name(s) of methods to debug as a character vector |
This method must be extended. This function replaces the matrix value at column i with the difference. between the values at columns i and (i-lag).
lag | - | How far back to diff. Defaults to 1. |
mutate | - | Whether to change the original instance, or create a new one. If FALSE, the instance performing the method will be left unchanged, and a modified copy will be returned. If true, then the instance will modify itself and return nothing. |
If mutate=FALSE
, a clone of this object will run the method and be returned. Otherwise, there is no return.
This method must be extended. Select the first k slices of the data in dimension direction.
k | - | The number of slices to keep. |
direction | - | The dimension to take a subset of. 1 for row, 2 for column. |
mutate | - | Whether to change the original instance, or create a new one. If FALSE, the instance performing the method will be left unchanged, and a modified copy will be returned. If true, then the instance will modify itself and return nothing. |
If mutate=FALSE
, a clone of this object will run the method and be returned. Otherwise, there is no return.
This function should be extended. Create a new instance of this class.
... | - | This function should take in any arguments just in case. |
This method must be extended. This function replaces the current matrix with a new matrix with one column for every column, and a row for every row/index combination. The column corresponding to the row and index will have the value of the original matrix in the same row, but index columns previous. This shift will introduce NAs where it passes off the end of the matrix.
indices | - | A sequence of lags to use as part of the data. Note that unless this list contains 0 , the data will all be shifted back by at least one year. |
mutate | - | Whether to change the original instance, or create a new one. If FALSE, the instance performing the method will be left unchanged, and a modified copy will be returned. If true, then the instance will modify itself and return nothing. |
If mutate=FALSE
, a clone of this object will run the method and be returned. Otherwise, there is no return.
This method must be extended. This function is a way to modify the data as though it were a matrix. self$mutate(row,col,data)
is equivalent to self$mat[row,col] <- data
.
rows | - | Which rows to modify. These can be numeric or names. |
cols | - | Which cols to modify. These can be numeric or names. |
data | - | The data to change the chosen values to. It needs to be the right shape. |
This method must be extended. This function rescales each element of our object according to f
f | - | a function which takes in a number and outputs a rescaled version of that number |
mutate | - | Whether to change the original instance, or create a new one. If FALSE, the instance performing the method will be left unchanged, and a modified copy will be returned. If true, then the instance will modify itself and return nothing. |
If mutate=FALSE
, a clone of this object will run the method and be returned. Otherwise, there is no return.
This method must be extended. Select only some of the simulations.
simulations | - | An index or list of column indices which simulations to keep. |
mutate | - | Whether to change the original instance, or create a new one. If FALSE, the instance performing the method will be left unchanged, and a modified copy will be returned. If true, then the instance will modify itself and return nothing. |
If mutate=FALSE
, a clone of this object will run the method and be returned. Otherwise, there is no return.
This method must be extended. Select the data corresponding to the rows rows
and the columns columns
. rows
and columns
can be either numeric or named indices.
rows | - | An row index or list of row indices which can be either numeric or named. |
cols | - | An column index or list of column indices which can be either numeric or named. |
mutate | - | Whether to change the original instance, or create a new one. If FALSE, the instance performing the method will be left unchanged, and a modified copy will be returned. If true, then the instance will modify itself and return nothing. |
If mutate=FALSE
, a clone of this object will run the method and be returned. Otherwise, there is no return.
This method must be extended. Apply a function FUNC to every simulation elementwise.
FUNC | - | a function which should be applied to every simulation. It should reduce each simulation to a single number. |
A MatrixData where return$mat
is FUNC
applied to every simulation.
This method must be extended. Select the last k
slices of the data in dimension direction
.
k | - | The number of slices to keep. |
direction | - | The dimension to take a subset of. 1 for row, 2 for column. |
mutate | - | Whether to change the original instance, or create a new one. If FALSE, the instance performing the method will be left unchanged, and a modified copy will be returned. If true, then the instance will modify itself and return nothing. |
If mutate=FALSE
, a clone of this object will run the method and be returned. Otherwise, there is no return.
A function for ceasing to debug methods. Normally a method will call the browser command every time it is run. This command will stop it from doing so.
string | - | The name(s) of the methods to stop debugging. |
Inherits from : ArrayData
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | SimulatedIncidenceMatrix <- R6Class(
classname = "SimulatedIncidenceMatrix",
inherit = AbstractSimulatedIncidenceMatrix,
private = list(
.ndim = 3,
ncore = 1,
.sample = 1,
parallelEnvironment = NULL
),
public = list(
initialize = function(data=MatrixData$new(),nsim=1){
if('AbstractSimulatedIncidenceMatrix' %in% class(data)){
private$.arr= data$simulations
private$.metaData = data$metaData
private$.dimData = data$dimData
private$.dnames = data$dnames
private$.dims = data$dims
private$.ndim = length(self$dims)
return()
}
else if('list' %in% class(data)){
if('MatrixData' %in% class(data[[1]])){
if((!missing(nsim)) && (nsim != length(data))){
stop("nsim is not used for list data.")
}
private$.dims = c(data[[1]]$nrow,data[[1]]$ncol,length(data))
private$.arr = array(NA,c(self$nrow,self$ncol,self$nsim))
if(self$nsim > 0){
for(i in 1: self$nsim){
private$.arr[,,i] = data[[i]]$mat
}
}
data = data[[1]]
private$.metaData = data$metaData
private$.dimData = list(data$rowData,data$colData)
if(!is.null(data$rnames)){
if(!is.null(data$cnames)){
private$.dnames = list(data$rnames,data$cnames,NULL)
} else{
private$.dnames = list(data$rnames,NULL,NULL)
}
} else if(!is.null(data$cnames)){
private$.dnames = list(NULL,data$cnames,NULL)
} else {
private$.dnames = NULL
}
dimnames(private$.arr) = private$.dnames
private$.dims = c(data$nrow,data$ncol,nsim)
private$.ndim = 3
return()
}
else{
stop("Not yet implemented")
}
} else if('MatrixData' %in% class(data)){
private$.arr= array(data$mat,c(data$nrow,data$ncol,nsim))
private$.metaData = data$metaData
private$.dimData = list(data$rowData,data$colData)
if(!is.null(data$rnames)){
if(!is.null(data$cnames)){
private$.dnames = list(data$rnames,data$cnames,NULL)
} else{
private$.dnames = list(data$rnames,NULL,NULL)
}
} else if(!is.null(data$cnames)){
private$.dnames = list(NULL,data$cnames,NULL)
} else {
private$.dnames = NULL
}
dimnames(private$.arr) = private$.dnames
private$.dims = c(data$nrow,data$ncol,nsim)
private$.ndim = 3
return()
}
else{
stop("Input data is not a valid type to make a SimulatedIncidenceMatrix")
}
stop("This is currently broken.")
rownames(private$.arr) <- rownames(data[[1]]$mat)
colnames(private$.arr) <- colnames(data[[1]]$mat)
private$.dimData = list(data$rowData,data$colData,NULL)
private$.metaData = data$metaData
if(!is.null(data$rnames)){
if(!is.null(data$cnames)){
private$.dnames = list(data$rnames,data$cnames,NULL)
} else{
private$.dnames = list(data$rnames,NULL,NULL)
}
} else if(!is.null(data$cnames)){
private$.dnames = list(NULL,data$cnames,NULL)
} else {
private$.dnames = NULL
}
private$.dims = c(data$nrow,data$ncol,nsim)
},
mean = function(){
"Compute the mean across simulations"
if('mean' %in% private$.debug){
browser()
}
return(IncidenceMatrix$new(apply(self$simulations,c(1,2),mean)))
},
median = function(){
if('median' %in% private$.debug){
browser()
}
return(IncidenceMatrix$new(apply(self$arr,c(1,2),median)))
},
addError = function(type,rows,cols,mutate = TRUE){
if('addError' %in% private$.debug){
browser()
}
if(missing(rows)){
rows = 1:self$nrow
}
if(missing(cols)){
cols = 1:self$ncol
}
if(type=='Poisson'){
private$.arr[rows,cols,] =
rpois(length(rows)*length(cols)*self$nsim,self$arr[rows,cols,])
} else{
stop("Not yet implemented")
}
},
subsample = function(simulations,mutate=TRUE){
if('subsample' %in% private$.debug){
browser()
}
if(!mutate){
rc = self$clone(TRUE)
rc$subsample(simulations,mutate=TRUE)
return(rc)
}
if(
(min(simulations) < 0) ||
(max(simulations) > self$nsim) ||
any(round(simulations) != simulations)
){
stop("simulations out of bounds.")
}
private$.dims[3] = length(simulations)
private$.arr = self$arr[,,simulations]
},
subset = function(rows,cols,mutate=TRUE){
if('subset' %in% private$.debug){
browser()
}
if(!mutate){
rc = self$clone(TRUE)
rc$subset(rows,cols,mutate=TRUE)
return(rc)
}
if(missing(rows) && missing(cols)){
return()
}
else if(missing(rows)){
rows = 1:self$nrow
}
else if(missing(cols)){
cols = 1:self$ncol
}
private$.arr = self$arr[rows,cols,,drop=FALSE]
private$.dims = c(nrow(self$arr),ncol(self$arr),self$nsim)
private$.dnames = dimnames(self$arr)
if(length(self$rowData)>0){
if(length(self$colData) > 0){
self$dimData = list(
lapply(self$rowData,function(x){x[rows,drop=FALSE]}),
lapply(self$colData,function(x){x[cols,drop=FALSE]})
)
} else {
self$rowData <- lapply(self$rowData,function(x){x[rows,drop=FALSE]})
}
} else if(length(self$colData)>0){
self$colData <- lapply(self$colData,function(x){x[cols,drop=FALSE]})
}
},
head = function(k,direction=2,mutate=FALSE){
if('head' %in% private$.debug){
browser()
}
if(k>dim(self$arr)[[direction]]){
stop("The size of the head is too large.")
}
indices = 1:k
if(direction==1){
private$.arr = self$arr[indices,,,drop=FALSE]
if(length(self$rowData)>0){
private$.dimData[[1]] =
lapply(self$rowData,function(x){x[indices,drop=FALSE]})
}
}
else if(direction==2){
private$.arr = self$arr[,indices,,drop=FALSE]
if(length(self$colData)>0){
private$.dimData[[2]] =
lapply(self$colData,function(x){x[indices,drop=FALSE]})
}
}
else{
stop("This direction is not allowed.")
}
private$.dims = dim(self$arr)
private$.dnames = dimnames(self$arr)
},
tail = function(k,direction=2){
if('tail' %in% private$.debug){
browser()
}
if(k>dim(self$arr)[[direction]]){
stop("The size of the tail is too large.")
}
indices = (dim(self$arr)[[direction]]-k+1):dim(self$arr)[[direction]]
if(direction==1){
private$.arr = self$arr[indices,,,drop=FALSE]
private$.dims = dim(self$arr)
if(length(self$rowData)>0){
self$rowData = lapply(self$rowData,function(x){x[indices,drop=FALSE]})
}
}
else if(direction==2){
private$.arr = self$arr[,indices,,drop=FALSE]
private$.dims = dim(self$arr)
if(length(self$colData)>0){
self$colData = lapply(self$colData,function(x){x[indices,drop=FALSE]})
}
}
else{
stop("This direction is not allowed.")
}
private$.dims = dim(self$arr)
private$.ndim = length(self$dims)
private$.dnames = dimnames(self$arr)
},
lag = function(indices,mutate = TRUE,na.rm=FALSE){
if('lag' %in% private$.debug){
browser()
}
if(mutate==FALSE){
tmp = self$clone(TRUE)
tmp$lag(indices=indices,mutate=TRUE)
return(tmp)
}
if((1+max(indices)) > self$ncol){
stop("We cannot go further back than the start of the matrix")
}
numLags = length(indices)
if(is.null(rownames(self$arr))){
rownames(private$.arr) = 1:(dim(self$arr)[[1]])
}
rownames = replicate(numLags,rownames(self$arr))
colnames = colnames(self$arr)
private$.arr <- array(self$arr,c(dim(self$arr),numLags))
if(numLags <= 0){
stop("indices must be nonempty for the calculation of lags to make sense.")
}
for(lag in 1:numLags){
private$.arr[,(1+indices[[lag]]):self$ncol,,lag] <-
self$arr[,1:(self$ncol-indices[[lag]]),,lag]
if(indices[[lag]] > 0){
private$.arr[,1:(indices[[lag]]),,lag] = NA
}
}
private$.arr = aperm(self$arr,c(1,4,2,3))
private$.arr = array(self$arr,c(self$nrow*numLags,self$ncol,self$nsim))
lagnames = t(replicate(self$nrow,paste('L',indices,sep='')))
rownames(private$.arr) <-
as.character(
array(paste(lagnames,"R",rownames,sep=''),numLags*self$nrow)
)
colnames(private$.arr) <- colnames
private$.dims[[1]] = self$nrow * numLags
if(!is.null(dimnames(self$arr))){
private$.dnames = dimnames(self$arr)
}
if(length(self$rowData) > 0){
self$rowData <- lapply(
self$rowData,
function(x){
c(unlist(recursive=FALSE,lapply(1:numLags,function(y){x})))
}
)
}
if(na.rm==TRUE){
self$subset(cols=!apply(self$arr,2,function(x){any(is.na(x))}))
}
},
addRows = function(rows){
if('addRows' %in% private$.debug){
browser()
}
if(rows == 0){
return()
}
abind(self$arr,array(NA,c(rows,self$ncol,self$nsim)),along=1) ->
private$.arr
private$.dims[[1]] = nrow(self$arr)
private$.dnames = dimnames(self$arr)
if(length(self$rowData) > 0){
self$rowData = lapply(self$rowData,function(x){c(x,replicate(rows,NA))})
}
},
addColumns = function(columns){
"This function adds columns to the data."
"@param columns The number of columns to add."
if('addColumns' %in% private$.debug){
browser()
}
if(columns == 0){
return()
}
abind(private$.arr,array(NA,c(self$nrow,columns,self$nsim)),along=2) ->
private$.arr
private$.dims[2]= ncol(self$arr)
private$.dnames = dimnames(private$.arr)
if(length(self$colData) > 0){
self$colData = lapply(self$colData,function(x){c(x,replicate(columns,NA))})
}
},
scale = function(f,mutate=TRUE){
if('scale' %in% private$.debug){
browser()
}
if(!mutate){
tmp = self$clone(TRUE)
tmp$scale(f=f,mutate=TRUE)
return(tmp)
}
private$.arr[] = f(private$.arr[])
},
diff = function(lag = 1,mutate=TRUE){
if('diff' %in% private$.debug){
browser()
}
if(!mutate){
tmp = self$clone(TRUE)
tmp$diff(lag=lag,mutate=TRUE)
return(tmp)
}
if(lag == 0){
if(!is.null(rownames(private$.arr))){
rownames(private$.arr) = paste("D",lag,"R",rownames(private$.arr),sep='')
}
private$.dnames = dimnames(private$.arr)
return()
}
if(lag < 0){
stop("lag must be non-negative.")
}
rn = rownames(private$.arr)
private$.arr <-
self$simulations- self$lag(indices=lag,mutate=FALSE)$simulations
if(!is.null(rn)){
rownames(private$.arr) = paste("D",lag,"R",rownames(private$.arr),sep='')
}
private$.dnames = dimnames(private$.arr)
},
mutate = function(rows,cols,data){
if('mutate' %in% private$.debug){
browser()
}
tmpdata = data
tmpdata = array(data,self$dims)
data = as.array(data)
if(missing(rows)){
rows = 1:self$nrow
if(!(is.null(self$cnames) || is.null(colnames(data)))){
private$.dnames[[2]][cols] = colnames(data)
colnames(private$.arr) = self$cnames
}
}
if(missing(cols)){
cols = 1:self$ncol
if(!(is.null(self$rnames) || is.null(rownames(data)))){
private$.dnames[[1]][rows] = rownames(data)
rownames(private$.arr) = self$rnames
}
}
if(is.null(dim(data))){
stop("Not yet implemented for non-matrixlike objects")
}
if(length(dim(data)) > 3){
stop("There are too many dimensions in data.")
}
if(length(dim(data)) == 3){
if(dim(data)[[3]] == self$nsim){
private$.arr[rows,cols,] = data
} else if(dim(data)[[3]] == 1){
private$.arr[rows,cols,] = replicate(self$nsim,data)
}
}
else{
private$.arr[rows,cols,] = replicate(self$nsim,data)
}
},
summarize = function(FUNC,...){
if('apply' %in% private$.debug){
browser()
}
return(IncidenceMatrix$new(
data=apply(private$.arr,c(1,2),FUNC),
rowData=self$rowData,
colData=self$colData,
metaData=self$metaData)
)
}
),
active = list(
sample = function(value){
"Randomly extract a simulation"
if("sample" %in% private$.debug){
browser()
}
return = FALSE
if(missing(value)){
if(self$nsim < 1){
return(private$.arr)
}
value = sample(self$nsim,1)
return = TRUE
}
if(floor(value) != value){
stop("sample must be an integer.")
}
private$.sample = value
if(return){
return(IncidenceMatrix$new(self))
}
},
mat = function(value){
private$.mat = adrop(private$.arr[,,private$.sample,drop=FALSE],3)
rownames(private$.mat) = rownames(private$.arr)
colnames(private$.mat) = colnames(private$.arr)
return(private$.mat)
},
simulations = function(value){
if(missing(value)){
return(private$.arr)
}
stop("Do not write directly to the simulations")
}
)
)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.