Skip to content

Instantly share code, notes, and snippets.

View pgstevenson's full-sized avatar

Paul Stevenson pgstevenson

View GitHub Profile
@pgstevenson
pgstevenson / copy_clip.R
Created September 4, 2019 03:28
Function to copy data from R to the clipboard
copy_clip <- function(x, row.names = FALSE, col.names = TRUE, ...) {
write.table(x, "clipboard", sep = "\t", row.names = row.names, col.names = col.names, ...)
}
@pgstevenson
pgstevenson / paste_clip.R
Created September 4, 2019 03:29
Function to paste data from clipboard into R
paste_clip <- function(header = TRUE, ...) {
read.table("clipboard", sep = "\t", header = header, ...)
}
@pgstevenson
pgstevenson / similar_strings.R
Last active September 5, 2019 05:01
Calculate string distance(s) & group into networks
#### libraries ----
# install.packages("intergraph")
library(dplyr)
library(purrr)
library(furrr)
library(GGally)
library(igraph)
library(stringdist)
@pgstevenson
pgstevenson / hist2.R
Created October 2, 2019 03:03
Create a histogram with a normal distribution overlay in ggplot2
hist2 <- function(x) {
dat <- tibble(x = x) %>%
na.omit()
ggplot(dat, aes(x = x)) +
geom_histogram(aes(y =..density..),
bins = 30,
colour = "black",
fill = "white") +
stat_function(fun = dnorm, args = list(mean = mean(dat$x), sd = sd(dat$x)))
}
# Model Diagnostics
**Reference only, remove from report before sending to the sponsor**
### Influential Observations
```{r influence_plot, out.extra = "figure", echo = F, warning = F, message = F, comment = NA, eval = F}
influencePlot(mods$iris_data, id.method = "identify", main = "Influence Plot", sub = "Circle size is proportial to Cook's Distance" )
```
fun <- function(d, est, se, p, expo = FALSE, dp = "%0.2f") {
mutate(d,
ci.lo = {{est}} - {{se}} * qnorm((1 + 0.95)/2),
ci.hi = {{est}} + {{se}} * qnorm((1 + 0.95)/2),
p.stars = case_when({{p}} < 0.001 ~ "***",
{{p}} < 0.01 ~ "**",
{{p}} < 0.05 ~ "*",
TRUE ~ ""),
across(c({{est}}, "ci.lo", "ci.hi"), ~case_when(expo ~ exp(.), TRUE ~ .)),
across(c({{est}}, "ci.lo", "ci.hi"), ~sprintf(dp, .)),
@pgstevenson
pgstevenson / RedCAP.R
Last active May 11, 2021 06:17
Helper functions to assist with RedCAP data cleaning
factor_table <- function(x) tibble(key = str_split(x, "\\|")[[1]]) %>%
separate(key, into = c("key", "value"), sep = ",", extra = "merge") %>%
mutate(across(everything(), trimws))
factor_convert <- function(x, d, dict) {
y <- factor_table(dict[dict$`Variable / Field Name` == cur_column(),]$`Choices, Calculations, OR Slider Labels`)
factor(x, levels = y$key, labels = y$value)
}
checkbox_labels <- function(x, dict) {
#### Upgrade R - do this in R GUI, not RStudio IDE ----
installr::updateR(TRUE)
#### Packages to install after R upgrade ----
# CRAN
install.packages(c("devtools", "tidyverse", "installr", "readxl", "janitor", "broom", "lme4", "broom.mixed", "officer", "yaml", "here", "igraph", "kableExtra", "lubridate", "repmis", "tidyselect", "labelled", "withr", "naturalsort"))