This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(qcc) | |
#' The data, from sample published by Donald Wheeler | |
my.xmr.raw <- c(5045,4350,4350,3975,4290,4430,4485,4285,3980,3925,3645,3760,3300,3685,3463,5200) | |
#' Create the individuals chart and qcc object | |
my.xmr.x <- qcc(my.xmr.raw, type = "xbar.one", plot = TRUE) | |
#' Create the moving range chart and qcc object. qcc takes a two-column matrix | |
#' that is used to calculate the moving range. | |
my.xmr.raw.r <- matrix(cbind(my.xmr.raw[1:length(my.xmr.raw)-1], my.xmr.raw[2:length(my.xmr.raw)]), ncol=2) | |
my.xmr.mr <- qcc(my.xmr.raw.r, type="R", plot = TRUE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(qcc) | |
library(ggplot2) | |
library(grid) | |
#' The data, from sample data provided by Donald Wheeler | |
my.xmr.raw <- c(5045,4350,4350,3975,4290,4430,4485,4285,3980,3925,3645,3760,3300,3685,3463,5200) | |
#' Create the individuals qcc object | |
my.xmr.x <- qcc(my.xmr.raw, type = "xbar.one", plot = FALSE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
#' create a data frame with test data. | |
my.df <- data.frame(index = 1:10, value = rnorm(10)) | |
#' create the ggplot object | |
my.ggp <- ggplot(data = my.df, aes(x = index, y = value)) + geom_point() + geom_line() | |
#' get the x- and y-axis ranges actually used in the graph | |
# This worked in early versions of ggplot2 (probably <2.2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Example Cleveland-style dot plot. | |
#' Two variables plotted on an facet plot. | |
#' Ordering of the data in the first facet. | |
#' Error bars plotted with each dot. | |
library(ggplot2) | |
library(reshape2) | |
library(plyr) | |
#' Create some data for the demonstration |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ggplot(NULL, aes(x=x, colour = distribution)) + | |
stat_function(fun=dnorm, data = data.frame(x = c(-6,6), distribution = factor(1)), size = 1) + | |
stat_function(fun=dt, args = list( df = 20), data = data.frame(x = c(-6,6), distribution = factor(2)), linetype = "dashed", size = 1) + | |
scale_colour_manual(values = c("blue","red"), labels = c("Normal","T-Distribution")) + | |
theme(text = element_text(size = 12), | |
legend.position = c(0.85, 0.75)) + | |
xlim(-4, 4) + | |
xlab(NULL) + | |
ylab(NULL) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' @title PRESS | |
#' @author Thomas Hopper | |
#' @description Returns the PRESS statistic (predictive residual sum of squares). | |
#' Useful for evaluating predictive power of regression models. | |
#' @param linear.model A linear regression model (class 'lm'). Required. | |
#' | |
PRESS <- function(linear.model) { | |
#' calculate the predictive residuals | |
pr <- residuals(linear.model)/(1-lm.influence(linear.model)$hat) | |
#' calculate the PRESS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) | |
# See \link{http://stackoverflow.com/questions/11792527/filtering-out-duplicated-non-unique-rows-in-data-table} | |
# for a discussion of how to eliminate duplicate rows. | |
# The problem is that the \code{unique()} function will use a key, if it exists. We need to | |
# eliminate the key. | |
# Create one column of data | |
temp1 <- data.table(sample(letters,size = 15, replace = FALSE)) | |
temp2 <- data.table(sample(letters,size = 15, replace = FALSE)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' When plotting multiple data series that share a common x axis but different y axes, | |
#' we can just plot each graph separately. This suffers from the drawback that the shared axis will typically | |
#' not align across graphs due to different plot margins. | |
#' One easy solution is to reshape2::melt() the data and use ggplot2's facet_grid() mapping. However, there is | |
#' no way to label individual y axes. | |
#' facet_grid() and facet_wrap() were designed to plot small multiples, where both x- and y-axis ranges are | |
#' shared acros all plots in the facetting. While the facet_ calls allow us to use different scales with | |
#' the \code{scales = "free"} argument, they should not be used this way. | |
#' A more robust approach is to the grid package grid.draw(), rbind() and ggplotGrob() to create a grid of | |
#' individual plots where the plot axes are properly aligned within the grid. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Data frame column names are rarely human-readable, concise and clear, but are usually meaningful. Rather | |
#' than trying to modify the data, we can provide custom labels for facets. | |
library(data.table) | |
library(lubridate) | |
library(reshape2) | |
library(ggplot2) | |
#' Download raw data from "Weather Data" at \link{http://datamonitoring.marec.gvsu.edu/DataDownload.aspx}, | |
#' rename the file to "Marec_weather.csv" and save it to /data/ in the current working directory. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' @title Returns a normally distributed vector within the 99.7% tolerance interval defined by minimum and maximum | |
#' @param n (required) The number of random numbers to generate | |
#' @param minimum (optional) The lower 99.9% tolerance limit | |
#' @param maximum (optional) The upper 99.9% tolerance limit | |
#' @return numeric vector with n elements randomly distributed so that approximately 99.7% of values will fall between minimum and maximum | |
#' @examples | |
#' rnorm.within(10) | |
#' rnorm.within(10, 10, 20) | |
#' summary(rnorm.within(10000, 10, 20)) | |
rnorm.within <- function(n, minimum=0, maximum=1) |
OlderNewer