Skip to content

Commit

Permalink
Merge pull request #55 from sinhrks/v0042
Browse files Browse the repository at this point in the history
Fix for CRAN
  • Loading branch information
sinhrks committed Oct 4, 2015
2 parents 4f4430f + ae6c799 commit 7242f49
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 74 deletions.
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: ggfortify
Type: Package
Title: Data Visualization Tools for Statistical Analysis Results
Version: 0.0.4
Date: 2015-09-20
Date: 2015-10-03
Author: Masaaki Horikoshi, Yuan Tang
Maintainer: Masaaki Horikoshi <[email protected]>
Description: Unified plotting tools for statistics commonly used, such as GLM, time series, PCA families, clustering and survival analysis. The package offers a single plotting interface for these analysis results and plots in a unified style using 'ggplot2'.
Expand All @@ -17,15 +17,18 @@ Suggests:
dlm,
fGarch,
forecast,
grDevices,
KFAS,
knitr,
MASS,
MSwM,
nlme,
stats,
strucchange,
survival,
timeSeries,
tseries,
utils,
vars,
xts,
zoo,
Expand Down
22 changes: 7 additions & 15 deletions R/base_fortify_ts.R
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,13 @@ fortify.tsmodel <- function(model, data = NULL,
if (is(model, 'Arima') || is(model, 'ar')) {
if (is.null(data)) {
data <- forecast::getResponse(model)
fit <- fitted(model)
fit <- stats::fitted(model)
} else {
fit <- data - residuals(model)
fit <- data - stats::residuals(model)
}
d <- ggplot2::fortify(data, is.date = is.date)
fit <- ggplot2::fortify(fit, data.name = 'Fitted', is.date = is.date)
resid <- ggplot2::fortify(residuals(model), data.name = 'Residuals', is.date = is.date)
resid <- ggplot2::fortify(stats::residuals(model), data.name = 'Residuals', is.date = is.date)

if (!is.null(predict)) {
pred <- ggplot2::fortify(predict$pred, data.name = 'Predicted')
Expand All @@ -276,8 +276,8 @@ fortify.tsmodel <- function(model, data = NULL,
} else if (is(model, 'HoltWinters')) {
# same as fracdiff and nnetar
d <- ggplot2::fortify(model$x, is.date = is.date)
fit <- ggplot2::fortify(fitted(model), data.name = 'Fitted', is.date = is.date)
resid <- ggplot2::fortify(residuals(model), data.name = 'Residuals', is.date = is.date)
fit <- ggplot2::fortify(stats::fitted(model), data.name = 'Fitted', is.date = is.date)
resid <- ggplot2::fortify(stats::residuals(model), data.name = 'Residuals', is.date = is.date)

if (!is.null(predict)) {
pred <- ggplot2::fortify(predict)
Expand All @@ -289,8 +289,8 @@ fortify.tsmodel <- function(model, data = NULL,
}
} else if (is(model, 'fracdiff') || is(model, 'nnetar')) {
d <- ggplot2::fortify(model$x, is.date = is.date)
fit <- ggplot2::fortify(fitted(model), data.name = 'Fitted', is.date = is.date)
resid <- ggplot2::fortify(residuals(model), data.name = 'Residuals', is.date = is.date)
fit <- ggplot2::fortify(stats::fitted(model), data.name = 'Fitted', is.date = is.date)
resid <- ggplot2::fortify(stats::residuals(model), data.name = 'Residuals', is.date = is.date)
} else if (is(model, 'fGARCH')) {
index <- attr(model@data, 'names')
index <- as.vector(index)
Expand Down Expand Up @@ -402,14 +402,6 @@ fortify.KFS <- fortify.tsmodel
#' autoplot(d.holt)
#' autoplot(d.holt, predict = predict(d.holt, n.ahead = 5))
#' autoplot(d.holt, predict = predict(d.holt, n.ahead = 5, prediction.interval = TRUE))
#'
#' form <- function(theta){
#' dlm::dlmModPoly(order=1, dV=exp(theta[1]), dW=exp(theta[2]))
#' }
#' model <- form(dlm::dlmMLE(Nile, parm=c(1, 1), form)$par)
#' filtered <- dlm::dlmFilter(Nile, model)
#' autoplot(filtered)
#' autoplot(dlm::dlmSmooth(filtered))
#' @export
autoplot.tsmodel <- function(object, data = NULL,
predict = NULL,
Expand Down
10 changes: 5 additions & 5 deletions R/fortify_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -366,18 +366,18 @@ autoplot.pca_common <- function(object, data = NULL,
if (frame) {
if (frame.type == 'convex') {
if (is.null(frame.colour) || !(frame.colour %in% colnames(plot.data))) {
hulls <- plot.data[chull(plot.data[c(x.column, y.column)]), ]
hulls <- plot.data[grDevices::chull(plot.data[c(x.column, y.column)]), ]
} else {
hulls <- plot.data %>%
dplyr::group_by_(frame.colour) %>%
dplyr::do(.[chull(.[c(x.column, y.column)]), ])
dplyr::do(.[grDevices::chull(.[c(x.column, y.column)]), ])
}
mapping = aes_string(colour = frame.colour, fill = frame.colour)
p <- p + ggplot2::geom_polygon(data = hulls, mapping = mapping,
alpha = frame.alpha)
} else if (frame.type %in% c('t', 'norm', 'euclid')) {
ggversion <- utils::packageVersion('ggplot2')
if (compareVersion(as.character(ggversion), '1.0.0') >= 0) {
if (utils::compareVersion(as.character(ggversion), '1.0.0') >= 0) {
mapping = aes_string(colur = frame.colour, fill = frame.colour)
p <- p + ggplot2::stat_ellipse(mapping = mapping,
level = frame.level, type = frame.type,
Expand Down Expand Up @@ -435,10 +435,10 @@ autoplot.dist <- autoplot.matrix
#' fortify(stepfun(c(1, 2, 3, 4, 5, 6, 7, 8, 10), c(4, 5, 6, 7, 8, 9, 10, 11, 12, 9)))
#' @export
fortify.stepfun <- function(model, data, ...) {
x <- knots(model)
x <- stats::knots(model)
lim <- range(x)
if (length(x) > 1L) {
dr <- max(0.08 * diff(lim), median(diff(x)))
dr <- max(0.08 * diff(lim), stats::median(diff(x)))
} else {
dr <- abs(x) / 16
}
Expand Down
16 changes: 8 additions & 8 deletions R/fortify_stats_lm.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ autoplot.lm <- function(object, which = c(1:3, 5), data = NULL,
} else if (is_glm) {
sqrt(summary(object)$dispersion)
} else {
sqrt(deviance(object)/stats::df.residual(object))
sqrt(stats::deviance(object)/stats::df.residual(object))
}
label.fitted <- ifelse(is_glm, 'Predicted values', 'Fitted values')
label.y23 <- ifelse(is_glm, 'Std. deviance resid.', 'Standardized residuals')
hii <- lm.influence(object, do.coef = FALSE)$hat
hii <- stats::lm.influence(object, do.coef = FALSE)$hat

if (is.logical(shape) && !shape) {
if (missing(label)) {
Expand All @@ -98,9 +98,9 @@ autoplot.lm <- function(object, which = c(1:3, 5), data = NULL,

if (label.n > 0L) {
r.data <- dplyr::arrange_(plot.data, 'dplyr::desc(abs(.resid))')
r.data <- head(r.data, label.n)
r.data <- utils::head(r.data, label.n)
cd.data <- dplyr::arrange_(plot.data, 'dplyr::desc(abs(.cooksd))')
cd.data <- head(cd.data, label.n)
cd.data <- utils::head(cd.data, label.n)
}

.smooth <- function(x, y) {
Expand Down Expand Up @@ -147,9 +147,9 @@ autoplot.lm <- function(object, which = c(1:3, 5), data = NULL,
if (show[2L]) {
t2 <- 'Normal Q-Q'
qprobs <- c(0.25, 0.75)
qy <- quantile(plot.data$.stdresid, probs = qprobs, names = FALSE,
type = 7, na.rm = TRUE)
qx <- qnorm(qprobs)
qy <- stats::quantile(plot.data$.stdresid, probs = qprobs, names = FALSE,
type = 7, na.rm = TRUE)
qx <- stats::qnorm(qprobs)
slope <- diff(qy) / diff(qx)
int <- qy[1L] - slope * qx[1L]

Expand Down Expand Up @@ -241,7 +241,7 @@ autoplot.lm <- function(object, which = c(1:3, 5), data = NULL,
title = t6)

g <- dropInf(hii/(1 - hii), hii)
p <- length(coef(object))
p <- length(stats::coef(object))
bval <- pretty(sqrt(p * plot.data$.cooksd / g), 5)
for (i in seq_along(bval)) {
bi2 <- bval[i]^2
Expand Down
24 changes: 12 additions & 12 deletions R/tslib.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ confint.acf <- function (x, ci = 0.95, ci.type = "white") {
with.ci.ma <- FALSE
}
clim0 <- if (with.ci)
qnorm((1 + ci)/2)/sqrt(x$n.used)
stats::qnorm((1 + ci)/2)/sqrt(x$n.used)
else c(0, 0)

Npgs <- 1L
Expand Down Expand Up @@ -182,7 +182,7 @@ confint.acf <- function (x, ci = 0.95, ci.type = "white") {
fitted.ar <- function(object, ...) {
requireNamespace('forecast')
x <- forecast::getResponse(object)
return(x - residuals(object))
return(x - stats::residuals(object))
}

#' Calcurate residuals for \code{stats::ar}
Expand Down Expand Up @@ -217,18 +217,18 @@ ggcpgram <- function (ts, taper = 0.1,

x <- as.vector(ts)
x <- x[!is.na(x)]
x <- spec.taper(scale(x, TRUE, FALSE), p = taper)
y <- Mod(fft(x)) ^ 2 / length(x)
x <- stats::spec.taper(scale(x, TRUE, FALSE), p = taper)
y <- Mod(stats::fft(x)) ^ 2 / length(x)
y[1L] <- 0
n <- length(x)
x <- (0:(n / 2)) * frequency(ts) / n
x <- (0:(n / 2)) * stats::frequency(ts) / n
if (length(x)%%2 == 0) {
n <- length(x) - 1
y <- y[1L:n]
x <- x[1L:n]
}
else y <- y[seq_along(x)]
xm <- frequency(ts) / 2
xm <- stats::frequency(ts) / 2
mp <- length(x) - 1
crit <- 1.358 / (sqrt(mp) + 0.12 + 0.11 / sqrt(mp))

Expand Down Expand Up @@ -269,7 +269,7 @@ ggtsdiag <- function(object, gof.lag = 10,
conf.int.fill = NULL, conf.int.alpha = 0.3,
ad.colour = '#888888', ad.linetype = 'dashed', ad.size = .2,
nrow = NULL, ncol = 1, ...) {
rs <- residuals(object)
rs <- stats::residuals(object)
if (is.null(rs)) {
rs <- object$residuals
}
Expand All @@ -284,7 +284,7 @@ ggtsdiag <- function(object, gof.lag = 10,
colour = ad.colour) +
ggplot2::ggtitle('Standardized Residuals')

acfobj <- stats::acf(rs, plot = FALSE, na.action = na.pass)
acfobj <- stats::acf(rs, plot = FALSE, na.action = stats::na.pass)
p.acf <- autoplot.acf(acfobj, conf.int = conf.int,
conf.int.colour = conf.int.colour,
conf.int.linetype = conf.int.linetype,
Expand All @@ -294,7 +294,7 @@ ggtsdiag <- function(object, gof.lag = 10,

nlag <- gof.lag
pval <- numeric(nlag)
for (i in 1L:nlag) pval[i] <- Box.test(rs, i, type = "Ljung-Box")$p.value
for (i in 1L:nlag) pval[i] <- stats::Box.test(rs, i, type = "Ljung-Box")$p.value
lb.df <- data.frame(Lag = 1L:nlag, `p value` = pval,
lower = -0.05, upper = 0.05)
# Unnable to create column with space by above expression
Expand Down Expand Up @@ -379,7 +379,7 @@ ggfreqplot <- function(data, freq = NULL,
is.univariate(data)

if (is.null(freq)) {
freq <- frequency(data)
freq <- stats::frequency(data)
}

if (is.null(nrow) && is.null(ncol)) {
Expand All @@ -401,8 +401,8 @@ ggfreqplot <- function(data, freq = NULL,
dplyr::summarise_(m = 'mean(Data)', s = 'sd(Data)')

p <- (1 - conf.int.value) / 2
summarised$lower <- qnorm(p, mean = summarised$m, sd = summarised$s)
summarised$upper <- qnorm(1 - p, mean = summarised$m, sd = summarised$s)
summarised$lower <- stats::qnorm(p, mean = summarised$m, sd = summarised$s)
summarised$upper <- stats::qnorm(1 - p, mean = summarised$m, sd = summarised$s)

d <- dplyr::left_join(d, summarised, by = 'Frequency')

Expand Down
8 changes: 0 additions & 8 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ deprecate.warning <- function(old.kw, new.kw) {
warning(message, call. = FALSE)
}

#' Raise error for unsupported type
#'
#' @return NULL
stop.unsupported.type <- function() {
stop(paste0('Unsupported class for autoplot: ', class(data)), call. = FALSE)
}


#' Post process for fortify.
#'
#' @param data data.frame
Expand Down
8 changes: 0 additions & 8 deletions man/autoplot.tsmodel.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,5 @@ d.holt <- stats::HoltWinters(USAccDeaths)
autoplot(d.holt)
autoplot(d.holt, predict = predict(d.holt, n.ahead = 5))
autoplot(d.holt, predict = predict(d.holt, n.ahead = 5, prediction.interval = TRUE))

form <- function(theta){
dlm::dlmModPoly(order=1, dV=exp(theta[1]), dW=exp(theta[2]))
}
model <- form(dlm::dlmMLE(Nile, parm=c(1, 1), form)$par)
filtered <- dlm::dlmFilter(Nile, model)
autoplot(filtered)
autoplot(dlm::dlmSmooth(filtered))
}

12 changes: 0 additions & 12 deletions man/stop.unsupported.type.Rd

This file was deleted.

10 changes: 5 additions & 5 deletions tests/testthat/test-base-infer.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,26 @@ test_that('infer, fortify and autoplot works for dlm::dlmSmooth', {

autoplot(filtered)
autoplot(smoothed)

form <- function(parm){
dlmModPoly(order = 2, dV = exp(parm[1]), dW = exp(parm[2:3]))
}
y <- log(Nile)
model <- form(dlm::dlmMLE(y, parm=log(c(var(y), 0.001, 0.001)), form)$par)
parm <- log(c(var(y), 0.001, 0.001))

filtered <- dlm::dlmFilter(y, model)
fortified <- fortify(filtered)
expect_equal(colnames(fortified), c('Index', 'Data', 'Fitted', 'Residuals'))
expect_equal(fortified$Index, nile_fortified$Index)

smoothed <- dlm::dlmSmooth(filtered)
expect_equal(infer(smoothed), 'dlmSmooth')

fortified <- fortify(smoothed)
expect_equal(colnames(fortified), c('Index', 'Data'))
expect_equal(fortified$Index, nile_fortified$Index)

autoplot(filtered)
autoplot(smoothed)
})
Expand Down

0 comments on commit 7242f49

Please sign in to comment.