Skip to content

Commit

Permalink
Explicit func calls to deal with global var NOTES
Browse files Browse the repository at this point in the history
  • Loading branch information
brews committed Jul 9, 2016
1 parent 19e975b commit 72a6c49
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
16 changes: 8 additions & 8 deletions R/io.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ write_fhx <- function(x, fname="") {
fl <- file(fname, open = "wt")
cat(paste(head_line, "\n", subhead_line, "\n", sep = ""),
file = fl, sep = "")
write.table(series_heading, fl,
append = TRUE, quote = FALSE,
sep = "", na = "!",
row.names = FALSE, col.names = FALSE)
utils::write.table(series_heading, fl,
append = TRUE, quote = FALSE,
sep = "", na = "!",
row.names = FALSE, col.names = FALSE)
cat("\n", file = fl, sep = "", append = TRUE)
write.table(out, fl,
append = TRUE, quote = FALSE,
sep = "", na = "!",
row.names = FALSE, col.names = FALSE)
utils::write.table(out, fl,
append = TRUE, quote = FALSE,
sep = "", na = "!",
row.names = FALSE, col.names = FALSE)
close(fl)
}
4 changes: 2 additions & 2 deletions R/plotting.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ plot_demograph <- function(x, color_group, color_id, facet_group, facet_id,
levels(events$rec_type)[levels(events$rec_type) %in% pithbark.types] <- "Pith/Bark"
events$rec_type <- factor(events$rec_type, levels = c("Scar", "Injury", "Pith/Bark"))

live <- aggregate(x$year, by = list(x$series), FUN = range, na.rm = TRUE)
live <- stats::aggregate(x$year, by = list(x$series), FUN = range, na.rm = TRUE)
live <- data.frame(series = live$Group.1,
first = live$x[, 1],
last = live$x[, 2],
rec_type = rep("non-recording", dim(live)[1]))
recorder <- x[x$rec_type == 'recorder_year', ]
if ( dim(recorder)[1] > 0 ) { # If there are recorder_years...
# Get the min and max of the recorder_years.
recorder <- aggregate(recorder$year, # TODO: rename this var.
recorder <- stats::aggregate(recorder$year, # TODO: rename this var.
by = list(recorder$series, recorder$rec_type),
FUN = range,
na.rm = TRUE)
Expand Down
56 changes: 28 additions & 28 deletions R/stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,13 @@ run_sea <- function(x, key, years_before=6, years_after=4,
key_event_table <- out_table[, -c(11:14)]
key_event_table[, 2] <- colMeans(event.table, na.rm=TRUE)
key_event_table[, 3] <- apply(event.table, 2, function(x) sum(!is.na(x)))
key_event_table[, 4] <- apply(event.table, 2, sd, na.rm=TRUE)
key_event_table[, 5] <- apply(event.table, 2, function(x) mean(x) - 1.960*sd(x, na.rm=TRUE))
key_event_table[, 6] <- apply(event.table, 2, function(x) mean(x) + 1.960*sd(x, na.rm=TRUE))
key_event_table[, 7] <- apply(event.table, 2, function(x) mean(x) - 2.575*sd(x, na.rm=TRUE))
key_event_table[, 8] <- apply(event.table, 2, function(x) mean(x) + 2.575*sd(x, na.rm=TRUE))
key_event_table[, 9] <- apply(event.table, 2, function(x) mean(x) - 3.294*sd(x, na.rm=TRUE))
key_event_table[, 10] <- apply(event.table, 2, function(x) mean(x) + 3.294*sd(x, na.rm=TRUE))
key_event_table[, 4] <- apply(event.table, 2, stats::sd, na.rm=TRUE)
key_event_table[, 5] <- apply(event.table, 2, function(x) mean(x) - 1.960*stats::sd(x, na.rm=TRUE))
key_event_table[, 6] <- apply(event.table, 2, function(x) mean(x) + 1.960*stats::sd(x, na.rm=TRUE))
key_event_table[, 7] <- apply(event.table, 2, function(x) mean(x) - 2.575*stats::sd(x, na.rm=TRUE))
key_event_table[, 8] <- apply(event.table, 2, function(x) mean(x) + 2.575*stats::sd(x, na.rm=TRUE))
key_event_table[, 9] <- apply(event.table, 2, function(x) mean(x) - 3.294*stats::sd(x, na.rm=TRUE))
key_event_table[, 10] <- apply(event.table, 2, function(x) mean(x) + 3.294*stats::sd(x, na.rm=TRUE))
key_event_table[, 11] <- apply(event.table, 2, min, na.rm=TRUE)
key_event_table[, 12] <- apply(event.table, 2, max, na.rm=TRUE)
key_event_table <- round(key_event_table, 3)
Expand All @@ -228,17 +228,17 @@ run_sea <- function(x, key, years_before=6, years_after=4,
rand_event_table <- out_table
rand_event_table[, 2] <- colMeans(re.table, na.rm=TRUE)
rand_event_table[, 3] <- apply(re.table, 2, function(x) sum(!is.na(x)))
rand_event_table[, 4] <- apply(re.table, 2, sd, na.rm=TRUE)
rand_event_table[, 5] <- apply(re.table, 2, function(x) mean(x) - 1.960*sd(x, na.rm=TRUE))
rand_event_table[, 6] <- apply(re.table, 2, function(x) mean(x) + 1.960*sd(x, na.rm=TRUE))
rand_event_table[, 7] <- apply(re.table, 2, function(x) mean(x) - 2.575*sd(x, na.rm=TRUE))
rand_event_table[, 8] <- apply(re.table, 2, function(x) mean(x) + 2.575*sd(x, na.rm=TRUE))
rand_event_table[, 9] <- apply(re.table, 2, function(x) mean(x) - 3.294*sd(x, na.rm=TRUE))
rand_event_table[, 10] <- apply(re.table, 2, function(x) mean(x) + 3.294*sd(x, na.rm=TRUE))
rand_event_table[, 11] <- apply(re.table, 2, function(x) quantile(x, .025, na.rm=TRUE))
rand_event_table[, 12] <- apply(re.table, 2, function(x) quantile(x, .975, na.rm=TRUE))
rand_event_table[, 13] <- apply(re.table, 2, function(x) quantile(x, .005, na.rm=TRUE))
rand_event_table[, 14] <- apply(re.table, 2, function(x) quantile(x, .995, na.rm=TRUE))
rand_event_table[, 4] <- apply(re.table, 2, stats::sd, na.rm=TRUE)
rand_event_table[, 5] <- apply(re.table, 2, function(x) mean(x) - 1.960*stats::sd(x, na.rm=TRUE))
rand_event_table[, 6] <- apply(re.table, 2, function(x) mean(x) + 1.960*stats::sd(x, na.rm=TRUE))
rand_event_table[, 7] <- apply(re.table, 2, function(x) mean(x) - 2.575*stats::sd(x, na.rm=TRUE))
rand_event_table[, 8] <- apply(re.table, 2, function(x) mean(x) + 2.575*stats::sd(x, na.rm=TRUE))
rand_event_table[, 9] <- apply(re.table, 2, function(x) mean(x) - 3.294*stats::sd(x, na.rm=TRUE))
rand_event_table[, 10] <- apply(re.table, 2, function(x) mean(x) + 3.294*stats::sd(x, na.rm=TRUE))
rand_event_table[, 11] <- apply(re.table, 2, function(x) stats::quantile(x, .025, na.rm=TRUE))
rand_event_table[, 12] <- apply(re.table, 2, function(x) stats::quantile(x, .975, na.rm=TRUE))
rand_event_table[, 13] <- apply(re.table, 2, function(x) stats::quantile(x, .005, na.rm=TRUE))
rand_event_table[, 14] <- apply(re.table, 2, function(x) stats::quantile(x, .995, na.rm=TRUE))
rand_event_table[, 15] <- apply(re.table, 2, min, na.rm=TRUE)
rand_event_table[, 16] <- apply(re.table, 2, max, na.rm=TRUE)
rand_event_table <- round(rand_event_table, 3)
Expand All @@ -247,16 +247,16 @@ run_sea <- function(x, key, years_before=6, years_after=4,

departure_table <- out_table[, -c(3, 4, 15, 16)]
departure_table[, 2] <- key_event_table[, 2] - rand_event_table[, 2]
departure_table[, 3] <- apply(re.table, 2, function(x) -1 * 1.960*sd(x, na.rm=TRUE))
departure_table[, 4] <- apply(re.table, 2, function(x) 1.960*sd(x, na.rm=TRUE))
departure_table[, 5] <- apply(re.table, 2, function(x) -1 * 2.575*sd(x, na.rm=TRUE))
departure_table[, 6] <- apply(re.table, 2, function(x) 2.575*sd(x, na.rm=TRUE))
departure_table[, 7] <- apply(re.table, 2, function(x) -1 * 3.294*sd(x, na.rm=TRUE))
departure_table[, 8] <- apply(re.table, 2, function(x) 3.294*sd(x, na.rm=TRUE))
departure_table[, 9] <- apply(re.table, 2, function(x) quantile(x, .025, na.rm=TRUE) - median(x))
departure_table[, 10] <- apply(re.table, 2, function(x) quantile(x, .975, na.rm=TRUE) + median(x))
departure_table[, 11] <- apply(re.table, 2, function(x) quantile(x, .005, na.rm=TRUE) - median(x))
departure_table[, 12] <- apply(re.table, 2, function(x) quantile(x, .995, na.rm=TRUE) + median(x))
departure_table[, 3] <- apply(re.table, 2, function(x) -1 * 1.960*stats::sd(x, na.rm=TRUE))
departure_table[, 4] <- apply(re.table, 2, function(x) 1.960*stats::sd(x, na.rm=TRUE))
departure_table[, 5] <- apply(re.table, 2, function(x) -1 * 2.575*stats::sd(x, na.rm=TRUE))
departure_table[, 6] <- apply(re.table, 2, function(x) 2.575*stats::sd(x, na.rm=TRUE))
departure_table[, 7] <- apply(re.table, 2, function(x) -1 * 3.294*stats::sd(x, na.rm=TRUE))
departure_table[, 8] <- apply(re.table, 2, function(x) 3.294*stats::sd(x, na.rm=TRUE))
departure_table[, 9] <- apply(re.table, 2, function(x) stats::quantile(x, .025, na.rm=TRUE) - stats::median(x))
departure_table[, 10] <- apply(re.table, 2, function(x) stats::quantile(x, .975, na.rm=TRUE) + stats::median(x))
departure_table[, 11] <- apply(re.table, 2, function(x) stats::quantile(x, .005, na.rm=TRUE) - stats::median(x))
departure_table[, 12] <- apply(re.table, 2, function(x) stats::quantile(x, .995, na.rm=TRUE) + stats::median(x))
departure_table <- round(departure_table, 3)

out_list <- list("Actual events" = key_event_table, "Simulated events" = rand_event_table,
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ sort.fhx <- function(x, decreasing=FALSE, ...) {
if (length(unique(x$series)) == 1) {
return(x)
}
series_minyears <- aggregate(year ~ series, x, min)
series_minyears <- stats::aggregate(year ~ series, x, min)
i <- order(series_minyears$year, decreasing = decreasing)
x$series <- factor(x$series,
levels = series_minyears$series[i],
Expand Down

0 comments on commit 72a6c49

Please sign in to comment.