This R-Markdown holds an assignment extending our Week 1 discussion of permutation tests. That lecture contains most of the background material for the assignment, but let me know if you have further questions.
We’ll begin by recapitulating the material from the lecture. Then I’ll ask you to extend that work.
d <- read_tsv("satisfaction_survey.txt")
The satisfaction scores by assigned sex1 show lower satisfaction for non-male employees:
| Gender | Mean Satisfaction | SD Satisfaction | N |
|---|---|---|---|
| neither | 3.30 | 0.67 | 10 |
| female | 3.62 | 1.02 | 108 |
| male | 4.20 | 1.09 | 182 |
The original question from HR was whether or not the difference between males and females is due to chance.
Since the survey represents a census, we shouldn’t do something simple, like a t-test, that’s designed to test for the difference between means. Instead we can test whether or not the association between the “labels” of assigned sex and the “scores” of satisfaction are is likely if the two columns are independent.
We can explore this question with a permutation test. The next code block performs the test and visualizes the results.
# Using "cache=T" means that the simulation is only run once and then stored.
# This is convenient for long-running calculations, but you should turn it off
# if you make changes to the code and need the block to be re-run.
get.assigned.sex.diff <- function(x){
male.score <- mean(x$satisfaction[x$assigned_sex=="male"])
female.score <- mean(x$satisfaction[x$assigned_sex=="female"])
return(male.score - female.score)
}
actual.value <- get.assigned.sex.diff(d)
n.sim <- 9999
results <- tibble(statistic = c(actual.value,
rep(NA,n.sim)))
new.d <- d %>%
select(assigned_sex,satisfaction)
for(i in 2:nrow(results)){
new.d$assigned_sex <- sample(new.d$assigned_sex)
results$statistic[i] <- get.assigned.sex.diff(new.d)
}
ggplot(results,
aes(x=statistic)) +
geom_density() +
geom_vline(xintercept=actual.value,color="red") +
theme_minimal() +
labs(x="Male Mean Minus Female Mean",
y="")
As we can see from the histogram, the value we we saw, 0.583, is quite extreme. The proportion of observations this extreme or more extreme than this are 10^{-4}. The two-sided test has the same p-value: 10^{-4}
In the following sections, I ask you to extend this test to a few other statistics and write up the answers to a few questions.
Instead of looking at average satisfaction, let’s look at employees who give a score of 5, 6, or 7. Perform a permutation test estimating whether or not difference of this proportion (answering 5, 6, or 7) by assigned sex for males and females is likely the result of chance.
Note that now we’re talking about proportions, so rather than taking
the average of satisfaction, you’ll need to be able to estimate the
fraction of respondents whose answers fall within a certain range.
Here’s some example code that shows you how to create a column like this
with mutate.
# to get you started, here's the initial proportion
d %>%
mutate(satisfied = if_else(satisfaction %in% c(5,6,7), 1, 0)) %>%
group_by(assigned_sex) %>%
summarize(`Portion Satisfied` = mean(satisfied)) %>%
rename(Gender = assigned_sex) %>% # make the column header nice
knitr::kable(col.names = c("Gender", "Portion Satisfied"), digits = 2)
| Gender | Portion Satisfied |
|---|---|
| female | 0.19 |
| male | 0.37 |
| neither | 0.00 |
This code creates a new column called satisfied, then
averages that by assigned sex, and presents the results in a nice
looking table. We can see that 37% of males are satisfied while only 19%
of females are.
There’s one thing that this code doesn’t do: make that new
column in the data permanently. In order to get that new
satisfied column to stay in the data frame, just assign it
back to itself with a line like this:
d <- d %>%
mutate(satisfied = if_else(satisfaction %in% c(5,6,7), 1, 0))
Sometimes you just want to create a new column temporarily, perhaps if it just exists for a chart or table. Other times it’s more convenient to just add the column into the data as we do above.
When you finish the permutation steps for this section, once again plot the distribution of the replicates and interpret the results.
get.assigned.sex.diff <- function(x){
male.score <- mean(x$satisfaction[x$assigned_sex=="male" & x$satisfaction %in% c(5,6,7)])
female.score <- mean(x$satisfaction[x$assigned_sex=="female" & x$satisfaction %in% c(5,6,7)])
return(male.score - female.score)
}
actual.value <- get.assigned.sex.diff(d)
n.sim <- 9999
results <- tibble(statistic = c(actual.value,
rep(NA,n.sim)))
new.d <- d %>%
filter(satisfaction %in% c(5,6,7)) %>%
select(assigned_sex, satisfaction)
for(i in 2:nrow(results)){
new.d$assigned_sex <- sample(new.d$assigned_sex)
results$statistic[i] <- get.assigned.sex.diff(new.d)
}
library(ggplot2)
ggplot(results,
aes(x=statistic)) +
geom_density() +
geom_vline(xintercept=actual.value,color="maroon") +
theme_minimal() +
labs(x="Male Mean Minus Female Mean (specifically for 5, 6 and 7 scores)",
y="")
Above we can see that we calculated the mean score differences for males and females for the 5, 6, and 7 score answers. We can see that although we are in a “hump” of the graph for our red line, it still falls closer to the tail region our graph so we would observe that difference is unlikely to have occurred by random chance alone. We might have significant differences in mean satisfaction scores between male and female employees who provided scores of 5, 6 or even 7. If it were more in the middle of our graph, we could state that there is not a significant observable difference and we could potentially experience this due to random chance.
There are 10 non-binary employees. Based on this new measure of satisfaction (answering 5, 6, or 7), do we see different levels of satisfaction between our “neither” group and the female group? Please make a similar chart and interpret it.
get.assigned.sex.diff <- function(x){
neither.score <- mean(x$satisfaction[x$assigned_sex=="neither" & x$satisfaction %in% c(5,6,7)])
female.score <- mean(x$satisfaction[x$assigned_sex=="female" & x$satisfaction %in% c(5,6,7)])
return(neither.score - female.score)
}
actual.value <- get.assigned.sex.diff(d)
n.sim <- 9999
results <- tibble(statistic = c(actual.value,
rep(NA,n.sim)))
new.d <- d %>%
filter(satisfaction %in% c(5,6,7)) %>%
select(assigned_sex, satisfaction)
for(i in 2:nrow(results)){
new.d$assigned_sex <- sample(new.d$assigned_sex)
results$statistic[i] <- get.assigned.sex.diff(new.d)
}
library(ggplot2)
ggplot(results,
aes(x=statistic)) +
geom_density() +
geom_vline(xintercept=actual.value,color="orange") +
theme_minimal() +
labs(x="Neither mean minus Female mean (specifically for 5, 6 and 7 scores)",
y="")
## Warning: Removed 10000 rows containing non-finite values (`stat_density()`).
## Warning: Removed 1 rows containing missing values (`geom_vline()`).
##I am getting a warning that 10000rows containing non-finite values went missing? Shouldnt it be nearly the same aside for accounting for the neither group?
The headquarters of aQuantive were in Seattle, but about half the employees were in New York, the nexus of advertising in the US. The data show lower satisfaction for New York employees:
d %>%
group_by(location) %>%
summarize(`Mean Sat.`=mean(satisfaction)) %>%
knitr::kable()
| location | Mean Sat. |
|---|---|
| New York | 3.892617 |
| Seattle | 4.033113 |
In this section, perform a permutation test on satisfaction by location and report the results.
observed_means <- d %>%
group_by(location) %>%
summarize('Mean Satisfaction' = mean(satisfaction)) %>%
filter(location %in% c("New York", "Seattle"))
observed_diffs <- diff(observed_means$'Mean Satisfaction')
get.location.diffs <- function(x){
ny.mean <- mean(x$satisfaction[x$location == "New York"])
seattle.mean <- mean(x$satisfaction[x$location == "Seattle"])
return(seattle.mean - ny.mean)
}
actual.value <- get.location.diffs(d)
n.sim <- 9999
results <- tibble(statistic = c(actual.value, rep(NA, n.sim)))
new.d <- d %>%
select(location, satisfaction)
for(i in 2:nrow(results)){
new.d$location <- sample(new.d$location)
results$statistic[i] <- get.location.diffs(new.d)
}
library(ggplot2)
ggplot(results,
aes(x = statistic)) +
geom_density() +
geom_vline(xintercept = actual.value, color = "darkgreen") +
theme_minimal() +
labs(x = "Seattle means minus New York means",
y = "")
The mean of the response is amenable to a t-test (if we were working with a a sample and not a census). This is because the mean of a sample has a distribution that can be mathematically proven to come from the \(t\) distribution. There is no analogous result for the median, our robust measure of the center of a distribution. In this section, please calculate the difference in medians between the male and female respondents. As before, plot the results and interpret them.
observed_medians <- d %>%
group_by(assigned_sex) %>%
summarize(Median <- median(satisfaction, na.rm = TRUE))
observed_diff_medians <- diff(observed_medians$Median)
## Warning: Unknown or uninitialised column: `Median`.
get.assigned.sex.diff.medians <- function(x){
male.median <- median(x$satisfaction[x$assigned_sex == "male"], na.rm = TRUE)
female.median <- median(x$satisfaction[x$assigned_sex == "female"], na.rm = TRUE)
return(female.median - male.median)
}
actual.value_median <- get.assigned.sex.diff.medians(d)
n.sim <- 9999
results_median <- tibble(statistic = c(actual.value_median, rep(NA, n.sim)))
new.d_median <- d %>%
select(assigned_sex, satisfaction)
for(i in 2:nrow(results_median)){
new.d_median$assigned_sex <- sample(new.d_median$assigned_sex)
results_median$statistic[i] <- get.assigned.sex.diff.medians(new.d_median)
}
ggplot(results_median,
aes(x = statistic)) +
geom_density() +
geom_vline(xintercept = actual.value_median, color = "lavender") +
theme_minimal() +
labs(x = "Female median minus Male median",
y = "")
##Im not sure what I did wrong here.
You do not need to write any code in this section.
Imagine HR was interested in the relationship between tenure and
satisfaction. Typically, correlation is used to measure this type of
association, with the most critical assumption being that the
relationship is linear. In this section, describe how you could carry
out a test of significance with the cor.test function. This
function uses a result from almost 100 years ago that the standard
deviation of the correlation coefficient is \(\frac{r \sqrt(n-2)}{\sqrt(1-r^2)}\) that
relies on a mathematically sophisticated approach that uses the method
of moments.
In order to use the cor.test function, we could start by naming the null hypothesis which would be; that there is no linear correlation between tenure and satisfaction in the population. The alternative would be opposite and state that there is a linear correlation between tenure and satisfaction in the population.
Utilizing the correlation coefficient, we would perform the test using the cor.test function and this would provide us with a p-value based on the distribution under the null. We would then compare this p-value to the significance level that we are going to use for this problem to decide if we would reject the null or fail to reject the null hypothesis.
Alternatively, you could test the significance using a permutation test. In this section, give some detail for the key steps you’d take to test the significance of the correlation coefficient.
You might wonder at this use of “assigned sex” rather than the more-common-in-a-business-context “gender” for this column name. It’s not a point that needs belaboring here, but “assigned sex” is a more accurate term for a male/female column than “gender”. If you’d like to learn more about this I’d be happy to chat about it. ↩︎