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

# Chapter 18: 9

```{r}
obesity.data <- read.csv('case1801.csv')
obese <- obesity.data[obesity.data$Obesity=='Obese',]
notobese <- obesity.data[obesity.data$Obesity=='NotObese',]

obesity.data
```

```{r}
n1 = obese$Deaths+obese$NonDeaths
n2 = notobese$Deaths+notobese$NonDeaths
pc = (obese$Deaths+notobese$Deaths)/(n1+n2)
```

## Part (a)

### Part (i)
```{r}
p1 = obese$Deaths/n1
p2 = notobese$Deaths/n2
```


Sample proportion of CVD deaths for obese group: $\pi_1=`r (p1)`$

Sample proportion of CVD deaths for nonbese group: $\pi_2=`r p2`$


### Part (ii)


```{r}
seci <- sqrt(p1*(1-p1)/n1+p2*(1-p2)/n2)
setest <- sqrt(pc*(1-pc)/n1+pc*(1-pc)/n2)
```

Standard error for difference : `r seci`

### Part (iii)



```{r}
difference <- p1-p2
Z <- difference/setest
halfwidth <- 1.96*setest
hci <- difference + halfwidth
lci <- difference - halfwidth
```

95% confidence interval: $[`r lci`, `r hci`]$


## Part (b)

```{r}
pval <- 1-pnorm(Z)
```


One sided p-value: `r pval`

## Part (c)

```{r}
w1 <- obese$Deaths/obese$NonDeaths
w2 <- notobese$Deaths/notobese$NonDeaths
oddsratio <- w1/w2
logodds <- log(oddsratio)
selogci <- sqrt(1/obese$Deaths + 1/obese$NonDeaths + 1/notobese$Deaths + 1/notobese$NonDeaths)
selogtest <- sqrt(1/(n1*pc*(1-pc)) + 1/(n2*pc*(1-pc)) )
logwidth <- 1.96*selogci
loglci <- logodds-logwidth
loghci <- logodds+logwidth
```
### Part (i)
Sample Odds: $\omega_1=`r w1`$ ; $\omega_2=`r w2`$

### Part (ii)

Odds ratio: $`r oddsratio`$

### Part (iii)

Standard error of the log odds ratio: $`r selogci`$

### Part (iv)

95% confidence interval for log odds ratio: $[`r loglci`, `r loghci`]$


## Part (d)
<!-- The odds of deaths for obese group are estimated to be `r oddsratio` times the odds of deaths for nonobese group(approximate 95% CI: $[`r loglci`, `r loghci`]$) .-->
While testing for equality, we opbtained a p-value of `r pval`. Also the 95% CI for log odds ratio is  $[`r loglci`, `r loghci`]$ which does not include the estimated odds ratio of  $`r oddsratio`$ and thus there is no evidence that odds ratio of deaths among obese grooup over nonobese groups is different from 1.

# Chapter 18: 11

## Part (a)
```{r}
smoker.data <- read.csv('smokers.csv')
smokers <- smoker.data[smoker.data$Smoker=='Smokers',]
nonsmokers <- smoker.data[smoker.data$Smoker=='Nonsmokers',]
```

```{r}
cancer.smokers <- smokers$Cancer/(smokers$Cancer+smokers$NoCancer)
```
Proportion of lung cancer patients among smokers: $`r cancer.smokers`$

## Part (b)

```{r}
cancer.nonsmokers <- nonsmokers$Cancer/(nonsmokers$Cancer+nonsmokers$NoCancer)
```

Proportion of lung cancer patients among nonsmokers: $`r cancer.smokers`$


## Part (c)

```{r}
difference.smokers <- cancer.smokers - cancer.nonsmokers 
difference.smokers
```