File: 3A-EquationsModelling.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 (468 lines) | stat: -rw-r--r-- 14,307 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
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
458
459
460
461
462
463
464
465
466
467
468

# This library is free software; you can redistribute it and/or
# Modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# Version 2 of the License, or (at your option) any later version.
#
# This library 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA

# Copyrights (C)
# For this R-port: 
#   1999 - 2004, 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


################################################################################
# REQUIRED PACKAGE:     DESCRIPTION:  
#  .systemfitBuiltin     
# FUNCTION:             SYSTEMFIT WRAPPER:
#  'fSYSTEM'             S4 Class Representation
#  systemFit             Wrapper Function for "systemfit" and "sem" Models:
#  * OLS                  Ordinary Least Squares
#  * WLS                  Weighted Least Squares
#  * SUR                  Seemingly Unrelated Regressions
#  * 2SLS                 Two-Stage Least Squares
#  * W2SLS                Weighted Two-Stage Least Squares
#  * 3SLS                 Three-Stage Least Squares
#  * W3SLS                Weighted Three-Stage Least Squares
# S3-METHODS:           DESCRIPTION:
#  print.fSYSTEM         S3: Print method for an object of class fSYSTEM  
#  ? plot 
#  summary.fSYSTEM       S3: Summary method for an object of class fSYSTEM
# S3-METHODS:           DESCRIPTION:
#  coef.fSYSTEM          S3: Method for coefficients
#  coefficients.fSYSTEM  S3: Synonyme for coef.fSYSTEM
#  fitted.fSYSTEM        S3: Method for fitted values
#  residuals.fSYSTEM     S3: Method for residual values
# S3-METHODS:           DESCRIPTION:
#  predict.fSYSTEM       S3: Prediction method for an object of class fSYSTEM
# FINMETRICS LIKE:      WRAPPER:
#  SUR                   SUR Wrapper
################################################################################


################################################################################
# REQUIRED PACKAGE - PACKAGE DESCRIPTION:
# Package: systemfit
# Version: 0.7-2
# Date: 2004/08/19
# Title: Simultaneous Equation Estimation Package
# Author: Jeff D. Hamann <jeff.hamann@forestinformatics.com> and
#   Arne Henningsen <ahenningsen@agric-econ.uni-kiel.de>
# Maintainer: Jeff D. Hamann <jeff.hamann@forestinformatics.com>
# Depends: R (>= 1.8.0)
# Description: This package contains functions for fitting simultaneous
#   systems of linear and nonlinear equations using Ordinary Least
#   Squares (OLS), Weighted Least Squares (WLS), Seemingly Unrelated
#   Regressions (SUR), Two-Stage Least Squares (2SLS), Weighted
#   Two-Stage Least Squares (W2SLS), Three-Stage Least Squares (3SLS),
#   and Weighted Three-Stage Least Squares (W3SLS).
# License: GPL version 2 or newer
# URL: http://www.r-project.org, http://www.forestinformatics.com, 
#   http://www.arne-henningsen.de
################################################################################


.systemBuiltin =
function(builtin = "/fMultivar/demo/systemfitBuiltin.R") 
{
    sink("@sink@")
    Builtin = paste(.Library, builtin, sep = "")
    source(Builtin)
    sink()
    unlink("@sink@")
}


# ------------------------------------------------------------------------------


setClass("fSYSTEM", 
    representation(
        call = "call",
        formula = "list",
        method = "character",
        parameter = "list",
        data = "list",
        fit = "list", 
        residuals = "data.frame",
        fitted.values = "data.frame",
        predicted.values = "data.frame",  
        title = "character",
        description = "character"
    )
)
       
      
# ------------------------------------------------------------------------------

   
systemFit = 
function(formula, data = list(), 
method = c("OLS", "WLS", "SUR", "2SLS", "W2SLS", "3SLS", "W3SLS"), 
title = NULL, description = NULL, ...)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Common function call for several system equation models.
    
    # Arguments:
    #   formula - the list of formulae describing the system of 
    #       equations
    #   data - the input data set in form of a 'data.frame' or 
    #       'timeSeries' object
    #   method - a character string describing the desired method, 
    #       one of: "OLS", "WLS", "SUR", "2SLS", "W2SLS", "3SLS", 
    #       or "W3SLS".
    #   title - a character string which allows for a project title
    #   description - a character string which allows for a brief 
    #       description
    #   ... - additional optional arguments to be passed to the 
    #       underlying function 'systemfit' 
    
    # Value:
    # The function 'systemFit' returns an object of class "fSYSTEM"
    # with the following slots:
    #   @call - the matched function call
    #   @data - the input data in form of a 'data.frame' or a 
    #       'timeSeries' object
    #   @description - a character string which allows for a brief 
    #       project description
    #   @method - the character string describing the desired method
    #   @formula - the list of formulae describing the system of 
    #       equations
    #   @title - a character string which allows for a project title
    #   @fit - a summary of the  results as a list returned from the 
    #       underlying functions from the 'systemfit' package.  

    # FUNCTION:
    
    # Match Arguments:
    method = match.arg(method)  
    
    # Fit:
    fit = systemfit(method = method, eqns = formula, 
        data = as.data.frame(data), ...) 
    class(fit) = c("list", "systemfit")
    
    # Add Rmetrics Conform Output: 
    fit$name = "systemfit"
    fit$coef = coef(fit)
        
    # Add Title and Description:
    if (is.null(title)) title = paste(method[1], "Estimation")
    if (is.null(description)) description = .description()
    
    # Return Value:
    new("fSYSTEM",     
        call = match.call(),
        formula = formula, 
        method = as.character(method), 
        parameter = list(),
        data = list(data = data),
        fit = fit,
        residuals = data.frame(residuals(fit)),
        fitted.values = data.frame(fitted(fit)), 
        predicted.values = data.frame(),
        title = as.character(title), 
        description = as.character(description) )
}

    
# ------------------------------------------------------------------------------    


print.fSYSTEM =
function(x, ...)
{   # A function implemented by Diethelm Wuertz

    # FUNCTION:
    
    # Title:
    cat("\nTitle:\n ")
    cat(paste(x@method, "Equations Fit\n"))
    
    # Formulas:
    cat("\nFormulas:\n")
    form = t(matrix(capture.output(x@formula), ncol = 2))
    cat(paste("", form[, 2]), sep = "\n")
    
    # Coefficidents:
    cat("\nCoefficients:\n")
    output = capture.output(data.frame(coef(x)))  
    for (i in 1:length(output)) output[i] = paste("", output[i])
    cat(output, fill = FALSE, sep = "\n")
    cat("\n")
    
    # Description:
    cat("Description:\n ")
    cat(x@description, "\n\n")
    
    # Return Value:
    invisible(x)
}  


# ------------------------------------------------------------------------------


# plot.fSYSTEM = 


# ------------------------------------------------------------------------------


summary.fSYSTEM = 
function(object, ...)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   S3 Summary Method for an object of class "fSYSTEM"
    
    # Arguments:
    #   object - an object of class "fSYSTEM"
    
    # FUNCTION:
    
    # Title:
    cat("\nTitle:\n ")
    cat(paste(object@method, "Equations Fit\n"))
    
    # Formulas:
    cat("\nFormulas:\n")
    form = t(matrix(capture.output(object@formula), ncol = 2))
    cat(paste("", form[, 2]), sep = "\n")
    
    # Coefficidents:
    cat("\nFitted Results:\n")
    output = capture.output(summary(object@fit))[-(1:3)]  
    for (i in 1:length(output)) output[i] = paste("", output[i])
    cat(output, fill = FALSE, sep = "\n")
    cat("\n")
    
    # Description:
    cat("Description:\n ")
    cat(object@description, "\n\n")
    
    # Return Value:
    invisible(object)  
}


# ------------------------------------------------------------------------------


coef.fSYSTEM = 
function(object, ...) 
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Returns the fitted values
    
    # FUNCTION:
    
    # From Slot:
    coef = object@fit$b
    coef = as.data.frame(coef)
   
    # Return Value:
    coef
}


# ------------------------------------------------------------------------------


fitted.fSYSTEM = 
function(object, ...) 
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Returns the fitted values
    
    # FUNCTION:
    
    # From Slot:
    x = object@fit
    
    # Fitted values:
    fitted = array(NA, c(length(x$eq[[1]]$fitted), x$g))
    colnames(fitted) = as.character(1:ncol(fitted))
    for (i in 1:x$g)  {
        fitted[, i] = x$eq[[i]]$fitted
        colnames(fitted)[i] = paste("eq", as.character(i), sep = "")
    }
    ans = data.frame(fitted)
    
    # Return Value:
    ans
}


# ------------------------------------------------------------------------------


residuals.fSYSTEM = 
function(object, ...) 
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Returns all residuals
    
    # FUNCTION:
    
    # From Slot:
    x = object@fit
    
    # Residuals:
    residuals = data.frame(eq1 = x$eq[[1]]$residuals)
    if (x$g > 1) {
        for (i in 2:x$g) {
            residuals = cbind(residuals, new = x$eq[[i]]$residuals)
                names(residuals)[i] = paste("eq", as.character(i), sep = "")
        }
    }
    ans = data.frame(residuals)
    
    # Return Value:
    ans
}


# ------------------------------------------------------------------------------


predict.fSYSTEM = 
function (object, newdata = object@data, se.fit = FALSE, se.pred = FALSE, 
interval = "none", ci = 0.95, ...) 
{   # A function implemented by Diethelm Wuertz
   
    # FUNCTION:
    
    # Predict - Don't print conflicts:
    sink("@sink@")
    ans = predict.systemfit(object = object@fit, data = newdata,
        se.fit = se.fit, se.pred = se.pred, interval = interval, 
        level = ci, ...)
    sink()
    unlink("@sink@")
    
    # Return Value:
    ans
}


# ------------------------------------------------------------------------------


SUR = 
function(formula, data = list(), ...)
{   # A function implemented by Diethelm Wuertz

    # FUNCTION:
    
    # Fit:
    ans = systemFit(formula = formula, data = data, method = "SUR", ...)
        
    # Return Value:
    ans
}


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


nlsystemFit = 
function(formula, data = list(), method = c("OLS", "SUR", "2SLS", "3SLS"), 
start = NULL, title = NULL, description = NULL, ...)
{   # A function implemented by Diethelm Wuertz

    # Description:
    #   Common function call for several system equation models.
    
    # Arguments:
    #   formula - the list of formulae describing the system of 
    #       equations
    #   data - the input data set in form of a 'data.frame' or 
    #       'timeSeries' object
    #   method - a character string describing the desired method, 
    #       one of: "OLS", "WLS", "SUR", "2SLS", "W2SLS", "3SLS", 
    #       or "W3SLS".
    #   title - a character string which allows for a project title
    #   description - a character string which allows for a brief 
    #       description
    #   ... - additional optional arguments to be passed to the 
    #       underlying function 'systemfit' 
    
    # Value:
    #   The function 'systemFit' returns an object of class "fSYSTEM"
    #   with the following slots:
    #       @call - the matched function call
    #       @data - the input data in form of a 'data.frame' or a 
    #           'timeSeries' object
    #       @description - a character string which allows for a brief 
    #           project description
    #       @method - the character string describing the desired method
    #       @formula - the list of formula describing the system of 
    #           equations
    #       @title - a character string which allows for a project title
    #       @fit - a summary of the  results as a list returned from the 
    #           underlying functions from the 'systemfit' package.  

    # FUNCTION:
    
    # nlsystemfit(method="OLS", eqns, startvals,
    #     eqnlabels = c(as.character(1:length(eqns))), inst = NULL,
    #     data = list(), solvtol = .Machine$double.eps,
    #     maxiter = 1000, ... )
    
    # Match Arguments:
    method = match.arg(method)  
    
    # Fit:
    fit = nlsystemfit(method = method, eqns = formula, 
        startvals = start, data = as.data.frame(data), ...) 
    class(fit) = c("list", "nlsystemfit.system")
    
    # Add Rmetrics Conform Output: 
    fit$name = "systemfit"
    fit$coef = coef(fit)
        
    # Add Title and Description:
    if (is.null(title)) title = paste(method[1], "Estimation")
    if (is.null(description)) description = .description()
    
    # Return Value:
    new("fSYSTEM",     
        call = match.call(),
        formula = formula, 
        method = as.character(method), 
        parameter = list(),
        data = list(data = data),
        fit = fit,
        residuals = data.frame(residuals(fit)),
        fitted.values = data.frame(fitted(fit)), 
        predicted.values = data.frame(),
        title = as.character(title), 
        description = as.character(description) )
}


# ------------------------------------------------------------------------------