procmod.frame.R 8.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#' @include internals.R
NULL

#' Internal function repeating a matrix.
#'
#' @description repeats several times the rows of a matrix
#'              to create a new matrix with more rows. The
#'              final row count must be a multiple of the
#'              initial row count
#'
#' @param x  The matrix to replicate
#' @param length.out  an interger value specifying the number of row
#'                    of the returned matrix
#'
#' @return a new matrix with the same number of columns but with `length.out`
#'         rows.
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#'

.rep_matrix = function(x,length.out) {
  N = nrow(x)

25
  if ((length.out %% N != 0L))
26 27 28 29 30 31 32 33 34 35 36 37 38
    stop(sprintf("The size of the longest object (%d) is not a multiple of the size of the shortest (%d)",
                 nrows,N),
         domain = NA)

  rep=x
  while (nrow(rep) < length.out)
    rep = rbind(rep,x)

  return(rep)
}

.procmod_coerce_value = function(x,nrows=0) {
  xi <- if (is.data.frame(x))
39
            as.matrix(x)
40
        else if (is.matrix(x) || inherits(x,'dist'))
41 42 43
            x
        else
            as.matrix(x)
44

45
  if (! (is.matrix(xi) || inherits(x,'dist')))
46
    stop('Value cannot be coerced to a Matrix')
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
  if (is.matrix(xi)) {
    dxi=dim(xi)
    rownamesi = rownames(xi)
    colnamesi = colnames(xi)
    
    xi  = as.numeric(xi)
    
    dim(xi)=dxi
    rownames(xi) = rownamesi
    colnames(xi) = colnamesi
    
    N = nrow(xi)

    if (nrows > 0L && N < nrows) {
      if (N > 0L && (nrows %% N == 0L))
        xi <- .rep_matrix(xi, length.out = nrows)
      else stop(sprintf(ngettext(N, "replacement has %d row, data has %d",
                                 "replacement has %d rows, data has %d"),
                        N, nrows),
                domain = NA)
    }
  }
  else {
    N = attr(xi,"Size")
    if (nrows > 0L && N != nrows) {
      stop(sprintf(ngettext(N, "replacement has %d row, data has %d",
                            "replacement has %d rows, data has %d"),
                   N, nrows),
           domain = NA)
    }
  }
79

80 81
  return(xi)
}
82

83 84 85 86 87 88
.siteNames = function(x) {
  if (inherits(x,'dist'))
    attr(x,'Labels')
  else
    rownames(x)
}
89

90 91 92 93
`.siteNames<-` = function(x,value) {
  if (inherits(x,'dist')) {
    stopifnot(is.null(value) || length(value)==attr(x,'Size'))
    attr(x,'Labels')=value
94
  }
95 96 97 98 99
  else
    rownames(x)=value
  
  x
}
100

101 102 103 104 105 106

.siteCount = function(x) {
  if (inherits(x,'dist'))
    attr(x,'Size')
  else
    nrow(x)
107 108
}

109 110 111 112 113 114 115 116 117 118

.siteSelect = function(x,select) {
  if (inherits(x,'dist')) 
    as.dist(as.matrix(x)[select,select,drop=FALSE])
  else
    x[select,,drop=FALSE]
  
}


119 120 121 122 123 124 125 126 127
#' Build a procmod.frame data structure.
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
procmod.frame = function(...,
                         row.names = NULL,
                         check.rows = FALSE,
                         check.names = TRUE,
128
                         reorder.rows = FALSE)
129
{
130
  has.row.names    = ! missing(row.names)
131 132 133 134 135 136

  varnames = dots.names(...)

  x <- list(...)
  n <- length(x)

137 138 139 140
#  message(x)
#  message(n)
#  message(row.names)

141
  if ((! has.row.names || is.null(row.names)) && n >= 1)
142
    row.names = .siteNames(x[[1]])
143

144
  nrows <- integer(n)
145
  value <- vector(mode = "list", length = n)
146

147 148 149 150 151 152 153 154 155 156
  names(value)=varnames

  types <- character(n)

  for (i in seq_len(n)) {
    if (i==1)
      xi <- .procmod_coerce_value(x[[i]])
    else
      xi <- .procmod_coerce_value(x[[i]],
                                  nrows = nrows[1])
157 158 159

    if (reorder.rows &&
        ! is.null(row.names) &&
160 161
        ! is.null(.siteNames(xi)))
      xi=.siteSelect(xi,row.names)
162

163
    nrows[i] <- .siteCount(xi)
164 165 166 167
    value[[i]]=xi
  }

  stopifnot(all(nrows[i]==nrows))
168
  # message(row.names," : ",length(row.names),",",nrows[i])
169 170
  if (length(row.names)==nrows[i]) {
    attr(value,"row.names")=row.names
171 172
    if (check.rows)
      for (i in seq_len(n)) {
173
        if (! all(row.names == .siteNames(value[[i]])))
174 175 176 177
          stop("Row names among matrices are not consistant")
      }
    else
      for (i in seq_len(n))
178
        .siteNames(value[[i]])=row.names
179
  }
180 181
  else
    for (i in seq_len(n))
182
      .siteNames(value[[i]])=NULL
183

184 185 186 187 188 189 190 191 192 193 194 195 196
  return(make_subS3Class(value, "procmod.frame"))
}


#' Check if an object is a Matrix Frame.
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
is.procmod.frame = function(x) {
  inherits(x, "procmod.frame")
}

197
#' Coerce to a ProcMod Frame.
198 199 200 201
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
202
as.procmod.frame = function(data,...) {
203 204 205 206 207 208 209 210
  UseMethod("as.procmod.frame",data)
}

#' Coerce to a ProcMod Frame.
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
211
as.procmod.frame.list = function(data,...) {
212 213
  data=c(data,list(...))
  do.call(procmod.frame,data)
214 215 216 217 218 219 220
}

#' Coerce to a ProcMod Frame.
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
221
as.procmod.frame.procmod.frame = function(data,...) {
222 223 224 225 226 227 228 229
  data
}

#' Coerce to a ProcMod Frame.
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
230
as.procmod.frame.array = function(data,...) {
231 232 233 234 235 236 237 238 239
  di = dim(data)
  stopifnot(length(di)==3)

  l = lapply(seq_len(di[3]),
             function(i) data[,,i])

  if (length(attr(data,"dimnames"))==3)
    names(l)=attr(data,"dimnames")[[3]]

240 241 242
  data=c(l,list(...))

  do.call(procmod.frame,l)
243 244
}

245 246 247 248 249 250 251 252 253
#' Coerce to a ProcMod Frame.
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
as.procmod.frame.pm = function(data,...) {
  vars.procmod(terms(data),data$model)
}

254 255 256 257 258 259
#' Dimensions of a Matrix Frame.
#'
#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
dim.procmod.frame = function(x)
260
  return(c(.siteCount(x[[1]]),length(x)))
261 262 263 264 265 266 267

#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
`[[<-.procmod.frame` = function (x, i, value)
{
  cl = class(x)
268
  nrows=.siteCount(x)
269 270 271 272
  class(x)="list"

  if (!is.null(value)) {
    value=.procmod_coerce_value(value,nrows)
273
    N <- .siteCount(value)
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

    if (N > nrows)
      stop(sprintf(ngettext(N, "replacement has %d row, data has %d",
                            "replacement has %d rows, data has %d"),
                   N, nrows),
           domain = NA)

    if (N < nrows)
      stop(sprintf(ngettext(N, "replacement has %d row, data has %d",
                            "replacement has %d rows, data has %d"),
                   N, nrows),
           domain = NA)

  }

289
  .siteNames(value)=attr(x,"row.names")
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

  x[[i]] <- value
  class(x)=cl

  return(x)
}

#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
`$<-.procmod.frame` = function (x, name, value) {
  x[[name]] <- value
  return(x)
}

#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
`[.procmod.frame` = function (x, i, j,
309
                              drop = TRUE)
310 311 312 313 314 315
{
  has.j    = !missing(j)
  has.i    = !missing(i)
  has.drop = !missing(drop)
  Narg = nargs() - 2 + (has.i | has.j | has.drop) - has.drop

316
  # message("Nargs = ",Narg," i:",has.i," j:",has.j," drop:",has.drop)
317 318 319 320

  if (!all(names(sys.call()) %in% c("", "drop")))
    warning("named arguments other than 'drop' are discouraged")

321

322
  if (! has.i && ! has.j)
323
      # Case 1 : X[]
324
      return(x)
325 326
  else if (! has.i && has.j) {
    # Case 2 : X[,j] ou x[i]
327
    # message('Case 2 : X[,j]')
328
      y <- as.list(x)[j]
329
  }
330 331
  else if ( has.i && Narg==1) {
    # Case 3 : X[,j] ou x[i]
332
    # message('Case 3 : X[i]')
333
    y <- as.list(x)[i]
334
  }
335 336
  else if ( has.i && !has.j && Narg>1) {
    # Case 4 : X[i,]
337
    # message('Case 4 : X[i,]')
338
    y = lapply(x, function(m) .siteSelect(m,i))
339 340
  }
  else if ( has.i && has.j ) {
341
    # message('Case 5 : X[i,j]')
342 343
    y = x[j,drop=FALSE]
    y = y[i,,drop=FALSE]
344 345
  }

346 347 348 349 350
  if (drop && length(y)==1L)
    y = y[[1]]
  else {
    y = make_subS3Class(y, "procmod.frame")
    attr(y,"row.names")=rownames(y[[1]])
351
  }
352 353

  return(y)
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
}

#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
subset.procmod.frame = function (x, subset, select, drop = FALSE, ...)
{
  r <- if (missing(subset))
    rep_len(TRUE, nrow(x))
  else {
    e <- substitute(subset)
    r <- eval(e, x, parent.frame())
    if (!is.logical(r))
      stop("'subset' must be logical")
    r & !is.na(r)
  }
  vars <- if (missing(select))
    TRUE
  else {
    nl <- as.list(seq_along(x))
    names(nl) <- names(x)
    eval(substitute(select), nl, parent.frame())
  }
  x[r, vars, drop = drop]
}

#' @author Eric Coissac
#' @author Christelle Gonindard-Melodelima
#' @export
as.list.procmod.frame = function(x, ...) {
  class(x)='list'
  return(x)
}
387