forked from avehtari/ROS-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
77 changed files
with
827 additions
and
380 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 7 additions & 6 deletions
13
Earnings/earnings_setup.R → Earnings/data/earnings_setup_data.R
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
setwd("~/AndrewFiles/books/regression.and.other.stories/Examples/Earnings") | ||
|
||
library("here") | ||
library("foreign") | ||
earnings <- read.dta("earnings.dta") | ||
|
||
## create variables for age and ethnicity categories | ||
#' Load original data | ||
earnings <- read.dta(here("Earnings/data","earnings.dta")) | ||
|
||
#' Create variables for age and ethnicity categories | ||
earnings$age <- 90 - earnings$yearbn # survey was conducted in 1990 | ||
earnings$age[earnings$age < 18] <- NA | ||
earnings$age_category <- with(earnings, ifelse(age < 35, 1, ifelse(age < 50, 2, 3))) | ||
earnings$eth <- with(earnings, ifelse(race==2, 1, ifelse(hisp==1, 2, ifelse(race==1, 3, 4)))) | ||
earnings$male <- 2 - earnings$sex | ||
|
||
## (For simplicity) remove cases with missing data and restrict to people with positive earnings born after 1925 | ||
|
||
#' (For simplicity) remove cases with missing data and restrict | ||
#' to people born after 1925 | ||
ok <- with(earnings, !is.na(earn + height + sex + age) & yearbn > 2) | ||
earnings_clean <- earnings[ok,] | ||
write.csv(earnings_clean, "earnings.csv") | ||
write.csv(earnings_clean, here("Earnings/data","earnings.csv")) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,52 @@ | ||
setwd("~/AndrewFiles/books/regression.and.other.stories/Examples/Earnings") | ||
#' --- | ||
#' title: "Regression and Other Stories: Earnings" | ||
#' author: "Andrew Gelman, Aki Vehtari" | ||
#' date: "`r format(Sys.Date())`" | ||
#' --- | ||
|
||
#' Bootstrapping to simulate the sampling distribution | ||
#' | ||
#' ------------- | ||
#' | ||
|
||
#' **Load libraries** | ||
#+ setup, message=FALSE, error=FALSE, warning=FALSE | ||
library("here") | ||
library("arm") | ||
|
||
## classical regressions and graphs for earnings example | ||
|
||
earnings_clean <- read.csv("earnings.csv") | ||
n <- nrow(earnings_clean) | ||
|
||
earn <- earnings_clean$earn | ||
height <- earnings_clean$height | ||
male <- earnings_clean$male | ||
|
||
colnames(height_data) <- c("ID", "earn", "height", "male") | ||
print(height_data[1:10,]) | ||
|
||
print(mean(height[male]) / mean(height[!male])) | ||
|
||
n <- length(height) | ||
#' **Load data** | ||
earnings <- read.csv(here("Earnings/data","earnings.csv")) | ||
earnings_all <- read.csv(here("Earnings/data","earnings.csv")) | ||
earnings_all$positive <- earnings_all$earn > 0 | ||
#' only non-zero earnings | ||
earnings <- earnings_all[earnings_all$positive, ] | ||
n <- nrow(earnings) | ||
earn <- earnings$earn | ||
male <- earnings$male | ||
print(earnings[1:10,]) | ||
|
||
#' **Median of women's earnings, divided by the median of men's earnings** | ||
print(median(earn[male==0]) / median(earn[male==1])) | ||
|
||
#' **A single bootstrap sample** | ||
n <- length(earn) | ||
boot <- sample(n, replace=TRUE) | ||
height_boot <- height[boot] | ||
earn_boot <- earn[boot] | ||
male_boot <- male[boot] | ||
ratio_boot <- mean(height_boot[male_boot]) / mean(height_boot[!male_boot]) | ||
ratio_boot <- median(earn_boot[male_boot==0]) / median(earn_boot[male_boot==1]) | ||
|
||
#' **A set of bootstrap simulations** | ||
Boot_ratio <- function(data){ | ||
n <- nrow(data) | ||
boot <- sample(n, replace=TRUE) | ||
height_boot <- data$height[boot] | ||
earn_boot <- data$earn[boot] | ||
male_boot <- data$male[boot] | ||
ratio_boot <- mean(height_boot[male_boot]) / mean(height_boot[!male_boot]) | ||
ratio_boot <- median(earn_boot[male_boot==0]) / median(earn_boot[male_boot==1]) | ||
return(ratio_boot) | ||
} | ||
n_sims <- 10000 | ||
output <- replicate(n_sims, Boot_ratio(data=earnings)) | ||
|
||
n_sims <- 100 | ||
output <- replicate(n_sims, Boot_ratio(data=earnings_clean)) | ||
|
||
#' **Summarize the results graphically and numerically** | ||
hist(output) | ||
print(sd(output)) |
This file contains 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.