data_fit_gof.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Data preparation, logistic fitting, prediction, and goodness-of-fit."""
  2. from pathlib import Path
  3. import numpy as np
  4. from scipy.io import loadmat
  5. from ..data import get_data
  6. from .core import (
  7. _sigmoid_stable,
  8. model_p,
  9. design_matrix,
  10. nll,
  11. llf,
  12. grad_nll,
  13. hess_nll,
  14. covariance,
  15. standard_errors,
  16. fit_newton,
  17. goodness_of_fit,
  18. fit_pack,
  19. trim_nc_by_value,
  20. fit_logistic_x,
  21. predict_curve_x,
  22. plot_overlay_two_panels_final,
  23. )
  24. def load_dataset(project_root=None, percentile=95):
  25. """Load the thesis SUV predictor and binary outcome arrays."""
  26. root = Path(project_root) if project_root else Path(__file__).resolve().parents[1]
  27. suv = loadmat(root / "suv_percentilesSLOthenUWM.mat")
  28. flags = loadmat(root / "flags_combined.mat")
  29. x, y = get_data(percentile, suv, flags)
  30. return np.asarray(x, float).ravel(), np.asarray(y, int).ravel()
  31. def prepare_full_trim(project_root=None, percentile=95, target=2.48122597, tol=0.05):
  32. """Return aligned FULL and TRIM datasets."""
  33. x_full, y_full = load_dataset(project_root, percentile)
  34. x_trim, y_trim = trim_nc_by_value(x_full, y_full, target=target, tol=tol)
  35. return {
  36. "FULL": (x_full, y_full),
  37. "TRIM": (x_trim, y_trim),
  38. }