Bivariate Hypothesis Test
Instructions
- Homework assignment is here with all questions commented out.
- Complete all of the coding tasks in the
.qmdfile - Upload your individual exercise to your
GitHubrepo by Wed 11:59pm. - Remember to clean your github repo and sort hw submissions by weeks. Each week should have one folder.
This homework closely follows the lab. You can copy and modify most of the code from the lab. For every test, follow the four steps from the lecture:
- Form the null and alternative hypotheses
- Examine Y, X, and the X-Y relationship in the sample
- Calculate the test statistic and p-value
- Decide whether to reject the null (report p<0.10, p<0.05, p<0.01)
# ##-------------------------------------------------------------------------
# ## R code for POLI502 Lab, week 11: Individual Exercises
# ## Written by: Howard Liu
# ##-------------------------------------------------------------------------
#
# library(gmodels) # for CrossTable()
# library(Hmisc) # for rcorr()
# library(ggplot2) # for graphics
#
# world.data <- read.csv("world.csv")
#
#
# # 1. Cross tabulation -----------------------------------------------------
#
# # In the lab, we tested whether ethnic fractionalization (X) is related to
# # having a PR system (Y). Now keep the same X, but change Y to whether a
# # country is a democracy (democ_regime).
#
#
# # Step 1. Write down the null and the alternative hypotheses.
# # What direction do you expect? (i.e., are more fractionalized countries
# # more or less likely to be democracies?)
#
# WRITE YOUR ANSWER HERE
#
#
# # Step 2.1. Describe Y. Create a new variable called democ that re-labels
# # democ_regime as "Democracy" (for "Yes") and "Non-democracy" (for "No").
# # Then make a frequency table of it.
#
# WRITE YOUR COMMANDS HERE
#
#
# # Step 2.2. Describe X. Create the ordered variable frac_eth3_ord
# # (Low < Medium < High) exactly as we did in the lab, and make a frequency
# # table of it.
#
# WRITE YOUR COMMANDS HERE
#
#
# # Step 2.3. Describe X-Y. Create a two-way table using the table function.
# # Remember: Y first, X next.
#
# WRITE YOUR COMMAND HERE
#
#
# # Steps 3 and 4. Use CrossTable to show column percentages and perform the
# # chi-squared test. This time, also add the option expected = TRUE, which
# # shows the EXPECTED count in each cell (i.e., what we would see if the
# # null hypothesis of no relationship were true).
#
# WRITE YOUR COMMAND HERE
#
#
# # (a) By hand: the expected count of a cell is
# # (row total x column total) / total N
# # Calculate the expected count for "Democracy" & "Low" fractionalization.
# # Does your number match the CrossTable output? Is the OBSERVED count
# # larger or smaller than the EXPECTED count?
#
# WRITE YOUR ANSWER HERE
#
#
# # (b) By hand: the degrees of freedom are (r - 1) x (c - 1), where r is the
# # number of rows and c is the number of columns. What is the df here?
#
# WRITE YOUR ANSWER HERE
#
#
# # (c) The critical value of chi-squared at p = 0.05 can be found with the
# # qchisq function. Fill in your df below. Is the chi-squared statistic
# # you obtained larger than the critical value?
#
# qchisq(0.95, df = YOUR_DF)
#
# WRITE YOUR ANSWER HERE
#
#
# # (d) Report. Following the sample write-up in the lab, write a short
# # paragraph that reports (1) the column percentages, (2) the
# # chi-squared statistic, df, and p-value, and (3) whether you reject
# # the null at the 90%, 95%, and 99% confidence levels.
# # How is this result different from the PR system result in the lab?
#
# WRITE YOUR ANSWER HERE
#
#
#
# # 2. Correlation ----------------------------------------------------------
#
# # In the lab, we tested whether union density (unions, X) is related to
# # labor freedom (Y). Now keep the same X, but change Y to economic
# # inequality in 2008 (gini08; higher values = more unequal).
#
#
# # Step 1. Write down the null and the alternative hypotheses.
# # Do you expect a positive or a negative relationship? Why?
#
# WRITE YOUR ANSWER HERE
#
#
# # Step 2.1. Describe Y: numerically summarize gini08 and draw a histogram
# # with informative axis labels.
#
# WRITE YOUR COMMANDS HERE
#
#
# # Step 2.2. Describe X: we already did this in the lab. Copy that code here.
#
# WRITE YOUR COMMANDS HERE
#
#
# # Step 2.3. Describe X-Y. Following Section 3 of the lab:
# # - create a data set (call it ineq.data) that drops observations where
# # either unions or gini08 is missing
# # - draw a scatterplot of unions (x-axis) and gini08 (y-axis)
# # - add a horizontal line at mean(Y) and a vertical line at mean(X)
#
# WRITE YOUR COMMANDS HERE
#
#
# # The two mean lines split the plot into four quadrants. Recall from the
# # lecture (covariance) that points in the top-right and bottom-left
# # quadrants push toward a POSITIVE relationship, while points in the
# # top-left and bottom-right quadrants push toward a NEGATIVE relationship.
# # Which quadrants contain most of the points? What does this suggest?
#
# WRITE YOUR ANSWER HERE
#
#
# # Steps 3 and 4. Calculate Pearson's correlation coefficient and its p-value
# # using as.matrix and rcorr, as in the lab.
#
# WRITE YOUR COMMANDS HERE
#
#
# # (a) What is r? How many observations (n) are used?
# # What are the degrees of freedom for this test (n - 2)?
#
# WRITE YOUR ANSWER HERE
#
#
# # (b) Report. Write a short paragraph like the sample write-up in the lab.
# # Make sure to mention the direction of the relationship, r, the
# # p-value, and the confidence level at which you can reject the null.
# # Hint: rcorr shows p = 0 when the p-value is very small. This does
# # not mean p is exactly zero; report it as p < 0.001.
#
# WRITE YOUR ANSWER HERE
#
#
#
# # 3. Difference of means --------------------------------------------------
#
# # The lecture covered a third test that we did not practice in the lab: the
# # difference of means test (categorical X, numerical Y). We use it here to
# # ask whether countries with a PR system (X) have more women in
# # parliament (women09, % of seats held by women in 2009; Y).
#
#
# # Step 1. Write down the null and the alternative hypotheses.
#
# WRITE YOUR ANSWER HERE
#
#
# # Step 2. Re-create the pr variable from the lab. Then compare the
# # distribution of women09 across the two groups with a box plot and with
# # the by function.
#
# world.data $ pr <- factor(world.data $ pr_sys,
# levels = c("Yes", "No"),
# labels = c("PR System", "Non-PR"))
#
# g <- ggplot(world.data[is.na(world.data $ women09) == FALSE, ],
# aes(x = pr, y = women09)) + geom_boxplot() + theme_bw()
# g <- g + xlab("Electoral system") + ylab("% of seats held by women")
# g
#
# by(world.data $ women09, world.data $ pr, summary)
#
# # What are the mean values of women09 in each group?
#
# WRITE YOUR ANSWER HERE
#
#
# # Steps 3 and 4. Run the t-test. Notice the formula form: Y ~ X
#
# t.test(women09 ~ pr, data = world.data)
#
# # Report the difference in means, the t statistic, and the p-value.
# # Do you reject the null? At what confidence level?
#
# WRITE YOUR ANSWER HERE
#
#
#
# # End of file