File: change_link.R

package info (click to toggle)
r-cran-mi 1.0-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,368 kB
  • sloc: sh: 13; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 2,911 bytes parent folder | download | duplicates (4)
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
# Part of the mi package for multiple imputation of missing data
# Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Trustees of Columbia University
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

## these change the link function used in the imputation process

setMethod("change_link", signature(data = "missing", y = "missing_variable", to = "character"), def = 
  function(y, to) {
    fam <- do.call(y@family$family, args = list(link = to))
    y@family <- fam
    validObject(y, complete = TRUE)
    return(y)
  })

setMethod("change_link", signature(data = "missing", y = "missing_variable", to = "missing"), def = 
  function(y, to) {
    cat("Likely choices include:", y@known_links, sep = "\n")
    return(invisible(NULL))
  })

setMethod("change_link", signature(data = "missing_data.frame", y = "character", to = "character"), def =
  function(data, y, to) {
    if(length(to) == 1) to <- rep(to, length(y))
    else if(length(to) != length(y)) stop("'y' and 'to' must have the same length")
    if(all(y %in% names(getClass("missing_variable")@subclasses))) {
      classes <- sapply(data@variables, class)
      y <- c(sapply(y, FUN = function(x) {
        names(classes[which(classes == x)])
      }))
      if(is.list(y)) stop(paste("no variables of class", names(y)[1]))
      else y <- y[1]
    }
    y <- match.arg(y, data@DIMNAMES[[2]], several.ok = TRUE)
    for(i in 1:length(y)) {
      data@variables[[y[i]]] <- change_link(y = data@variables[[y[i]]], to = to[i])
    }
    return(invisible(data))
  })

setMethod("change_link", signature(data = "missing_data.frame", y = "numeric", to = "character"), def =
  function(data, y, to) {
    if(length(to) == 1) to <- rep(to, length(y))
    else if(length(to) != length(y)) stop("'y' and 'to' must have the same length")
    for(i in 1:length(y)) {
      data@variables[[y]] <- change_link(y = data@variables[[y]], to = to[i])
    }
    return(invisible(data))
  })

setMethod("change_family", signature(data = "missing_data.frame", y = "logical", to = "character"), def =
  function(data, y, to) {
    if(length(y) != data@DIM[2]) {
      stop("the length of 'y' must equal the number of variables in 'data'")
    }
    return(change_link(data, which(y), to))
  })