---
title: "MATH-650 Assignment 2"
author: 'Saket Choudhary (USCID: 2170058637) (skchoudh@usc.edu)'
date: "09/08/2015"
output:
  pdf_document: default
  html_document:
    keep_md: yes
---


```{r}
ex0112<-read.csv('ex0112.csv')
fishoil.diet <- ex0112[ex0112$Diet=='FishOil',]
regularoil.diet <- ex0112[ex0112$Diet=='RegularOil',]




```


## Part(a)
```{r}
n1 <- nrow(fishoil.diet)
n2 <- nrow(regularoil.diet)
mu1 <- mean(fishoil.diet$BP)
mu2 <- mean(regularoil.diet$BP)
s1 <- sd(fishoil.diet$BP)
s2 <- sd(regularoil.diet$BP)
```

Average of group with diet 'FishOil': $\mu_f=$ `r mu1`

Standard deviation  of group with diet 'FishOil': $\sigma_f=$ `r s1`



Average of group with diet 'RegularOil': $\mu_r=$ `r mu2`

Standard deviation  of group with diet 'RegularOil': $\sigma_r=$ `r s2`


## Part(b)
```{r}
sp <- sqrt( ( (n1-1)*s1^2 + (n2-1)*s2^2 ) / (n1+n2-2) )
```

Pooled standard deviation = $s_P=\sqrt{\frac{(n_1-1)s_1^2+(n_2-1)s_2^2}{(n_1+n_2-2)}}$


$s_P=$ `r sp`



## Part (c)
```{r}
se <- sp*sqrt(1/n1+1/n2)
```

Standard error $SE(\bar{Y_2}-\bar{Y_1})=$ `r se`

## Part (d)
```{r}
df <- n1+n2-2
qt975 <- qt(c(.975), df=df)
```
Degrees of freedom = $n_1+n_2-2$= `r df`

$97.5^{th}$ percentile of $t-distribution$ ($df=$ `r df`):   `r qt975`

## Part (e)
```{r}
alpha <- 0.05
t <- qt(1-alpha/2,df)
CI_l <- (mu2-mu1)-t*se
CI_h <- (mu2-mu1)+t*se
```
$95\%$ CI for $\mu_2-\mu_1$: $[$`r CI_l`,`r CI_h`$]$
$t=$ `r t`

## Part (f)

```{r}
T <- (mu2-mu1)/se
p <-  pt(T, df=df)
```

The t-statistic is given by: $t=$ `r T` with $df=$ `r df`


## Part (g)
The appropriate one sided p-value is(since $t<0$): `r 1-p/2`

# Problem (14)

```{r}
ttest <- t.test( regularoil.diet$BP, fishoil.diet$BP, alternative="greater", var.equal=F)
ttest
```
p-value from 't.test' = `r ttest$p.value`

# Problem 19

## Part (19a)

```{r}
n2 <- nrow(regularoil.diet)
mu2 <- mean(regularoil.diet$BP)
s2 <- sd(regularoil.diet$BP)
df2 <- n2-1
```

Average=: `r mu2`
Standard Devaiation: `r s2`
Degree of Freedom: `r df2`

## Part (19b)
```{r}
se2 <- s2/sqrt(n2)
```

Standard error of the average: `r se2`


## Part (19c)

```{r}
qt975_2 <- qt(c(.975), df=df2)
CI_l2 <- mu2 - qt975_2*se2
CI_h2 <- mu2 + qt975_2*se2
```


CI: $[$`r CI_l2`,`r CI_h2`$]$

## Part 19(d)

```{r}
T2 <- mu2/se2
p2 <- pt(T2, df=df2)
```
p-value: `r p2`

t-statistics: `r T2`
