---
title: 'PSYC 193R: Homework 6'
output: html_notebook
---

Due date: 11:59pm, Friday March 2, 2018

- Name: 
- PID: 

Grading Rubic            | Percentage
------------------------ | -------------
Section A                | 50%
Section B                | 30%
Scientific Communication | 10%
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_hw06_(your pid).Rmd; e.g., psyc193r_hw06_A01234565.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_hw06_A01234567.Rmd & psyc193r_hw06_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: 
This study is inspired by [West et al. (2017)](http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0187779), although the data were recreated.  

Middle-aged people often complain about a decline in memory. It has been a challenge to develop useful tools to stop the decline. Some scientists were interested in using video games to improve mid-aged adults' mental functioning, which has shown some promise in the past with younger adults. They recruited a group of participants with little video game and musical training.  
Participants were randomly assigned into three conditions, control, musical training, and video game. The musical training was designed to be an active control.  
It is also known from the literature that the size of [hippocampus](https://en.wikipedia.org/wiki/Hippocampus) is related to the memory. After the 6-month training, memory capacity and the size of hippocampus were measured. 

In the first section of the analysis, the researchers would like to see if there are differences in the sizes of hippocampus in the conditions. You can ignore the memory scores for that section  
In the second section of the analysis, they would like to see if the size of hippocampus is statistically related with memory score, to confirm previous finding in the literature. 

You can access the data file at: 
https://psyc193r.ucsd.edu/data/psyc193r_game_data.csv

Import the data from the URL, saving it to a variable "gameData". 
```{r}
gameData = NA
```

The data frame has 6 variables:  

- subjId: participant number  
- gender: gender of the participant (0 == female)  
- age: age of the participant
- condition: 0 -- control, 1 -- musical training, 2 -- video game
- hippoVol: volume of the hippocampus (in $cm^{3}$)
- memory: scores in a memory test (20-point)

Check whether the data structure is ideal for the analysis you planned to perform. 

## Section A
In this section, we are *only interested* in the difference in hippocampal volume in different conditions.  

## 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? What was the mean age of the sample and the standard deviation?  

```{r}
# some calculations, if needed

```

> 

1.4 What were the theoretical 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? How many participants were there in each condition? 

```{r}

```

> 

1.7 State the expected results in words (not formulas).  

> If the manipulation does not work:  
If the manipulation works:  


## Part 2: Statistical Power  

We all know that memory or brain structures are plastic, but it is not easy to change that.  
The researchers expected only 10% of the variance can be attributed to the conditions.  

2.1 With the expected effect size, what was the minimal sample size (in total) needed to get a significant result with 80% probability if there was a true effect?  
```{r}
# load the package (if needed)


# Calculations

```

2.2 The researchers were most interested in the difference between the control and the video game conditions. For an effect size of 0.2, how many subjects did the researchers need *per condition* to achieve an 80% power?
```{r}

```

Briefly explain what the calculation (Parts 2.1 & 2.2) above means.

> 


## Part 3: Data Cleaning / Processing + Assumptions checking, Hypothesis Testing
3.1 Make sure the variables are in the correct "classes"
```{r}

```

Implementation check:  
```{r}
# check the classes of the variables

```

3.2 State the assumptions, and perform calculations if needed
```{r}
# assumption 1: (state your assumption)

# assumption 2: (state your assumption)

# assumption 3: (state your assumption)

# Other assumptions: (if any)

```

Describe the assumptions you checked, and report the results and statistics when appropriate. 

> 

3.3 Summary statistics, saving them into some variables that you can use later
```{r}
# mean of the conditions
gameSummary = NA 

# print out the means


# sd of the conditions
gameSummary.sd = NA 

# print out the sd


# number of participants in the conditions
gameSummary.n = NA

# print out the numbers

```

3.4 Perform an appropriate statistical test.  

Instructions:  

- Use the 4-step approach  
- Use the appropriate function  
- You do not need to report power, effect size, or confidence intervals in this section  
- Comment all steps that do not require calculations.  
```{r}
# Step 1: 

# Step 2: 

# Step 3: 

# Step 4: 

```

## Part 4: Type I error, and Effect Size  
4.1 Type I error
Calculate / estimate the family-wise type I error of the test you performed above
```{r}

```

4.2 Effect Size
Calculate / estimate the effect size of the study (from data)
```{r}

```

Explain what the calculations (4.1 & 4.2) above means.  

> 

## Part 5: Post-hoc test
5.1 Briefly explain whether / why a post-hoc test is needed. 

> 

5.2 Performed the post-hoc test if needed. 
```{r}

```

5.3 Explain the results of the post-hoc test (if performed). Report the differences between conditions, and the adjusted p-values. 

> 

5.4 What was the overall Type I error of the test after performing the post-hoc tests? 

> 

## Part 6: Visualization
Make a data frame that contains information needed for the graph.
Construct the data frame by adding columns to the gameSummary variable you created earlier. 
```{r}
# data frame for the graph

```

Make a graph to depict the results. 
```{r}
# load the relevant package


# make a graph

```

Describe the main results with the graph, and state what the error bars denote. 

> 

## Section B
In this section, we are *only interested* in the relationship between hippocampal volume and memory capacity.  

## Part 7: Design of the study  
(Skip a line and start your answer with a ">", so that your answer appears in a "block")  
7.1. Did the researchers take an experimental, quasi-experiment or observational approach to study the research question?    

> 

7.2 What was the research question?  

> 

7.3 What were the theoretical IV(s) and DV(s)? (if any)

> IV(s):   
DV(s): 

7.4 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): 

7.5 State the expected results in words (not formulas).  

> If the manipulation does not work:  
If the manipulation works:  

## Part 8: Data formatting + Assumptions checking
8.1 Factorize / as.numeric variables
```{r}
# Factorize


# as.numeric

```

8.2 State the assumptions, and perform calculations if needed
```{r}
# assumption 1: (state your assumption)

# assumption 2: (state your assumption)

# assumption 3: (state your assumption)

# Other assumptions: (if any)

```

Describe the assumptions you checked, and report the results and statistics when appropriate. 

> 

## Part 9: Correlation, Regression, Effect size and Hypothesis testing
9.1 Calculate the correlation coefficients (r)
```{r}
# correlation coefficient

```

State the correlation coefficient, test statistics for the correlation coefficient and conclude whether the two variable are statistically related.  

> 

9.2 Build a linear model to describe the relationship between the two variables
```{r}
# build a model

# read out the summary of the model

```

Describe the relationship between the two variables in a formula. 

> 

9.3 Calculated the variance explained by the model 
```{r}
# variance explained by the model

```

The % of variance explained by the model is: 

> 

9.4 Prediction  
If a new middle-aged female has a hippocampal volume of 3.4 $cm^{3}$, what would you expect her memory score to be? 
```{r}
# generate a new data frame for the iv

# predict her memory score

```
Explain what it means. 

> 

## Part 10: Visualization
10.1 Plot a scatterplot with the fitted regression line
```{r}
# load the package for plotting

# Plot the graph

```

State what the shaded region denotes. 

> 

10.2 Residual plot
```{r}
# Calculate the residuals


# Plot the residual plot

```

Explain what we can conclude from the residual plot.  

> 

## Part 11. Scientific Communications (no coding needed)
Summarize what you found in the study. Explain the background in 2 - 3 sentences. State the research question. Justify the number of participants used in the study (when appropriate). Explain the results of the hypothesis testing, and include all relevant test staitsics in a format that you would see in a scientific journal. Also include relevant summary statistics and effect size of the test. Explain the conclusion / recommendations in a way that the general public can understand. You can assume that all the graphs are included.  
(answer this question in a "block")

Section A

> 

Section B

> 


#### End of Homework 6