5

I want to use the natural cubic smoothing splines smooth.spline from R in Python (like som many others want as well (Python natural smoothing splines, Is there a Python equivalent to the smooth.spline function in R, Python SciPy UnivariateSpline vs R smooth.spline, ...)) Therefore I am using rpy2 like described in https://morioh.com/p/eb4151821dc4, but I want to set directly lambda instead of spar:

import rpy2.robjects as robjects
r_y = robjects.FloatVector(y_train)
r_x = robjects.FloatVector(x_train)

r_smooth_spline = robjects.r['smooth.spline'] #extract R function# run smoothing function
spline1 = r_smooth_spline(x=r_x, y=r_y, lambda=42)
#alternative: spline1 = r_smooth_spline(x=r_x, y=r_y, spar=0.7) would work fine, but I would like to control lambda dirctly
ySpline=np.array(robjects.r['predict'](spline1,robjects.FloatVector(x_smooth)).rx2('y'))
plt.plot(x_smooth,ySpline)

When I do this the line spline1 = r_smooth_spline(x=r_x, y=r_y, lambda=42) doesn't work because Python has already a predefined interpretation of lambda (you can see this from the blue code-highlighting of lambda) :( I want lambda to be interpreted as the smoothing penalty parameter lambda.

If I replace lambda by spar I would get a natural cubic spline, but I want to control lambda directly.

Jakob
  • 1,063
  • 9
  • 17

3 Answers3

5

This little trick will work around the specific problem you're having, by allowing you to write "lambda" in a string.

kwargs = {"x": r_x, "y": r_y, "lambda":  42}
spline1 = r_smooth_spline(**kwargs)

In the general case, you can pass around argument containers easily with tuples and dicts.

# as normal
f = function("foo", "bar", my_kwarg="my_value")

# the same call using argument containers
args = ("foo", "bar")
kwargs = {"my_kwarg": "my_value"}
f = function(*args, **kwargs)
Jwely
  • 682
  • 5
  • 18
4

Perhaps you could use rpy2's Function.rcall() method when calling smooth.spline?

import rpy2.robjects as robjects
r_y = robjects.FloatVector(y_train)
r_x = robjects.FloatVector(x_train)

r_smooth_spline = robjects.r['smooth.spline']
args = (('x',r_x), ('y',r_y), ('lambda',42)) # pattern (('argname', value),...)

# import R's "GlobalEnv" to evaluate the function
from rpy2.robjects import globalenv

spline1 = r_smooth_spline.rcall(args, globalenv)
Andy B
  • 66
  • 5
  • This a link to outdated documentation. You'll want https://rpy2.github.io/doc/v3.2.x/html/robjects_functions.html – lgautier Oct 04 '19 at 18:47
1

You can use Python's **<dict> in a function call to specify R named arguments that have a name that is not syntactically valid in Python.

See the documentation for more details: https://rpy2.github.io/doc/v3.2.x/html/robjects_functions.html

lgautier
  • 11,363
  • 29
  • 42
  • code example? How would the line `spline1 = r_smooth_spline(x=r_x, y=r_y, lambda=42)` look like when I use `**`? `spline1 = r_smooth_spline(x=r_x, y=r_y, **=42)` doesn't work... – Jakob Oct 04 '19 at 15:52
  • Check the doc. Code examples are there. – lgautier Oct 04 '19 at 18:45
  • https://stackoverflow.com/a/58240214/7735095 added a nice code example. I think that corresponds to your documentation. – Jakob Oct 04 '19 at 18:52
  • Both answers entered after mine are in the documentation ;-) – lgautier Oct 04 '19 at 20:14