df <- read.csv("data.csv") %>% rename(
  id = R0000100,
  sample_id = R0173600,
  household_id = R0000149,
  race = R0214700,
  sex = R0214800,
  birth_year = R0000500,
  birth_month = R0000300,
  asvab_ar = R0648309,
  asvab_wk = R0648343,
  asvab_pc = R0648382,
  asvab_mk = R0648402,
  rel_with_household_r_1 = R0000151,
  rel_with_household_r_2 = R0000153,
  rel_with_household_r_3 = R0000155,
  rel_with_household_r_4 = R0000157,
  rel_with_household_r_5 = R0000159
) %>% mutate(
  asvab_ar = if_else(asvab_ar < 0, NA, asvab_ar),
  asvab_wk = if_else(asvab_wk < 0, NA, asvab_wk),
  asvab_pc = if_else(asvab_pc < 0, NA, asvab_pc),
  asvab_mk = if_else(asvab_mk < 0, NA, asvab_mk),
  sex = sex - 1,
  birth_date = 1900 + birth_year + (birth_month / 12), # birth year is given as the last two digits of the year
) %>% select(
  id,
  household_id,
  sample_id,
  race,
  sex,
  birth_date,
  asvab_ar,
  asvab_wk,
  asvab_pc,
  asvab_mk,
  rel_with_household_r_1,
  rel_with_household_r_2,
  rel_with_household_r_3,
  rel_with_household_r_4,
  rel_with_household_r_5
)

# Create IQ score with a white mean of 100 and a white SD of 15

df <- df %>% mutate(
  full_sample_asvab_z_score = scale(scale(asvab_ar) + scale(asvab_wk) + scale(asvab_pc) + scale(asvab_mk))[,1]
)

white_df <- df %>% filter(sample_id <= 8, race == 3)

white_asvab_z_score_mean <- mean(white_df$full_sample_asvab_z_score, na.rm = T)
white_asvab_z_score_sd <- sd(white_df$full_sample_asvab_z_score, na.rm = T)

df <- df %>% mutate(
  iq = (((full_sample_asvab_z_score - white_asvab_z_score_mean) / white_asvab_z_score_sd) * 15) + 100
) %>% select(
  -c(full_sample_asvab_z_score, asvab_ar, asvab_wk, asvab_pc, asvab_mk)
)

# Remove the influence of birth date and sex from IQ

iq_model <- lm(iq ~ birth_date, data = df)

iq_mean <- mean(df$iq, na.rm = T)
iq_sd <- sd(df$iq, na.rm = T)

adjusted_iq_df <- df %>% filter(
  !is.na(iq)
) %>% mutate(
  scaled_iq_residual =  scale(iq_model$residuals)[,1],
  adjusted_iq = iq_mean + (scaled_iq_residual * iq_sd)
) %>% select(id, adjusted_iq)

df <- df %>%
  left_join(adjusted_iq_df, by = "id")

# Remove respondents from households with non-sibling respondents

df <- df %>%
  filter(
    rel_with_household_r_1 %in% c(-4, 6, 7), # -4 = valid skip, 6 = brother, 7 = sister
    rel_with_household_r_2 %in% c(-4, 6, 7),
    rel_with_household_r_3 %in% c(-4, 6, 7),
    rel_with_household_r_4 %in% c(-4, 6, 7),
    rel_with_household_r_5 %in% c(-4, 6, 7)
  ) %>% select(-starts_with("rel_with_household_r_"))

# Run model

m1_df <- df %>%
  filter(!is.na(adjusted_iq), !is.na(birth_date)) %>%
  group_by(household_id) %>%
  filter(n() > 1) %>% # Only keep groups with more than one respondent
  mutate(
    birth_date_delta = birth_date - mean(birth_date),
    adjusted_iq_delta = adjusted_iq - mean(adjusted_iq)
  )

m1 <- lm(scale(adjusted_iq_delta) ~ scale(birth_date_delta), data = m1_df)

summary(m1)
## 
## Call:
## lm(formula = scale(adjusted_iq_delta) ~ scale(birth_date_delta), 
##     data = m1_df)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.1694 -0.6560  0.0007  0.6330  3.5516 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             9.475e-18  1.386e-02   0.000        1    
## scale(birth_date_delta) 1.088e-01  1.386e-02   7.847 5.14e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9942 on 5142 degrees of freedom
## Multiple R-squared:  0.01183,    Adjusted R-squared:  0.01164 
## F-statistic: 61.58 on 1 and 5142 DF,  p-value: 5.14e-15