---
# System prepended metadata

title: Research Group Discussion NO. 9
tags: [statistics, OR, RR, R]

---

# Research Group Discussion NO. 9

###### tags: `R` `statistics` `OR` `RR`

## Dataset

**Data Set Information:**

plosone dataset contains the medical records of **66 patients who had Hepatic portal venousgas(HPVG)**. Each patient profile has **13 clinical variables**.

heart dataset contains the medical records of **303 patients who had heart failure**, collected during their follow-up period, where each patient profile has **13 clinical features**.

## Relative Risk and Odds Ratio 
Before obtaining RR or OR, we can first check the correlation and Chi-square.

```r=
# load library
library(ggplot2) #graph
library(PerformanceAnalytics) #correlation
library(epiR) #RR or OR

# import heart.txt and pone_data.txt files 
data_ER <- read.table("data/pone_data.txt", header = T, sep = "\t")
data_heart <- read.table("data/heart.txt", header = T, sep = "\t")

# correlation matrix
chart.Correlation(data_ER, histogram=TRUE, method = "spearman", pch=19)
chart.Correlation(data_heart, histogram=TRUE, method = "spearman", pch=19)

```
For calculate RR, OR or Chi-square, first, we need to create a table.

```r=
# make a table
# use factor to set the sex and target as categorical variables
# Reset the level (reorder the column)
data_heart$sex <- factor(data_heart$sex)
data_heart$target <- factor(data_heart$target, levels = c(1, 0))
my_table <- table( Sex = data_heart$sex, Outcome = data_heart$target)
my_table

# change row name
rownames(my_table) <- c("female","male")
my_table

# change column name
colnames(my_table) <- c("Heart_Failure", "Control")
my_table

# let us make a graph
ggplot(data = data_heart, aes(x = target, fill = sex )) + geom_bar()

# little adjustment
ggplot(data = data_heart, aes(x = target, fill = sex )) + 
  geom_bar(position = "dodge") +
  scale_x_discrete(labels = c("HF","Control"))+
  scale_fill_discrete(labels = c("Female","Male"))

```
Then, we performed chi-square test.
```r=
# perform Chi-square test of independence
chisq.test(my_table)

```
Let's use epiR package to calculate RR and OR....

```r=
# RR and OR : cohort.count, case.control
epi.2by2(my_table, method = "cohort.count", conf.level = 0.95)
epi.2by2(my_table, method = "case.control", conf.level = 0.95)

```
Obtain the OR for our logistic regression.

```r=
# build models (multivariate logistic)
model <- glm(outcome ~ rr + age + bp + RAPS, data = data_ER, family = binomial)
summary(model)
coefficients(model)
exp(coefficients(model)) # OR
exp(confint(model)) # CI for OR

```










