This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "CS130 Causal Inference Assignment Spring 2024" | |
output: html_document | |
date: "2024-04-01" | |
--- | |
```{r setup, include=FALSE} | |
knitr::opts_chunk$set(echo = TRUE) | |
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm(list=ls()) | |
training <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vSUROPfTOZfUEpf6Ebby-vta5zWCwt9KK-KAwSvpToGQjQSKdhYsUfoHxYxvbOYxW8_IQxBD9FqWFJg/pub?gid=383144413&single=true&output=csv") | |
head(training) | |
# plot the data with big green dots | |
plot(training$x, training$y, main = "Training Data", pch = 16, cex = 3, col = "green") | |
################################################ | |
#### RUN 3 DIFFERENT MODELS ON THE TRAINING SET |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(432) | |
# imagine these are scores before a program: | |
before <- rnorm(50, mean = 0, sd = 10) | |
# imagine these are scores after a program | |
after <- before + rnorm(50, mean = 5, sd = 20) | |
# the scores are correlated, but not perfectly correlated | |
# correlation = 0.32... the program helps, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
simulate_raquetball = function(number_of_games, prob_win_serve, | |
prob_win_noserve, points_to_win) | |
{ | |
# variable to track the number of wins | |
wins = 0 | |
# variable to track the number of simulated games | |
total_games = 1 | |
# list where we will store the number of volleys per game |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("MASS") | |
library(MASS) | |
data(Pima.tr) | |
data(Pima.te) | |
############# | |
## STEP 1: Logistic regression, predict diabetes yes or no ## | |
logistic_reg <- glm(type ~ ., data = Pima.tr, family = binomial) # basic model | |
predict_logistic.tr <- predict(logistic_reg, type = "response") # predicted probabilities (TRAINING SET) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## This long coding example shows you how to obtain | |
## confidence intervals for logistic regression. | |
## The appendix at the very bottom also shows you how to obtain | |
## something analogous to prediction intervals | |
## for a logistic regression. | |
## Here's a High-level summary of the basic procedure, step-by-step: | |
## Step 1: Run desired logistic regression, including any desired interactions | |
## |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Quiz 3 ANSWERS | |
####### | |
# Read the article: | |
# https://www.menshealth.com/trending-news/a30894231/amazon-interview-sock-puzzle/ | |
# 1. Write a function that will simulate the act of pulling 2 socks out of the drawer | |
# exactly as described in the Men's Health article. (i.e., selection without replacement) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###### Quiz 2 ####### | |
# For this quiz you will analyze UN Peacekeeping data. | |
# At any given time, the UN is involved with many peacekeeping missions around the world. | |
# Almost all member-states contribute personnel to those missions. There are five types of personnel: | |
# experts on mission, troops, staff officers, individual police, and formed police units. | |
# In its efforts to involve more women in its global operations, the UN reports, monthly, the | |
# gender of each person sent on a mission. You can read more about it here: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
NewerOlder