This file contains 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
#' Model Fit Statistics | |
#' @description Returns lm model fit statistics R-squared, adjusted R-squared, | |
#' predicted R-squared and PRESS. | |
#' Thanks to John Mount for his 6-June-2014 blog post, R style tip: prefer functions that return data frames" for | |
#' the idea \url{http://www.win-vector.com/blog/2014/06/r-style-tip-prefer-functions-that-return-data-frames} | |
#' @param ... One or more \code{lm()} models. | |
#' @return A data frame with rows for R-squared, adjusted R-squared, Predictive R-squared and PRESS statistics, and a column for each model passed to the function. | |
model_fit_stats <- function(...) { | |
var_names <- as.character(match.call())[-1] | |
dots <- list(...) |
This file contains 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) | |
# create a dummy dataframe with 100,000 groups and 1,000,000 rows | |
# and partition by group_id | |
df <- data.frame(group_id=sample(1:1e5, 1e6, replace=T), | |
val=sample(1:100, 1e6, replace=T)) %>% | |
group_by(group_id) | |
# filter rows with a value of 1 naively | |
system.time(df %>% filter(val == 1)) |
This file contains 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
## Download growth chart summary statistics for Hong Kong children, ages 6 to 18, for 1963, 1993, 2005/6 | |
## Data from | |
## So, Hung-Kwan et al. “Secular Changes in Height, Weight and Body Mass Index in Hong Kong Children.” BMC Public Health 8 (2008): 320. PMC. Web. 29 Oct. 2017. | |
## Article at \url{https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2572616/} | |
## PMC Copyright and reuse terms: \url{https://www.ncbi.nlm.nih.gov/pmc/about/copyright/} | |
## Heights in cm | |
## Weights in kg | |
## Libraries #### | |
library(rvest) |
This file contains 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
## Height and Weight of 18 year olds | |
## from Hong Kong 1993 Growth Survey data, | |
## simulated by SOCR from reported summary statistics | |
## Heights in inches | |
## Weights in pounds | |
## Explanation \url{http://wiki.stat.ucla.edu/socr/index.php/SOCR_Data_Dinov_020108_HeightsWeights} | |
## Data \url{http://socr.ucla.edu/docs/resources/SOCR_Data/SOCR_Data_Dinov_020108_HeightsWeights.html} | |
## Libraries #### | |
library(rvest) # Web scraping |
This file contains 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
## Top Charities for Hurricane Harvey Relief | |
## According to both Charity Navigator and Charity Watch | |
## Approach: | |
## Scrape data from Charity Navigator and Charity Watch. | |
## Merge and display the intersection (common entries) of | |
## the two data sets. | |
## ** BROKEN ** As of 2017-10-29, Charity Navigator has changed their page | |
## and the organization of the table of charities. | |
## Libraries #### |
This file contains 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
## Access data provided by packages | |
## List all available data sets, plus ancillary information #### | |
the_data <- data() | |
## Extract just the packages as a data frame #### | |
the_data_df <- data.frame(the_data$results, stringsAsFactors = FALSE) | |
## Extract specific data sets #### | |
specific_data <- get(the_data_df$Item[1]) |
This file contains 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
## Demonstration of central limit theorem | |
## Based on code in an anonymous comment to the blog post at \url{https://sas-and-r.blogspot.com/2012/01/example-919-demonstrating-central-limit.html} | |
## Libraries #### | |
library(nortest) | |
library(dplyr) | |
library(ggplot2) | |
## Data used #### | |
# right-triangle distribution (peak at 0; minimum at 1) |
This file contains 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
#' @description Returns a list of all packages on CRAN | |
#' @param columns_list A character vector of field names to return from package DESCRIPTION files | |
#' @return A data frame containing all packages on CRAN | |
#' @details Function modified from StackOverflow answer at \url{https://stackoverflow.com/a/11561793}. | |
#' @importFrom magrittr %>% | |
#' @importFrom tibble as.tibble | |
#' @importFrom dplyr select_ | |
getCRANPackages <- function(columns_list = c("Package", "Title", "Version", "Date", "Published", "URL")) { | |
contrib.url(getOption("repos")["CRAN"], "source") | |
description <- sprintf("%s/web/packages/packages.rds", |
This file contains 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
At the top of your R markdown, beneath the yaml title block, add the following `<script>` section | |
--- | |
<script type="text/x-mathjax-config"> | |
MathJax.Hub.Config({ | |
TeX: { | |
equationNumbers: { | |
autoNumber: "all", | |
formatNumber: function (n) {return n} |