File: Methods.R

package info (click to toggle)
r-cran-modeltools 0.2-23-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 184 kB
  • sloc: sh: 39; makefile: 2
file content (192 lines) | stat: -rw-r--r-- 5,568 bytes parent folder | download | duplicates (3)
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

### Definition and construction method for class `ModelEnv'

setMethod("initialize", signature = "ModelEnv", 
    definition = function(.Object) {

        ### a new environment: all data are stored here 
        .Object@env <- new.env()
    
        ### extract a variable names `which' from the environment
        .Object@get <-
            function(which) get(which, envir = .Object@env, 
                                inherits = FALSE)

        ### set a variable
        .Object@set <-
            function(which, data) assign(which, data, .Object@env)

        return(.Object)
    }
)

### some utility methods for ModelEnv onjects

setMethod("show", signature = "ModelEnv", 
    definition = function(object) {

    cat("\n")
    cat("A", class(object), "with \n\n")
    n <- NULL
    if (has(object, "response")) {
        cat("  response variable(s):  ",
            colnamesnum(object@get("response")), "\n")
        n <- nrow(object@get("response"))
    }
    else if (has(object, "responseMatrix")) {
        cat("  response matrix column(s): ",
            colnamesnum(object@get("responseMatrix")), "\n")
        n <- nrow(object@get("responseMatrix"))
    }
    
    if (has(object, "input")) {
        cat("  input variable(s):     ",
            colnamesnum(object@get("input")), "\n")
        n <- nrow(object@get("input"))
    }
    else if (has(object, "designMatrix")) {
        cat("  design matrix column(s): ",
            colnamesnum(object@get("designMatrix")), "\n")
        n <- nrow(object@get("designMatrix"))
    }
    
    if (is.null(n)) 
        cat("  no observations\n")
    else
        cat("  number of observations:", n, "\n")

    if(length(object@hooks)>0){
            for(n in 1:length(object@hooks)){
                if(n==1)
                    cat("  hooks                 : ")
                else
                    cat("                          ")
                
                cat(paste(names(object@hooks)[n],"(",
                          paste(names(object@hooks[[n]]), collapse=", "),
                          ")", sep=""), "\n")
            }
        }
                    
    cat("\n")
        

})

## Utility function: return either names or number of columns
colnamesnum <- function(x)
{
    if(is.null(colnames(x)))
        return(ncol(x))
    else
        return(colnames(x))
}


setGeneric("has", function(object, which) standardGeneric("has"))

setMethod("has", signature(object = "ModelEnv", which = "character"),
    definition = function(object, which) {
        exists(which, envir = object@env, inherits = FALSE)
    }
)

setGeneric("dimension", function(object, which) standardGeneric("dimension"))

setMethod("dimension", signature(object = "ModelEnv", which = "character"),
    definition = function(object, which) {
        if (has(object, which))
            eval(parse(text = paste("dim(",which,")")) , envir = object@env)
        else
            NULL
    }
)


setGeneric("empty", function(object) standardGeneric("empty"))

setMethod("empty", signature(object = "ModelEnv"),
          definition = function(object) length(ls(object@env))==0)


###**********************************************************

setGeneric("clone", function(object, ...) standardGeneric("clone"))

## the set() method of ModelEnvFormula objects uses lexical scope on
## various bits and pieces, hence cloning currently returns only a
## ModelEnv object, which only has a trivial get method and no set
## method

setMethod("clone", signature = "ModelEnv", 
    definition = function(object, copydata = TRUE) {

    z <- new(class(object))

    if (extends(class(object), "ModelEnvFormula"))
        z@formula <- object@formula

    ### call set and get from object, however, they work in z@env
    ### <FIXME> what about parent.frame() ???
    z@set <- function(which = NULL, data = NULL, frame = parent.frame(), 
                      envir = z@env)
        object@set(which = which, data = data, frame = frame, env = envir)
    z@get <- function(which, data = NULL, frame = parent.frame(), 
                      envir = z@env)
        object@get(which = which, data = data, frame = frame, env = envir)
    ### </FIXME>

    if (copydata) {
        for (name in ls(object@env))
            assign(name, object@get(name), envir = z@env)
    }
    return(z)
})

setGeneric("subset", function(x, ...) standardGeneric("subset"))

setMethod("subset", signature = "ModelEnv",
          definition = function(x, subset, clone = TRUE, ...)
{
    MYSUBSET <- function(x, subset, ...){
        if (is(x, "matrix"))
            x[subset,,drop=FALSE]
        else
            subset(x, subset, ...)
    }

    z <- MEapply(x, MYSUBSET, clone=clone, subset=subset, ...)

    if (!clone)
        invisible(z)
    else
        return(z)
    
})

### dpp, fit and predict generics for StatModel objects

setGeneric("fit", function(model, data, ...) standardGeneric("fit"))

setMethod("fit", signature = signature(model = "StatModel", 
                                       data = "ModelEnv"),
    definition = function(model, data, ...)
        model@fit(data, ...)
)

setGeneric("dpp", function(model, ...) standardGeneric("dpp"))

setMethod("dpp", signature = "StatModel", 
    definition = function(model, ...)
        model@dpp(...)
)

### don't want to redefine stats:::predict, but ...

Predict <- function(object, ...) {
    if ("statmodel" %in% names(object)) {
        if (is(object$statmodel, "StatModel"))
            return(object$statmodel@predict(object, ...))
    }
    return(predict(object, ...))
}