This is a handbag of commands that I find useful.

Get it started:

devtools::install_github("inductivestep/handbag")
library(handbag)
## Welcome to handbag!
## Follow @InductiveStep on Twitter to improve my Kardashian Index

Enumerating contigency tables

Here are all 2 by 2 tables in which the total sum is 3:

enum_contingency_tables(2, 2, 3)
## [[1]]
##      [,1] [,2]
## [1,]    0    0
## [2,]    0    3
## 
## [[2]]
##      [,1] [,2]
## [1,]    0    1
## [2,]    0    2
## 
## [[3]]
##      [,1] [,2]
## [1,]    0    2
## [2,]    0    1
## 
## [[4]]
##      [,1] [,2]
## [1,]    0    3
## [2,]    0    0
## 
## [[5]]
##      [,1] [,2]
## [1,]    0    0
## [2,]    1    2
## 
## [[6]]
##      [,1] [,2]
## [1,]    0    1
## [2,]    1    1
## 
## [[7]]
##      [,1] [,2]
## [1,]    0    2
## [2,]    1    0
## 
## [[8]]
##      [,1] [,2]
## [1,]    0    0
## [2,]    2    1
## 
## [[9]]
##      [,1] [,2]
## [1,]    0    1
## [2,]    2    0
## 
## [[10]]
##      [,1] [,2]
## [1,]    0    0
## [2,]    3    0
## 
## [[11]]
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    2
## 
## [[12]]
##      [,1] [,2]
## [1,]    1    1
## [2,]    0    1
## 
## [[13]]
##      [,1] [,2]
## [1,]    1    2
## [2,]    0    0
## 
## [[14]]
##      [,1] [,2]
## [1,]    1    0
## [2,]    1    1
## 
## [[15]]
##      [,1] [,2]
## [1,]    1    1
## [2,]    1    0
## 
## [[16]]
##      [,1] [,2]
## [1,]    1    0
## [2,]    2    0
## 
## [[17]]
##      [,1] [,2]
## [1,]    2    0
## [2,]    0    1
## 
## [[18]]
##      [,1] [,2]
## [1,]    2    1
## [2,]    0    0
## 
## [[19]]
##      [,1] [,2]
## [1,]    2    0
## [2,]    1    0
## 
## [[20]]
##      [,1] [,2]
## [1,]    3    0
## [2,]    0    0

Analysis helper functions

library(tidyverse)
tib <- expand.grid(like_peas   = c(0,1), 
                   like_cheese = c(0,1),
                   like_chips  = c(0,1)) %>% as_tibble()

tib
## # A tibble: 8 x 3
##   like_peas like_cheese like_chips
##       <dbl>       <dbl>      <dbl>
## 1         0           0          0
## 2         1           0          0
## 3         0           1          0
## 4         1           1          0
## 5         0           0          1
## 6         1           0          1
## 7         0           1          1
## 8         1           1          1
tib$like <- handbag::binary_patterns_var(tib, "like_")
tib
## # A tibble: 8 x 4
##   like_peas like_cheese like_chips like                
##       <dbl>       <dbl>      <dbl> <chr>               
## 1         0           0          0 (None)              
## 2         1           0          0 peas                
## 3         0           1          0 cheese              
## 4         1           1          0 peas & cheese       
## 5         0           0          1 chips               
## 6         1           0          1 peas & chips        
## 7         0           1          1 cheese & chips      
## 8         1           1          1 peas, cheese & chips

Special relativity: playing around with spaceships

Let’s go on holiday

You want to travel to a space hotel 4 light years away from earth, so you set off in a spaceship at 0.8 times the speed of light, c.

Length contraction means the distance from the perspective of the spaceship is shorter: 2.4 light years, which would be experienced as taking 3 years at 0.8c.

From the perspective of earth, the trip would take 5 years, though it would take an additional 4 years for a radio signal to return to say that you arrived okay.

Let’s go on holiday again

You want to travel to a space hotel 4 light years away from earth, so you set off in a spaceship at 0.999 times the speed of light, c.

Length contraction means the distance from the perspective of the spaceship is shorter: 0.1788407 light years, which would be experienced as taking 0.1790197 years at 0.999c.

From the perspective of earth, the trip would take 4.004004 years, though it would take an additional 4 years for a radio signal to return to say that you arrived okay.

Let’s go on holiday again - really fast this time…

This time you’re going at the speed of light, c. Same distance: 4 light years.

Length contraction means the distance from the perspective of the spaceship is shorter: 0 light years, which would be experienced as taking 0 years (no time!) at c.

From the perspective of earth, the trip would take 4 years, though it would take an additional 4 years for a radio signal to return to say that you arrived okay.

Another example

This one is from Andrew Duffy.

You want to travel to a space hotel 9.5 light years away from earth, so you set off in a spaceship at 0.95 times the speed of light, c.

Length contraction means the distance from the perspective of the spaceship is shorter: 2.966374 light years, which would be experienced as taking 3.122499 years at 0.95c.

From the perspective of earth, the trip would take 10 years, though it would take an additional 9.5 years for a radio signal to return to say that you arrived okay.