---
title: "Homework 4 (due Thursday, November 1st)"
author: I agree to abide by the Stern Code of Conduct - (NAME & NYU ID)
output: html_document
---
```{r include=FALSE}
library(tidyverse)
```
### Question 1
Nagy and others (2007) conducted an experiment to determine if there was a difference between female infants and male infants in social behavior, specifically how often they imitated finger movements made by the experimenter. Gestures in response to the experimenter were coded as 1, 2, or 3 with higher scores for more finger movement.
The mean score for 19 females was 1.47 with an SD of 0.24, and the mean score for 23 males was 1.34 with an SD of 0.15.
#### Part a.
Use this information to compute a **95\% confidence interval** for each group by changing the relevant lines below. The intervals will be plotted when you knit this document.
```{r}
# Enter data here. Note that group 1 is female and group 2 is male.
mean1 <- 0.5
# delete this line and enter code to calculate qt(something, n-1) * sd/sqrt(n)
# modify lower and upper to mean1 - something and mean1 + something
lower1 <- 0
upper1 <- 1
# now do the same for the male group--beware the different sample size!
mean2 <- 0.5
lower2 <- 0
upper2 <- 1
# Don't change anything else in this section below this line
data <- data.frame(sex = c("female", "male"),
mean = c(mean1, mean2),
lower = c(lower1, lower2),
upper = c(upper1, upper2))
ggplot(data, aes(sex, mean)) + geom_point() +
geom_errorbar(aes(x = sex, ymin = lower, ymax = upper)) +
ylim(c(0,3)) + theme_minimal() + ylab("Imitation scores")
```
#### Part b.
The code below computes a 95\% confidence interval for the difference between imitation scores for the two groups: female minus male.
```{r}
# The only part you need to modify is inside the qt() function.
s1 <- 0.24^2/19
s2 <- 0.15^2/23
df <- (s1+s2)^2/(s1^2/18 + s2^2/22)
delta <- sqrt(s1+s2) * qt(0.975, df)
effect <- 1.47 - 1.34
c(effect - delta, effect + delta)
```
Change the confidence level from 95\% to 99\%. Does the new interval contain the number 0? In other words, is the lower limit now negative?
```
answer here
```
#### Part c.
Continuing part b, suppose that the experiment was repeated with a new sample of data from different infants and computed a new 95\% confidence interval for the difference between female and male imitation scores. Which of the following numbers might be different in this new experiment: (1) population means, (2) population variances, (3) sample means, (4) sample variances, (5) the center of the CI, (6) the length of the CI, (7) whether or not the CI contains zero, (8) whether or not the CI contains the difference in population means.
```
answer here
```
### Question 2
In this problem you will generate data where you know the true value of the mean and then construct a confidence interval from that data. First select (highlight) the lines of code below, and then in the menu in the top left click "Code" and then click "Comment/Uncomment Lines." Enter the numbers you want, and then knit.
```{r}
# n <- (write the sample size you want here)
# mu <- (write the true mean you want here)
# sigma2 <- (write the true variance you want here)
# X <- rnorm(n, mean = mu, sd = sqrt(sigma2))
# Xbar <- mean(X)
# SE <- sd(X)/sqrt(n)
# C <- qt(.995, n-1)
# c(Xbar - C * SE, Xbar + C * SE)
```
Now uncomment the line below and change "?" to compute the same confidence interval as the one above, check your answer by seeing that they are the same.
```{r}
# t.test(X, conf.level = ?)
```
Finally, if you now knit the document again without changing any code, do you get the same interval or one that is different?
```
Answer here
```
### Question 3
Finish this question last, after printing your homework. You may assume the sample size is 50. In plot (a), shade the region corresponding to the probability that the T-statistic is larger than c, where c is the constant (multiplier of the standard error) for a **95\%** confidence interval. In plot (b), shade the region corresponding to the sample mean being within the **80\%** interval distance from the population mean.
```{r echo=F}
range <- data.frame(t = c(-3,3), alternative = c("(a)", "(b)"))
ggplot(range, aes(t)) +
stat_function(fun = dt, args = list(df = 39)) +
facet_grid(alternative~.) +
ylab("") +
theme(axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme_minimal()
```
# References
Nagy, E., Kompagne, H., Orvos, H., & Pal, A. (2007). Gender‐related differences in neonatal imitation. *Infant and Child Development*, 16(3), 267-276.
https://pdfs.semanticscholar.org/7def/e1f9967d6ddf4e5a24ccc07b7aff4d4daf14.pdf