Skip to content

Instantly share code, notes, and snippets.

View cddesja's full-sized avatar

Chris Desjardins cddesja

  • Saint Michael's College
  • Colchester, Vermont
  • 02:28 (UTC -04:00)
View GitHub Profile
cleanNAs <- function(x){
if(sum(is.na(x)) == length(x)) {
x <- NA
} else
x <- x[!is.na(x)]
x
}
dat <- read.csv("testing.csv", header = TRUE)
dat$clean <- apply(dat, 1, cleanNAs)
get.r2 <- function(mod){
ll_null <- logLik(glm(model.frame(mod)[,1] ~ 1 + model.frame(mod)[, "cond"], family = "binomial"))
ll_full <- logLik(mod)
1 - ll_full/ll_null
}
#' Obtain fit stats based on best threshold
#'
#' @param mod a fitted model
get.coords <- function(mod, x = x){

Categorical Variables

@cddesja
cddesja / inference_in_r.R
Last active November 11, 2019 17:13
Statistical Inference in R
# Load the Lock5Data package
library("Lock5Data")
# If you get an error, you need to install Lock5Data
# install.packages("Lock5Data")
################################
# -- Descriptive Statistics -- #
################################
@cddesja
cddesja / lattice_addons.R
Last active September 10, 2019 17:45
Lattice Add Ons
# lattice add-ons
library(lattice)
#' Wilkinson Dot Plot
#' @param x A single variable
wilkplot <- function(x, ...){
# create the table
x.var <- as.numeric(names(table(x)))
y.var <- c(table(x))
@cddesja
cddesja / power_talk.R
Created May 1, 2019 16:44
R code used in the power analysis talk for RMCC
## ----include = FALSE, message = FALSE, warning = FALSE-------------------
library(tidyverse)
cohen.d <- function(samp1, samp2){
xbar1 <- mean(samp1)
xbar2 <- mean(samp2)
n1 <- length(samp1)
n2 <- length(samp2)
s1 <- var(samp1)
s2 <- var(samp2)
pooled.sd <- sqrt(((n1 - 1)*s1 + (n2 - 1)*s2) / (n1 + n2 - 2))
@cddesja
cddesja / big_data_activities.R
Last active April 5, 2019 15:37
Big Data Activities
## 4.2.1 Exercises ----
pisa <- fread("pisa.csv", na.strings = "")
## 4.3.1 Exercises ----
# 1. Subset all the Female students (ST004D01T) in Germany
germany_fem <- pisa[CNTRYID == "Germany" & ST004D01T == "Female"]
# 2. How many female students are there in Germany?
nrow(germany_fem)
program exercise
! define constants
real, parameter :: pi = 3.14159
real :: a = 2.0, b = 5.0, c = 3.0, x1, x2, x3
! calculate values
x1 = (pi / 6.0) * (3 * a ** 2 + 3 * b ** 2 + c ** 2)
x2 = -(2*a / c) * cos(b) * sin(b)
x3 = (-b + sqrt(b**2 - 4 * a * c)) / 2 * a
@cddesja
cddesja / cohen.R
Last active October 17, 2018 16:04
visualizing cohen's d
# load packages
library(ggplot2)
# true parameters ----
mu_1 <- 2
mu_2 <- 4
sigma2_1 <- 2
sigma2_2 <- 1
# sample size ----
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable}
\newtcbox{\mybox}[1][red]{on line,arc=0pt,outer arc=0pt,colback=#1!10!white,colframe=#1!50!black, boxsep=0pt,left=1pt,right=1pt,top=2pt,bottom=2pt, boxrule=0pt,bottomrule=1pt,toprule=1pt}
\newtcbox{\xmybox}[1][red]{on line, arc=7pt,colback=#1!10!white,colframe=#1!50!black, before upper={\rule[-3pt]{0pt}{10pt}},boxrule=1pt, boxsep=0pt,left=6pt,right=6pt,top=2pt,bottom=2pt}