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
| outs5SD <- function(vec, n_outsONLY = FALSE){ | |
| m = mean(vec, na.rm = T) | |
| s = sd(vec, na.rm = T) | |
| # if less than 5 sd below or above the mean replace with na's | |
| print(paste0(length(vec[vec < m - s*5 | vec > m + s*5]), " number of outliers above or below 5SD")) | |
| vec[vec < m - s*5 | vec > m + s*5] <- rep(NA, length(vec[vec < m - s*5 | vec > m + s*5])) | |
| if(n_outsONLY == FALSE) { | |
| return(vec) | |
| } else {} # no return of vector | |
| } |
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
| if (!require(pacman)) {install.packages("pacman")} | |
| pacman::p_load(data.table, psych) | |
| load(url("http://alecri.github.io/downloads/data/dental.RData")) # snagging random long data | |
| setDT(dental) | |
| dental | |
| dental$delta <- dental$y14 - dental$y8 | |
| describe(dental$delta) |
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
| cols_to_res <- colnames(cog)[str_detect(colnames(cog), "_T")] | |
| col_res <- function(df, response_var, predictor_string, model_func = lm){ | |
| # predictor_string <- paste(predictor_vars, collapse = " + ") | |
| # Construct the full formula dynamically | |
| formula_string <- paste(response_var, "~", predictor_string) | |
| print(formula_string) | |
| current_formula <- as.formula(formula_string) | |
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
| #!/bin/bash | |
| # Check if a file name is provided | |
| if [ -z "$1" ]; then | |
| echo "Usage: $0 <filename>" | |
| exit 1 | |
| fi | |
| # Extract the file name and ensure it has the .R extension | |
| filename="$1" |
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(purrr); library(stringr) | |
| map_chr(str_split(df$col2, ", "), 1) # the number indicates the vec you want based on the split |
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
| 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(as.character(scale(x)))), .SDcols=col_fence.s] # making new ones with a .s suffex | |
| # super sketchy do to only as.numeric incase it is a factor!!! updated to as.numeric(as.character |
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
| 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) |
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
| # 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)){ |
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
| 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 |
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
| # 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))) |
NewerOlder