---
title: "R Notebook"
output: html_notebook
---

# Lecture 5: Effect Size, Type I/II errors, Power

## Effect Size of a study
Cohen's d = (mu_treatment - mu_baseline) / sigma
```{r}
# List out all information you have

# calculate cohen's d
cohens_d = NA
```
- How would you describe the effect size of The Official SAT Practice?  


## Confidence Interval
Confidence interval is a reliability measure of your treated sample means
```{r}
# List our all information you have

# Construct a 95% Conidence Interval with pnorm() or qnorm()

```
- How would you explain to other people what that interval means?  


## Statistical Power
Power is an estimation of the likelihood to reject H0 when there is an effect (H0 is false)
- Step 1: Converting population distributions into sample distributions
i.e., Find the standard error of the sampling distributions
```{r}
# List our all information you have

# Find the standard error

```

- Step 2: Find cutoff points in terms of raw scores
```{r}
# Find the cutoff scores under H0 sampling distribution

```

- Step 3: Find the power
```{r}
# Find the area beyond the cutoff scores under H1 sampling distribution

```

## Statistical power function
Write a function that calculate the power.  
So that you can re-use the function in a different scenario.  
Note: The function only works when sample_mean > baseline_pop_mean  
You can make it more general
```{r}
# The power function
power = function(treatment_mean, baseline_pop_mean, pop_sd, sample_size, alpha = 0.05){
  # Calculate the standard error
  se = NA

  # Find the cutoff points in raw scores
  cutoff = NA

  # Find the probability of getting a sample mean 
  # more extreme than the cutoff scores
  # under H1
  stat_power = NA

  # save sample_size and power to an array
  answer = NA

  # return the array
  return (answer)
}

```

#### Test the function
```{r}
# List our all information you have
treatment_mean = NA
baseline_pop_mean = NA
pop_sd = NA
sample_size = NA
alpha = NA

power(treatment_mean, baseline_pop_mean, pop_sd, sample_size, alpha)
```
- How should we interpret the number above?


## Estimating sample size
For-loop for calculating the number of participants needed for the Offical SAT Practice study
```{r}
# set up the parameters (inputs) for the task
treatment_mean = NA
baseline_pop_mean = NA
pop_sd = NA
alpha = NA

# Use a for-loop to iterate the sample size, and to get the corresponding power
# given other parameters do not change
for(NA in seq(from = NA, to = NA, by = NA)){
  # Calculate the power for that given i as sample size
  stat_power = NA

  # Print the sample size and the estimated power
  print(paste0("Sample size: ", i, ", Power = ", stat_power[2]))
}
```

- What is the sample size needed to achieve a 80% power?

- How would you describe this analysis to another scientist?
