Skip to content

Instantly share code, notes, and snippets.

library(Matching)
data(lalonde)
#### EASY QUANTILE EFFECTS FOR RCTs...
## MEDIAN EFFECT
quantile(lalonde$re78[lalonde$treat == 1], probs = 0.5) -
quantile(lalonde$re78[lalonde$treat == 0], probs = 0.5)
#### 0.9 QUANTILE EFFECT
# DATA PREPROCESSING
foo <- read.csv("https://tinyurl.com/y2zt2hyc")
foo <- foo[, c(6:8, 11:16, 99, 50, 114, 49, 63, 136, 109, 126, 48, 160, 142, 10)]
foo <- foo[c(-19, -47), ]
which(is.na(foo) == TRUE)
head(foo)
# What is the 3 digit country code associated with the first row of the data set?
foo$clust2[1]
foo$yrbeg[1]
## -----------------------------------------------------------------------------------------------------------
library(maps)
data(us.cities)
head(us.cities)
map(database = "usa")
capitals <- subset(us.cities, capital == 2) # subset state capitals
dd <- read.csv("progresa.csv")
dim(dd)
dd <- na.omit(dd)
dim(dd)
### QUESTION 1
# An example of what I'm looking for
par(mfrow = c(2,1))
install.packages("tree")
library(MASS)
library(tree)
head(Pima.tr)
#############
set.seed(1234)
# enter your code here
# fit a tree in the training set
### This exercise requires installing a bunch of packages---
### Unfortunately, the precise sequence and rules for installing may vary
### depending upon your computer and configuration.
## ***Taken from Chapter 5 in Kosuke Imai's "Quantitative Social Science"
## Transcribed by Alexis Diamond, all errors my own...
##########################################################################
# assuming you have downloaded the data (Data1.csv) correctly,
# as discussed here: https://piazza.com/class/l7oq25mqbrz1nd/post/110
# you may need to change the file location in quotes below, to suit where your file is
apple <- read.csv("~/Documents/Data1.csv", stringsAsFactors = F, encoding="UTF-8")
str(apple)
install.packages("tm")
# assuming you have downloaded the data (Data1.csv) correctly,
# as discussed here: https://piazza.com/class/l7oq25mqbrz1nd/post/110
# you may need to change the file location in quotes below, to suit where your file is
apple <- read.csv("~/Documents/Data1.csv", stringsAsFactors = F, encoding="UTF-8")
str(apple)
library(tm)
## We're going to be running regressions...
## If a predicted value is positive, we're going to say it's a prediction for hamilton authorship.
## If a predicted value is negative, we're going to say it's a prediction for madison authorship.
author <- rep(NA, nrow(dtm1)) # a vector with a missing value
author[hamilton] <- 1 # 1 if Hamilton
author[madison] <- -1 # -1 if Madison
## data frame for regression
author.data <- data.frame(author = author[c(hamilton, madison)],
## Authorship prediction
## authorship of some Federalist Papers is unknown
## We use the 66 essays attributed to either Hamilton or Madison to
## predict the authorship of the 11 disputed papers.
## Since each paper deals with a different topic, we focus on usage of articles,
## prepositions, and conjuctions. We analyze the frequency of the following
## 10 words: although, always, commonly, consequently, considerable, enough, there, upon, while,
## and whilst.