# IPCRidge


IPC-weighted ridge regression for survival data.


Usage

``` python
IPCRidge(
    alpha=1.0,
    *,
    standardize=True,
)
```


A linear survival model that corrects for censoring bias via inverse-probability-of-censoring (IPC) reweighting rather than the Cox partial likelihood. The model fits a weighted ridge regression on observed (uncensored) log-times:

 \hat{\beta} = \arg\min\_{\beta} \sum\_{i:\\\delta_i=1} w_i \bigl(\log T_i - X_i \beta\bigr)^2 + \alpha \\\beta\\\_2^2 

where w_i = 1 / \hat{G}(T_i^-) are IPC weights from the Kaplan-Meier estimate of the censoring distribution. This eliminates censoring bias from the least-squares objective without assuming proportional hazards.

Centering and optional standardization are applied before fitting, and coefficients are returned on the original covariate scale. The intercept is always estimated.


## Parameters


`alpha: float = ``1.0`  
Ridge penalty strength. `0` gives unpenalized IPC-weighted OLS.

`standardize: bool = ``True`  
Standardize covariates to unit variance before penalizing (default `True`). Coefficients are returned on the original scale.


## Examples

Fit an IPC-weighted ridge on the lung dataset:


``` python
import greenwood as gw

lung = gw.load_dataset("lung", backend="polars")

ridge = gw.IPCRidge(alpha=1.0).fit("Surv(time, status) ~ age + sex", data=lung)
ridge
```


    IPCRidge (IPC-weighted ridge, alpha=1.0)

                     coef
    (Intercept)     6.107
    age          -0.02134
    sex             0.573

    n = 228, events = 165 (used for fitting)


## Methods

| Name | Description |
|----|----|
| [fit()](#fit) | Fit the IPC-weighted ridge regression to survival data. |
| [predict()](#predict) | Predict from the fitted IPC-weighted ridge model. |
| [to_frame()](#to_frame) | Return the coefficient table as a DataFrame. |

------------------------------------------------------------------------


### fit()


Fit the IPC-weighted ridge regression to survival data.


Usage

``` python
fit(
    surv,
    covariates=None,
    *,
    data=None,
)
```


Only uncensored subjects contribute to the fit. Each is weighted by 1/\hat{G}(T_i^-) from the Kaplan-Meier censoring estimate, so subjects who experienced events when censoring was heavy receive greater influence.


#### Parameters


`surv: Surv | Outcome | str`  
A right-censored [Surv](Surv.md#greenwood.Surv) response, built with `gw.Surv(time=..., event=...)`. An [Outcome](Outcome.md#greenwood.Outcome) or a formula string such as `'Surv(time, status == 2) ~ age + sex'` is also accepted, with its columns read from `data`. The right-hand side sets `covariates`.

`covariates: Any = None`  
A dataframe (pandas or polars), a 2-D array, or a formula string (e.g., `"age + sex"`) evaluated against `data`. A list of column names in `data` also works.

`data: Any = None`  
A data frame (pandas, Polars, PyArrow, DuckDB, a lazy frame, …) holding the columns named by the response and `covariates`. When [surv](Outcome.md#greenwood.Outcome.surv) is an [Outcome](Outcome.md#greenwood.Outcome) or a formula, rows with a missing value in any column used are dropped before fitting. Covariate formula strings and lists of column names are also resolved here.


#### Returns


`IPCRidge`  
The fitted estimator with cached coefficient arrays.


------------------------------------------------------------------------


### predict()


Predict from the fitted IPC-weighted ridge model.


Usage

``` python
predict(
    newdata=None,
    *,
    type="lp",
)
```


#### Parameters


`newdata: Any = None`  
Covariate values for prediction. `None` uses the training data. A data frame may also hold other columns. The covariates are picked out by name and coded as they were at fit time, so the frame the model was fit on can be passed as is.

`type: str = ``"lp"`  
Prediction type:

- `"lp"`: the linear predictor \hat{\beta}\_0 + X\hat{\beta} (predicted log-time). Higher values indicate longer expected survival.
- `"response"`: \exp(\text{lp}), predicted survival time on the original scale.


#### Returns


`ndarray`  
Predicted values, shape `(n_subjects,)`.


------------------------------------------------------------------------


### to_frame()


Return the coefficient table as a DataFrame.


Usage

``` python
to_frame(
    *,
    format=None,
)
```


#### Parameters


`format: str | None = None`  
Output format: `None` (default), `"pandas"`, `"polars"`, or `"pyarrow"`.


#### Returns


`DataFrame`  
Columns: `term`, `estimate`.
