File: ChisquaredDistribution.R

package info (click to toggle)
r-cran-distr 2.9.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,344 kB
  • sloc: ansic: 199; sh: 13; makefile: 2
file content (79 lines) | stat: -rw-r--r-- 2,575 bytes parent folder | download | duplicates (2)
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

################################
##
## Class: ChisqParameter
##
################################

## Access Methods
setMethod("df", "ChisqParameter", function(x, df1, df2, log = FALSE) x@df)
setMethod("ncp", "ChisqParameter", function(object) object@ncp)
## Replace Methods
setReplaceMethod("df", "ChisqParameter", function(object, value)
                 { object@df <- value; object})
setReplaceMethod("ncp", "ChisqParameter", function(object, value)
                 { object@ncp <- value; object})


setValidity("ChisqParameter", function(object){
  if(length(df(object)) != 1)
    stop("df has to be a numeric of length 1")    
  if(df(object) <= 0 + .Machine$double.eps^.5 )
    stop("df has to be positive")
  if(length(ncp(object)) != 1)
    stop("ncp has to be a numeric of length 1")    
  if(ncp(object) < 0)
    stop("ncp has to be not negative")
  else return(TRUE)  })


################################
##
## Class: Chi squared distribution
##
################################

Chisq <- function(df = 1, ncp = 0) new("Chisq", df = df, ncp = ncp)

## wrapped access methods
setMethod("df", "Chisq", function(x, df1, df2, log = FALSE) df(param(x)))
setMethod("ncp", "Chisq", function(object) ncp(param(object)))

## wrapped replace methods
setMethod("df<-", "Chisq", 
           function(object, value) 
               new("Chisq", df = value, ncp = ncp(object)))
setMethod("ncp<-", "Chisq", 
           function(object, value) new("Chisq", df = df(object), ncp = value))

setMethod("+", c("Chisq","Chisq"),
          function(e1,e2){
            newdf <- df(e1) + df(e2)
            newncp <- ncp(e1) + ncp(e2)
            return(new("Chisq", df = newdf, ncp = newncp, .withArith = TRUE))
          })

### new from version 1.9 on:

setMethod("shape", "Chisq", 
           function(object){ 
               if (isTRUE(all.equal(ncp(object),0))) 
                   df(object)/2
               else stop(gettextf("%s is not a Gamma distribution"),
                                   deparse(substitute(object))
                         )
                            }
           )

setMethod("scale", "Chisq", 
           function(x, center = TRUE, scale = TRUE){ 
               if (isTRUE(all.equal(ncp(x),0))) 
                                   2
               else stop(gettextf("%s is not a Gamma distribution"),
                                   deparse(substitute(object))
                         )
                            }
           )

###### end new version 1.9