Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
R in Action, Second Edition.pdf
Скачиваний:
540
Добавлен:
26.03.2016
Размер:
20.33 Mб
Скачать

500 CHAPTER 21 Creating a package

list to c("wmc",

"list"). This is a critical step in creating generic functions for han-

dling the object.

 

Table 21.1 List object returned by the wmc() function

 

 

 

Component

 

Description

 

 

 

CALL

 

Function call

data

 

Data frame containing the dependent and grouping variable

sumstats

 

Data frame with groups as columns and n, median, and mad as rows

kw

 

Five-component list containing the results of the Kruskal–Wallis test

method

 

One-element character vector containing the method used to adjust p-values for

 

 

multiple comparisons

wmc

 

Four-column data frame containing the multiple comparisons

vnames

 

Variable names

 

 

 

Although the list provides all the information required, you’d rarely access the components directly. Instead, you can create generic print(), summary(), and plot() functions to present this information in more concise and meaningful ways. These generic functions are considered next.

21.2.2Printing the results

Most analytic functions of any breadth come with generic print() and summary() functions. print() provides basic or raw information about an object, and summary() provides more detailed or processed (summarized) information. A plot()function is frequently included when a plot makes sense in the given context.

Following the S3 OOP guidelines described in section 20.3.1, if an object has the class attribute "foo", then print(x) executes print.foo(x) if it exists or print.default(x) otherwise. The same goes for summary() and plot(). Because the oneway() function returns an object of class "oneway", you need to define print

.oneway(), summary.oneway(), and plot.oneway() functions. The print.oneway() function is given in listing 21.3.

For the life data, print(results) produces basic information about the multiple comparisons:

data: hlef by region

Multiple Comparisons (Wilcoxon Rank Sum Tests)

Probability Adjustment = holm

 

Group.1

Group.2

W

p

1

South North Central 28.0 0.008583

2

South

West 27.0 0.004738

3

South

Northeast 17.0 0.008583

4

North Central

West 63.5

1.000000

5

North Central

Northeast 42.0

1.000000

6

West

Northeast 54.5

1.000000

Developing the package

501

An informative header is printed, followed by Wilcoxon statistics and adjusted p-values for each pair of groups (Group.1 with Group.2).

Listing 21.3 Contents of the print.R file

#' @title Print multiple comparisons #'

#' @description

#' \code{print.oneway} prints pairwise group comparisons. #'

#' @details

#' This function prints Wilcoxon pairwise multiple comparisons created #' by the \code{\link{oneway}} function.

#'

 

 

 

 

 

 

#' @param x an object of class \code{oneway}.

 

 

#' @param ... additional arguments passed to the function.

 

 

#' @method print oneway

 

 

 

 

 

#' @export

 

 

 

 

 

#' @return the input object is returned silently.

 

 

#' @author Rob Kabacoff <rkabacoff@@statmethods.net>

 

 

#' @examples

 

 

 

 

 

#' results <- oneway(hlef ~ region, life)

 

 

#' print(results)

 

 

 

 

 

print.oneway <- function(x, ...){

 

 

 

 

 

if (!inherits(x, "oneway"))

 

 

b Checks input

 

 

 

 

 

 

stop("Object must be of class 'oneway'")

 

 

 

cat("data:", x$vnames[1], "by", x$vnames[2], "\n\n")

 

c Prints the

 

cat("Multiple Comparisons (Wilcoxon Rank Sum Tests)\n")

 

cat(paste("Probability Adjustment = ", x$method, "\n", sep=""))

 

header

print(x$wmc, ...)

 

 

 

 

 

 

}

 

 

 

 

 

 

d Prints the table

 

 

The header contains comments starting with #' that will be used by the roxygen2 package to create package documentation (see section 21.3). The inherits() function is used to make sure the submitted object has class "oneway" b. A set of cat() functions prints a description of the analysis c. (This could have been written as a single cat() function, but I thought the current code was easier to read.) Finally, print.default() is called to print the multiple comparisons d. The summary

.oneway() function is considered next.

21.2.3Summarizing the results

The summary() function produces more comprehensive and processed output than the print() function. For the healthy life-expectancy data, the summary(results) statement produces the following:

data: hlef on region

Omnibus Test

Kruskal-Wallis chi-squared = 17.8749, df = 3, p-value = 0.0004668

502

 

CHAPTER 21 Creating a package

Descriptive Statistics

 

 

 

 

South North Central

West Northeast

n

16.000

12.00

13.0000

9.000

median 13.000

15.40

15.6000

15.700

mad

1.483

1.26

0.7413

0.593

Multiple Comparisons (Wilcoxon Rank Sum Tests)

Probability Adjustment = holm

 

Group.1

Group.2

W

p

 

1

South

North Central 28.0

0.008583

**

2

South

West 27.0 0.004738 **

3

South

Northeast 17.0 0.008583 **

4

North Central

West 63.5

1.000000

 

5

North Central

Northeast 42.0

1.000000

 

6

West

Northeast 54.5 1.000000

 

---

 

 

 

 

Signif. codes:

0 '***' 0.001

'**'

0.01 '*'

0.05 '.' 0.1 ' ' 1

The output includes the results of the Kruskal–Wallis test, descriptive statistics (sample sizes, median, and median absolute deviations) for each group, and the multiple comparisons. In addition, the multiple-comparison table is annotated with stars to highlight significant results. The code for the summary.oneway() function is given in the following listing.

Listing 21.4 Contents of the summary.R file

#' @title Summarize oneway nonparametric analyses #'

#' @description

#' \code{summary.oneway} summarizes the results of a oneway #' nonparametric analysis.

#'

#' @details

#' This function prints a summary of analyses produced by

#' the \code{\link{oneway}} function. This includes descriptive #' statistics by group, an omnibus Kruskal-Wallis test, and

#' Wilcoxon pairwise multiple comparisons. #'

#' @param object an object of class \code{oneway}. #' @param ... additional parameters.

#' @method summary oneway #' @export

#' @return the input object is returned silently.

#' @author Rob Kabacoff <rkabacoff@@statmethods.net> #' @examples

#' results <- oneway(hlef ~ region, life) #' summary(results)

summary.oneway <- function(object, ...){ if (!inherits(object, "oneway"))

stop("Object must be of class 'oneway'") if(!exists("digits")) digits <- 4L

kw <- object$kw wmc <- object$wmc

Developing the package

503

cat("data:", object$vnames[1], "on", object$vnames[2], "\n\n")

 

 

cat("Omnibus Test\n")

 

 

 

 

 

 

cat(paste("Kruskal-Wallis chi-squared = ",

 

 

 

 

round(kw$statistic,4),

 

 

 

b Kruskal–Wallis

 

 

", df = ", round(kw$parameter, 3),

 

 

 

", p-value = ",

 

 

 

test

 

 

format.pval(kw$p.value, digits = digits),

 

 

 

"\n\n", sep=""))

 

 

 

 

 

 

cat("Descriptive Statistics\n")

 

c Descriptive statistics

 

 

 

 

 

print(object$sumstats, ...)

 

 

 

 

 

 

wmc$stars <- " "

 

 

 

 

 

 

 

 

 

 

 

 

 

 

wmc$stars[wmc$p <

.1] <- "."

 

 

d Table annotation

 

 

wmc$stars[wmc$p <

.05] <- "*"

 

 

 

 

wmc$stars[wmc$p <

.01] <- "**"

 

 

 

 

 

 

wmc$stars[wmc$p < .001] <- "***"

 

 

 

 

 

 

names(wmc)[which(names(wmc)=="stars")] <- " "

 

 

Pairwise

 

cat("\nMultiple Comparisons (Wilcoxon Rank Sum Tests)\n")

 

e

cat(paste("Probability Adjustment = ", object$method, "\n", sep=""))

multiple

 

print(wmc, ...)

 

 

 

 

 

comparisons

 

cat("---\nSignif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' '

 

 

1\n")

 

 

 

 

 

 

 

}

 

 

 

 

 

The class of the object passed to the function must be "oneway" or an error is thrown. Notice that the input parameter in the print.oneway() function is called x, but in the summary() function it’s called object. I chose these names to be consistent with the argument names in the print.default() and summary.default() functions provided by the base R installation. After the informational details, the results of the Kruskal–Wallis test are printed b. The format.pval() function formats the p-value in the output.

Next, you print the descriptive statistics (n, median, mad) for each group c. Before printing the data frame of pairwise multiple comparisons, a column of stars is added d. This column serves as an annotation for the table and indicates the level of significance each test would achieve (.1, .05, .01, or .001). Nonsignificant results are represented by a blank (empty string). The statement

names(wmc)[which(names(wmc)==”stars”)] <- “ “

removes the column name for the annotation column. You could have used the statement

names(wmc)[5] <- " "

but that would break if the column order was changed in the future. The annotated results are printed e, and a key describing the meaning of the annotations is printed below the table.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]