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
plannedContrastBF <- function(numerator_formula, denominator_formula, multicore = FALSE, ...) { | |
if(!is.list(numerator_formula)) numerator_formula <- list(numerator_formula) | |
formulae <- c(denominator_formula, numerator_formula) | |
if(multicore) { | |
doMC::registerDoMC() | |
linear_models <- foreach::"%dopar%"( | |
foreach::foreach(model_formula = formulae, .options.multicore = list(preschedule = FALSE, set.seed = TRUE)), | |
BayesFactor::lmBF(model_formula, ...) | |
) | |
} else { |
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
install_load_all <- function(x) { | |
.install_load_all <- function(x) { | |
if(!require(x, character.only = TRUE)) { | |
install.packages(x, repos = "http://cran.us.r-project.org") | |
library(x, character.only = TRUE) | |
} | |
} | |
invisible(sapply(x, .install_load_all)) | |
} |
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("dplyr") | |
library("tidyr") | |
library("likert") | |
library("papaja") | |
dat <- data.frame( | |
Year = factor(seq(2009, 2017, 2)) | |
, Democrats = c(17, 11, 12, 7, 6) | |
, Mixed = c(24, 17, 14, 20, 19) | |
, Republicans = c(9, 22, 24, 23, 25) |
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
#' Plot network of package dependencies | |
#' | |
#' @param pkg package description, can be path or package name. See \code{\link[devtools]{as.package}} for | |
#' more information. | |
#' | |
#' @details The resulting plot visualizes the network of package dependencies. If you are trying to cut down | |
#' on package dependencies look for big red dots that represent a lot of upstream but few downstream | |
#' dependencies. | |
#' @import ggplot2 | |
#' @export |
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
# x data.frame. Aggregated data for the factorial design, e.g., from aggregate(measure ~ subject * factor1 * factor2, data, mean). | |
# measure Character. Name of the measure. | |
# formula Formula. A formula specifying the factor (combination) for which to calculate the correlation, e.g., `subject ~ factor1` for a | |
# main effect or `subject ~ factor1 + factor2` (yes, it needs to be a "+") for an interaction. Note that G*Power can be | |
# used to perform power analyses for up to two repeated measures factors as long as one of them has only two levels. | |
# To do so, enter the larger number of factor levels into the field "Number of measurements" and multiply the effect | |
# size f by √p, where p is the number of levels of the other factor). If both factor have more than two levels G*Power | |
# will underestimate the required sample size! (See http://goo.gl/RgciM4 [German] for details) | |
# type Character. Specifies the estimation app |
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
batch_read_github <- function(url, pattern, read_fun, ...) { | |
if(!require("rvest")) stop("Please install the 'rvest' package.") | |
if(!require("RCurl")) stop("Please install the 'RCurl' package.") | |
# Fetch file names | |
github_page <- read_html(url) | |
file_nodes <- html_nodes(github_page, ".content .css-truncate-target .js-navigation-open") | |
file_names <- html_text(file_nodes) | |
file_url <- html_attr(file_nodes, "href")[grep(pattern, file_names)] | |
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
batch_read <- function(path, pattern, recursive = FALSE, read_fun, ...) { | |
data.files <- list.files(path, pattern = pattern, recursive = recursive) | |
data <- lapply(paste0(path, data.files), read_fun, ...) | |
data <- do.call("rbind", data) | |
data | |
} |
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
n_bf <- 50 | |
# Simulate data | |
library("BayesFactor") | |
previous_data <- rnorm(2, mean = 0, sd = 3) | |
uninformative_bf <- function(x, previous_data = NULL, mu = 0) { | |
bf <- as.vector(ttestBF(c(previous_data, x), mu = mu)) | |
(1 - bf)^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
label_test <- cars | |
attr(label_test, "variable.labels") <- c("Speed (km/h)", "Distance traveled (km)") | |
View(label_test) | |
str(label_test) # data.frame attribute | |
# Alternative using Hmisc (does not show up in RStudio Viewer) | |
label_test <- cars | |
Hmisc::label(label_test$speed) <- "Speed (km/h)" | |
Hmisc::label(label_test$dist) <- "Distance traveled (km)" |
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
# Function definition | |
add_event <- function(start, end = NULL, label = NULL, line = 0.5, las = 1, col = scales::alpha("steelblue", 0.5)) { | |
if(is.null(end)) { | |
end <- paste(start, "23:59:59") | |
start <- paste(start, "00:00:00") | |
} | |
start <- as.POSIXct(start) | |
end <- as.POSIXct(end) | |