[Numpy-discussion] smoothing function

Nathaniel Smith njs at pobox.com
Thu May 15 12:17:43 EDT 2014


On Thu, May 15, 2014 at 1:04 PM, rodrigo koblitz
<rodrigokoblitz at gmail.com> wrote:
> Buenos,
> I'm reading Zuur book (ecology models with R) and try make it entire in
> python.
> Have this function in R:
> M4 <- gam(So ∼ s(De) + factor(ID), subset = I1)
>
> the 's' term indicated with So is modelled as a smoothing function of De
>
> I'm looking for something close to this in python.

The closest thing that doesn't require writing your own code is
probably to use patsy's [1] support for (simple unpenalized) spline
basis transformations [2]. I think using statsmodels this works like:

import statsmodels.formula.api as smf
# adjust '5' to taste -- bigger = wigglier, less bias, more overfitting
results = smf.ols("So ~ bs(De, 5) + C(ID)", data=my_df).fit()
print results.summary()

To graph the resulting curve you'll want to use the results to somehow
do "prediction" -- I'm not sure what the API for that looks like in
statsmodels. If you need help figuring it out then the asking on the
statsmodels list or stackoverflow is probably the quickest way to get
help.

-n

[1] http://patsy.readthedocs.org/en/latest/
[2] http://patsy.readthedocs.org/en/latest/builtins-reference.html#patsy.builtins.bs

-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org



More information about the NumPy-Discussion mailing list