Skip to content

Instantly share code, notes, and snippets.

View fditraglia's full-sized avatar

Francis DiTraglia fditraglia

View GitHub Profile
@fditraglia
fditraglia / lecture22.R
Created November 25, 2013 21:13
R Code for Power Example from Lecture 22
coin.power <- function(a, p, n){
c <- qnorm(1 - a/2)
mu <- sqrt(n) * (2 * p - 1)
sigma <- sqrt(4 * p * (1 - p))
less.than <- pnorm( -c, mean = mu, sd = sigma)
greater.than <- 1 - pnorm(c, mean = mu, sd = sigma)
power <- less.than + greater.than
return(power)
}
@fditraglia
fditraglia / HW10.R
Created November 18, 2013 03:28
R Code to Accompany Solutions to Homework #10
group <- c('treatment', 'control', 'refused')
n.children <- 1000 * c(200, 200, 340)
n.polio <- c(57, 142, 157)
rate <- n.polio/n.children
polio.data <- data.frame(group, n.children, n.polio, rate)
polio.data
treatment <- subset(polio.data, group == 'treatment')
@fditraglia
fditraglia / HW9.R
Created November 4, 2013 01:05
R Code to Accompany Solutions to Homework 9
pnorm(10, mean = 12, sd = 2)
pnorm(250, mean = 300, sd = 10)
n <- 950:961
width <- 2 * qnorm(1 - 0.01/2) * 6/sqrt(n)
@fditraglia
fditraglia / tikz_flowcharts.tex
Created November 1, 2013 16:37
This file contains some simple flowchart examples in tikz. The content is based on slides from my presentation at the 2013 Midwest Econometrics Group meetings.
% I learned how to make these flowcharts by looking at some very helpful examples online:
%
% http://www.texample.net/tikz/examples/simple-flow-chart/
% http://www.texample.net/tikz/examples/borrowers-and-lenders/
% http://www.texample.net/tikz/examples/entity-relationship-diagram/
%
% For a long list of tikz examples, see http://www.texample.net/tikz/examples/
\documentclass{beamer}
\usepackage{amssymb, amsmath, amsthm}
@fditraglia
fditraglia / HW8.R
Last active December 26, 2015 17:58
R code to accompany solutions to Homework #8
ones <- rep(1, 10)
ones
cumsum(ones)
cumsum(1:10)
n <- 10000
sims <- rnorm(n, mean = 0, sd = 10)
running.mean <- cumsum(sims)/(1:n)
@fditraglia
fditraglia / friends_of_normal.R
Created October 23, 2013 16:16
Source Code for "Friends of the Normal Distribution," available at http://www.ditraglia.com/econ103/friends_of_normal.html
#Function to draw a single Chi-squared random variable with degrees of freedom equal to df
my.rchisq <- function(df){
#Draw df independent standard normals
normal.sims <- rnorm(df)
#Square them and sum the result
chi.sims <- sum(normal.sims^2)
@fditraglia
fditraglia / HW7.R
Created October 21, 2013 19:58
R code to accompany solutions to Homework #7.
x <- seq(from = -5, to = 5, by = 0.01)
y1 <- dnorm(x)
y2 <- dt(x, df = 1)
y <- cbind(y1, y2)
matplot(x, y, lty = 1, type = 'l', ylab = 'f(x)')
@fditraglia
fditraglia / Rtutorial5.R
Last active December 26, 2015 02:19
R Tutorial #5 - Transcript and Suggested Solution Code
x <- seq(from = -1, to = 1, by = 0.5)
y <- x^2
plot(x, y)
x <- seq(from = -1, to = 1, by = 0.1)
y <- x^2
plot(x, y)
@fditraglia
fditraglia / Bernoulli_Binomial.R
Created October 16, 2013 17:22
This is the source code to accompany my example on the Bernoulli and Binomial RVs: http://www.ditraglia.com/econ103/Bernoulli_Binomial.html
#This function simulates n independent, identically distributed Bernoulli Random Variables
rbern <- function(n, p){
sims <- sample(0:1, size = n, replace = TRUE, prob = c(1-p, p))
return(sims)
}
@fditraglia
fditraglia / Rtutorial4.R
Created October 7, 2013 19:40
R Tutorial #4 - Transcript and Suggested Solution Code
marbles <- c('red', 'blue', 'green')
sample(x = marbles, size = 2, replace = FALSE)
sample(x = 1:10, size = 5, replace = FALSE)