3 print.myfit <- function(x, ...)
5 cat(paste("\nObserved and fitted values for", x$type, "distribution\n"))
6 if(x$method[1] == "fixed")
7 cat("with fixed parameters \n\n")
9 cat(paste("with parameters estimated by `", paste(x$method, collapse = " "), "' \n\n", sep = ""))
10 RVAL <- cbind(x$count, x$observed, x$fitted)
11 colnames(RVAL) <- c("count", "observed", "fitted")
12 rownames(RVAL) <- rep("", nrow(RVAL))
16 fitted.myfit <- function(object, ...)
20 summary.myfit <- function(object, ...)
23 obsrvd <- object$observed
25 expctd <- fitted(object)
27 G2 <- sum(ifelse(obsrvd == 0, 0, obsrvd * log(obsrvd/expctd))) * 2
31 "poisson" = { pfun <- "ppois" },
32 "binomial" = { pfun <- "pbinom" },
33 "nbinomial" = { pfun <- "pnbinom" })
34 p.hat <- diff(c(0, do.call(pfun, c(list(q = count[-n]), object$par)), 1))
35 #expctd <- p.hat * sum(obsrvd)
36 X2 <- sum((obsrvd - expctd)^2/expctd)
38 names(G2) <- "Likelihood Ratio"
39 names(X2) <- "Pearson"
40 if(any(expctd < 5) & object$method[1] != "ML")
41 warning("Chi-squared approximation may be incorrect")
43 RVAL <- switch(object$method[1],
46 "fixed" = { c(X2, G2) }
49 RVAL <- cbind(RVAL, df, pchisq(RVAL, df = df, lower.tail = FALSE))
50 colnames(RVAL) <- c("X^2", "df", "P(> X^2)")
52 cat(paste("\n\t Goodness-of-fit test for", object$type, "distribution\n\n"))
57 myfit <- function(x, method = c("ML", "MinChisq"), par = NULL) {
62 if (length(dim(x)) > 1)
63 stop("x must be a 1-way table")
65 count <- as.numeric(names(x))
67 if (!(!is.null(ncol(x)) && ncol(x) == 2))
68 stop("x must be a 2-column matrix or data.frame")
69 freq <- as.vector(x[, 1])
70 count <- as.vector(x[, 2])
72 #ncount <- 0:max(count)
75 method <- match.arg(method)
77 if (!is.list(par)) stop("`par' must be a named list")
78 if (!(names(par) == "lambda")) stop("`par' must specify `lambda'")
81 } else if (method == "ML") {
83 par <- weighted.mean(count, freq)
84 } else if (method == "MinChisq") {
87 p.hat <- diff(c(0, ppois(count[-n], lambda = x),1)) # DO NOT USE THIS NOW.
88 expected <- sum(freq) * p.hat
89 sum((freq - expected)^2/expected)
91 par <- optimize(chi2, range(count))$minimum
93 par <- list(lambda = par)
94 p.hat <- dpois(count, lambda = par$lambda)
96 zoomratio <- sum(freq) / sum(p.hat)
97 expected <- p.hat * zoomratio
98 print(c(sum(freq),sum(p.hat),zoomratio))
99 #print(cbind(p.hat,expected))
101 df <- switch(method[1], MinChisq = {
106 c(length(freq), sum(freq > 0)) + df
108 RVAL <- list(observed = freq, count = count, fitted = expected,
109 type = "poisson", method = method, df = df, par = par, zoom = zoomratio)
110 class(RVAL) <- "myfit"
116 scale2pois <- function(x, lambda, cntmin = NULL, cntmax = NULL) {
121 if (length(dim(x)) > 1)
122 stop("x must be a 1-way table")
124 count <- as.numeric(names(x))
126 if (!(!is.null(ncol(x)) && ncol(x) == 2))
127 stop("x must be a 2-column matrix or data.frame")
128 freq <- as.vector(x[, 1])
129 count <- as.vector(x[, 2])
131 #ncount <- 0:max(count)
134 p.hat <- dpois(count, lambda = lambda)
135 if (is.null(cntmin) | is.na(cntmin))
137 if (is.null(cntmax) | is.na(cntmax))
139 flag <- (count >= cntmin) & (count <= cntmax)
140 #print(c(cntmin,cntmax,count,flag))
141 zoomratio <- sum(p.hat[flag]) / sum(freq[flag])
142 scaled <- freq * zoomratio
143 #expected <- p.hat / zoomratio
144 #print(c(sum(freq),sum(scaled),sum(p.hat),sum(expected),zoomratio))
145 #print(cbind(freq,scaled, p.hat,expected))
147 RVAL <- list(ret = cbind(scaled,count), zoom = zoomratio, cntmin = cntmin, cntmax = cntmax)
151 cdf2pdf <- function(x, rescale = FALSE) {
152 mylen <- length(x) - 1
153 #RVAL <- diff(c(0, x, 1))[1:mylen]
154 RVAL <- diff(c(0, x), 1)[1:mylen]
162 # http://www.csee.ogi.edu/~gormanky/papers/handbook/ky.R
163 pdf2cdf <- function(x, complete = FALSE, rescale = FALSE) {
164 # make a sorted PDF into a sorted CDF
167 if (complete | rescale) {
168 RVAL <- c(x,x[mylen])
170 #length(RVAL) <- mylen
173 RVAL[i] <- RVAL[i] + RVAL[i-1]
176 RVAL <- RVAL/RVAL[mylen]