################################################################################
#I this script we fit the regression of the simulated outcome on sex, age and
#cohabitation status and show how we can correct for the bias induced by the
#measurement error in cohabitation status in the GGS data.
################################################################################

# load packages and simulated GGS data
library(mecor)
library(tidyverse)
library(broom)
input_data <- read_csv("simulated_data.csv")

#seed
set.seed(101)

# Naive fit
fit <- input_data |>
  group_by(year) |>
  do(tidy(lm(outcome ~ cohab + sex + age, data = .)))

round(fit |> filter(term == "cohab") |> pull(estimate), 2)
round(fit |> filter(term == "cohab") |> pull(std.error), 2)

# Fit on gold-standard administrative data (real values)
fit_gold <- input_data |>
  group_by(year) |>
  do(tidy(lm(outcome ~ cohab_true + sex + age, data = .)))

round(fit_gold |> filter(term == "cohab_true") |> pull(estimate), 2)
round(fit_gold |> filter(term == "cohab_true") |> pull(std.error), 2)

# What if we do not have gold-standard data for all measurements
input_data_missing <- input_data |>
  mutate(missing = rbinom(nrow(input_data),1,0.25),
         cohab_true_missing = case_when(missing == 1 ~ NA,
                                        TRUE ~ cohab_true))

fit_gold_missing <- input_data_missing |>
  group_by(year) |>
  do(as.data.frame(summary(mecor(formula = outcome ~ MeasError(substitute = cohab, reference = cohab_true_missing) + sex + age,
                                 data = .,
                                 method = "standard", # defaults to "standard"
                                 B = 0), zerovar = TRUE, fieller = TRUE)[["c"]]$coefficients))

fit_gold_missing$term = fit_gold$term

round(fit_gold_missing |> filter(term == "cohab_true") |> pull(Estimate), 2)
round(fit_gold_missing |> filter(term == "cohab_true") |> pull(SE), 2)


# 75% missing
input_data_missing <- input_data |>
  mutate(missing = rbinom(nrow(input_data),1,0.75),
         cohab_true_missing = case_when(missing == 1 ~ NA,
                                        TRUE ~ cohab_true))

fit_gold_missing <- input_data_missing |>
  group_by(year) |>
  do(as.data.frame(summary(mecor(formula = outcome ~ MeasError(substitute = cohab, reference = cohab_true_missing) + sex + age,
                                 data = .,
                                 method = "standard", # defaults to "standard"
                                 B = 0), zerovar = TRUE, fieller = TRUE)[["c"]]$coefficients))

fit_gold_missing$term = fit_gold$term

round(fit_gold_missing |> filter(term == "cohab_true") |> pull(Estimate), 2)
round(fit_gold_missing |> filter(term == "cohab_true") |> pull(SE), 2)

# 99% missing
input_data_missing <- input_data |>
  mutate(missing = rbinom(nrow(input_data),1,0.99),
         cohab_true_missing = case_when(missing == 1 ~ NA,
                                        TRUE ~ cohab_true))

fit_gold_missing <- input_data_missing |>
  group_by(year) |>
  do(as.data.frame(summary(mecor(formula = outcome ~ MeasError(substitute = cohab, reference = cohab_true_missing) + sex + age,
                                 data = .,
                                 method = "standard", # defaults to "standard"
                                 B = 0), zerovar = TRUE, fieller = TRUE)[["c"]]$coefficients))

fit_gold_missing$term = fit_gold$term

round(fit_gold_missing |> filter(term == "cohab_true") |> pull(Estimate), 2)
round(fit_gold_missing |> filter(term == "cohab_true") |> pull(SE), 2)
