Skip to content

Instantly share code, notes, and snippets.

View njudd's full-sized avatar
🦄
magic unicorn

Nicholas Judd njudd

🦄
magic unicorn
View GitHub Profile
@njudd
njudd / rStandardize_dataTable.txt
Created May 7, 2024 15:09
R data table standardize a set of cols
pacman::p_load(data.table, stringr)
setDT(ct)
col_fence.s <- c("col1", "col2", "col3")
ct[,(str_c(col_fence.s, ".s")) := lapply(.SD, function(x) as.numeric(scale(x))), .SDcols=col_fence.s] # making new ones with a .s suffex
@njudd
njudd / gist:792f13e2932ae95c2d4355b6c393f9f9
Created July 4, 2023 12:45
Normal Density function plotted
pl1 <- ggplot(df, aes(x)) +
geom_rect(aes(xmin = .8, xmax = 1.2, ymin = 0, ymax = 0.4, fill = "lightblue", alpha = .3)) +
stat_function(fun = dnorm, n = 200, args = list(mean = 0, sd = 1)) +
#geom_density(color = "darkblue", size = 1) +
theme_minimal(base_size = 12) +
theme(legend.position = "none") +
labs(y = "Density of participants (n = 200)", x = "Ability on a Task (SD)") +
scale_x_continuous(breaks = c(-3, -2, -1, 0, 1, 2, 3), limits = c(-3.5, 3.5))
ggsave("~/Desktop/abilitychallange_s1.png", pl1, bg = "white", width = 8, height = 4)
@njudd
njudd / AIC weights
Created December 26, 2022 14:27
Extracting and plotting AIC weights
# Nicholas Judd | Donders Institute
# 26-12-2022
# njudd.com
# replicating https://twitter.com/rogierK/status/1004443199031607296/photo/2
# Wagenmakers, E.-J., & Farrell, S. (2004).
# AIC model selection using Akaike weights.
# Psychonomic Bulletin & Review, 11(1), 192–196.
if (!require(pacman)){
@njudd
njudd / BasePipeBYE
Last active December 9, 2022 16:19
Showing how to get out of the R base pipe!
iris |>
(\(.) {head(.)})()
# or an example with plotting
diamonds |> (\(.) {
ggplot(., aes(carat, price)) + geom_point()
}) () #https://stackoverflow.com/questions/73752006/using-the-base-r-pipe-to-pipe-ggplot2-to-plotly
@njudd
njudd / CorMatbyGroup.R
Created October 31, 2022 21:53
Correlation Mat's and their p-vals by group
# No need to call Bill Gates!
# here is how you get correlation tables by group with their p-val matries and even plot them
library(Hmisc); library(tidyverse); library(corrplot)
result <- iris |> # base R pipe
#select() # remove col's you don't want in the correlation matrix
group_by(Species) |> # grouping col
group_map(~rcorr(as.matrix(.))) |>
setNames(unique(sort(iris$Species)))
@njudd
njudd / anova.R
Last active October 28, 2019 05:48
## Trying to figure out what R is doing with anova's
# The difference of aov() and lm() simply changes how summary() acts
# summary(aov()) is the same as anova(lm(), which are both TYPE 1!
# for type = III SS you need to use Anova in the library(car) package
# https://rcompanion.org/rcompanion/d_04.html
# Different SS primer
# https://stats.stackexchange.com/questions/20452/how-to-interpret-type-i-type-ii-and-type-iii-anova-and-manova/20455#20455