---
title: 'PSYC 193R: Homework 4'
output: html_notebook
---

Due date: 11:59pm, Friday Feb 9, 2018

- Name: 
- PID: 

Grading Rubic | Percentage
------------- | -------------
Part 1        | 10%
Part 2        | 25%
Part 3        | 25%
Part 4        | 10%
Part 5        | 20%
Code Clarity (e.g., commenting, blocking, breaking down problems) | 10%


### Notes: 
To make sure your code runs: When you are done programming, close RStudio so that everything in the Console and the Environment is cleared. Then press "Run All" from the menu bar and make sure there are no errors. 

- Save a copy of this R Notebook and rename it to psyc193r_hw04_(your pid).Rmd; e.g., psyc193r_hw04_A01234567.Rmd
- When you hit "Preview" in the menu bar, an html file will be generated
- You will have to upload this R Notebook and the html output; i.e., psyc193r_hw04_A01234567.Rmd & psyc193r_hw04_A01234567.nb.html using the submission link on the class website
- Codes are run in sequential order: the code appear earlier on may be needed for later parts. When you run some codes, make sure the gray boxes above it are also executed
- Unless otherwise specified, use alpha = 0.05 (two-tailed test) for hypothesis testing and confidence interval construction
- list out the information you have whenever needed to make sure they have not been overwritten in the boxes above

### Goals: 
The objective of this assignment is to practice doing Hypothesis Testing (again), and calculate other statistics that are informative. 


## Background of the project: 
Lie detection is a good skill to have, for ordinary people and for personnel working in the law enforcement. Many online companies now offer lie detection training in the hope to improve people's skills on that.  
Starting 2017, the UC San Diego Police Department requires all their officers to go through an online lie detection training. To evaluate the effectiveness of the training, all officers took a test. Each officer was asked to watch 10 videos, in which actors described an event. Half of the stories actually happened, and the other half was made up. The officers had to decide which of the stories were real.  
The data set is stored at:  
https://psyc193r.ucsd.edu/data/psyc193r_lieDetection_data.csv

Import the data from the URL, saving it to a variable "lieDetectionData". 
```{r}

```

The data set has a "long" format. Each row represents a particular trial of an officer. The data set has 4 columns:  

- officer: UCSD Police officer number
- trial: the trial number
- correct: whether the trial was answered correctly
- responseTime: the time it took the officer to answer the trial

Your task is to perform an appropriate statistical test on the data set. 


## Part 1: Design of the study  
(SKip a line and start your answer with a ">", so that your answer appears in a "block")  

1.1. Was the study an experiment, a quasi-experiment, or an observational study? Why?  

> 

1.2 What was the research question?  

> 

1.3 What was the target population?   

> 

1.4 What were the IV(s) and DV(s)? (if any)

> IV(s):   
DV(s): 

1.5 How were the IV(s) and DV(s) operationalized? (if any; also, include the levels of the variables and scales of measurement)  

> IV(s):     
DV(s): 

1.6 Was it a between- / within-subjects design?  

> 

1.7 What are the expected results?  

> If the manipulation does not work:  
If the manipulation works:  


## Part 2: Statistical Power  

The police department expected that the online course would have a medium effect. They expected an effect size of 0.5.  

Hint: You can import a package for the calculations  

2.1 With the sample size they had, what was the power they could expect?  
```{r}
# load the package for power calculation

# number of subjects in the data set
sample_size = length(unique(lieDetectionData$officer))

# power calculation

```

Briefly explain what the calculation above means.  

> 

2.2 With the expected effect size, what was the minimal sample size needed to get a significant result with 80% probability?  
```{r}

```

Briefly explain what the calculation above means.

> 


## Part 3: Data Cleaning / Processing + Hypothesis Testing
3.1 Make sure the variables are in the correct "classes"
```{r}

```

Implementation check:  
"officer" should be a factor, "correct" should be a numeric
```{r}
# Run this box, do not change anything
class(lieDetectionData$officer)
class(lieDetectionData$correct)
```


3.2 Create a new variable "lieDetection" that contains two variables:  

- officer: UCSD Police officer number
- accuracy: percentage of trials an officer answer correctly
```{r}

```

Implementation check: 
```{r}
head(lieDetection)
```

3.3 Check the assumptions / distribution(s) of your data
```{r}

```

3.4 Perform an appropriate statistical test for the effectiveness of the online course, and report the results.  

Instructions:  

- Use the 4-step approach  
- Do not use the t.test() function  
- You do not need to report power, effect size, or confidence intervals in this section  
- Comment all steps that does not require calculations.  
```{r}
# Step 0: 

# Step 1: 

# Step 2: 

# Step 3: 

# Step 4: See page 35 of lecture 7

```

3.5 Use the t.test() function to check your answer above, you don't need to explain the output
```{r}

```


## Part 4: Type I error, Effect Size, Confidence Interval
#### Type I error
Calculate / estimate the type I error of the test you performed above and explain your answer
```{r}

```

#### Effect Size
Calculate / estimate the effect sizes of the study from the data.  
```{r}
# Effect size rom study: 

```

Explain what the calculations above means.  

> 

#### Confidence Interval
State the 95% confidence interval of the test above. You can obtain the answer from the t.test() output.  

> 

## Part 5: Scientific Communications (no coding needed)
An important part of doing science is to communicate our ideas well within and beyond the scientific community. We do science because we believe what we do has an impact on the society (somehow, maybe, hopefully positive).  
Based on all the analysis you have (Parts 2 to 4), explain to the public what the study was about, and the major findings. Include all the test statistics, but make sure to explain them in terms that are understandable by the UC Regents, UC San Diego Police Department, The UC San Diego community, and the general public. It would be helpful to imagine that you are explaining results to the UC San Diego Police Department, so that they can make an informed decision on whether or not to require future new officers to complete the online training.    
(answer this question in a "block")

> 

#### End of Homework 4