Skip to content

Instantly share code, notes, and snippets.

View josealvarez97's full-sized avatar
Playing soccer

José Alvarez C. josealvarez97

Playing soccer
  • San Francisco, California
View GitHub Profile
library(haven)
df <- read_dta("C:/Users/Vinic/Downloads/turnout.dta")
View(df)
df[1,1]
df[1,]
lm2 <- glm(turnout ~ ., data = df, family = binomial)
summary(lm2)
library(boot)
#estimate the mean via bootstrapping
boot.mean <- function(data,index) return(mean(data[index]))
#calculate the CI via t-distribution
t.dist.ci <- function(samp) {
df <- length(samp) - 1
factors <- qt(c(0.025, 0.975), df = df)
samp.mean <- mean(samp)
@diamonaj
diamonaj / gist:cee915abc59a5b8cc9a64fb2ed50d0f4
Last active October 19, 2018 12:27
Correlated vs. Uncorrelated
# EXERCISE TO BUILD INTUITION FOR CORRELATED VS. UNCORRELATED DATA
# PLEASE FOCUS ON UNDERSTANDING THE BELOW
### DO NOT JUST EXECUTE ALL THE CODE IN ONE BATCH--RUN IT LINE BY LINE...
### Simulation of analysis on correlated data
set.seed(1314)
nsims <- 10000
################ PRELIMINARIES
library(MASS)
data(Pima.tr)
library(tree)
library(randomForest)
## STEP 1: Logistic regression ##
logistic_reg <- glm(type ~ ., data = Pima.tr, family = binomial) # basic model
predict_logistic.tr <- predict(logistic_reg, type = "response") # predicted probabilities (TRAINING SET)
storage.vector <- NA
# Function that assigns treatment/control depending on
# propensity scores (assignment probabilities)
experiment <- function(vector.of.probabilities = NULL) {
k = 0
for (i in 1:length(vector.of.probabilities)) {
if(
sample(x = c(1,0), size = 1, prob = c(vector.of.probabilities[i],
1 - vector.of.probabilities[i])) == 1) {
# An addition
5 + 5
# A subtraction
5 - 5
# A multiplication
3 * 5
# A division
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# Comparison of logicals
TRUE == FALSE
# Comparison of numerics
-6 * 14 != 17 - 101
# Comparison of character strings
"useR" == "user"
# Compare a logical with a numeric
PEACEKEEPING WORKOUT (based on King, Gary;Zeng, Langche, 2007,
"Replication data for: When Can History be Our Guide?
The Pitfalls of Counterfactual Inference",
https://hdl.handle.net/1902.1/DXRXCFAWPK,
Harvard Dataverse, V4,
UNF:3:DaYlT6QSX9r0D50ye+tXpA== [fileUNF] )
# CONSIDER USING THE JUPYTER NOTEBOOK WITH R-SERVER KERNEL (NEVER R-SAGE KERNEL)
foo <- read.csv("https://course-resources.minerva.kgi.edu/uploaded_files/mke/00086677-3767/peace.csv")
# extract relevant columns