Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions pymgcv/gam.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ def fit(
data=data_to_rdf(data, include=self.referenced_variables),
family=self.family.rfamily,
method=method,
weights=ro.NULL if weights is None else np.asarray(weights),
weights=ro.NULL if weights is None else to_rpy(np.asarray(weights)),
optimizer=to_rpy(np.array(optimizer)),
scale=0 if scale is None else (-1 if scale == "unknown" else scale),
select=select,
Expand Down Expand Up @@ -1177,7 +1177,7 @@ def fit(
data=data_to_rdf(data, include=self.referenced_variables),
family=self.family.rfamily,
method=method,
weights=ro.NULL if weights is None else np.asarray(weights),
weights=ro.NULL if weights is None else ro.FloatVector(np.asarray(weights)), #issue: can't use to_rpy(np.asarray(weights))
scale=0 if scale is None else (-1 if scale == "unknown" else scale),
select=select,
gamma=gamma,
Expand Down
24 changes: 23 additions & 1 deletion tests/gam_test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def mgcv_gam(
with ro.local_context() as env:
env["data"] = data_to_rdf(data, include=self.gam_model.referenced_variables)
for k, v in self.add_to_r_env.items():
env[k] = v
env[k] = ro.FloatVector(v) if isinstance(v, np.ndarray) else v #issue: can't use to_rpy(v)
result = ro.r(self.mgcv_call)
assert isinstance(result, ro.ListVector)
return result
Expand Down Expand Up @@ -554,6 +554,27 @@ def family_test_cases() -> dict[str, GAMTestCase]:
),
}

def weighted_gam(gam_type: type[AbstractGAM]) -> GAMTestCase:
"""test case for GAM with observation weights."""
data = get_test_data()
n = len(data)

rng = np.random.default_rng(42)
weights = rng.uniform(0.5, 2.0, size=n)

method = "REML"

return GAMTestCase(
mgcv_args=f"y ~ s(x), weights=w_arr, method='{method}'",
gam_model=gam_type({"y": S("x")}),
data=data,
add_to_r_env={"w_arr": weights},
fit_kwargs={
"weights": weights,
"method": method
}
)


def get_test_cases() -> dict[str, GAMTestCase]:
supported_types_and_cases = [
Expand All @@ -580,6 +601,7 @@ def get_test_cases() -> dict[str, GAMTestCase]:
linear_functional_tensor_2d_gam,
markov_random_field_neighbours,
markov_random_field_polys,
weighted_gam
],
),
]
Expand Down