Skip to content

Instantly share code, notes, and snippets.

View shv38339's full-sized avatar

Steele H. Valenzuela shv38339

  • Portland, Oregon
View GitHub Profile
@shv38339
shv38339 / testing.r
Last active June 30, 2017 18:24
Statistical Testing in R including groupings in dplyr
# ANOVA testing by group
data %>%
group_by(your_group) %>%
do(tidy(aov(y ~ factor(x), data = .))) %>%
select(your_group, p.value)
# CHISQUARE testing by group
data %>%
group_by(your_group) %>%
summarise(pvalue = chisq.test(x, y)$p.value) %>%
@shv38339
shv38339 / app.R
Created June 3, 2017 16:06
Cascadia R Conference - Lightning Talk - Shiny App
#libraries
library(shiny)
library(ggplot2)
library(dplyr)
library(DT)
library(tableone)
# data
# mtcars[, c("cyl", "vs", "am", "gear", "carb")] <- lapply(mtcars[, c("cyl", "vs", "am", "gear", "carb")], factor)
# data1
ggplot(${1:data = }, aes(${2:x = )}) +
geom_histogram(aes(y = ..density..), ${3:binwidth = }, colour = "black", fill = "white") +
geom_density(alpha = .2, fill = "#FF6666") +
geom_vline(aes(xintercept = mean(${4:}), color = "mean"), linetype = "dashed", size = 2) +
geom_vline(aes(xintercept = median(${5:}), color = "median"), linetype = "dashed", size = 2) +
scale_color_manual(name = "Dispersion", values = c(median = "blue", mean = "red"))
@shv38339
shv38339 / tbl_steele.r
Created May 20, 2017 22:31
Table Function to output descriptive statistics
# try to do table function in base R
tbl_steele <- function(data, var, ...){
require(descr)
require(htmlTable)
levels_logic <- lapply(data[, var], function(x) levels(x))
levels_logic1 <- lapply(levels_logic, function(x) is.null(x)) # combine these statements in the future
if(sum(unlist(levels_logic1)) < length(var)){
cat("Are all of your variables properly labelled?\n")
@shv38339
shv38339 / model_output.r
Created April 21, 2017 21:55
This function accept glm and geeglm/gee objects. It outputs the variable names, variables levels, followed by the odds ratios, confidence intervals, and pvalues. Additionally, the reference levels are included in the output which is not included in the original R glm objects.
model_output <- function(mod_obj){
# libraries
require(geepack); require(tibble); require(dplyr)
xvar <- names(lm_object$model)[-1]
yvar <- names(lm_object$model)[1] # idky i need this but just in case
data <- lm_object$data
# xvar levels
xvar_levels <- sapply(data[, xvar], levels)
xvar_levels_unlist <- unlist(lapply(seq_along(xvar_levels),
@shv38339
shv38339 / html_table.r
Last active April 18, 2017 20:33
Code snippet displays a customized HTML table function and an example. My next step is to copy and paste this HTML table into an excel file, which I then copy and paste into a word document.
# Quite a few requirements so read closely
# - variable must filter out NAs
# - variable must be a factor
# - variable must have factor labels (i think)
# - must be slightly comfortable with dplyr
# FUNCTION
tbl_steele <- function(data, names){
require(htmlTable)
a <- lapply(data, function(x) freq(x, plot = F))
@shv38339
shv38339 / list_levels.r
Created April 18, 2017 16:13
Combine list names with list levels in order to imitate the coefficient output from summary(lm(...)) objects in R (aka smushed variable names with variable levels)
# place list levels in object
list_levels <-
mtcars %>% select(vs, gear) %>%
transmute(vs_fctr = factor(vs, labels = c("0. Zero", "1. One")),
gear_fctr = factor(gear, labels = c("3. Three", "4. Four", "5. Five"))) %>%
sapply(levels) # sapply extracts unique levels from all variables
# i still don't understand this code but it works...
lapply(seq_along(list_levels),
function(i) paste(names(list_levels)[[i]], list_levels[[i]], sep = "")) %>%