Skip to content

Instantly share code, notes, and snippets.

View jrosen48's full-sized avatar

Joshua Rosenberg jrosen48

View GitHub Profile
@jrosen48
jrosen48 / read_purrr.R
Created June 30, 2017 20:25
reading all files in a directory using purrr in R
purrr::map_df(list.files("data"), read_csv)
@jrosen48
jrosen48 / restore_packages.R
Created July 8, 2017 14:36 — forked from arne-cl/restore_packages.R
save/load/install a list of your currently installed R packages
# restore_packages.R
#
# installs each package from the stored list of packages
# source: http://hlplab.wordpress.com/2012/06/01/transferring-installed-packages-between-different-installations-of-r/
load("~/installed_packages.rda")
for (count in 1:length(installedpackages)) {
install.packages(installedpackages[count])
}
@jrosen48
jrosen48 / drop_gather
Created July 9, 2017 01:09
drop from gather using standard evaluation
mtcars %>%
select_("mpg", "cyl", "disp", "hp") %>%
gather_("variable", "val", colnames(.)[!colnames(.) %in% c("hp")])
@jrosen48
jrosen48 / sample_n_groups
Created July 15, 2017 22:47
Sample by group in dplyr
sample_n_groups = function(tbl, size, replace = FALSE, weight = NULL) {
# regroup when done
grps = tbl %>% groups %>% lapply(as.character) %>% unlist
# check length of groups non-zero
keep = tbl %>% summarise() %>% ungroup() %>% sample_n(size, replace, weight)
# keep only selected groups, regroup because joins change count.
# regrouping may be unnecessary but joins do something funky to grouping variable
tbl %>% right_join(keep, by=grps) %>% group_by_(.dots = grps)
}
@jrosen48
jrosen48 / t_tester
Last active July 20, 2017 20:46
R function to compare two means using a t-test; returns the group means, test statistic and associated p-value, and effect size (Cohen's D)
t_tester <- function(dv, fac, df) {
# takes raw (unquoted) names for dv (dependent variable) and fac (factor) as well as the data.frame
# returns the t-test statistic, p-value, and effect size (Cohen's D)
dv_q <- as.character(substitute(dv))
fac_q <- as.character(substitute(fac))
dv_enquo <- enquo(dv)
fac_enquo <- enquo(fac)
@jrosen48
jrosen48 / extract_pred_re
Created September 20, 2017 21:25
extract predicted random effects from a lme4 model
library(magrittr)
library(lme4)
lmer(challenge ~ (1|program_ID) + (1|participant_ID) + (1|beep_ID_new), data = df) %>%
ranef() %>%
extract2("participant_ID") %>%
rownames_to_column(var = "participant_ID") %>%
rename()
@jrosen48
jrosen48 / lmer_effect()
Last active October 10, 2017 02:32
A function to calculate Cohen's d for models estimated with lmer()
library(dplyr)
library(rlang)
library(broom)
calculate_pooled_sd <- function(sd_group1, n_group1, sd_group2, n_group2) {
sqrt(((n_group1 - 1) * (sd_group1 ^ 2) + (n_group2 - 1) * (sd_group2 ^ 2)) / (n_group1 + n_group2 - 2))
}
lmer_effect <- function(df, dv, lmer_model_object, var_name) {

Keybase proof

I hereby claim:

To claim this, I am signing this object:

library(httr)
library(rjson)
# Go to https://developer.nest.com/clients to register a new Nest client
# Nest OAuth 2.0 endpoints
nest <- oauth_endpoint(
request=NULL,
authorize="https://home.nest.com/login/oauth2?state=login",
access="https://api.home.nest.com/oauth2/access_token"