---
title: "Problem Set 2"
author: "Your Name Here"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output: pdf_document
---

```{r setup, include=FALSE}
options(width=80)
knitr::opts_chunk$set(echo = TRUE, out.width = 40, tidy=T, tidy.opts=list(width.cutoff=60)) #This code should automatically wrap text when knitting to pdf. 
```

### Introduction

*Welcome to your second problem set. This one focuses on the R skills we have practiced so far: loading and exploring data, descriptive statistics, cleaning and recoding variables, comparing groups, and graphing relationships. It then asks a series of question about research design, using theory, and, of course, causality. Please be sure that you answer every question (and each subquestion) thoroughly.*

*Give yourself plenty of time to complete the assignment (You will need to build in time for R frustration, freakouts, celebrations, etc.- though, hopefully, fewer than last time) Please fill in your answers to each question below where it is written. For questions that require coding with R complete your coding in the sections provided and write out answers, if necessary, in the text section. Remember to knit as you go and be sure the work you turn in is your own.*

\*Please submit both your pdf file and the .Rmd file on the course website before Tuesday at 8 PM(Wednesday is a long day). Label your file with pset#\_name (ie pset2_colin.pdf)\*

# Part 1 - Data Analysis

This week we are using survey data from the **World Values Survey (Wave 7) for Thailand**, collected in 2017-2018. You can download the data from the [final project page](https://cmu26.colinkuehl.com/project.html) on the course website. Download the WVS Data and the WVS Codebook.

**Important:** Unlike the country-level data from Problem Set 1, this is *individual-level* survey data. Each row is one survey respondent. Also, read the codebook carefully before interpreting anything - for many of these survey questions **lower numbers mean "more"** (for example, on the happiness question 1 = Very happy and 4 = Not at all happy). Getting the direction of a scale wrong is one of the most common (and embarrassing) mistakes in survey research.

1.  *Set up: Set your working directory and load the WVS dataset using read.csv(). (Remember: the setwd() command must be in your code for it to knit, and the csv file must be in that folder.)Also, library any packages you think you'll need.*

```{r}

```

2.  *A first look at the data: a) Show the first few rows of the data b) How many observations (respondents) are in this dataset? c) How many variables? d) Show the names of the variables. e) Use str() on the age variable - what type of variable does R think it is?*

```{r}

```

3.  *Descriptive statistics: Let's look at the variable* life_satisfaction *(respondents rate how satisfied they are with their life as a whole from 1 to 10).*

*a) Find the mean, median, variance, max, and min for this variable. This variable has missing values. b) How many NAs does this variable have? c) Produce a histogram of life_satisfaction (Be sure that it is properly labeled). The use of colors etc. is encouraged. d) In a sentence or two, how would you describe life satisfaction in Thailand?*

```{r}

```

4.  *Cleaning and recoding: Look at the variable* sex. *a) Run table() on it. The output isn't very informative - you have to go to the codebook to know what 1 and 2 mean. b) Create a new variable (call it something like* sexlabel*) that recodes the values into meaningful labels using case_when() or ifelse(). c) Run table() and prop.table(table()) on your new variable. What proportion of respondents are female? d) Create a properly labeled barplot of your new variable.*

```{r}

```

5.  *Comparing groups: Is there a happiness gap between men and women in Thailand? The variable* happiness *runs from 1 = Very happy to 4 = Not at all happy.*

*a) Create a crosstab of happiness and your labeled sex variable using table(). b) Raw counts are hard to compare when the groups are different sizes, so convert your crosstab to proportions with prop.table() using the margin option (round it to make it readable). Which margin is the right choice here, and why? c) Now use group_by() and summarise() to find the mean happiness score and the number of respondents for each sex. d) Interpret: do men and women in Thailand report different levels of happiness? (Careful with the direction of the scale!)*

```{r}

```

6.  *Looking at relationships: We might hypothesize that people who feel more freedom and control over their lives (*freedom_choice_control*, 1 = none at all to 10 = a great deal) are more satisfied with their lives (*life_satisfaction*).*

*a) In this scenario, what is the independent variable? b) What is the dependent variable? c) Create a scatter plot of the relationship (Be sure each variable is on the correct axis and that everything is labeled). Because both variables only take whole-number values, many respondents will be stacked on the same point - use geom_jitter() (or plot a random sample of respondents like we did in class) so we can actually see the data. d) Add a trend line for the hypothesized relationship. e) How would you describe the relationship? f) Is the relationship causal? Why or why not?*

```{r}

```

7.  *Subsetting: You wonder whether the relationship in question 6 looks the same for younger and older Thais. a) Use filter() (or which()) to create two subsets: respondents under 35 years old, and respondents 55 and older (use the* age *variable). b) How many respondents are in each subset? c) Re-create the graph from question 6 for each subset (give each graph a title so we can tell them apart). d) Does the relationship look similar or different across the two groups?*

```{r}

```

8.  *Your turn: Look through the codebook and pick one variable we haven't used yet that you find interesting. a) Report its descriptive statistics and say what they mean in plain language (check the codebook so you interpret the scale correctly). b) Create an appropriate, properly labeled graph of the variable (histogram or barplot - if the variable is categorical, consider recoding it with meaningful labels first). c) What do you find interesting or surprising?*

```{r}

```

# Part 2 - Conceptual Questions

In your own words . . . .

1.  Explain why "exogeneity" or "as-if random assignment" is necessary to be certain of causality.

2.  Why are we ok with random error but systematic error is a big problem? Give an example of systematic error.

3.  In what circumstances might the median be a better measure of central tendency than the mean? Provide a real world example.

4.  In social science research, how would you define generalizability? Give an example.

5.  What is the difference between theory and a hypothesis? What role does a literature review play?

6.  How long(hours) did this problem set take you?And who else in the class did you work with while completing it?

7.  Anything else Dr. Colin should know about you, this problem set, this class, CMU, life, etc? Feel free to actually vent:
