File: 2C-TermPlots.R

package info (click to toggle)
fmultivar 240.10068-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,492 kB
  • ctags: 272
  • sloc: fortran: 1,128; ansic: 764; sh: 22; makefile: 1
file content (198 lines) | stat: -rw-r--r-- 6,429 bytes parent folder | download
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

# 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, 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.
#
# A copy of the GNU General Public License is available via WWW at
# http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
# writing to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA  02111-1307  USA. 

# Copyrights (C)
# for this R-port: 
#   1999 - 2006, Diethelm Wuertz, GPL
#   Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
#   info@rmetrics.org
#   www.rmetrics.org
# for the code accessed (or partly included) from other R-ports:
#   see R's copyright and license files
# for the code accessed (or partly included) from contributed R-ports
# and other sources
#   see Rmetrics's copyright file


################################################################################
# FUNCTION:                 REGRESSION TERM PLOTS
#  .termPlot                 Line Plot          
#  .termPersp                Perspective Plot         
#  .termContour              Contour Plot             
################################################################################


.termPlot =
function(object, terms = NULL)
{   # A function Implemented by Diethelm Wuertz

    # FUNCTION:
    
    # Settings:
    data = object@fit$model
    colNames = all.vars(object@formula)
    data = as.data.frame(object@data)[, colNames]
    X = data[, -1]
    Y = data[, 1]
    if (is.null(terms)) namesX = colNames[-1] else namesX = terms
    namesY = colnames(data)[1]
    meanX = matrix(sapply(X, mean), nrow = nrow(X), ncol = ncol(X), 
        byrow = TRUE)
    colnames(meanX) <- colNames[-1]
     
    # Terms:   
    for (term in namesX) {    
        # Data:
        Xp = meanX
        Xq = Xp[, term] = X[, term]
        Yp = predict(object, Xp, se.fit = TRUE)
        Yr = as.vector(object@residuals) + Yp$fit
        # Sort:
        IDX = sort(Xq, index.return = TRUE)$ix 
        xIDX = Xq[IDX]
        yIDX = Yp$fit[IDX]
        dyIDX = Yp$se.fit[IDX]
        yrange = range(c(yIDX, Yr))   
        # Plot:
        plot(xIDX, yIDX, type = "l", ylim = yrange, col = "red",
            xlab = term, ylab = namesY) 
        Main  = paste(object@method, ": ", namesY, " ~ ", term, sep = "")
        title(main = Main)
        lines(xIDX, yIDX + dyIDX, col = "red", lty = 3)
        lines(xIDX, yIDX - dyIDX, col = "red", lty = 3)
        grid()      
        # Add Residuals:
        points(as.vector(Xq), Yr, pch = 19, cex = 0.7, col = "grey")
        lines(xIDX, yIDX, col = "red")
        # Add Rug:
        rug(Xq, ticksize = 0.03, side = 1, quiet = TRUE) 
        rug(Yr, ticksize = 0.03, side = 2, quiet = TRUE)
    }
    
    # Return Value:
    invisible()
}


################################################################################   


.termPersp =
function(object, terms = NULL, gridPoints = 25)
{   # A function Implemented by Diethelm Wuertz

    # FUNCTION:
    
    # Settings:
    data = object@fit$model
    colNames = all.vars(object@formula)
    data = as.data.frame(object@data)[, colNames]
    X = data[, -1]
    Y = data[, 1]
    if (is.null(terms)) namesX = colNames[-1] else namesX = terms
    namesY = colnames(data)[1]
    Mx = My = gridPoints 
    meanX = matrix(sapply(X, mean), nrow = Mx*My, ncol = ncol(X), 
        byrow = TRUE)
    colnames(meanX) <- colNames[-1]
    
    # Terms:
    N = length(namesX)
    for (i in 1:(N-1)) {    
        xrange = range(X[, namesX[i]])
        x = seq(xrange[1], xrange[2], length = Mx)
        for (j in (i+1):N) { 
            yrange = range(X[, namesX[j]])
            y = seq(yrange[1], yrange[2], length = My)
            # Grid 2D:
            xoy = cbind(rep(x, My), as.vector(matrix(y, Mx, My, byrow = TRUE)))
            XY = matrix(xoy, Mx * My, 2, byrow = FALSE)
            gridX = XY[, 1]
            gridY = XY[, 2]
            Z = meanX
            Z[, namesX[i]] = gridX
            Z[, namesX[j]] = gridY
            Zp = predict(object, Z)               
            # Plot:
            z = matrix(Zp, nrow = Mx) 
            .perspPlot(x, y, z, xlab = namesX[i], ylab = namesX[j], 
                zlab = namesY)
            Main = paste(object@method, ": ", namesY, " ~ ", namesX[i], " + ", 
                namesX[j], sep = "")
            title(main = Main)
        }
    }
    
    # Return Value:
    invisible()
}


################################################################################   


.termContour =
function(object, terms = NULL, gridPoints = 25)
{   # A function Implemented by Diethelm Wuertz

    # FUNCTION:
    
    # Settings:
    data = object@fit$model
    colNames = all.vars(object@formula)
    data = as.data.frame(object@data)[, colNames]
    X = data[, -1]
    Y = data[, 1]
    if (is.null(terms)) namesX = colNames[-1] else namesX = terms
    namesY = colnames(data)[1]
    Mx = My = gridPoints 
    meanX = matrix(sapply(X, mean), nrow = Mx*My, ncol = ncol(X), 
        byrow = TRUE)
    colnames(meanX) <- colNames[-1]
    
    # Terms:
    N = length(namesX)
    for (i in 1:(N-1)) {    
        xrange = range(X[, namesX[i]])
        x = seq(xrange[1], xrange[2], length = Mx)
        for (j in (i+1):N) { 
            yrange = range(X[, namesX[j]])
            y = seq(yrange[1], yrange[2], length = My)
            # Grid 2D:
            xoy = cbind(rep(x, My), as.vector(matrix(y, Mx, My, byrow = TRUE)))
            XY = matrix(xoy, Mx * My, 2, byrow = FALSE)
            gridX = XY[, 1]
            gridY = XY[, 2]
            Z = meanX
            Z[, namesX[i]] = gridX
            Z[, namesX[j]] = gridY
            Zp = predict(object, Z)               
            # Plot:
            z = matrix(Zp, nrow = Mx) 
            contour(x, y, z, xlab = namesX[i], ylab = namesX[j])
            Main = paste(object@method, ": ", namesY, " ~ ", namesX[i], " + ", 
                namesX[j], sep = "")
            title(main = Main)
        }
    }
    
    # Return Value:
    invisible()
}


################################################################################