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

504

CHAPTER 21 Creating a package

21.2.4Plotting the results

The final function, plot(), visualizes the results returned by the oneway() function:

plot(results, col="lightblue", main="Multiple Comparisons", xlab="US Region",

ylab="Healthy Life Expectancy (years) at Age 65")

The resulting plot is provided in figure 21.3.

Unlike standard box plots, this plot provides annotations that indicate the median and sample size for each group, and a dashed line indicating the overall group median. The code for the plot.oneway() function is given next.

Listing 21.5 Contents of the plot.R file

#' @title Plot nonparametric group comparisons

 

 

 

 

#'

 

 

 

 

 

 

 

#' @description

 

 

 

 

#' \code{plot.oneway} plots nonparametric group comparisons.

 

#'

 

 

 

 

 

 

 

#' @details

 

 

 

 

#' This function plots nonparametric group comparisons

 

 

 

#' created by the \code{\link{oneway}} function using

 

 

 

#' annotated side by side boxplots. Medians and

 

 

 

 

#' sample sizes are placed at the top of the chart.

 

 

 

#' The overall median is represented by a horizontal

 

 

 

#' dashed line.

 

 

 

 

#'

 

 

 

 

 

 

 

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

 

 

 

 

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

 

 

 

 

#' \code{\link{boxplot}} function.

 

 

 

 

#' @method plot oneway

 

 

 

 

#' @export

 

 

 

 

#' @return NULL

 

 

 

 

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

 

 

 

#' @examples

 

 

 

 

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

 

 

 

 

#' plot(results, col="lightblue", main="Multiple Comparisons",

 

#'

xlab="US Region", ylab="Healthy Life Expectancy at Age 65")

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

 

 

 

 

 

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

 

 

b Checks input

 

 

 

 

 

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

 

 

 

 

data <- x$data

 

 

 

 

 

 

 

 

 

 

y <- data[,1]

 

 

 

 

 

g <- data[,2]

 

 

 

 

 

stats <- x$sumstats

 

 

 

c Generates the

 

lbl <- paste("md=", stats[2,], "\nn=", stats[1,], sep="")

 

box plots

 

opar <- par(no.readonly=TRUE)

 

 

 

 

 

par(mar=c(5,4,8,2))

 

 

 

 

 

boxplot(y~g, ...)

 

 

 

 

 

abline(h=median(y), lty=2, col="darkgrey")

 

d Annotates the plot

 

 

 

axis(3, at=1:length(lbl), labels=lbl, cex.axis=.9)

par(opar)

}

Developing the package

505

Again, you check the class of the object passed to the function b. Next, the original data are extracted and the box plots are produced c. Annotations (medians, sample sizes, overall median line) are then added d. You use the abline() function to add the overall median line and the axis() function to add the medians and sample sizes at the top of the graph.

Now that the functions have been created, it’s time to add data to test them.

21.2.5Adding sample data to the package

When you’re creating a package, it’s a good idea to include one or more datasets that can be used to try out the included functions. For the npar package, this involves adding the life data frame, as shown in the next listing. You add data frames to a package as .rda files.

Listing 21.6 Creating the life data frame

region <- c(rep("North Central", 12), rep("Northeast", 9), rep("South", 16), rep("West", 13))

state <- c("IL","IN","IA","KS","MI","MN","MO","NE","ND","OH","SD","WI", "CT","ME","MA","NH","NJ","NY","PA","RI","VT","AL","AR","DE", "FL","GA","KY","LA","MD","MS","NC","OK","SC","TN","TX","VA", "WV","AK","AZ","CA","CO","HI","ID","MT","NV","NM","OR","UT", "WA","WY")

hlem <- c(12.6,12.2,13.4,13.1,12.8,14.3,11.7,13.1,12.9,12.2,13.3,13.4, 14.3,13.5,13.8,14,12.9,13.6,12.8,13.1,13.9,10.3,11.6,13.5, 14.3,11.6,10.2,11.6,13.3,10.1,11.7,10.8,12,11.2,12.2,13.3, 10.3,13.3,13.7,13.8,14.3,15,13.1,13.4,12.8,13.1,13.9,14.3,14, 13.7)

hlef <- c(14.3,14.1,15.9,15.1,14.7,16.7,14,15.7,16,14,16.4,16.1,16.7, 15.7,15.9,16,14.8,15.3,14.8,15.6,16.2,11.7,12.7,15.7,16.4, 13.1,11.6,12.3,15.3,11.4,13.5,12.9,13.6,12.5,13.4,14.9,11.6, 14.9,16.3,15.5,16.2,17.3,15.1,15.6,14.5,14.7,16,15.7,16,15.2)

life <- data.frame(region=factor(region), state=factor(state), hlem, hlef)

save(life, file='life.rda')

The save() function saves the data frame life.rda in the current working directory. When you build the final package in section 21.4, you’ll move this file to a data subdirectory in the package file tree.

You also need to create a .R file that documents the data frame. The code is given next.

Listing 21.7 Contents of the life.R file

#' @title Healthy Life Expectancy at Age 65 #'

#' @description A dataset containing the healthy life expectancy (expected #' years of life in good health) at age 65, by US state in 2007-2009.

#' Estimates are reported separately for men and women. #'

#' @docType data

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