Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjusting for overdispersion #40

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions ext_models/chen2021Fitness/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def fit(
k,
n,
generation_time,
reproduction_number
reproduction_number,
quasi=False
) -> Tuple[GrowthNumbers, GLMResultsWrapper]:
# adapt the data to the required format
x = t
Expand All @@ -27,6 +28,20 @@ def fit(
# The following cov matrix returned by the method is the same as invFI
cov = model.cov_params()

if quasi:
# Follow a quasilikelihood approach to adjusting for overdispersion according to
# McCullagh and Nelder, Generalized Linear Models (second edition, chapters 4.5, 9)
# Calculate the Pearson chi-square statistic
pearson_chi2 = sum((model.resid_pearson)**2)

# Calculate the dispersion factor
n_obs = model.nobs # number of observations
df_residuals = model.df_resid # residual degrees of freedom
dispersion_factor = pearson_chi2 / df_residuals

# Adjust the covariance matrix for overdispersion, but not for underdispersion (conservative)
cov = cov * max(dispersion_factor, 1.0)

# We transform these two parameters for our parameterization of interest
# and use a delta method to get the correct variance

Expand Down Expand Up @@ -65,12 +80,28 @@ def fit(
def predict(
model: GLMResultsWrapper,
t: np.ndarray,
alpha: float
alpha: float,
quasi=False
) -> ValueWithCi2:
X = sm.add_constant(t)
proba = model.predict(X)
cov = model.cov_params()

if quasi:
# Follow a quasilikelihood approach to adjusting for overdispersion according to
# McCullagh and Nelder, Generalized Linear Models (second edition, chapters 4.5, 9)

# Calculate the Pearson chi-square statistic
pearson_chi2 = sum((model.resid_pearson)**2)

# Calculate the dispersion factor
n_obs = model.nobs # number of observations
df_residuals = model.df_resid # residual degrees of freedom
dispersion_factor = pearson_chi2 / df_residuals

# Adjust the covariance matrix for overdispersion, but not for underdispersion (conservative)
cov = cov * max(dispersion_factor, 1.0)

# we need to estimate confidence interval for predicted probabilities using the delta method as well

# the proportion is a function of the parameters
Expand Down