Skip to content

Instantly share code, notes, and snippets.

View chrissyhroberts's full-sized avatar

Chrissy h Roberts (He/Him) chrissyhroberts

View GitHub Profile
library(tidyverse)
library(nlme)
# create a dummy dataframe
# Here the interruption takes place at week 51
df<-tibble(
Time = 1:100,
Intervention = c(rep(0,50),rep(1,50)),
Post.intervention.time = c(rep(0,50),1:50),
quantity.x = c(sort(sample(200:300,size = 50,replace = T),decreasing = T)+sample(-20:20,50,replace = T),c(sort(sample(20:170,size = 50,replace = T),decreasing = T)+sample(-40:40,50,replace = T)))
# Example code to extract x and y values from a geom_smooth
library(tidyverse)
#make a dummy data set with 11 observations and increasing number
df<-tibble(month=1:11,
n = c(100,140,200,260,360,470,560,630,770,990,1100))
@chrissyhroberts
chrissyhroberts / AIC.model.selection
Created June 11, 2021 08:55
Model selection using AIC
https://www.scribbr.com/statistics/akaike-information-criterion/
An introduction to the Akaike information criterion
Published on March 26, 2020 by Rebecca Bevans.
The Akaike information criterion (AIC) is a mathematical method for evaluating how well a model fits the data it was generated from. In statistics, AIC is used to compare different possible models and determine which one is the best fit for the data. AIC is calculated from:
the number of independent variables used to build the model.
the maximum likelihood estimate of the model (how well the model reproduces the data).
@chrissyhroberts
chrissyhroberts / table.univariate.regressions
Created May 27, 2021 09:08
Make a univariate regression table for many variables from raw data frame
library (gtsummary)
Univariate Regression
The tbl_uvregression() function produces a table of univariate regression models. The function is a wrapper for tbl_regression(), and as a result, accepts nearly identical function arguments. The function’s results can be modified in similar ways to tbl_regression().
trial %>%
select(response, age, grade) %>%
tbl_uvregression(
method = glm,
y = response,
@chrissyhroberts
chrissyhroberts / lm_left__lm_right
Created May 27, 2021 09:04
Plot lm model fits either right or left, time series counterfactuals
library(ggplot2)
library(dplyr)
library(tidyr)
##########################################################################################
# Make a new kind of modelling which only predicts either right or left
##########################################################################################
{
## decorate lm object with a new class lm_right
@chrissyhroberts
chrissyhroberts / get_text_before_after_match_R
Created April 8, 2021 10:03
extract substrings before and after a match in r
#return text after match
a <- "asdfgpopgazelle"
data.table::tstrsplit(a, "pop")[[2]]
"gazelle"
#return all splits
a <- "asdfgpopgazelle"
> data.table::tstrsplit(a, "g")
@chrissyhroberts
chrissyhroberts / update_R_chrissy
Last active May 9, 2022 11:13
Updates R and adds back libraries
#Bare bones updater for R version control.
#########################################
# Takes the long way round, by creating an installer script for packages.
# Accounts for weird behaviour where installing packages from within loops or apply commands doesn't handle
# dependencies well an leads to lots of packages failing to install.
# This script contains some code from updateR package, which has never worked for me on its own
# This can take a while but does a much better job of comprehensively rebuilding your catalogue
# Obviously this only works with CRAN packages, but will spit out a list of packages you need
# to manually install at the end.
library(utf8)
# second entry is encoded in latin-1, but declared as UTF-8
x <- c("fa\u00E7ile", "fa\xE7ile", "fa\xC3\xA7ile")
Encoding(x) <- c("UTF-8", "UTF-8", "bytes")
as_utf8(x) # fails
#> Error in as_utf8(x): entry 2 has wrong Encoding; marked as "UTF-8" but leading byte 0xE7 followed by invalid continuation byte (0x69) at position 4
# mark the correct encoding
@chrissyhroberts
chrissyhroberts / HazRatio_chart
Last active January 30, 2023 21:33
draw a quick HR and CI with summary values instead of raw data.
require(ggplot2)
bingo<-tibble(
IMD = c("IMD1","IMD2","IMD3","IMD4","IMD5"),
ABSOLUTE.CHANGE = c(-3.1,-6.6,-1.6,-2.0,-0.9),
LCI = c(-0.4,-4.4,0.49,-1.06,-0.2),
UCI = c(-5.7,-8.8,-3.62,-2.9,-1.5)
)
ggplot(bingo,aes(x=IMD,y=ABSOLUTE.CHANGE,colour=IMD))+
@chrissyhroberts
chrissyhroberts / ssh-copy-id
Created January 21, 2019 11:45
copy ssh id to a server to enable login without password
ssh-copy-id -i ~/.ssh/mykey user@host