
---
title: "Twin IQ Correlation"
output:
  html_document
editor_options: 
  chunk_output_type: inline
---

```{r load-libraries, message = FALSE}
library(tidyverse)
library(ggpubr) # for stat_cor

theme_set(theme_classic(base_size = 12) + 
            theme(legend.position = "none")) 
```

```{r load-data, message = FALSE}
twins <- read_csv("https://dyurovsky.github.io/85309/data/demos/twins.csv")

twins
```

What is the correlation between two twins IQs?

```{r compute-correlation}
twin_cor <- twins %>%
  summarise(correlation = cor(twin_a, twin_b)) %>%
  pull()

twin_cor
```

Plot the correlation between twin IQs
```{r plot-corrs, fig.width = 3, fig.height = 3, message = FALSE}
ggplot(twins, aes(x = twin_a, y = twin_b)) + 
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, color = "#bb0000") + 
  stat_cor()

```

Make a linear regression predicting twin b from twin a
```{r linear-model}
twin_lm <- lm(twin_b ~ twin_a, data = twins)

summary(twin_lm)
```

the slope of a regression line is equal to the correlation when the independent and dependent variables are both standardiZed and no intercept is fit
```{r scale-twins}
scale_twins <- twins %>%
  mutate(twin_a = scale(twin_a), twin_b = scale(twin_b))

scale_twins


scale_twin_lm <- lm(twin_b ~ twin_a + 0, data = scale_twins)

summary(scale_twin_lm)
twin_cor

scale_twin_reverse_lm <- lm(twin_a ~ twin_b + 0, data = scale_twins)
summary(scale_twin_reverse_lm)
```
