## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE,
  message = FALSE
)

## ----setup--------------------------------------------------------------------
library(CGMissingDataR)

## ----example-data-------------------------------------------------------------
data("CGMExmplDat10Pct")

summary_table <- data.frame(
  Rows = nrow(CGMExmplDat10Pct),
  Columns = ncol(CGMExmplDat10Pct),
  Subjects = length(unique(CGMExmplDat10Pct$USUBJID)),
  MissingGlucose = sum(is.na(CGMExmplDat10Pct$LBORRES)),
  MissingPercent = round(mean(is.na(CGMExmplDat10Pct$LBORRES)) * 100, 1)
)

summary_table
head(CGMExmplDat10Pct)

## ----basic-imputation---------------------------------------------------------
impute_out <- suppressWarnings(
  run_missing_glucose_imputation(
    CGMExmplDat10Pct,
    target_col = "LBORRES",
    feature_cols = c("AGE", "hba1c", "SEX"),
    id_col = "USUBJID",
    time_col = "Time",
    imputer_backend = "mice",
    xgb_nrounds = 5
  )
)

## ----output-shape-------------------------------------------------------------
class(impute_out)
nrow(impute_out)
names(impute_out)

## ----output-preview-----------------------------------------------------------
head(impute_out[c(
  "USUBJID",
  "SEX",
  "Time",
  "LBORRES",
  "AGE",
  "hba1c",
  "imputed_glucose_value"
)])

## ----original-target-unchanged------------------------------------------------
sum(is.na(CGMExmplDat10Pct$LBORRES))
sum(is.na(impute_out$LBORRES))
sum(is.na(impute_out$imputed_glucose_value))

## ----missing-row-preview------------------------------------------------------
missing_rows <- is.na(impute_out$LBORRES)
head(impute_out[missing_rows, c(
  "USUBJID",
  "Time",
  "LBORRES",
  "imputed_glucose_value"
)])

## ----internal-feature-check---------------------------------------------------
grep("^lag[0-9]+$|^rollmean$|^TimeSeries$|^TimeDifferenceMinutes$", names(impute_out), value = TRUE)

## ----rounding-example, eval = FALSE-------------------------------------------
# impute_out$imputed_glucose_value_rounded <- round(impute_out$imputed_glucose_value)

## ----python-backend-example, eval = FALSE-------------------------------------
# out_py <- run_missing_glucose_imputation(
#   CGMExmplDat10Pct,
#   target_col = "LBORRES",
#   feature_cols = c("AGE", "hba1c"),
#   id_col = "USUBJID",
#   time_col = "Time",
#   imputer_backend = "sklearn",
#   xgb_nrounds = 5
# )
# 
# head(out_py[c(
#   "USUBJID",
#   "Time",
#   "LBORRES",
#   "imputed_glucose_value"
# )])

## ----export-example, eval = FALSE---------------------------------------------
# out <- run_missing_glucose_imputation(
#   CGMExmplDat10Pct,
#   target_col = "LBORRES",
#   feature_cols = c("AGE", "hba1c"),
#   id_col = "USUBJID",
#   time_col = "Time",
#   imputer_backend = "mice",
#   export = TRUE
# )

## ----session-info-------------------------------------------------------------
utils::sessionInfo()

